Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DropdownToolbar.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2020 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.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.awt.event.MouseEvent;
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.util.ArrayList;
27 import java.util.EnumSet;
28 import java.util.List;
29 import java.util.logging.Level;
30 import javax.swing.SwingUtilities;
31 import javax.swing.event.PopupMenuEvent;
32 import javax.swing.event.PopupMenuListener;
37 import org.sleuthkit.datamodel.DataSource;
38 import org.sleuthkit.datamodel.TskCoreException;
39 
45 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
46 class DropdownToolbar extends javax.swing.JPanel {
47 
48  private static final long serialVersionUID = 1L;
49  private static final Logger logger = Logger.getLogger(DropdownToolbar.class.getName());
50  private static DropdownToolbar instance;
51  private SearchSettingsChangeListener searchSettingsChangeListener;
52  private boolean active = false;
53  private DropdownSingleTermSearchPanel dropPanel = null;
54  private DropdownListSearchPanel listsPanel = null;
55  private List<DataSource> dataSources = new ArrayList<>();
63  public synchronized static DropdownToolbar getDefault() {
64  if (instance == null) {
65  instance = new DropdownToolbar();
66  }
67  return instance;
68  }
69 
75  private DropdownToolbar() {
76  initComponents();
77  customizeComponents();
78  }
79 
84  private void customizeComponents() {
85  searchSettingsChangeListener = new SearchSettingsChangeListener();
86  KeywordSearch.getServer().addServerActionListener(searchSettingsChangeListener);
87  Case.addEventTypeSubscriber(EnumSet.of(Case.Events.CURRENT_CASE, Case.Events.DATA_SOURCE_ADDED), searchSettingsChangeListener);
88 
89  listsPanel = DropdownListSearchPanel.getDefault();
90  listsPanel.addSearchButtonActionListener((ActionEvent e) -> {
91  listsMenu.setVisible(false);
92  });
93  listsPanel.addPropertyChangeListener(searchSettingsChangeListener);
94  // Adding border of six to account for menu border
95  listsMenu.setSize(listsPanel.getPreferredSize().width + 6, listsPanel.getPreferredSize().height + 6);
96  listsMenu.add(listsPanel);
97  listsMenu.addPopupMenuListener(new PopupMenuListener() {
98  @Override
99  public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
100  listsButton.setSelected(true);
101  }
102 
103  @Override
104  public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
105  listsButton.setSelected(false);
106  }
107 
108  @Override
109  public void popupMenuCanceled(PopupMenuEvent e) {
110  listsButton.setSelected(false);
111  }
112  });
113 
114  dropPanel = DropdownSingleTermSearchPanel.getDefault();
115  dropPanel.addPropertyChangeListener(searchSettingsChangeListener);
116  dropPanel.addSearchButtonActionListener(new ActionListener() {
117  @Override
118  public void actionPerformed(ActionEvent e) {
119  searchMenu.setVisible(false);
120  }
121  });
122  searchMenu.setSize(dropPanel.getPreferredSize().width + 6, dropPanel.getPreferredSize().height + 6);
123  searchMenu.add(dropPanel);
124  searchMenu.addPopupMenuListener(new PopupMenuListener() {
125  @Override
126  public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
127  searchDropButton.setSelected(true);
128  }
129 
130  @Override
131  public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
132  searchDropButton.setSelected(false);
133  }
134 
135  @Override
136  public void popupMenuCanceled(PopupMenuEvent e) {
137  searchDropButton.setSelected(false);
138  }
139  });
140 
141  }
142 
143  private void maybeShowListsPopup(MouseEvent evt) {
144  if (!active || !listsButton.isEnabled()) {
145  return;
146  }
147  if (evt != null && !SwingUtilities.isLeftMouseButton(evt)) {
148  return;
149  }
150  listsPanel.setDataSources(dataSources);
151  listsPanel.updateDataSourceListModel();
152  listsMenu.show(listsButton, listsButton.getWidth() - listsMenu.getWidth(), listsButton.getHeight() - 1);
153  }
154 
155  private void maybeShowSearchPopup(MouseEvent evt) {
156  if (!active || !searchDropButton.isEnabled()) {
157  return;
158  }
159  if (evt != null && !SwingUtilities.isLeftMouseButton(evt)) {
160  return;
161  }
162  dropPanel.setDataSources(dataSources);
163  dropPanel.updateDataSourceListModel();
164  searchMenu.show(searchDropButton, searchDropButton.getWidth() - searchMenu.getWidth(), searchDropButton.getHeight() - 1);
165  }
166 
167  private class SearchSettingsChangeListener implements PropertyChangeListener {
168 
169  @Override
170  public void propertyChange(PropertyChangeEvent evt) {
172  String changed = evt.getPropertyName();
173  if (changed.equals(Case.Events.CURRENT_CASE.toString())) {
174  if (null != evt.getNewValue()) {
175  boolean disableSearch = false;
176  /*
177  * A case has been opened.
178  */
179  try {
180  Server server = KeywordSearch.getServer();
181  if (server.coreIsOpen() == false) {
182  disableSearch = true;
183  }
184  else {
185  Index indexInfo = server.getIndexInfo();
186 
187  // Check the Solr schema version and enable ad-hoc search UI components.
188  boolean schemaIsCompatible = indexInfo.isCompatible(IndexFinder.getCurrentSchemaVersion());
189  if (!schemaIsCompatible) {
190  logger.log(Level.WARNING, "Text index schema version {0} is not compatible with current schema", indexInfo.getSchemaVersion()); //NON-NLS
191  disableSearch = true;
192  } else {
193  listsButton.setEnabled(true);
194  searchDropButton.setEnabled(true);
195  dropPanel.setRegexSearchEnabled(true);
196  active = true;
197  }
198  }
199  } catch (NoOpenCoreException ex) {
200  /*
201  * Error, disable the ad hoc search UI components.
202  */
203  logger.log(Level.SEVERE, "Error getting text index info", ex); //NON-NLS
204  disableSearch = true;
205  }
206 
207  //set the data source list
208  try {
209  dataSources = getDataSourceList();
210  } catch (TskCoreException ex) {
211  logger.log(Level.SEVERE, "Error getting text index info", ex); //NON-NLS
212  disableSearch = true;
213  } catch (NoCurrentCaseException ex) {
214  logger.log(Level.SEVERE, "Exception while getting current case.", ex); //NON-NLS
215  disableSearch = true;
216  }
217  if (disableSearch) {
218  searchDropButton.setEnabled(false);
219  listsButton.setEnabled(false);
220  active = false;
221  }
222 
223  } else {
224  /*
225  * A case has been closed.
226  */
227  dropPanel.clearSearchBox();
228  searchDropButton.setEnabled(false);
229  listsButton.setEnabled(false);
230  active = false;
231  }
232  } else if (changed.equals(Server.CORE_EVT)) {
233  final Server.CORE_EVT_STATES state = (Server.CORE_EVT_STATES) evt.getNewValue();
234  switch (state) {
235  case STARTED:
236  try {
237  final int numIndexedFiles = KeywordSearch.getServer().queryNumIndexedFiles();
238  KeywordSearch.fireNumIndexedFilesChange(null, numIndexedFiles);
240  logger.log(Level.SEVERE, "Error executing Solr query", ex); //NON-NLS
241  }
242  break;
243  case STOPPED:
244  break;
245  default:
246  }
247  } else if (changed.equals(Case.Events.DATA_SOURCE_ADDED.toString())) {
248  DataSource newDataSource = (DataSource) evt.getNewValue();
249  dataSources.add(newDataSource);
250  }
251  }
252  }
253  }
254 
261  private synchronized List<DataSource> getDataSourceList() throws NoCurrentCaseException, TskCoreException {
262  Case openCase = Case.getCurrentCaseThrows();
263  return openCase.getSleuthkitCase().getDataSources();
264  }
270  @SuppressWarnings("unchecked")
271  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
272  private void initComponents() {
273 
274  listsMenu = new javax.swing.JPopupMenu();
275  searchMenu = new javax.swing.JPopupMenu();
276  listsButton = new javax.swing.JButton();
277  searchDropButton = new javax.swing.JButton();
278  jSeparator1 = new javax.swing.JSeparator();
279 
280  setOpaque(false);
281 
282  listsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/watchbutton-icon.png"))); // NOI18N
283  listsButton.setText(org.openide.util.NbBundle.getMessage(DropdownToolbar.class, "ListBundleName")); // NOI18N
284  listsButton.setBorderPainted(false);
285  listsButton.setContentAreaFilled(false);
286  listsButton.setEnabled(false);
287  listsButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/watchbutton-icon-rollover.png"))); // NOI18N
288  listsButton.setRolloverSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/watchbutton-icon-pressed.png"))); // NOI18N
289  listsButton.addMouseListener(new java.awt.event.MouseAdapter() {
290  public void mousePressed(java.awt.event.MouseEvent evt) {
291  listsButtonMousePressed(evt);
292  }
293  });
294  listsButton.addActionListener(new java.awt.event.ActionListener() {
295  public void actionPerformed(java.awt.event.ActionEvent evt) {
296  listsButtonActionPerformed(evt);
297  }
298  });
299 
300  searchDropButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/searchbutton-icon.png"))); // NOI18N
301  searchDropButton.setText(org.openide.util.NbBundle.getMessage(DropdownToolbar.class, "KeywordSearchPanel.searchDropButton.text")); // NOI18N
302  searchDropButton.setBorderPainted(false);
303  searchDropButton.setContentAreaFilled(false);
304  searchDropButton.setEnabled(false);
305  searchDropButton.setMaximumSize(new java.awt.Dimension(146, 27));
306  searchDropButton.setMinimumSize(new java.awt.Dimension(146, 27));
307  searchDropButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/searchbutton-icon-rollover.png"))); // NOI18N
308  searchDropButton.setRolloverSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/searchbutton-icon-pressed.png"))); // NOI18N
309  searchDropButton.addMouseListener(new java.awt.event.MouseAdapter() {
310  public void mousePressed(java.awt.event.MouseEvent evt) {
311  searchDropButtonMousePressed(evt);
312  }
313  });
314 
315  jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
316 
317  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
318  this.setLayout(layout);
319  layout.setHorizontalGroup(
320  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
321  .addGroup(layout.createSequentialGroup()
322  .addComponent(listsButton)
323  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
324  .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 7, javax.swing.GroupLayout.PREFERRED_SIZE)
325  .addGap(1, 1, 1)
326  .addComponent(searchDropButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
327  .addContainerGap())
328  );
329  layout.setVerticalGroup(
330  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
331  .addGroup(layout.createSequentialGroup()
332  .addGap(0, 0, Short.MAX_VALUE)
333  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
334  .addComponent(listsButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
335  .addComponent(searchDropButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
336  .addComponent(jSeparator1)))
337  );
338  }// </editor-fold>//GEN-END:initComponents
339 
340  private void listsButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listsButtonMousePressed
341  maybeShowListsPopup(evt);
342  }//GEN-LAST:event_listsButtonMousePressed
343 
344  private void listsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listsButtonActionPerformed
345  // TODO add your handling code here:
346  }//GEN-LAST:event_listsButtonActionPerformed
347 
348  private void searchDropButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_searchDropButtonMousePressed
349  maybeShowSearchPopup(evt);
350  }//GEN-LAST:event_searchDropButtonMousePressed
351 
352  // Variables declaration - do not modify//GEN-BEGIN:variables
353  private javax.swing.JSeparator jSeparator1;
354  private javax.swing.JButton listsButton;
355  private javax.swing.JPopupMenu listsMenu;
356  private javax.swing.JButton searchDropButton;
357  private javax.swing.JPopupMenu searchMenu;
358  // End of variables declaration//GEN-END:variables
359 
360 }
static void fireNumIndexedFilesChange(Integer oldNum, Integer newNum)

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