Autopsy  4.20.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DropdownSingleTermSearchPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-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.keywordsearch;
20 
21 import java.awt.EventQueue;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.awt.event.FocusEvent;
25 import java.awt.event.FocusListener;
26 import java.beans.PropertyChangeEvent;
27 import java.beans.PropertyChangeListener;
28 import java.util.ArrayList;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Set;
32 import java.util.logging.Level;
33 import javax.swing.JMenuItem;
34 import javax.swing.event.ListSelectionEvent;
35 import org.openide.util.NbBundle;
38 
53 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
54 public class DropdownSingleTermSearchPanel extends AdHocSearchPanel {
55 
56  private static final long serialVersionUID = 1L;
57  private static final Logger LOGGER = Logger.getLogger(DropdownSingleTermSearchPanel.class.getName());
58  private static DropdownSingleTermSearchPanel defaultInstance = null;
59  private boolean ingestRunning;
60 
61 
69  public static synchronized DropdownSingleTermSearchPanel getDefault() {
70  if (null == defaultInstance) {
71  defaultInstance = new DropdownSingleTermSearchPanel();
72  }
73  return defaultInstance;
74  }
75 
80  @NbBundle.Messages({"DropdownSingleTermSearchPanel.selected=Ad Hoc Search data source filter is selected"})
82  initComponents();
83  customizeComponents();
84  dataSourceList.setModel(getDataSourceListModel());
85  this.dataSourceList.addListSelectionListener((ListSelectionEvent evt) -> {
86  firePropertyChange(Bundle.DropdownSingleTermSearchPanel_selected(), null, null);
87  });
88  }
89 
94  private void customizeComponents() {
95  keywordTextField.addFocusListener(new FocusListener() {
96  @Override
97  public void focusGained(FocusEvent e) {
98  }
99 
100  @Override
101  public void focusLost(FocusEvent e) {
102  if (keywordTextField.getText().isEmpty()) {
103  clearSearchBox();
104  }
105  }
106  });
107 
108  keywordTextField.setComponentPopupMenu(rightClickMenu);
109  ActionListener actList = (ActionEvent e) -> {
110  JMenuItem jmi = (JMenuItem) e.getSource();
111  if (jmi.equals(cutMenuItem)) {
112  keywordTextField.cut();
113  } else if (jmi.equals(copyMenuItem)) {
114  keywordTextField.copy();
115  } else if (jmi.equals(pasteMenuItem)) {
116  keywordTextField.paste();
117  } else if (jmi.equals(selectAllMenuItem)) {
118  keywordTextField.selectAll();
119  }
120  };
121  ingestRunning = IngestManager.getInstance().isIngestRunning();
122  updateIngestIndexLabel();
123 
124  jSaveSearchResults.setSelected(true);
125 
126  IngestManager.getInstance().addIngestJobEventListener(new PropertyChangeListener() {
127  @Override
128  public void propertyChange(PropertyChangeEvent evt) {
129  Object source = evt.getSource();
130  if (source instanceof String && ((String) source).equals("LOCAL")) { //NON-NLS
131  EventQueue.invokeLater(() -> {
132  ingestRunning = IngestManager.getInstance().isIngestRunning();
133  updateIngestIndexLabel();
134  });
135  }
136  }
137  });
138 
139  cutMenuItem.addActionListener(actList);
140  copyMenuItem.addActionListener(actList);
141  pasteMenuItem.addActionListener(actList);
142  selectAllMenuItem.addActionListener(actList);
143  }
144 
150  void addSearchButtonActionListener(ActionListener actionListener) {
151  searchButton.addActionListener(actionListener);
152  }
153 
158  void clearSearchBox() {
159  keywordTextField.setText("");
160  }
161 
162  void setRegexSearchEnabled(boolean enabled) {
163  exactRadioButton.setSelected(true);
164  regexRadioButton.setEnabled(enabled);
165  }
166 
173  @NbBundle.Messages({"DropdownSingleTermSearchPanel.warning.title=Warning",
174  "DropdownSingleTermSearchPanel.warning.text=Boundary characters ^ and $ do not match word boundaries. Consider\nreplacing with an explicit list of boundary characters, such as [ \\.,]"})
175  @Override
176  List<KeywordList> getKeywordLists() {
177  if (regexRadioButton.isSelected()) {
178  if((keywordTextField.getText() != null) &&
179  (keywordTextField.getText().startsWith("^") ||
180  (keywordTextField.getText().endsWith("$") && ! keywordTextField.getText().endsWith("\\$")))) {
181 
182  KeywordSearchUtil.displayDialog(NbBundle.getMessage(this.getClass(), "DropdownSingleTermSearchPanel.warning.title"),
183  NbBundle.getMessage(this.getClass(), "DropdownSingleTermSearchPanel.warning.text"),
184  KeywordSearchUtil.DIALOG_MESSAGE_TYPE.WARN);
185  }
186  }
187  List<Keyword> keywords = new ArrayList<>();
188  keywords.add(new Keyword(keywordTextField.getText(), !regexRadioButton.isSelected(), exactRadioButton.isSelected()));
189  List<KeywordList> keywordLists = new ArrayList<>();
190  keywordLists.add(new KeywordList(keywords));
191  return keywordLists;
192  }
193 
197  @Override
198  protected void postFilesIndexedChange() {
199  updateIngestIndexLabel();
200  }
201 
207  @SuppressWarnings("unchecked")
208  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
209  private void initComponents() {
210 
211  queryTypeButtonGroup = new javax.swing.ButtonGroup();
212  rightClickMenu = new javax.swing.JPopupMenu();
213  cutMenuItem = new javax.swing.JMenuItem();
214  copyMenuItem = new javax.swing.JMenuItem();
215  pasteMenuItem = new javax.swing.JMenuItem();
216  selectAllMenuItem = new javax.swing.JMenuItem();
217  keywordTextField = new javax.swing.JTextField();
218  searchButton = new javax.swing.JButton();
219  exactRadioButton = new javax.swing.JRadioButton();
220  substringRadioButton = new javax.swing.JRadioButton();
221  regexRadioButton = new javax.swing.JRadioButton();
222  dataSourceCheckBox = new javax.swing.JCheckBox();
223  jScrollPane1 = new javax.swing.JScrollPane();
224  dataSourceList = new javax.swing.JList<>();
225  ingestIndexLabel = new javax.swing.JLabel();
226  jSaveSearchResults = new javax.swing.JCheckBox();
227 
228  org.openide.awt.Mnemonics.setLocalizedText(cutMenuItem, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSearchPanel.cutMenuItem.text")); // NOI18N
229  rightClickMenu.add(cutMenuItem);
230 
231  org.openide.awt.Mnemonics.setLocalizedText(copyMenuItem, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSearchPanel.copyMenuItem.text")); // NOI18N
232  rightClickMenu.add(copyMenuItem);
233 
234  org.openide.awt.Mnemonics.setLocalizedText(pasteMenuItem, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSearchPanel.pasteMenuItem.text")); // NOI18N
235  rightClickMenu.add(pasteMenuItem);
236 
237  org.openide.awt.Mnemonics.setLocalizedText(selectAllMenuItem, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSearchPanel.selectAllMenuItem.text")); // NOI18N
238  rightClickMenu.add(selectAllMenuItem);
239 
240  keywordTextField.setFont(keywordTextField.getFont().deriveFont(keywordTextField.getFont().getSize()+3f));
241  keywordTextField.setText(org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSearchPanel.keywordTextField.text")); // NOI18N
242  keywordTextField.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(192, 192, 192), 1, true));
243  keywordTextField.setMinimumSize(new java.awt.Dimension(2, 25));
244  keywordTextField.setPreferredSize(new java.awt.Dimension(2, 25));
245  keywordTextField.addMouseListener(new java.awt.event.MouseAdapter() {
246  public void mouseClicked(java.awt.event.MouseEvent evt) {
247  keywordTextFieldMouseClicked(evt);
248  }
249  });
250  keywordTextField.addActionListener(new java.awt.event.ActionListener() {
251  public void actionPerformed(java.awt.event.ActionEvent evt) {
252  keywordTextFieldActionPerformed(evt);
253  }
254  });
255 
256  searchButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/search-icon.png"))); // NOI18N
257  org.openide.awt.Mnemonics.setLocalizedText(searchButton, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSearchPanel.searchButton.text")); // NOI18N
258  searchButton.addActionListener(new java.awt.event.ActionListener() {
259  public void actionPerformed(java.awt.event.ActionEvent evt) {
260  searchButtonActionPerformed(evt);
261  }
262  });
263 
264  queryTypeButtonGroup.add(exactRadioButton);
265  org.openide.awt.Mnemonics.setLocalizedText(exactRadioButton, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSearchPanel.exactRadioButton.text")); // NOI18N
266 
267  queryTypeButtonGroup.add(substringRadioButton);
268  org.openide.awt.Mnemonics.setLocalizedText(substringRadioButton, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSearchPanel.substringRadioButton.text")); // NOI18N
269 
270  queryTypeButtonGroup.add(regexRadioButton);
271  org.openide.awt.Mnemonics.setLocalizedText(regexRadioButton, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSearchPanel.regexRadioButton.text")); // NOI18N
272 
273  org.openide.awt.Mnemonics.setLocalizedText(dataSourceCheckBox, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSingleTermSearchPanel.dataSourceCheckBox.text")); // NOI18N
274  dataSourceCheckBox.addActionListener(new java.awt.event.ActionListener() {
275  public void actionPerformed(java.awt.event.ActionEvent evt) {
276  dataSourceCheckBoxActionPerformed(evt);
277  }
278  });
279 
280  dataSourceList.setMinimumSize(new java.awt.Dimension(0, 200));
281  jScrollPane1.setViewportView(dataSourceList);
282 
283  ingestIndexLabel.setFont(ingestIndexLabel.getFont().deriveFont(ingestIndexLabel.getFont().getSize()-1f));
284  org.openide.awt.Mnemonics.setLocalizedText(ingestIndexLabel, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSingleTermSearchPanel.ingestIndexLabel.text")); // NOI18N
285 
286  org.openide.awt.Mnemonics.setLocalizedText(jSaveSearchResults, org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSingleTermSearchPanel.jSaveSearchResults.text")); // NOI18N
287  jSaveSearchResults.setToolTipText(org.openide.util.NbBundle.getMessage(DropdownSingleTermSearchPanel.class, "DropdownSingleTermSearchPanel.jSaveSearchResults.toolTipText")); // NOI18N
288 
289  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
290  this.setLayout(layout);
291  layout.setHorizontalGroup(
292  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
293  .addGroup(layout.createSequentialGroup()
294  .addGap(5, 5, 5)
295  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
296  .addGroup(layout.createSequentialGroup()
297  .addComponent(exactRadioButton)
298  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
299  .addComponent(substringRadioButton)
300  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
301  .addComponent(regexRadioButton))
302  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 297, javax.swing.GroupLayout.PREFERRED_SIZE)
303  .addComponent(jSaveSearchResults)
304  .addGroup(layout.createSequentialGroup()
305  .addComponent(searchButton)
306  .addGap(18, 18, 18)
307  .addComponent(ingestIndexLabel))
308  .addComponent(keywordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
309  .addComponent(dataSourceCheckBox))
310  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
311  );
312  layout.setVerticalGroup(
313  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
314  .addGroup(layout.createSequentialGroup()
315  .addContainerGap()
316  .addComponent(keywordTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
317  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
318  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
319  .addComponent(exactRadioButton)
320  .addComponent(substringRadioButton)
321  .addComponent(regexRadioButton))
322  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
323  .addComponent(dataSourceCheckBox)
324  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
325  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
326  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
327  .addComponent(jSaveSearchResults)
328  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
329  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
330  .addComponent(searchButton)
331  .addComponent(ingestIndexLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
332  .addContainerGap(22, Short.MAX_VALUE))
333  );
334  }// </editor-fold>//GEN-END:initComponents
335 
341  private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
342  keywordTextFieldActionPerformed(evt);
343  }//GEN-LAST:event_searchButtonActionPerformed
344 
350  private void keywordTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keywordTextFieldActionPerformed
351  try {
352  search(jSaveSearchResults.isSelected());
353  } catch (Exception e) {
354  LOGGER.log(Level.SEVERE, "Error performing ad hoc single keyword search", e); //NON-NLS
355  }
356  }//GEN-LAST:event_keywordTextFieldActionPerformed
357 
363  private void keywordTextFieldMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_keywordTextFieldMouseClicked
364  if (evt.isPopupTrigger()) {
365  rightClickMenu.show(evt.getComponent(), evt.getX(), evt.getY());
366  }
367  }//GEN-LAST:event_keywordTextFieldMouseClicked
368 
369  private void dataSourceCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dataSourceCheckBoxActionPerformed
370  updateDataSourceListModel();
371  }//GEN-LAST:event_dataSourceCheckBoxActionPerformed
372 
376  void updateDataSourceListModel() {
377  getDataSourceListModel().removeAllElements();
378  for (String dsName : getDataSourceArray()) {
379  getDataSourceListModel().addElement(dsName);
380  }
381  setComponentsEnabled();
382  firePropertyChange(Bundle.DropdownSingleTermSearchPanel_selected(), null, null);
383  }
384 
388  private void setComponentsEnabled() {
389  if (getDataSourceListModel().size() > 1) {
390  this.dataSourceCheckBox.setEnabled(true);
391 
392  boolean enabled = this.dataSourceCheckBox.isSelected();
393  this.dataSourceList.setEnabled(enabled);
394  if (enabled) {
395  this.dataSourceList.setSelectionInterval(0, this.dataSourceList.getModel().getSize()-1);
396  } else {
397  this.dataSourceList.setSelectedIndices(new int[0]);
398  }
399  } else {
400  this.dataSourceCheckBox.setEnabled(false);
401  this.dataSourceCheckBox.setSelected(false);
402  this.dataSourceList.setEnabled(false);
403  this.dataSourceList.setSelectedIndices(new int[0]);
404  }
405  }
406 
411  @Override
412  Set<Long> getDataSourcesSelected() {
413  Set<Long> dataSourceObjIdSet = new HashSet<>();
414  for (Long key : getDataSourceMap().keySet()) {
415  String value = getDataSourceMap().get(key);
416  for (String dataSource : this.dataSourceList.getSelectedValuesList()) {
417  if (value.equals(dataSource)) {
418  dataSourceObjIdSet.add(key);
419  }
420  }
421  }
422  return dataSourceObjIdSet;
423  }
424 
428  private void updateIngestIndexLabel() {
429  if (ingestRunning) {
430  ingestIndexLabel.setText(NbBundle.getMessage(this.getClass(), "KeywordSearchListsViewerPanel.initIngest.ongoingIngestMsg", filesIndexed));
431  } else {
432  ingestIndexLabel.setText(NbBundle.getMessage(this.getClass(), "KeywordSearchListsViewerPanel.initIngest.fileIndexCtMsg", filesIndexed));
433  }
434  }
435 
436  // Variables declaration - do not modify//GEN-BEGIN:variables
437  private javax.swing.JMenuItem copyMenuItem;
438  private javax.swing.JMenuItem cutMenuItem;
439  private javax.swing.JCheckBox dataSourceCheckBox;
440  private javax.swing.JList<String> dataSourceList;
441  private javax.swing.JRadioButton exactRadioButton;
442  private javax.swing.JLabel ingestIndexLabel;
443  private javax.swing.JCheckBox jSaveSearchResults;
444  private javax.swing.JScrollPane jScrollPane1;
445  private javax.swing.JTextField keywordTextField;
446  private javax.swing.JMenuItem pasteMenuItem;
447  private javax.swing.ButtonGroup queryTypeButtonGroup;
448  private javax.swing.JRadioButton regexRadioButton;
449  private javax.swing.JPopupMenu rightClickMenu;
450  private javax.swing.JButton searchButton;
451  private javax.swing.JMenuItem selectAllMenuItem;
452  private javax.swing.JRadioButton substringRadioButton;
453  // End of variables declaration//GEN-END:variables
454 }
static synchronized IngestManager getInstance()
void addIngestJobEventListener(final PropertyChangeListener listener)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

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