Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
DropdownListSearchPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2015 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.keywordsearch;
20 
21 import java.awt.*;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.logging.Level;
30 import javax.swing.JCheckBox;
31 import javax.swing.JTable;
32 import javax.swing.ListSelectionModel;
33 import javax.swing.event.ListSelectionEvent;
34 import javax.swing.event.ListSelectionListener;
35 import javax.swing.table.AbstractTableModel;
36 import javax.swing.table.TableCellRenderer;
37 import javax.swing.table.TableColumn;
38 import org.openide.util.NbBundle;
39 import org.openide.util.actions.SystemAction;
42 import javax.swing.ImageIcon;
43 import static javax.swing.SwingConstants.CENTER;
44 import javax.swing.table.DefaultTableCellRenderer;
45 
50 class DropdownListSearchPanel extends KeywordSearchPanel {
51 
52  private static final Logger logger = Logger.getLogger(DropdownListSearchPanel.class.getName());
53  private static DropdownListSearchPanel instance;
54  private XmlKeywordSearchList loader;
55  private final KeywordListsTableModel listsTableModel;
56  private final KeywordsTableModel keywordsTableModel;
57  private ActionListener searchAddListener;
58  private boolean ingestRunning;
59 
63  private DropdownListSearchPanel() {
64  listsTableModel = new KeywordListsTableModel();
65  keywordsTableModel = new KeywordsTableModel();
66  initComponents();
67  customizeComponents();
68  }
69 
70  static synchronized DropdownListSearchPanel getDefault() {
71  if (instance == null) {
72  instance = new DropdownListSearchPanel();
73  }
74  return instance;
75  }
76 
77  private void customizeComponents() {
78  listsTable.setTableHeader(null);
79  listsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
80  //customize column witdhs
81  final int leftWidth = leftPane.getPreferredSize().width;
82  TableColumn column;
83  for (int i = 0; i < listsTable.getColumnCount(); i++) {
84  column = listsTable.getColumnModel().getColumn(i);
85  if (i == 0) {
86  column.setPreferredWidth(((int) (leftWidth * 0.10)));
87  column.setCellRenderer(new LeftCheckBoxRenderer());
88  } else {
89  column.setPreferredWidth(((int) (leftWidth * 0.89)));
90  }
91  }
92  final int rightWidth = rightPane.getPreferredSize().width;
93  for (int i = 0; i < keywordsTable.getColumnCount(); i++) {
94  column = keywordsTable.getColumnModel().getColumn(i);
95  if (i == 0) {
96  column.setPreferredWidth(((int) (rightWidth * 0.78)));
97  } else {
98  column.setPreferredWidth(((int) (rightWidth * 0.20)));
99  column.setCellRenderer(new CheckBoxRenderer());
100  }
101  }
102 
103  loader = XmlKeywordSearchList.getCurrent();
104  listsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
105  @Override
106  public void valueChanged(ListSelectionEvent e) {
107  ListSelectionModel listSelectionModel = (ListSelectionModel) e.getSource();
108  if (!listSelectionModel.isSelectionEmpty()) {
109  int index = listSelectionModel.getMinSelectionIndex();
110  KeywordList list = listsTableModel.getListAt(index);
111  keywordsTableModel.resync(list);
112  } else {
113  keywordsTableModel.deleteAll();
114  }
115  }
116  });
117 
118  ingestRunning = IngestManager.getInstance().isIngestRunning();
119  updateComponents();
120 
121  IngestManager.getInstance().addIngestJobEventListener(new PropertyChangeListener() {
122  @Override
123  public void propertyChange(PropertyChangeEvent evt) {
124  Object source = evt.getSource();
125  if (source instanceof String && ((String) source).equals("LOCAL")) { //NON-NLS
126  EventQueue.invokeLater(new Runnable() {
127  @Override
128  public void run() {
129  ingestRunning = IngestManager.getInstance().isIngestRunning();
130  updateComponents();
131  }
132  });
133  }
134  }
135  });
136 
137  searchAddListener = new ActionListener() {
138  @Override
139  public void actionPerformed(ActionEvent e) {
140  if (ingestRunning) {
141  SearchRunner.getInstance().addKeywordListsToAllJobs(listsTableModel.getSelectedLists());
142  logger.log(Level.INFO, "Submitted enqueued lists to ingest"); //NON-NLS
143  } else {
144  searchAction(e);
145  }
146  }
147  };
148 
149  searchAddButton.addActionListener(searchAddListener);
150  }
151 
152  private void updateComponents() {
153  ingestRunning = IngestManager.getInstance().isIngestRunning();
154  if (ingestRunning) {
155  searchAddButton.setText(NbBundle.getMessage(this.getClass(), "KeywordSearchListsViewerPanel.initIngest.addIngestTitle"));
156  searchAddButton.setToolTipText(NbBundle.getMessage(this.getClass(), "KeywordSearchListsViewerPanel.initIngest.addIngestMsg"));
157 
158  } else {
159  searchAddButton.setText(NbBundle.getMessage(this.getClass(), "KeywordSearchListsViewerPanel.initIngest.searchIngestTitle"));
160  searchAddButton.setToolTipText(NbBundle.getMessage(this.getClass(), "KeywordSearchListsViewerPanel.initIngest.addIdxSearchMsg"));
161  }
162  listsTableModel.resync();
163  updateIngestIndexLabel();
164  }
165 
166  private void updateIngestIndexLabel() {
167  if (ingestRunning) {
168  ingestIndexLabel.setText(NbBundle.getMessage(this.getClass(), "KeywordSearchListsViewerPanel.initIngest.ongoingIngestMsg", filesIndexed));
169  } else {
170  ingestIndexLabel.setText(NbBundle.getMessage(this.getClass(), "KeywordSearchListsViewerPanel.initIngest.fileIndexCtMsg", filesIndexed));
171  }
172  }
173 
174  @Override
175  protected void postFilesIndexedChange() {
176  updateIngestIndexLabel();
177  }
178 
182  void resync() {
183  listsTableModel.resync();
184  }
185 
191  @SuppressWarnings("unchecked")
192  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
193  private void initComponents() {
194 
195  jSplitPane1 = new javax.swing.JSplitPane();
196  leftPane = new javax.swing.JScrollPane();
197  listsTable = new javax.swing.JTable();
198  rightPane = new javax.swing.JScrollPane();
199  keywordsTable = new javax.swing.JTable();
200  manageListsButton = new javax.swing.JButton();
201  searchAddButton = new javax.swing.JButton();
202  ingestIndexLabel = new javax.swing.JLabel();
203 
204  setFont(getFont().deriveFont(getFont().getStyle() & ~java.awt.Font.BOLD, 11));
205 
206  jSplitPane1.setFont(leftPane.getFont());
207 
208  leftPane.setFont(leftPane.getFont().deriveFont(leftPane.getFont().getStyle() & ~java.awt.Font.BOLD, 11));
209  leftPane.setMinimumSize(new java.awt.Dimension(150, 23));
210 
211  listsTable.setBackground(new java.awt.Color(240, 240, 240));
212  listsTable.setFont(listsTable.getFont().deriveFont(listsTable.getFont().getStyle() & ~java.awt.Font.BOLD, 11));
213  listsTable.setModel(listsTableModel);
214  listsTable.setShowHorizontalLines(false);
215  listsTable.setShowVerticalLines(false);
216  listsTable.getTableHeader().setReorderingAllowed(false);
217  leftPane.setViewportView(listsTable);
218 
219  jSplitPane1.setLeftComponent(leftPane);
220 
221  rightPane.setFont(rightPane.getFont().deriveFont(rightPane.getFont().getStyle() & ~java.awt.Font.BOLD, 11));
222 
223  keywordsTable.setBackground(new java.awt.Color(240, 240, 240));
224  keywordsTable.setFont(keywordsTable.getFont().deriveFont(keywordsTable.getFont().getStyle() & ~java.awt.Font.BOLD, 11));
225  keywordsTable.setModel(keywordsTableModel);
226  keywordsTable.setGridColor(new java.awt.Color(153, 153, 153));
227  rightPane.setViewportView(keywordsTable);
228 
229  jSplitPane1.setRightComponent(rightPane);
230 
231  manageListsButton.setFont(manageListsButton.getFont().deriveFont(manageListsButton.getFont().getStyle() & ~java.awt.Font.BOLD, 11));
232  manageListsButton.setText(org.openide.util.NbBundle.getMessage(DropdownListSearchPanel.class, "KeywordSearchListsViewerPanel.manageListsButton.text")); // NOI18N
233  manageListsButton.setToolTipText(org.openide.util.NbBundle.getMessage(DropdownListSearchPanel.class, "KeywordSearchListsViewerPanel.manageListsButton.toolTipText")); // NOI18N
234  manageListsButton.addActionListener(new java.awt.event.ActionListener() {
235  public void actionPerformed(java.awt.event.ActionEvent evt) {
236  manageListsButtonActionPerformed(evt);
237  }
238  });
239 
240  searchAddButton.setFont(searchAddButton.getFont().deriveFont(searchAddButton.getFont().getStyle() & ~java.awt.Font.BOLD, 11));
241  searchAddButton.setText(org.openide.util.NbBundle.getMessage(DropdownListSearchPanel.class, "KeywordSearchListsViewerPanel.searchAddButton.text")); // NOI18N
242  searchAddButton.addActionListener(new java.awt.event.ActionListener() {
243  public void actionPerformed(java.awt.event.ActionEvent evt) {
244  searchAddButtonActionPerformed(evt);
245  }
246  });
247 
248  ingestIndexLabel.setFont(ingestIndexLabel.getFont().deriveFont(ingestIndexLabel.getFont().getStyle() & ~java.awt.Font.BOLD, 10));
249  ingestIndexLabel.setText(org.openide.util.NbBundle.getMessage(DropdownListSearchPanel.class, "KeywordSearchListsViewerPanel.ingestIndexLabel.text")); // NOI18N
250 
251  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
252  this.setLayout(layout);
253  layout.setHorizontalGroup(
254  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
255  .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
256  .addGroup(layout.createSequentialGroup()
257  .addContainerGap()
258  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
259  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
260  .addComponent(searchAddButton)
261  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 220, Short.MAX_VALUE)
262  .addComponent(manageListsButton))
263  .addGroup(layout.createSequentialGroup()
264  .addComponent(ingestIndexLabel)
265  .addGap(0, 317, Short.MAX_VALUE)))
266  .addContainerGap())
267  );
268  layout.setVerticalGroup(
269  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
270  .addGroup(layout.createSequentialGroup()
271  .addComponent(jSplitPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
272  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
273  .addComponent(ingestIndexLabel)
274  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
275  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
276  .addComponent(manageListsButton)
277  .addComponent(searchAddButton))
278  .addContainerGap())
279  );
280  }// </editor-fold>//GEN-END:initComponents
281 
282  private void manageListsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_manageListsButtonActionPerformed
283  SystemAction.get(KeywordSearchConfigurationAction.class).performAction();
284  }//GEN-LAST:event_manageListsButtonActionPerformed
285 
286  private void searchAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchAddButtonActionPerformed
287  // TODO add your handling code here:
288  }//GEN-LAST:event_searchAddButtonActionPerformed
289 
290  // Variables declaration - do not modify//GEN-BEGIN:variables
291  private javax.swing.JLabel ingestIndexLabel;
292  private javax.swing.JSplitPane jSplitPane1;
293  private javax.swing.JTable keywordsTable;
294  private javax.swing.JScrollPane leftPane;
295  private javax.swing.JTable listsTable;
296  private javax.swing.JButton manageListsButton;
297  private javax.swing.JScrollPane rightPane;
298  private javax.swing.JButton searchAddButton;
299  // End of variables declaration//GEN-END:variables
300 
301  private void searchAction(ActionEvent e) {
302  setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
303 
304  try {
305  search();
306  } finally {
307  setCursor(null);
308  }
309  }
310 
311  @Override
312  List<KeywordList> getKeywordLists() {
313  return listsTableModel.getSelectedListsL();
314  }
315 
316  void addSearchButtonActionListener(ActionListener al) {
317  searchAddButton.addActionListener(al);
318  }
319 
320  private class KeywordListsTableModel extends AbstractTableModel {
321  //data
322 
323  private XmlKeywordSearchList listsHandle = XmlKeywordSearchList.getCurrent();
324  private List<ListTableEntry> listData = new ArrayList<>();
325 
326  @Override
327  public int getColumnCount() {
328  return 2;
329  }
330 
331  @Override
332  public int getRowCount() {
333  return listData.size();
334  }
335 
336  @Override
337  public String getColumnName(int column) {
338  String ret = null;
339  switch (column) {
340  case 0:
341  ret = NbBundle.getMessage(this.getClass(), "KeywordSearch.selectedColLbl");
342  break;
343  case 1:
344  ret = NbBundle.getMessage(this.getClass(), "KeywordSearch.nameColLbl");
345  break;
346  default:
347  break;
348  }
349  return ret;
350  }
351 
352  @Override
353  public Object getValueAt(int rowIndex, int columnIndex) {
354  Object ret = null;
355  ListTableEntry entry = null;
356  //iterate until row
357  Iterator<ListTableEntry> it = listData.iterator();
358  for (int i = 0; i <= rowIndex; ++i) {
359  entry = it.next();
360  }
361  if (null != entry) {
362  switch (columnIndex) {
363  case 0:
364  ret = (Object) entry.selected;
365  break;
366  case 1:
367  ret = (Object) entry.name;
368  break;
369  default:
370  break;
371  }
372  }
373  return ret;
374  }
375 
376  @Override
377  public boolean isCellEditable(int rowIndex, int columnIndex) {
378  return (columnIndex == 0 && !ingestRunning);
379  }
380 
381  @Override
382  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
383  if (columnIndex == 0) {
384  ListTableEntry entry = null;
385  Iterator<ListTableEntry> it = listData.iterator();
386  for (int i = 0; i <= rowIndex; i++) {
387  entry = it.next();
388  }
389  if (entry != null) {
390  entry.selected = (Boolean) aValue;
391  if (ingestRunning) {
392  //updateUseForIngest(getListAt(rowIndex), (Boolean) aValue);
393  }
394  }
395 
396  }
397  }
398 
399  @Override
400  public Class<?> getColumnClass(int c) {
401  return getValueAt(0, c).getClass();
402  }
403 
404  List<String> getAllLists() {
405  List<String> ret = new ArrayList<>();
406  for (ListTableEntry e : listData) {
407  ret.add(e.name);
408  }
409  return ret;
410  }
411 
412  KeywordList getListAt(int rowIndex) {
413  return listsHandle.getList((String) getValueAt(rowIndex, 1));
414  }
415 
416  List<String> getSelectedLists() {
417  List<String> ret = new ArrayList<>();
418  for (ListTableEntry e : listData) {
419  if (e.selected) {
420  ret.add(e.name);
421  }
422  }
423  return ret;
424  }
425 
426  List<KeywordList> getSelectedListsL() {
427  List<KeywordList> ret = new ArrayList<>();
428  for (String s : getSelectedLists()) {
429  ret.add(listsHandle.getList(s));
430  }
431  return ret;
432  }
433 
434  boolean listExists(String list) {
435  List<String> all = getAllLists();
436  return all.contains(list);
437  }
438 
439  //resync model from handle, then update table
440  void resync() {
441  listData.clear();
442  addLists(listsHandle.getListsL());
443  fireTableDataChanged();
444  }
445 
446  //add lists to the model
447  private void addLists(List<KeywordList> lists) {
448  for (KeywordList list : lists) {
449  if (!listExists(list.getName())) {
450  listData.add(new ListTableEntry(list, ingestRunning));
451  }
452  }
453  }
454 
455  //single model entry
456  private class ListTableEntry implements Comparable<ListTableEntry> {
457 
458  String name;
459  Boolean selected;
460 
461  ListTableEntry(KeywordList list, boolean ingestRunning) {
462  this.name = list.getName();
463  if (ingestRunning) {
464  this.selected = list.getUseForIngest();
465  } else {
466  this.selected = false;
467  }
468  }
469 
470  @Override
471  public int compareTo(ListTableEntry e) {
472  return this.name.compareTo(e.name);
473  }
474  }
475  }
476 
477  private class KeywordsTableModel extends AbstractTableModel {
478 
479  List<KeywordTableEntry> listData = new ArrayList<>();
480 
481  @Override
482  public int getRowCount() {
483  return listData.size();
484  }
485 
486  @Override
487  public int getColumnCount() {
488  return 2;
489  }
490 
491  @Override
492  public String getColumnName(int column) {
493  String ret = null;
494  switch (column) {
495  case 0:
496  ret = NbBundle.getMessage(this.getClass(), "KeywordSearch.nameColLbl");
497  break;
498  case 1:
499  ret = NbBundle.getMessage(this.getClass(), "KeywordSearch.regExColLbl");
500  break;
501  default:
502  break;
503  }
504  return ret;
505  }
506 
507  @Override
508  public Object getValueAt(int rowIndex, int columnIndex) {
509  Object ret = null;
510  KeywordTableEntry entry = null;
511  //iterate until row
512  Iterator<KeywordTableEntry> it = listData.iterator();
513  for (int i = 0; i <= rowIndex; ++i) {
514  entry = it.next();
515  }
516  if (null != entry) {
517  switch (columnIndex) {
518  case 0:
519  ret = (Object) entry.name;
520  break;
521  case 1:
522  ret = (Object) entry.regex;
523  break;
524  default:
525  break;
526  }
527  }
528  return ret;
529  }
530 
531  @Override
532  public boolean isCellEditable(int rowIndex, int columnIndex) {
533  return false;
534  }
535 
536  @Override
537  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
538  }
539 
540  @Override
541  public Class<?> getColumnClass(int c) {
542  return getValueAt(0, c).getClass();
543  }
544 
545  void resync(KeywordList list) {
546  listData.clear();
547  for (Keyword k : list.getKeywords()) {
548  listData.add(new KeywordTableEntry(k));
549  }
550  fireTableDataChanged();
551  }
552 
553  void deleteAll() {
554  listData.clear();
555  fireTableDataChanged();
556  }
557 
558  //single model entry
559  private class KeywordTableEntry implements Comparable<KeywordTableEntry> {
560 
561  String name;
562  Boolean regex;
563 
564  KeywordTableEntry(Keyword keyword) {
565  this.name = keyword.getSearchTerm();
566  this.regex = !keyword.searchTermIsLiteral();
567  }
568 
569  @Override
570  public int compareTo(KeywordTableEntry e) {
571  return this.name.compareTo(e.name);
572  }
573  }
574  }
575 
576  private class LeftCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
577 
578  @Override
580  JTable table, Object value,
581  boolean isSelected, boolean hasFocus,
582  int row, int column) {
583 
584  this.setHorizontalAlignment(JCheckBox.CENTER);
585  this.setVerticalAlignment(JCheckBox.CENTER);
586 
587  setEnabled(!ingestRunning);
588 
589  boolean selected = (Boolean) table.getModel().getValueAt(row, 0);
590  setSelected(selected);
591 
592  if (isSelected) {
593  setBackground(listsTable.getSelectionBackground());
594  } else {
595  setBackground(listsTable.getBackground());
596  }
597 
598  return this;
599  }
600  }
601 
606  private class CheckBoxRenderer extends DefaultTableCellRenderer {
607 
608  private static final long serialVersionUID = 1L;
609  final ImageIcon theCheck = new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/checkmark.png")); // NON-NLS
610 
611  CheckBoxRenderer() {
612  setHorizontalAlignment(CENTER);
613  }
614 
615  @Override
616  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
617 
618  if ((value instanceof Boolean)) {
619  if ((Boolean) value) {
620  setIcon(theCheck);
621  setToolTipText(Bundle.IsRegularExpression());
622  } else {
623  setIcon(null);
624  setToolTipText(null);
625  }
626  }
627  return this;
628  }
629  }
630 }
Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)

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