Autopsy  4.5.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.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import javax.swing.JPanel;
27 import javax.swing.JTable;
28 import javax.swing.ListSelectionModel;
29 import javax.swing.ScrollPaneConstants;
30 import javax.swing.SwingWorker;
31 import javax.swing.table.TableCellRenderer;
32 import javax.swing.table.TableColumnModel;
33 import org.netbeans.swing.etable.ETableColumn;
34 import org.netbeans.swing.etable.ETableColumnModel;
35 import org.netbeans.swing.outline.Outline;
36 import org.openide.explorer.ExplorerManager;
37 import org.openide.nodes.AbstractNode;
38 import org.openide.nodes.Children;
39 
40 class SQLiteTableView extends JPanel implements ExplorerManager.Provider {
41 
42  private final org.openide.explorer.view.OutlineView outlineView;
43  private final Outline outline;
44  private final ExplorerManager explorerManager;
45 
50  SQLiteTableView() {
51 
52  initComponents();
53  outlineView = new org.openide.explorer.view.OutlineView();
54  add(outlineView, BorderLayout.CENTER);
55  outlineView.setPropertyColumns(); // column headers will be set later
56  outlineView.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
57  outlineView.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
58 
59  outline = outlineView.getOutline();
60 
61  outline.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
62  outline.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
63  outline.setRowSelectionAllowed(false);
64  outline.setRootVisible(false);
65 
66  explorerManager = new ExplorerManager();
67  }
68 
74  void setupTable(List<Map<String, Object>> tableRows) {
75 
76 
77  if (Objects.isNull(tableRows) || tableRows.isEmpty()) {
78  outlineView.setPropertyColumns();
79  } else {
80 
81  // Set up the column names
82  Map<String, Object> row = tableRows.get(0);
83  String[] propStrings = new String[row.size() * 2];
84  int i = 0;
85  for (Map.Entry<String, Object> col : row.entrySet()) {
86  String colName = col.getKey();
87  propStrings[2 * i] = colName;
88  propStrings[2 * i + 1] = colName;
89  i++;
90  }
91 
92  outlineView.setPropertyColumns(propStrings);
93  }
94 
95  // Hide the 'Nodes' column
96  TableColumnModel columnModel = outline.getColumnModel();
97  ETableColumn column = (ETableColumn) columnModel.getColumn(0);
98  ((ETableColumnModel) columnModel).setColumnHidden(column, true);
99 
100  // Set the Nodes for the ExplorerManager.
101  // The Swingworker ensures that setColumnWidths() is called after all nodes have been created.
102  new SwingWorker<Boolean, Void>() {
103  @Override
104  protected Boolean doInBackground() throws Exception {
105 
106  explorerManager.setRootContext(new AbstractNode(Children.create(new SQLiteTableRowFactory(tableRows), true)));
107  return false;
108  }
109 
110  @Override
111  protected void done() {
112  super.done();
113 
114  setColumnWidths();
115  }
116  }.execute();
117 
118  }
119 
120  private void setColumnWidths() {
121  int margin = 4;
122  int padding = 8;
123 
124  // find the maximum width needed to fit the values for the first N rows, at most
125  final int rows = Math.min(20, outline.getRowCount());
126  for (int col = 1; col < outline.getColumnCount(); col++) {
127  int columnWidthLimit = 500;
128  int columnWidth = 50;
129 
130  for (int row = 0; row < rows; row++) {
131  TableCellRenderer renderer = outline.getCellRenderer(row, col);
132  Component comp = outline.prepareRenderer(renderer, row, col);
133 
134  columnWidth = Math.max(comp.getPreferredSize().width, columnWidth);
135  }
136 
137  columnWidth += 2 * margin + padding; // add margin and regular padding
138  columnWidth = Math.min(columnWidth, columnWidthLimit);
139  outline.getColumnModel().getColumn(col).setPreferredWidth(columnWidth);
140  }
141  }
142 
143 
149  @SuppressWarnings("unchecked")
150  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
151  private void initComponents() {
152 
153  setLayout(new java.awt.BorderLayout());
154  }// </editor-fold>//GEN-END:initComponents
155 
156  @Override
157  public ExplorerManager getExplorerManager() {
158  return explorerManager;
159  }
160 
161  // Variables declaration - do not modify//GEN-BEGIN:variables
162  // End of variables declaration//GEN-END:variables
163 }

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