Autopsy  4.20.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.noLimitLabel.setEnabled(enable);
138  this.dateFormatLabel.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  dateFormatLabel = new javax.swing.JLabel();
167  noLimitLabel = 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  dateFormatLabel.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.dateFormatLabel.text")); // NOI18N
199  dateFormatLabel.setFont(dateFormatLabel.getFont().deriveFont(dateFormatLabel.getFont().getSize()-1f));
200 
201  noLimitLabel.setText(org.openide.util.NbBundle.getMessage(DateSearchPanel.class, "DateSearchPanel.noLimitLabel.text")); // NOI18N
202  noLimitLabel.setFont(noLimitLabel.getFont().deriveFont(noLimitLabel.getFont().getSize()-1f));
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.setOpaque(false);
237  fromDatePicker.addFocusListener(new java.awt.event.FocusAdapter() {
238  public void focusLost(java.awt.event.FocusEvent evt) {
239  fromDatePickerFocusLost(evt);
240  }
241  });
242  fromDatePicker.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
243  public void propertyChange(java.beans.PropertyChangeEvent evt) {
244  fromDatePickerPropertyChange(evt);
245  }
246  });
247 
248  toDatePicker.setOpaque(false);
249  toDatePicker.addFocusListener(new java.awt.event.FocusAdapter() {
250  public void focusLost(java.awt.event.FocusEvent evt) {
251  toDatePickerFocusLost(evt);
252  }
253  });
254  toDatePicker.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
255  public void propertyChange(java.beans.PropertyChangeEvent evt) {
256  toDatePickerPropertyChange(evt);
257  }
258  });
259 
260  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
261  this.setLayout(layout);
262  layout.setHorizontalGroup(
263  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
264  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
265  .addGap(0, 0, Short.MAX_VALUE)
266  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
267  .addGroup(layout.createSequentialGroup()
268  .addComponent(jLabel4)
269  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
270  .addComponent(timeZoneComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
271  .addGroup(layout.createSequentialGroup()
272  .addComponent(modifiedCheckBox)
273  .addGap(6, 6, 6)
274  .addComponent(accessedCheckBox)
275  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
276  .addComponent(createdCheckBox)
277  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
278  .addComponent(changedCheckBox)))
279  .addGap(33, 33, 33))
280  .addGroup(layout.createSequentialGroup()
281  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
282  .addGroup(layout.createSequentialGroup()
283  .addContainerGap()
284  .addComponent(noLimitLabel)
285  .addGap(18, 18, 18)
286  .addComponent(dateFormatLabel))
287  .addGroup(layout.createSequentialGroup()
288  .addComponent(dateCheckBox)
289  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
290  .addComponent(fromDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
291  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
292  .addComponent(jLabel1)
293  .addGap(10, 10, 10)
294  .addComponent(toDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
295  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
296  );
297  layout.setVerticalGroup(
298  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
299  .addGroup(layout.createSequentialGroup()
300  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
301  .addGroup(layout.createSequentialGroup()
302  .addComponent(dateCheckBox)
303  .addGap(18, 18, 18))
304  .addGroup(layout.createSequentialGroup()
305  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
306  .addComponent(jLabel1)
307  .addComponent(fromDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
308  .addComponent(toDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
309  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
310  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
311  .addComponent(dateFormatLabel)
312  .addComponent(noLimitLabel))
313  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
314  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
315  .addComponent(jLabel4)
316  .addComponent(timeZoneComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
317  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
318  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
319  .addComponent(modifiedCheckBox, javax.swing.GroupLayout.Alignment.TRAILING)
320  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
321  .addComponent(accessedCheckBox)
322  .addComponent(createdCheckBox)
323  .addComponent(changedCheckBox)))
324  .addGap(0, 0, 0))
325  );
326  }// </editor-fold>//GEN-END:initComponents
327 
328  private void dateCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dateCheckBoxActionPerformed
329  this.setComponentsEnabled();
330  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
331  }//GEN-LAST:event_dateCheckBoxActionPerformed
332 
333  private void modifiedCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_modifiedCheckBoxActionPerformed
334  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
335  }//GEN-LAST:event_modifiedCheckBoxActionPerformed
336 
337  private void accessedCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accessedCheckBoxActionPerformed
338  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
339  }//GEN-LAST:event_accessedCheckBoxActionPerformed
340 
341  private void createdCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createdCheckBoxActionPerformed
342  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
343  }//GEN-LAST:event_createdCheckBoxActionPerformed
344 
345  private void changedCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changedCheckBoxActionPerformed
346  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
347  }//GEN-LAST:event_changedCheckBoxActionPerformed
348 
349  private void fromDatePickerPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fromDatePickerPropertyChange
350  if (evt.getNewValue() instanceof Date) {
351  setFromDate((Date) evt.getNewValue());
352  }
353  }//GEN-LAST:event_fromDatePickerPropertyChange
354 
355  private void toDatePickerPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_toDatePickerPropertyChange
356  if (evt.getNewValue() instanceof Date) {
357  setToDate((Date) evt.getNewValue());
358  }
359  }//GEN-LAST:event_toDatePickerPropertyChange
360 
361  private void fromDatePickerFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_fromDatePickerFocusLost
362  // set the "from" calendar button to listen to change in the text field
363  String fromDateString = this.fromDatePicker.getText();
364  if (!fromDateString.equals("")) {
365  try {
366  Date fromDate = dateFormat.parse(fromDateString);
367  fromDatePicker.setDate(fromDate.toInstant().atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
368  } catch (ParseException ex) {
369  // for now, no need to show the error message to the user her
370  }
371  }
372  }//GEN-LAST:event_fromDatePickerFocusLost
373 
374  private void toDatePickerFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_toDatePickerFocusLost
375  // set the "to" calendar button to listen to change in the text field
376  String toDateString = this.toDatePicker.getText();
377  if (!toDateString.equals("")) {
378  try {
379  Date toDate = dateFormat.parse(toDateString);
380  toDatePicker.setDate(toDate.toInstant().atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
381  } catch (ParseException ex) {
382  // for now, no need to show the error message to the user here
383  }
384  }
385  }//GEN-LAST:event_toDatePickerFocusLost
386 
393  private void setFromDate(Date date) {
394  String dateStringResult = "";
395  Instant ins = null;
396  if (date != null) {
397  dateStringResult = dateFormat.format(date);
398  ins = date.toInstant();
399  }
400 
401  fromDatePicker.setText(dateStringResult);
402  if (ins != null) {
403  fromDatePicker.setDate(ins.atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
404  } else {
405  fromDatePicker.setDate(null);
406  }
407  }
408 
414  private void setToDate(Date date) {
415  String dateStringResult = "";
416  Instant ins = null;
417  if (date != null) {
418  dateStringResult = dateFormat.format(date);
419  ins = date.toInstant();
420  }
421  toDatePicker.setText(dateStringResult);
422  if (ins != null) {
423  toDatePicker.setDate(ins.atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
424  } else {
425  toDatePicker.setDate(null);
426  }
427  }
428 
429  boolean isValidSearch() {
430  return this.accessedCheckBox.isSelected() ||
431  this.changedCheckBox.isSelected() ||
432  this.createdCheckBox.isSelected() ||
433  this.modifiedCheckBox.isSelected();
434  }
435  // Variables declaration - do not modify//GEN-BEGIN:variables
436  private javax.swing.JCheckBox accessedCheckBox;
437  private javax.swing.JCheckBox changedCheckBox;
438  private javax.swing.JMenuItem copyMenuItem;
439  private javax.swing.JCheckBox createdCheckBox;
440  private javax.swing.JMenuItem cutMenuItem;
441  private javax.swing.JCheckBox dateCheckBox;
442  private javax.swing.JLabel dateFormatLabel;
443  private com.github.lgooddatepicker.components.DatePicker fromDatePicker;
444  private javax.swing.JLabel jLabel1;
445  private javax.swing.JLabel jLabel4;
446  private javax.swing.JCheckBox modifiedCheckBox;
447  private javax.swing.JLabel noLimitLabel;
448  private javax.swing.JMenuItem pasteMenuItem;
449  private javax.swing.JPopupMenu rightClickMenu;
450  private javax.swing.JMenuItem selectAllMenuItem;
451  private javax.swing.JComboBox<String> timeZoneComboBox;
452  private com.github.lgooddatepicker.components.DatePicker toDatePicker;
453  // End of variables declaration//GEN-END:variables
454 
455  void addDateChangeListener() {
456  DateChangeListener dcl = (DateChangeEvent event) -> {
457  firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
458  };
459 
460  fromDatePicker.addDateChangeListener(dcl);
461  toDatePicker.addDateChangeListener(dcl);
462  }
463 
464 }
465 

Copyright © 2012-2022 Basis Technology. Generated on: Tue Aug 1 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.