Autopsy  4.14.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-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.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  if (IndexFinder.getCurrentSolrVersion().equals(indexInfo.getSolrVersion())) {
187  /*
188  * Solr version is current, so check the Solr
189  * schema version and selectively enable the ad
190  * hoc search UI components.
191  */
192  boolean schemaIsCompatible = indexInfo.isCompatible(IndexFinder.getCurrentSchemaVersion());
193  listsButton.setEnabled(schemaIsCompatible);
194  searchDropButton.setEnabled(true);
195  dropPanel.setRegexSearchEnabled(schemaIsCompatible);
196  active = true;
197  } else {
198  /*
199  * Unsupported Solr version, disable the ad hoc
200  * search UI components.
201  */
202  disableSearch = true;
203  }
204  }
205  } catch (NoOpenCoreException ex) {
206  /*
207  * Error, disable the ad hoc search UI components.
208  */
209  logger.log(Level.SEVERE, "Error getting text index info", ex); //NON-NLS
210  disableSearch = true;
211  }
212 
213  //set the data source list
214  try {
215  dataSources = getDataSourceList();
216  } catch (TskCoreException ex) {
217  logger.log(Level.SEVERE, "Error getting text index info", ex); //NON-NLS
218  disableSearch = true;
219  } catch (NoCurrentCaseException ex) {
220  logger.log(Level.SEVERE, "Exception while getting current case.", ex); //NON-NLS
221  disableSearch = true;
222  }
223  if (disableSearch) {
224  searchDropButton.setEnabled(false);
225  listsButton.setEnabled(false);
226  active = false;
227  }
228 
229  } else {
230  /*
231  * A case has been closed.
232  */
233  dropPanel.clearSearchBox();
234  searchDropButton.setEnabled(false);
235  listsButton.setEnabled(false);
236  active = false;
237  }
238  } else if (changed.equals(Server.CORE_EVT)) {
239  final Server.CORE_EVT_STATES state = (Server.CORE_EVT_STATES) evt.getNewValue();
240  switch (state) {
241  case STARTED:
242  try {
243  final int numIndexedFiles = KeywordSearch.getServer().queryNumIndexedFiles();
244  KeywordSearch.fireNumIndexedFilesChange(null, numIndexedFiles);
246  logger.log(Level.SEVERE, "Error executing Solr query", ex); //NON-NLS
247  }
248  break;
249  case STOPPED:
250  break;
251  default:
252  }
253  } else if (changed.equals(Case.Events.DATA_SOURCE_ADDED.toString())) {
254  DataSource newDataSource = (DataSource) evt.getNewValue();
255  dataSources.add(newDataSource);
256  }
257  }
258  }
259  }
260 
267  private synchronized List<DataSource> getDataSourceList() throws NoCurrentCaseException, TskCoreException {
268  Case openCase = Case.getCurrentCaseThrows();
269  return openCase.getSleuthkitCase().getDataSources();
270  }
276  @SuppressWarnings("unchecked")
277  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
278  private void initComponents() {
279 
280  listsMenu = new javax.swing.JPopupMenu();
281  searchMenu = new javax.swing.JPopupMenu();
282  listsButton = new javax.swing.JButton();
283  searchDropButton = new javax.swing.JButton();
284  jSeparator1 = new javax.swing.JSeparator();
285 
286  setOpaque(false);
287 
288  listsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/watchbutton-icon.png"))); // NOI18N
289  listsButton.setText(org.openide.util.NbBundle.getMessage(DropdownToolbar.class, "ListBundleName")); // NOI18N
290  listsButton.setBorderPainted(false);
291  listsButton.setContentAreaFilled(false);
292  listsButton.setEnabled(false);
293  listsButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/watchbutton-icon-rollover.png"))); // NOI18N
294  listsButton.setRolloverSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/watchbutton-icon-pressed.png"))); // NOI18N
295  listsButton.addMouseListener(new java.awt.event.MouseAdapter() {
296  public void mousePressed(java.awt.event.MouseEvent evt) {
297  listsButtonMousePressed(evt);
298  }
299  });
300  listsButton.addActionListener(new java.awt.event.ActionListener() {
301  public void actionPerformed(java.awt.event.ActionEvent evt) {
302  listsButtonActionPerformed(evt);
303  }
304  });
305 
306  searchDropButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/searchbutton-icon.png"))); // NOI18N
307  searchDropButton.setText(org.openide.util.NbBundle.getMessage(DropdownToolbar.class, "KeywordSearchPanel.searchDropButton.text")); // NOI18N
308  searchDropButton.setBorderPainted(false);
309  searchDropButton.setContentAreaFilled(false);
310  searchDropButton.setEnabled(false);
311  searchDropButton.setMaximumSize(new java.awt.Dimension(146, 27));
312  searchDropButton.setMinimumSize(new java.awt.Dimension(146, 27));
313  searchDropButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/searchbutton-icon-rollover.png"))); // NOI18N
314  searchDropButton.setRolloverSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/org/sleuthkit/autopsy/keywordsearch/searchbutton-icon-pressed.png"))); // NOI18N
315  searchDropButton.addMouseListener(new java.awt.event.MouseAdapter() {
316  public void mousePressed(java.awt.event.MouseEvent evt) {
317  searchDropButtonMousePressed(evt);
318  }
319  });
320 
321  jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
322 
323  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
324  this.setLayout(layout);
325  layout.setHorizontalGroup(
326  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
327  .addGroup(layout.createSequentialGroup()
328  .addComponent(listsButton)
329  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
330  .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 7, javax.swing.GroupLayout.PREFERRED_SIZE)
331  .addGap(1, 1, 1)
332  .addComponent(searchDropButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
333  .addContainerGap())
334  );
335  layout.setVerticalGroup(
336  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
337  .addGroup(layout.createSequentialGroup()
338  .addGap(0, 0, Short.MAX_VALUE)
339  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
340  .addComponent(listsButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
341  .addComponent(searchDropButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
342  .addComponent(jSeparator1)))
343  );
344  }// </editor-fold>//GEN-END:initComponents
345 
346  private void listsButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listsButtonMousePressed
347  maybeShowListsPopup(evt);
348  }//GEN-LAST:event_listsButtonMousePressed
349 
350  private void listsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listsButtonActionPerformed
351  // TODO add your handling code here:
352  }//GEN-LAST:event_listsButtonActionPerformed
353 
354  private void searchDropButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_searchDropButtonMousePressed
355  maybeShowSearchPopup(evt);
356  }//GEN-LAST:event_searchDropButtonMousePressed
357 
358  // Variables declaration - do not modify//GEN-BEGIN:variables
359  private javax.swing.JSeparator jSeparator1;
360  private javax.swing.JButton listsButton;
361  private javax.swing.JPopupMenu listsMenu;
362  private javax.swing.JButton searchDropButton;
363  private javax.swing.JPopupMenu searchMenu;
364  // End of variables declaration//GEN-END:variables
365 
366 }
static void fireNumIndexedFilesChange(Integer oldNum, Integer newNum)

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.