Autopsy  4.6.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SQLiteTableView.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2018 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.contentviewers;
20 
21 import java.awt.BorderLayout;
22 import java.awt.Component;
23 import java.awt.event.ActionEvent;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Objects;
28 import javax.swing.AbstractAction;
29 import javax.swing.Action;
30 import javax.swing.JMenu;
31 import javax.swing.JMenuItem;
32 import javax.swing.JPanel;
33 import javax.swing.JTable;
34 import javax.swing.ListSelectionModel;
35 import javax.swing.ScrollPaneConstants;
36 import javax.swing.SwingWorker;
37 import javax.swing.table.TableCellRenderer;
38 import javax.swing.table.TableColumn;
39 import javax.swing.table.TableColumnModel;
40 import org.netbeans.swing.etable.ETableColumn;
41 import org.netbeans.swing.etable.ETableColumnModel;
42 import org.netbeans.swing.outline.Outline;
43 import org.openide.explorer.ExplorerManager;
44 import org.openide.nodes.AbstractNode;
45 import org.openide.nodes.Children;
46 import org.openide.util.NbBundle;
47 import org.openide.util.actions.Presenter;
48 
52 class SQLiteTableView extends JPanel implements ExplorerManager.Provider {
53 
54  private final org.openide.explorer.view.OutlineView outlineView;
55  private final Outline outline;
56  private final ExplorerManager explorerManager;
57 
62  SQLiteTableView() {
63 
64  initComponents();
65  outlineView = new org.openide.explorer.view.OutlineView();
66  add(outlineView, BorderLayout.CENTER);
67  outlineView.setPropertyColumns(); // column headers will be set later
68  outlineView.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
69  outlineView.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
70 
71  outline = outlineView.getOutline();
72 
73  outline.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
74  outline.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
75  outline.setRowSelectionAllowed(false);
76  outline.setRootVisible(false);
77 
78  outline.setCellSelectionEnabled(true);
79  explorerManager = new ExplorerManager();
80  }
81 
87  @NbBundle.Messages({"SQLiteTableView.DisplayAs.text=Display as",
88  "SQLiteTableView.DisplayAsMenuItem.Date=Date",
89  "SQLiteTableView.DisplayAsMenuItem.RawData=Raw Data"
90  })
91  void setupTable(List<Map<String, Object>> tableRows) {
92 
93 
94  if (Objects.isNull(tableRows) || tableRows.isEmpty()) {
95  outlineView.setPropertyColumns();
96  } else {
97 
98  // Set up the column names
99  Map<String, Object> row = tableRows.get(0);
100  String[] propStrings = new String[row.size() * 2];
101  int i = 0;
102  for (Map.Entry<String, Object> col : row.entrySet()) {
103  String colName = col.getKey();
104  propStrings[2 * i] = colName;
105  propStrings[2 * i + 1] = colName;
106  i++;
107  }
108 
109  outlineView.setPropertyColumns(propStrings);
110  }
111 
112  // Hide the 'Nodes' column
113  TableColumnModel columnModel = outline.getColumnModel();
114  ETableColumn column = (ETableColumn) columnModel.getColumn(0);
115  ((ETableColumnModel) columnModel).setColumnHidden(column, true);
116 
117  // Set the Nodes for the ExplorerManager.
118  // The Swingworker ensures that setColumnWidths() is called after all nodes have been created.
119  new SwingWorker<Boolean, Void>() {
120  @Override
121  protected Boolean doInBackground() throws Exception {
122 
123  List<Action> nodeActions = new ArrayList<>();
124 
125  nodeActions.add(new ParseColAction(Bundle.SQLiteTableView_DisplayAs_text(), outline) );
126 
127  explorerManager.setRootContext(new AbstractNode(Children.create(new SQLiteTableRowFactory(tableRows, nodeActions), true)));
128  return false;
129  }
130 
131  @Override
132  protected void done() {
133  super.done();
134 
135  setColumnWidths();
136  }
137  }.execute();
138 
139  }
140 
141  private void setColumnWidths() {
142  int margin = 4;
143  int padding = 8;
144 
145  // find the maximum width needed to fit the values for the first N rows, at most
146  final int rows = Math.min(20, outline.getRowCount());
147  for (int col = 1; col < outline.getColumnCount(); col++) {
148  int columnWidthLimit = 500;
149  int columnWidth = 50;
150 
151  for (int row = 0; row < rows; row++) {
152  TableCellRenderer renderer = outline.getCellRenderer(row, col);
153  Component comp = outline.prepareRenderer(renderer, row, col);
154 
155  columnWidth = Math.max(comp.getPreferredSize().width, columnWidth);
156  }
157 
158  columnWidth += 2 * margin + padding; // add margin and regular padding
159  columnWidth = Math.min(columnWidth, columnWidthLimit);
160  outline.getColumnModel().getColumn(col).setPreferredWidth(columnWidth);
161  }
162  }
163 
164 
170  @SuppressWarnings("unchecked")
171  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
172  private void initComponents() {
173 
174  setLayout(new java.awt.BorderLayout());
175  }// </editor-fold>//GEN-END:initComponents
176 
177  @Override
178  public ExplorerManager getExplorerManager() {
179  return explorerManager;
180  }
181 
182  // Variables declaration - do not modify//GEN-BEGIN:variables
183  // End of variables declaration//GEN-END:variables
184 
185 
190  private class ParseColAction extends AbstractAction implements Presenter.Popup {
191  private final Outline outline;
192  private final String displayName;
193 
194  ParseColAction(String displayName, Outline outline ) {
195  super(displayName);
196  this.outline = outline;
197  this.displayName = displayName;
198  }
199 
200  @Override
201  public void actionPerformed(ActionEvent e) {
202 
203  }
204 
205  @Override
206  public JMenuItem getPopupPresenter() {
207  return new DisplayColAsMenu();
208  }
209 
213  private class DisplayColAsMenu extends JMenu {
214 
215  DisplayColAsMenu() {
216  super(displayName);
217  initMenu();
218  }
219 
220  final void initMenu() {
221 
222  int selCol = outline.getSelectedColumn();
223  if (selCol < 0 ) {
224  selCol = 1;
225  }
226 
227  TableColumnModel columnModel = outline.getColumnModel();
228  TableColumn column = columnModel.getColumn(selCol);
229 
230  JMenuItem parseAsEpochItem = new JMenuItem(Bundle.SQLiteTableView_DisplayAsMenuItem_Date());
231  parseAsEpochItem.addActionListener((ActionEvent evt) -> {
232  column.setCellRenderer(new EpochTimeCellRenderer(true));
233  });
234  parseAsEpochItem.setEnabled(false);
235  add(parseAsEpochItem);
236 
237  JMenuItem parseAsOriginalItem = new JMenuItem(Bundle.SQLiteTableView_DisplayAsMenuItem_RawData());
238  parseAsOriginalItem.addActionListener((ActionEvent evt) -> {
239  column.setCellRenderer(new EpochTimeCellRenderer(false));
240  });
241  parseAsOriginalItem.setEnabled(false);
242  add(parseAsOriginalItem);
243 
244  // Enable the relevant menuitem based on the current display state of the column
245  TableCellRenderer currRenderer = column.getCellRenderer();
246  if (currRenderer instanceof EpochTimeCellRenderer) {
247  if (((EpochTimeCellRenderer) currRenderer).isRenderingAsEpoch()) {
248  parseAsOriginalItem.setEnabled(true);
249  } else {
250  parseAsEpochItem.setEnabled(true);
251  }
252  }
253  else {
254  parseAsEpochItem.setEnabled(true);
255  }
256  }
257  }
258  }
259 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon May 7 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.