Autopsy  4.19.1
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 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
53 class SQLiteTableView extends JPanel implements ExplorerManager.Provider {
54 
55  private final org.openide.explorer.view.OutlineView outlineView;
56  private final Outline outline;
57  private final ExplorerManager explorerManager;
58 
63  SQLiteTableView() {
64 
65  initComponents();
66  outlineView = new org.openide.explorer.view.OutlineView();
67  add(outlineView, BorderLayout.CENTER);
68  outlineView.setPropertyColumns(); // column headers will be set later
69  outlineView.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
70  outlineView.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
71 
72  outline = outlineView.getOutline();
73 
74  outline.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
75  outline.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
76  outline.setRowSelectionAllowed(false);
77  outline.setRootVisible(false);
78 
79  outline.setCellSelectionEnabled(true);
80  explorerManager = new ExplorerManager();
81  }
82 
88  @NbBundle.Messages({"SQLiteTableView.DisplayAs.text=Display as",
89  "SQLiteTableView.DisplayAsMenuItem.Date=Date",
90  "SQLiteTableView.DisplayAsMenuItem.RawData=Raw Data"
91  })
92  void setupTable(List<Map<String, Object>> tableRows) {
93 
94 
95  if (Objects.isNull(tableRows) || tableRows.isEmpty()) {
96  outlineView.setPropertyColumns();
97  } else {
98 
99  // Set up the column names
100  Map<String, Object> row = tableRows.get(0);
101  String[] propStrings = new String[row.size() * 2];
102  int i = 0;
103  for (Map.Entry<String, Object> col : row.entrySet()) {
104  String colName = col.getKey();
105  propStrings[2 * i] = colName;
106  propStrings[2 * i + 1] = colName;
107  i++;
108  }
109 
110  outlineView.setPropertyColumns(propStrings);
111  }
112 
113  // Hide the 'Nodes' column
114  TableColumnModel columnModel = outline.getColumnModel();
115  ETableColumn column = (ETableColumn) columnModel.getColumn(0);
116  ((ETableColumnModel) columnModel).setColumnHidden(column, true);
117 
118  // Set the Nodes for the ExplorerManager.
119  // The Swingworker ensures that setColumnWidths() is called after all nodes have been created.
120  new SwingWorker<Boolean, Void>() {
121  @Override
122  protected Boolean doInBackground() throws Exception {
123 
124  List<Action> nodeActions = new ArrayList<>();
125 
126  nodeActions.add(new ParseColAction(Bundle.SQLiteTableView_DisplayAs_text(), outline) );
127 
128  explorerManager.setRootContext(new AbstractNode(Children.create(new SQLiteTableRowFactory(tableRows, nodeActions), true)));
129  return false;
130  }
131 
132  @Override
133  protected void done() {
134  super.done();
135 
136  setColumnWidths();
137  }
138  }.execute();
139 
140  }
141 
142  private void setColumnWidths() {
143  int margin = 4;
144  int padding = 8;
145 
146  // find the maximum width needed to fit the values for the first N rows, at most
147  final int rows = Math.min(20, outline.getRowCount());
148  for (int col = 1; col < outline.getColumnCount(); col++) {
149  int columnWidthLimit = 500;
150  int columnWidth = 50;
151 
152  for (int row = 0; row < rows; row++) {
153  TableCellRenderer renderer = outline.getCellRenderer(row, col);
154  Component comp = outline.prepareRenderer(renderer, row, col);
155 
156  columnWidth = Math.max(comp.getPreferredSize().width, columnWidth);
157  }
158 
159  columnWidth += 2 * margin + padding; // add margin and regular padding
160  columnWidth = Math.min(columnWidth, columnWidthLimit);
161  outline.getColumnModel().getColumn(col).setPreferredWidth(columnWidth);
162  }
163  }
164 
165 
171  @SuppressWarnings("unchecked")
172  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
173  private void initComponents() {
174 
175  setLayout(new java.awt.BorderLayout());
176  }// </editor-fold>//GEN-END:initComponents
177 
178  @Override
179  public ExplorerManager getExplorerManager() {
180  return explorerManager;
181  }
182 
183  // Variables declaration - do not modify//GEN-BEGIN:variables
184  // End of variables declaration//GEN-END:variables
185 
186 
191  private class ParseColAction extends AbstractAction implements Presenter.Popup {
192  private final Outline outline;
193  private final String displayName;
194 
195  ParseColAction(String displayName, Outline outline ) {
196  super(displayName);
197  this.outline = outline;
198  this.displayName = displayName;
199  }
200 
201  @Override
202  public void actionPerformed(ActionEvent e) {
203 
204  }
205 
206  @Override
207  public JMenuItem getPopupPresenter() {
208  return new DisplayColAsMenu();
209  }
210 
214  private class DisplayColAsMenu extends JMenu {
215 
216  DisplayColAsMenu() {
217  super(displayName);
218  initMenu();
219  }
220 
221  final void initMenu() {
222 
223  int selCol = outline.getSelectedColumn();
224  if (selCol < 0 ) {
225  selCol = 1;
226  }
227 
228  TableColumnModel columnModel = outline.getColumnModel();
229  TableColumn column = columnModel.getColumn(selCol);
230 
231  JMenuItem parseAsEpochItem = new JMenuItem(Bundle.SQLiteTableView_DisplayAsMenuItem_Date());
232  parseAsEpochItem.addActionListener((ActionEvent evt) -> {
233  column.setCellRenderer(new EpochTimeCellRenderer(true));
234  });
235  parseAsEpochItem.setEnabled(false);
236  add(parseAsEpochItem);
237 
238  JMenuItem parseAsOriginalItem = new JMenuItem(Bundle.SQLiteTableView_DisplayAsMenuItem_RawData());
239  parseAsOriginalItem.addActionListener((ActionEvent evt) -> {
240  column.setCellRenderer(new EpochTimeCellRenderer(false));
241  });
242  parseAsOriginalItem.setEnabled(false);
243  add(parseAsOriginalItem);
244 
245  // Enable the relevant menuitem based on the current display state of the column
246  TableCellRenderer currRenderer = column.getCellRenderer();
247  if (currRenderer instanceof EpochTimeCellRenderer) {
248  if (((EpochTimeCellRenderer) currRenderer).isRenderingAsEpoch()) {
249  parseAsOriginalItem.setEnabled(true);
250  } else {
251  parseAsEpochItem.setEnabled(true);
252  }
253  }
254  else {
255  parseAsEpochItem.setEnabled(true);
256  }
257  }
258  }
259  }
260 }

Copyright © 2012-2021 Basis Technology. Generated on: Thu Sep 30 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.