Autopsy  3.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileSearchPanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 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 
20 /*
21  * FileSearchPanel.java
22  *
23  * Created on Mar 5, 2012, 1:51:50 PM
24  */
25 package org.sleuthkit.autopsy.filesearch;
26 
27 import java.awt.BorderLayout;
28 import java.awt.Component;
29 import java.awt.Cursor;
30 import java.awt.Dimension;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.logging.Level;
38 
39 import org.openide.util.NbBundle;
41 import javax.swing.BoxLayout;
42 import javax.swing.JButton;
43 import javax.swing.JLabel;
44 import javax.swing.JPanel;
45 import javax.swing.border.EmptyBorder;
46 import org.openide.DialogDisplayer;
47 import org.openide.NotifyDescriptor;
48 import org.openide.windows.TopComponent;
57 
61  class FileSearchPanel extends javax.swing.JPanel {
62 
63  private List<FilterArea> filterAreas = new ArrayList<FilterArea>();
64  private JButton searchButton;
65  private static int resultWindowCount = 0; //keep track of result windows so they get unique names
66 
70  public FileSearchPanel() {
71  initComponents();
72  customizeComponents();
73 
74  }
75 
79  private void customizeComponents() {
80 
81  this.setLayout(new BorderLayout());
82 
83  JPanel filterPanel = new JPanel();
84  filterPanel.setLayout(new BoxLayout(filterPanel, BoxLayout.Y_AXIS));
85  filterPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
86 
87  this.add(filterPanel, BorderLayout.CENTER);
88 
89  JLabel label = new JLabel(NbBundle.getMessage(this.getClass(), "FileSearchPanel.custComp.label.text"));
90  label.setAlignmentX(Component.LEFT_ALIGNMENT);
91  label.setBorder(new EmptyBorder(0, 0, 10, 0));
92  filterPanel.add(label);
93 
94  // Create and add filter areas
95  this.filterAreas.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.name"), new NameSearchFilter()));
96 
97  List<FileSearchFilter> metadataFilters = new ArrayList<FileSearchFilter>();
98  metadataFilters.add(new SizeSearchFilter());
99  metadataFilters.add(new DateSearchFilter());
100  this.filterAreas.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.metadata"), metadataFilters));
101 
102  this.filterAreas.add(new FilterArea(NbBundle.getMessage(this.getClass(), "FileSearchPanel.filterTitle.knownStatus"), new KnownStatusSearchFilter()));
103 
104  for (FilterArea fa : this.filterAreas) {
105  fa.setMaximumSize(new Dimension(Integer.MAX_VALUE, fa.getMinimumSize().height));
106  fa.setAlignmentX(Component.LEFT_ALIGNMENT);
107  filterPanel.add(fa);
108  }
109 
110  // Create and add search button
111  this.searchButton = new JButton(NbBundle.getMessage(this.getClass(), "FileSearchPanel.searchButton.text"));
112  this.searchButton.setAlignmentX(Component.LEFT_ALIGNMENT);
113  filterPanel.add(searchButton);
114 
115  addListenerToAll(new ActionListener() {
116  @Override
117  public void actionPerformed(ActionEvent e) {
118  search();
119  }
120  });
121  }
122 
126  private boolean anyFiltersEnabled() {
127  for (FileSearchFilter filter : this.getFilters()) {
128  if (filter.isEnabled()) {
129  return true;
130  }
131  }
132 
133  return false;
134  }
135 
140  private void search() {
141  // change the cursor to "waiting cursor" for this operation
142  this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
143  try {
144  if (this.anyFiltersEnabled()) {
145  String title = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.title", ++resultWindowCount);
146  String pathText = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.pathText");
147 
148  // try to get the number of matches first
149  Case currentCase = Case.getCurrentCase(); // get the most updated case
150  long totalMatches = 0;
151  List<AbstractFile> contentList = null;
152  try {
153  SleuthkitCase tskDb = currentCase.getSleuthkitCase();
154  //ResultSet rs = tempDb.runQuery(this.getQuery("count(*) as TotalMatches"));
155  contentList = tskDb.findAllFilesWhere(this.getQuery());
156 
157  } catch (TskCoreException ex) {
158  Logger logger = Logger.getLogger(this.getClass().getName());
159  logger.log(Level.WARNING, "Error while trying to get the number of matches.", ex); //NON-NLS
160  }
161 
162  if (contentList == null) {
163  contentList = Collections.<AbstractFile>emptyList();
164  }
165 
166  final TopComponent searchResultWin = DataResultTopComponent.createInstance(title, pathText,
167  new TableFilterNode(new SearchNode(contentList), true), contentList.size());
168 
169  searchResultWin.requestActive(); // make it the active top component
170 
176  if (totalMatches > 10000) {
177  // show info
178  String msg = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.msg", totalMatches);
179  String details = NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.results.details");
180  MessageNotifyUtil.Notify.info(msg, details);
181  }
182  } else {
183  throw new FilterValidationException(
184  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.exception.noFilterSelected.msg"));
185  }
186  } catch (FilterValidationException ex) {
187  NotifyDescriptor d = new NotifyDescriptor.Message(
188  NbBundle.getMessage(this.getClass(), "FileSearchPanel.search.validationErr.msg", ex.getMessage()));
189  DialogDisplayer.getDefault().notify(d);
190  } finally {
191  this.setCursor(null);
192  }
193  }
194 
195 
196 
206  private String getQuery() throws FilterValidationException {
207 
208  //String query = "select " + tempQuery + " from tsk_files where 1";
209  String query = " 1";
210 
211  for (FileSearchFilter f : this.getEnabledFilters()) {
212  query += " and (" + f.getPredicate() + ")"; //NON-NLS
213  }
214 
215  return query;
216  }
217 
218  private Collection<FileSearchFilter> getFilters() {
219  Collection<FileSearchFilter> filters = new ArrayList<FileSearchFilter>();
220 
221  for (FilterArea fa : this.filterAreas) {
222  filters.addAll(fa.getFilters());
223  }
224 
225  return filters;
226  }
227 
228  private Collection<FileSearchFilter> getEnabledFilters() {
229  Collection<FileSearchFilter> enabledFilters = new ArrayList<FileSearchFilter>();
230 
231  for (FileSearchFilter f : this.getFilters()) {
232  if (f.isEnabled()) {
233  enabledFilters.add(f);
234  }
235  }
236 
237  return enabledFilters;
238  }
239 
240  void addListenerToAll(ActionListener l) {
241  searchButton.addActionListener(l);
242  for (FilterArea fa : this.filterAreas) {
243  for (FileSearchFilter fsf : fa.getFilters()) {
244  fsf.addActionListener(l);
245  }
246  }
247  }
248 
254  @SuppressWarnings("unchecked")
255  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
256  private void initComponents() {
257 
258  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
259  this.setLayout(layout);
260  layout.setHorizontalGroup(
261  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
262  .addGap(0, 300, Short.MAX_VALUE)
263  );
264  layout.setVerticalGroup(
265  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
266  .addGap(0, 376, Short.MAX_VALUE)
267  );
268  }// </editor-fold>//GEN-END:initComponents
269  // Variables declaration - do not modify//GEN-BEGIN:variables
270  // End of variables declaration//GEN-END:variables
271 }

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