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