Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
FilesIdentifierIngestJobSettingsPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014-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.modules.interestingitems;
20 
21 import java.awt.Component;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Observable;
25 import java.util.Observer;
26 import java.util.TreeMap;
27 import javax.swing.JLabel;
28 import javax.swing.JTable;
29 import javax.swing.table.AbstractTableModel;
30 import javax.swing.table.DefaultTableCellRenderer;
31 import javax.swing.table.TableColumn;
32 import org.openide.util.NbBundle.Messages;
36 
40 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
41 final class FilesIdentifierIngestJobSettingsPanel extends IngestModuleIngestJobSettingsPanel implements Observer {
42 
43  @Messages({
44  "FilesIdentifierIngestJobSettingsPanel.updateError=Error updating interesting files sets settings file.",
45  "FilesIdentifierIngestJobSettingsPanel.getError=Error getting interesting files sets from settings file."
46  })
47 
48  private final FilesSetsTableModel tableModel;
49 
50  // This map of interesting interesting files set names to files sets is used
51  // both to sort interesting files sets by name and to detect when a new
52  // files set is defined, so that the new set can be enabled by default.
53  private TreeMap<String, FilesSet> filesSetSnapshot;
54 
62  static FilesIdentifierIngestJobSettingsPanel makePanel(FilesIdentifierIngestJobSettings settings) {
63  FilesIdentifierIngestJobSettingsPanel panel = new FilesIdentifierIngestJobSettingsPanel(settings);
64 
65  // Observe the interesting item definitions manager for changes to the
66  // interesting file set definitions. This is used to keep this panel in
67  // synch with changes made using the global settings/option panel for
68  // this module.
69  FilesSetsManager.getInstance().addObserver(panel);
70 
71  return panel;
72  }
73 
78  private FilesIdentifierIngestJobSettingsPanel(FilesIdentifierIngestJobSettings settings) {
79  initComponents();
80 
81  /*
82  * Make a table row object for each interesting files set, bundling the
83  * set with an enabled flag. The files sets are loaded into a tree map
84  * so they are sorted by name. The keys set also serves as a cache of
85  * set names so that new sets can be easily detected in the override of
86  * Observer.update().
87  */
88  List<FilesSetRow> filesSetRows = new ArrayList<>();
89  try {
90  this.filesSetSnapshot = new TreeMap<>(FilesSetsManager.getInstance().getInterestingFilesSets());
91  } catch (FilesSetsManager.FilesSetsManagerException ex) {
92  MessageNotifyUtil.Message.error(Bundle.FilesIdentifierIngestJobSettingsPanel_getError());
93  this.filesSetSnapshot = new TreeMap<>();
94  }
95  for (FilesSet set : this.filesSetSnapshot.values()) {
96  filesSetRows.add(new FilesSetRow(set, settings.interestingFilesSetIsEnabled(set.getName())));
97  }
98 
99  // Make a table model to manage the row objects.
100  this.tableModel = new FilesSetsTableModel(filesSetRows);
101 
102  // Set up the table component that presents the table model that allows
103  // users to enable and disable interesting files set definitions for an
104  // ingest job.
105  this.filesSetTable.setModel(tableModel);
106  this.filesSetTable.setTableHeader(null);
107  this.filesSetTable.setRowSelectionAllowed(false);
108  final int width = this.filesSetScrollPane.getPreferredSize().width;
109  this.filesSetTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
110  TableColumn column;
111  for (int i = 0; i < this.filesSetTable.getColumnCount(); i++) {
112  column = this.filesSetTable.getColumnModel().getColumn(i);
113  if (i == 0) {
114  column.setPreferredWidth(((int) (width * 0.07)));
115  } else {
116  column.setCellRenderer(new FileSetsTableCellRenderer());
117  column.setPreferredWidth(((int) (width * 0.92)));
118  }
119  }
120  }
121 
122  @Override
123  public IngestModuleIngestJobSettings getSettings() {
124  List<String> enabledInterestingFilesSets = new ArrayList<>();
125  List<String> disabledInterestingFilesSets = new ArrayList<>();
126  for (FilesSetRow rowModel : this.tableModel.filesSetRows) {
127  if (rowModel.isEnabled()) {
128  enabledInterestingFilesSets.add(rowModel.getFilesSet().getName());
129  } else {
130  disabledInterestingFilesSets.add(rowModel.getFilesSet().getName());
131  }
132  }
133  return new FilesIdentifierIngestJobSettings(enabledInterestingFilesSets, disabledInterestingFilesSets);
134  }
135 
136  @Override
137  public void update(Observable o, Object arg
138  ) {
139  // Get the user's current enabled/disabled settings.
140  FilesIdentifierIngestJobSettings settings = (FilesIdentifierIngestJobSettings) this.getSettings();
141 
142  // Refresh the view of the interesting files set definitions.
143  List<FilesSetRow> rowModels = new ArrayList<>();
144  TreeMap<String, FilesSet> newFilesSetSnapshot;
145  try {
146  newFilesSetSnapshot = new TreeMap<>(FilesSetsManager.getInstance().getInterestingFilesSets());
147  } catch (FilesSetsManager.FilesSetsManagerException ex) {
148  MessageNotifyUtil.Message.error(Bundle.FilesIdentifierIngestJobSettingsPanel_updateError());
149  return;
150  }
151  for (FilesSet set : newFilesSetSnapshot.values()) {
152  if (this.filesSetSnapshot.keySet().contains(set.getName())) {
153  // Preserve the current enabled/diabled state of the set.
154  rowModels.add(new FilesSetRow(set, settings.interestingFilesSetIsEnabled(set.getName())));
155  } else {
156  // New sets are enabled by default.
157  rowModels.add(new FilesSetRow(set, true));
158  }
159  }
160  this.tableModel.resetTableData(rowModels);
161 
162  // Cache the snapshot so it will be avaialble for the next update.
163  this.filesSetSnapshot = newFilesSetSnapshot;
164  }
165 
169  private static final class FileSetsTableCellRenderer extends DefaultTableCellRenderer {
170 
171  private static final long serialVersionUID = 1L;
172 
173  @Override
175  JTable table, Object value,
176  boolean isSelected, boolean hasFocus,
177  int row, int column) {
178  JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
179  label.setToolTipText(label.getText());
180  return label;
181  }
182  }
183 
188  private final static class FilesSetsTableModel extends AbstractTableModel {
189 
190  private List<FilesSetRow> filesSetRows;
191 
200  FilesSetsTableModel(List<FilesSetRow> filesSetRows) {
201  this.filesSetRows = filesSetRows;
202  }
203 
210  void resetTableData(List<FilesSetRow> filesSetRows) {
211  this.filesSetRows = filesSetRows;
212  this.fireTableDataChanged();
213  }
214 
215  @Override
216  public int getRowCount() {
217  return this.filesSetRows.size();
218  }
219 
220  @Override
221  public int getColumnCount() {
222  return 2;
223  }
224 
225  @Override
226  public Object getValueAt(int rowIndex, int columnIndex) {
227  if (columnIndex == 0) {
228  return this.filesSetRows.get(rowIndex).isEnabled();
229  } else {
230  return this.filesSetRows.get(rowIndex).getFilesSet().getName();
231  }
232  }
233 
234  @Override
235  public boolean isCellEditable(int rowIndex, int columnIndex) {
236  return (columnIndex == 0);
237  }
238 
239  @Override
240  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
241  if (columnIndex == 0) {
242  this.filesSetRows.get(rowIndex).setEnabled((Boolean) aValue);
243  }
244  }
245 
246  @Override
247  public Class<?> getColumnClass(int c) {
248  return getValueAt(0, c).getClass();
249  }
250  }
251 
255  private final static class FilesSetRow {
256 
257  private final FilesSet set;
258  private boolean enabled;
259 
260  FilesSetRow(FilesSet set, boolean enabled) {
261  this.set = set;
262  this.enabled = enabled;
263  }
264 
265  FilesSet getFilesSet() {
266  return this.set;
267  }
268 
269  boolean isEnabled() {
270  return this.enabled;
271  }
272 
273  void setEnabled(boolean enabled) {
274  this.enabled = enabled;
275  }
276  }
277 
283  @SuppressWarnings("unchecked")
284  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
285  private void initComponents() {
286 
287  filesSetScrollPane = new javax.swing.JScrollPane();
288  filesSetTable = new javax.swing.JTable();
289 
290  setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(FilesIdentifierIngestJobSettingsPanel.class, "FilesIdentifierIngestJobSettingsPanel.border.title"))); // NOI18N
291 
292  filesSetTable.setBackground(new java.awt.Color(240, 240, 240));
293  filesSetTable.setModel(new javax.swing.table.DefaultTableModel(
294  new Object [][] {
295 
296  },
297  new String [] {
298 
299  }
300  ));
301  filesSetTable.setShowHorizontalLines(false);
302  filesSetTable.setShowVerticalLines(false);
303  filesSetScrollPane.setViewportView(filesSetTable);
304 
305  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
306  this.setLayout(layout);
307  layout.setHorizontalGroup(
308  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
309  .addGroup(layout.createSequentialGroup()
310  .addContainerGap()
311  .addComponent(filesSetScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
312  .addContainerGap())
313  );
314  layout.setVerticalGroup(
315  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
316  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
317  .addContainerGap()
318  .addComponent(filesSetScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE)
319  .addContainerGap())
320  );
321  }// </editor-fold>//GEN-END:initComponents
322 
323  // Variables declaration - do not modify//GEN-BEGIN:variables
324  private javax.swing.JScrollPane filesSetScrollPane;
325  private javax.swing.JTable filesSetTable;
326  // End of variables declaration//GEN-END:variables
327 }
Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

Copyright © 2012-2022 Basis Technology. Generated on: Fri Jan 27 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.