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

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