Autopsy  4.7.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
DateSearchPanel.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.filesearch;
20 
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.text.DateFormat;
24 import java.util.List;
25 import javax.swing.JCheckBox;
26 import javax.swing.JComboBox;
27 import javax.swing.JFormattedTextField;
28 import javax.swing.JMenuItem;
29 import javax.swing.JPopupMenu;
30 import com.github.lgooddatepicker.components.DatePicker;
31 import com.github.lgooddatepicker.optionalusertools.PickerUtilities;
32 import com.github.lgooddatepicker.components.DatePickerSettings;
33 import com.github.lgooddatepicker.optionalusertools.DateChangeListener;
34 import com.github.lgooddatepicker.zinternaltools.DateChangeEvent;
35 import java.text.ParseException;
36 import java.time.Instant;
37 import java.util.Date;
38 
42 @SuppressWarnings("PMD.SingularField") // UI widgets cause lots of false positives
43 class DateSearchPanel extends javax.swing.JPanel {
44 
45  private final DatePickerSettings fromDateSettings = new DatePickerSettings();
46  private final DatePickerSettings toDateSettings = new DatePickerSettings();
47  DateFormat dateFormat;
48  List<String> timeZones;
49 
50  DateSearchPanel(DateFormat dateFormat, List<String> timeZones) {
51  this.dateFormat = dateFormat;
52  this.timeZones = timeZones;
53 
54  initComponents();
55  customizeComponents();
56  }
57 
58  private void customizeComponents() {
59  fromDateSettings.setFormatForDatesCommonEra(PickerUtilities.createFormatterFromPatternString("MM/dd/yyyy", fromDateSettings.getLocale()));
60  toDateSettings.setFormatForDatesCommonEra(PickerUtilities.createFormatterFromPatternString("MM/dd/yyyy", toDateSettings.getLocale()));
61  fromDateSettings.setAllowKeyboardEditing(false);
62  toDateSettings.setAllowKeyboardEditing(false);
63 
64  ActionListener actList = new ActionListener() {
65  @Override
66  public void actionPerformed(ActionEvent e) {
67  JMenuItem jmi = (JMenuItem) e.getSource();
68  /*
69  * Because there are two text fields, we have to determine which
70  * invoked the popupmenu
71  */
72  JFormattedTextField jftf = (JFormattedTextField) ((JPopupMenu) jmi.getParent()).getInvoker();
73  if (jmi.equals(cutMenuItem)) {
74  jftf.cut();
75  } else if (jmi.equals(copyMenuItem)) {
76  jftf.copy();
77  } else if (jmi.equals(pasteMenuItem)) {
78  jftf.paste();
79  } else if (jmi.equals(selectAllMenuItem)) {
80  jftf.selectAll();
81  }
82  }
83  };
84  cutMenuItem.addActionListener(actList);
85  copyMenuItem.addActionListener(actList);
86  pasteMenuItem.addActionListener(actList);
87  selectAllMenuItem.addActionListener(actList);
88 
89  this.setComponentsEnabled();
90  }
91 
92  JCheckBox getAccessedCheckBox() {
93  return accessedCheckBox;
94  }
95 
96  JCheckBox getChangedCheckBox() {
97  return changedCheckBox;
98  }
99 
100  JCheckBox getCreatedCheckBox() {
101  return createdCheckBox;
102  }
103 
104  JCheckBox getDateCheckBox() {
105  return dateCheckBox;
106  }
107 
108  String getFromDate() {
109  return fromDatePicker.getText();
110  }
111 
112  String getToDate() {
113  return toDatePicker.getText();
114  }
115 
116  JCheckBox getModifiedCheckBox() {
117  return modifiedCheckBox;
118  }
119 
120  JComboBox<String> getTimeZoneComboBox() {
121  return timeZoneComboBox;
122  }
123 
124  void setTimeZones(List<String> newTimeZones) {
125  this.timeZones = newTimeZones;
126  this.timeZoneComboBox.removeAllItems();
127  for (String tz : newTimeZones) {
128  this.timeZoneComboBox.addItem(tz);
129  }
130  }
131 
132  private void setComponentsEnabled() {
133  boolean enable = this.dateCheckBox.isSelected();
134  this.fromDatePicker.setEnabled(enable);
135  this.jLabel1.setEnabled(enable);
136  this.toDatePicker.setEnabled(enable);
137  this.jLabel2.setEnabled(enable);
138  this.jLabel3.setEnabled(enable);
139  this.jLabel4.setEnabled(enable);
140  this.timeZoneComboBox.setEnabled(enable);
141  this.modifiedCheckBox.setEnabled(enable);
142  this.accessedCheckBox.setEnabled(enable);
143  this.changedCheckBox.setEnabled(enable);
144  this.createdCheckBox.setEnabled(enable);
145  }
146 
152  @SuppressWarnings("unchecked")
153  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
154  private void initComponents() {
155 
156  rightClickMenu = new javax.swing.JPopupMenu();
157  cutMenuItem = new javax.swing.JMenuItem();
158  copyMenuItem = new javax.swing.JMenuItem();
159  pasteMenuItem = new javax.swing.JMenuItem();
160  selectAllMenuItem = new javax.swing.JMenuItem();
161  jLabel1 = new javax.swing.JLabel();
162  jLabel4 = new javax.swing.JLabel();
163  dateCheckBox = new javax.swing.JCheckBox();
164  timeZoneComboBox = new JComboBox<>(this.timeZones.toArray(new String[this.timeZones.size()]));
165  timeZoneComboBox.setRenderer(new DateSearchFilter.ComboBoxRenderer());
166  jLabel3 = new javax.swing.JLabel();
167  jLabel2 = new javax.swing.JLabel();
168  modifiedCheckBox = new javax.swing.JCheckBox();
169  changedCheckBox = new javax.swing.JCheckBox();
170  accessedCheckBox = new javax.swing.JCheckBox();
171  createdCheckBox = new javax.swing.JCheckBox();
172  fromDatePicker = new DatePicker(fromDateSettings);
173  toDatePicker = new DatePicker(toDateSettings);
174 
175  cutMenuItem.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.cutMenuItem.text")); // NOI18N
176  rightClickMenu.add(cutMenuItem);
177 
178  copyMenuItem.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.copyMenuItem.text")); // NOI18N
179  rightClickMenu.add(copyMenuItem);
180 
181  pasteMenuItem.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.pasteMenuItem.text")); // NOI18N
182  rightClickMenu.add(pasteMenuItem);
183 
184  selectAllMenuItem.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.selectAllMenuItem.text")); // NOI18N
185  rightClickMenu.add(selectAllMenuItem);
186 
187  jLabel1.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.jLabel1.text")); // NOI18N
188 
189  jLabel4.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.jLabel4.text")); // NOI18N
190 
191  dateCheckBox.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.dateCheckBox.text")); // NOI18N
192  dateCheckBox.addActionListener(new java.awt.event.ActionListener() {
193  public void actionPerformed(java.awt.event.ActionEvent evt) {
194  dateCheckBoxActionPerformed(evt);
195  }
196  });
197 
198  jLabel3.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.jLabel3.text")); // NOI18N
199  jLabel3.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
200 
201  jLabel2.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.jLabel2.text")); // NOI18N
202  jLabel2.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
203 
204  modifiedCheckBox.setSelected(true);
205  modifiedCheckBox.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.modifiedCheckBox.text")); // NOI18N
206  modifiedCheckBox.addActionListener(new java.awt.event.ActionListener() {
207  public void actionPerformed(java.awt.event.ActionEvent evt) {
208  modifiedCheckBoxActionPerformed(evt);
209  }
210  });
211 
212  changedCheckBox.setSelected(true);
213  changedCheckBox.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.changedCheckBox.text")); // NOI18N
214  changedCheckBox.addActionListener(new java.awt.event.ActionListener() {
215  public void actionPerformed(java.awt.event.ActionEvent evt) {
216  changedCheckBoxActionPerformed(evt);
217  }
218  });
219 
220  accessedCheckBox.setSelected(true);
221  accessedCheckBox.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.accessedCheckBox.text")); // NOI18N
222  accessedCheckBox.addActionListener(new java.awt.event.ActionListener() {
223  public void actionPerformed(java.awt.event.ActionEvent evt) {
224  accessedCheckBoxActionPerformed(evt);
225  }
226  });
227 
228  createdCheckBox.setSelected(true);
229  createdCheckBox.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.createdCheckBox.text")); // NOI18N
230  createdCheckBox.addActionListener(new java.awt.event.ActionListener() {
231  public void actionPerformed(java.awt.event.ActionEvent evt) {
232  createdCheckBoxActionPerformed(evt);
233  }
234  });
235 
236  fromDatePicker.addFocusListener(new java.awt.event.FocusAdapter() {
237  public void focusLost(java.awt.event.FocusEvent evt) {
238  fromDatePickerFocusLost(evt);
239  }
240  });
241  fromDatePicker.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
242  public void propertyChange(java.beans.PropertyChangeEvent evt) {
243  fromDatePickerPropertyChange(evt);
244  }
245  });
246 
247  toDatePicker.addFocusListener(new java.awt.event.FocusAdapter() {
248  public void focusLost(java.awt.event.FocusEvent evt) {
249  toDatePickerFocusLost(evt);
250  }
251  });
252  toDatePicker.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
253  public void propertyChange(java.beans.PropertyChangeEvent evt) {
254  toDatePickerPropertyChange(evt);
255  }
256  });
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  .addGroup(layout.createSequentialGroup()
263  .addContainerGap()
264  .addComponent(jLabel2)
265  .addGap(18, 18, 18)
266  .addComponent(jLabel3)
267  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
268  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
269  .addGap(0, 0, Short.MAX_VALUE)
270  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
271  .addGroup(layout.createSequentialGroup()
272  .addComponent(jLabel4)
273  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
274  .addComponent(timeZoneComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
275  .addGroup(layout.createSequentialGroup()
276  .addComponent(modifiedCheckBox)
277  .addGap(6, 6, 6)
278  .addComponent(accessedCheckBox)
279  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
280  .addComponent(createdCheckBox)
281  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
282  .addComponent(changedCheckBox)))
283  .addGap(33, 33, 33))
284  .addGroup(layout.createSequentialGroup()
285  .addComponent(dateCheckBox)
286  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
287  .addComponent(fromDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
288  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
289  .addComponent(jLabel1)
290  .addGap(10, 10, 10)
291  .addComponent(toDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
292  .addContainerGap())
293  );
294  layout.setVerticalGroup(
295  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
296  .addGroup(layout.createSequentialGroup()
297  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
298  .addGroup(layout.createSequentialGroup()
299  .addComponent(dateCheckBox)
300  .addGap(18, 18, 18))
301  .addGroup(layout.createSequentialGroup()
302  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
303  .addComponent(jLabel1)
304  .addComponent(fromDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
305  .addComponent(toDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
306  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
307  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
308  .addComponent(jLabel3)
309  .addComponent(jLabel2))
310  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
311  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
312  .addComponent(jLabel4)
313  .addComponent(timeZoneComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
314  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
315  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
316  .addComponent(modifiedCheckBox, javax.swing.GroupLayout.Alignment.TRAILING)
317  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
318  .addComponent(accessedCheckBox)
319  .addComponent(createdCheckBox)
320  .addComponent(changedCheckBox)))
321  .addGap(0, 0, 0))
322  );
323  }// </editor-fold>//GEN-END:initComponents
324 
325  private void dateCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dateCheckBoxActionPerformed
326  this.setComponentsEnabled();
327  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
328  }//GEN-LAST:event_dateCheckBoxActionPerformed
329 
330  private void modifiedCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifiedCheckBoxActionPerformed
331  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
332  }//GEN-LAST:event_modifiedCheckBoxActionPerformed
333 
334  private void accessedCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accessedCheckBoxActionPerformed
335  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
336  }//GEN-LAST:event_accessedCheckBoxActionPerformed
337 
338  private void createdCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createdCheckBoxActionPerformed
339  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
340  }//GEN-LAST:event_createdCheckBoxActionPerformed
341 
342  private void changedCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changedCheckBoxActionPerformed
343  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
344  }//GEN-LAST:event_changedCheckBoxActionPerformed
345 
346  private void fromDatePickerPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fromDatePickerPropertyChange
347  if (evt.getNewValue() instanceof Date) {
348  setFromDate((Date) evt.getNewValue());
349  }
350  }//GEN-LAST:event_fromDatePickerPropertyChange
351 
352  private void toDatePickerPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_toDatePickerPropertyChange
353  if (evt.getNewValue() instanceof Date) {
354  setToDate((Date) evt.getNewValue());
355  }
356  }//GEN-LAST:event_toDatePickerPropertyChange
357 
358  private void fromDatePickerFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_fromDatePickerFocusLost
359  // set the "from" calendar button to listen to change in the text field
360  String fromDateString = this.fromDatePicker.getText();
361  if (!fromDateString.equals("")) {
362  try {
363  Date fromDate = dateFormat.parse(fromDateString);
364  fromDatePicker.setDate(fromDate.toInstant().atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
365  } catch (ParseException ex) {
366  // for now, no need to show the error message to the user her
367  }
368  }
369  }//GEN-LAST:event_fromDatePickerFocusLost
370 
371  private void toDatePickerFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_toDatePickerFocusLost
372  // set the "to" calendar button to listen to change in the text field
373  String toDateString = this.toDatePicker.getText();
374  if (!toDateString.equals("")) {
375  try {
376  Date toDate = dateFormat.parse(toDateString);
377  toDatePicker.setDate(toDate.toInstant().atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
378  } catch (ParseException ex) {
379  // for now, no need to show the error message to the user here
380  }
381  }
382  }//GEN-LAST:event_toDatePickerFocusLost
383 
390  private void setFromDate(Date date) {
391  String dateStringResult = "";
392  Instant ins = null;
393  if (date != null) {
394  dateStringResult = dateFormat.format(date);
395  ins = date.toInstant();
396  }
397 
398  fromDatePicker.setText(dateStringResult);
399  if (ins != null) {
400  fromDatePicker.setDate(ins.atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
401  } else {
402  fromDatePicker.setDate(null);
403  }
404  }
405 
411  private void setToDate(Date date) {
412  String dateStringResult = "";
413  Instant ins = null;
414  if (date != null) {
415  dateStringResult = dateFormat.format(date);
416  ins = date.toInstant();
417  }
418  toDatePicker.setText(dateStringResult);
419  if (ins != null) {
420  toDatePicker.setDate(ins.atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
421  } else {
422  toDatePicker.setDate(null);
423  }
424  }
425 
426  boolean isValidSearch() {
427  return this.accessedCheckBox.isSelected() ||
428  this.changedCheckBox.isSelected() ||
429  this.createdCheckBox.isSelected() ||
430  this.modifiedCheckBox.isSelected();
431  }
432  // Variables declaration - do not modify//GEN-BEGIN:variables
433  private javax.swing.JCheckBox accessedCheckBox;
434  private javax.swing.JCheckBox changedCheckBox;
435  private javax.swing.JMenuItem copyMenuItem;
436  private javax.swing.JCheckBox createdCheckBox;
437  private javax.swing.JMenuItem cutMenuItem;
438  private javax.swing.JCheckBox dateCheckBox;
439  private com.github.lgooddatepicker.components.DatePicker fromDatePicker;
440  private javax.swing.JLabel jLabel1;
441  private javax.swing.JLabel jLabel2;
442  private javax.swing.JLabel jLabel3;
443  private javax.swing.JLabel jLabel4;
444  private javax.swing.JCheckBox modifiedCheckBox;
445  private javax.swing.JMenuItem pasteMenuItem;
446  private javax.swing.JPopupMenu rightClickMenu;
447  private javax.swing.JMenuItem selectAllMenuItem;
448  private javax.swing.JComboBox<String> timeZoneComboBox;
449  private com.github.lgooddatepicker.components.DatePicker toDatePicker;
450  // End of variables declaration//GEN-END:variables
451 
452  void addDateChangeListener() {
453  DateChangeListener dcl = (DateChangeEvent event) -> {
454  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
455  };
456 
457  fromDatePicker.addDateChangeListener(dcl);
458  toDatePicker.addDateChangeListener(dcl);
459  }
460 
461 }
462 

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jun 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.