19 package org.sleuthkit.autopsy.filesearch;
21 import java.awt.Component;
22 import java.awt.Cursor;
23 import java.awt.GridLayout;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.beans.PropertyChangeEvent;
27 import java.beans.PropertyChangeListener;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.logging.Level;
33 import javax.swing.JLabel;
34 import javax.swing.JPanel;
35 import javax.swing.border.EmptyBorder;
36 import org.openide.DialogDisplayer;
37 import org.openide.NotifyDescriptor;
38 import org.openide.util.NbBundle;
39 import org.openide.windows.TopComponent;
54 @SuppressWarnings(
"PMD.SingularField")
55 class FileSearchPanel extends javax.swing.JPanel {
57 private final List<FileSearchFilter> filters =
new ArrayList<>();
58 private static int resultWindowCount = 0;
59 private static final String EMPTY_WHERE_CLAUSE = NbBundle.getMessage(DateSearchFilter.class,
"FileSearchPanel.emptyWhereClause.text");
68 public FileSearchPanel() {
70 customizeComponents();
77 private void customizeComponents() {
79 JLabel label =
new JLabel(NbBundle.getMessage(
this.getClass(),
"FileSearchPanel.custComp.label.text"));
80 label.setAlignmentX(Component.LEFT_ALIGNMENT);
81 label.setBorder(
new EmptyBorder(0, 0, 10, 0));
83 JPanel panel1 =
new JPanel();
84 panel1.setLayout(
new GridLayout(1,2));
85 panel1.add(
new JLabel(
""));
86 JPanel panel2 =
new JPanel();
87 panel2.setLayout(
new GridLayout(1,2, 20, 0));
88 JPanel panel3 =
new JPanel();
89 panel3.setLayout(
new GridLayout(1,2, 20, 0));
90 JPanel panel4 =
new JPanel();
91 panel4.setLayout(
new GridLayout(1,2, 20, 0));
92 JPanel panel5 =
new JPanel();
93 panel5.setLayout(
new GridLayout(1,2, 20, 0));
96 NameSearchFilter nameFilter =
new NameSearchFilter();
97 SizeSearchFilter sizeFilter =
new SizeSearchFilter();
98 DateSearchFilter dateFilter =
new DateSearchFilter();
99 KnownStatusSearchFilter knowStatusFilter =
new KnownStatusSearchFilter();
100 HashSearchFilter hashFilter =
new HashSearchFilter();
101 MimeTypeFilter mimeTypeFilter =
new MimeTypeFilter();
102 DataSourceFilter dataSourceFilter =
new DataSourceFilter();
104 panel2.add(
new FilterArea(NbBundle.getMessage(
this.getClass(),
"FileSearchPanel.filterTitle.name"),nameFilter));
106 panel3.add(
new FilterArea(NbBundle.getMessage(
this.getClass(),
"FileSearchPanel.filterTitle.metadata"),sizeFilter));
108 panel2.add(
new FilterArea(NbBundle.getMessage(
this.getClass(),
"FileSearchPanel.filterTitle.metadata"), dateFilter));
109 panel3.add(
new FilterArea(NbBundle.getMessage(
this.getClass(),
"FileSearchPanel.filterTitle.knownStatus"), knowStatusFilter));
111 panel5.add(
new FilterArea(NbBundle.getMessage(
this.getClass(),
"HashSearchPanel.md5CheckBox.text"), hashFilter));
112 panel5.add(
new JLabel(
""));
113 panel4.add(
new FilterArea(NbBundle.getMessage(
this.getClass(),
"FileSearchPanel.filterTitle.metadata"), mimeTypeFilter));
114 panel4.add(
new FilterArea(NbBundle.getMessage(
this.getClass(),
"DataSourcePanel.dataSourceCheckBox.text"), dataSourceFilter));
115 filterPanel.add(panel1);
116 filterPanel.add(panel2);
117 filterPanel.add(panel3);
118 filterPanel.add(panel4);
119 filterPanel.add(panel5);
121 filters.add(nameFilter);
122 filters.add(sizeFilter);
123 filters.add(dateFilter);
124 filters.add(knowStatusFilter);
125 filters.add(hashFilter);
126 filters.add(mimeTypeFilter);
127 filters.add(dataSourceFilter);
129 for (FileSearchFilter filter : this.getFilters()) {
130 filter.addPropertyChangeListener(
new PropertyChangeListener() {
132 public void propertyChange(PropertyChangeEvent evt) {
133 searchButton.setEnabled(isValidSearch());
137 addListenerToAll(
new ActionListener() {
139 public void actionPerformed(ActionEvent e) {
143 searchButton.setEnabled(isValidSearch());
149 private boolean isValidSearch() {
150 boolean enabled =
false;
151 for (FileSearchFilter filter : this.getFilters()) {
152 if (filter.isEnabled()) {
154 if (!filter.isValid()) {
155 errorLabel.setText(filter.getLastError());
161 errorLabel.setText(
"");
169 private void search() {
171 this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
173 if (this.isValidSearch()) {
174 String title = NbBundle.getMessage(this.getClass(),
"FileSearchPanel.search.results.title", ++resultWindowCount);
175 String pathText = NbBundle.getMessage(this.getClass(),
"FileSearchPanel.search.results.pathText");
178 Case currentCase = Case.getCurrentCaseThrows();
179 long totalMatches = 0;
180 List<AbstractFile> contentList = null;
182 SleuthkitCase tskDb = currentCase.getSleuthkitCase();
183 contentList = tskDb.findAllFilesWhere(this.getQuery());
185 }
catch (TskCoreException ex) {
186 Logger logger = Logger.getLogger(this.getClass().getName());
187 logger.log(Level.WARNING,
"Error while trying to get the number of matches.", ex);
190 if (contentList == null) {
191 contentList = Collections.<AbstractFile>emptyList();
194 SearchNode sn =
new SearchNode(contentList);
195 final TopComponent searchResultWin = DataResultTopComponent.createInstance(title, pathText,
196 new TableFilterNode(sn,
true, sn.getName()), contentList.size());
198 searchResultWin.requestActive();
205 if (totalMatches > 10000) {
207 String msg = NbBundle.getMessage(this.getClass(),
"FileSearchPanel.search.results.msg", totalMatches);
208 String details = NbBundle.getMessage(this.getClass(),
"FileSearchPanel.search.results.details");
209 MessageNotifyUtil.Notify.info(msg, details);
212 throw new FilterValidationException(
213 NbBundle.getMessage(
this.getClass(),
"FileSearchPanel.search.exception.noFilterSelected.msg"));
215 }
catch (FilterValidationException | NoCurrentCaseException ex) {
216 NotifyDescriptor d =
new NotifyDescriptor.Message(
217 NbBundle.getMessage(
this.getClass(),
"FileSearchPanel.search.validationErr.msg", ex.getMessage()));
218 DialogDisplayer.getDefault().notify(d);
220 this.setCursor(null);
242 private String getQuery() throws FilterValidationException {
247 for (FileSearchFilter f : this.getEnabledFilters()) {
248 String result = f.getPredicate();
249 if (!result.isEmpty()) {
251 query +=
" AND (" + result +
")";
253 query +=
" (" + result +
")";
259 if (query.isEmpty()) {
260 throw new FilterValidationException(EMPTY_WHERE_CLAUSE);
265 private Collection<FileSearchFilter> getFilters() {
269 private Collection<FileSearchFilter> getEnabledFilters() {
270 Collection<FileSearchFilter> enabledFilters =
new ArrayList<>();
272 for (FileSearchFilter f : this.getFilters()) {
274 enabledFilters.add(f);
278 return enabledFilters;
281 void addListenerToAll(ActionListener l) {
282 searchButton.addActionListener(l);
283 for (FileSearchFilter fsf : getFilters()) {
284 fsf.addActionListener(l);
293 @SuppressWarnings(
"unchecked")
295 private
void initComponents() {
297 filterPanel =
new javax.swing.JPanel();
298 searchButton =
new javax.swing.JButton();
299 errorLabel =
new javax.swing.JLabel();
301 setPreferredSize(
new java.awt.Dimension(300, 300));
303 filterPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
304 filterPanel.setPreferredSize(
new java.awt.Dimension(300, 400));
305 filterPanel.setLayout(
new javax.swing.BoxLayout(filterPanel, javax.swing.BoxLayout.Y_AXIS));
307 searchButton.setText(
org.openide.util.NbBundle.getMessage(FileSearchPanel.class,
"FileSearchPanel.searchButton.text"));
309 errorLabel.setText(
org.openide.util.NbBundle.getMessage(FileSearchPanel.class,
"FileSearchPanel.errorLabel.text"));
310 errorLabel.setForeground(
new java.awt.Color(255, 51, 51));
312 javax.swing.GroupLayout layout =
new javax.swing.GroupLayout(
this);
313 this.setLayout(layout);
314 layout.setHorizontalGroup(
315 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
316 .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
317 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
319 .addComponent(errorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
320 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
321 .addComponent(searchButton)
324 layout.setVerticalGroup(
325 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
326 .addGroup(layout.createSequentialGroup()
327 .addComponent(filterPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
328 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
329 .addComponent(searchButton)
330 .addComponent(errorLabel))
336 private javax.swing.JLabel errorLabel;
337 private javax.swing.JPanel filterPanel;
338 private javax.swing.JButton searchButton;