19 package org.sleuthkit.autopsy.filesearch;
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;
42 @SuppressWarnings(
"PMD.SingularField")
43 class DateSearchPanel extends javax.swing.JPanel {
45 private final DatePickerSettings fromDateSettings =
new DatePickerSettings();
46 private final DatePickerSettings toDateSettings =
new DatePickerSettings();
47 DateFormat dateFormat;
48 List<String> timeZones;
50 DateSearchPanel(DateFormat dateFormat, List<String> timeZones) {
51 this.dateFormat = dateFormat;
52 this.timeZones = timeZones;
55 customizeComponents();
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);
64 ActionListener actList =
new ActionListener() {
66 public void actionPerformed(ActionEvent e) {
67 JMenuItem jmi = (JMenuItem) e.getSource();
72 JFormattedTextField jftf = (JFormattedTextField) ((JPopupMenu) jmi.getParent()).getInvoker();
73 if (jmi.equals(cutMenuItem)) {
75 }
else if (jmi.equals(copyMenuItem)) {
77 }
else if (jmi.equals(pasteMenuItem)) {
79 }
else if (jmi.equals(selectAllMenuItem)) {
84 cutMenuItem.addActionListener(actList);
85 copyMenuItem.addActionListener(actList);
86 pasteMenuItem.addActionListener(actList);
87 selectAllMenuItem.addActionListener(actList);
89 this.setComponentsEnabled();
92 JCheckBox getAccessedCheckBox() {
93 return accessedCheckBox;
96 JCheckBox getChangedCheckBox() {
97 return changedCheckBox;
100 JCheckBox getCreatedCheckBox() {
101 return createdCheckBox;
104 JCheckBox getDateCheckBox() {
108 String getFromDate() {
109 return fromDatePicker.getText();
113 return toDatePicker.getText();
116 JCheckBox getModifiedCheckBox() {
117 return modifiedCheckBox;
120 JComboBox<String> getTimeZoneComboBox() {
121 return timeZoneComboBox;
124 void setTimeZones(List<String> newTimeZones) {
125 this.timeZones = newTimeZones;
126 this.timeZoneComboBox.removeAllItems();
127 for (String tz : newTimeZones) {
128 this.timeZoneComboBox.addItem(tz);
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);
152 @SuppressWarnings(
"unchecked")
154 private
void initComponents() {
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);
175 cutMenuItem.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.cutMenuItem.text"));
176 rightClickMenu.add(cutMenuItem);
178 copyMenuItem.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.copyMenuItem.text"));
179 rightClickMenu.add(copyMenuItem);
181 pasteMenuItem.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.pasteMenuItem.text"));
182 rightClickMenu.add(pasteMenuItem);
184 selectAllMenuItem.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.selectAllMenuItem.text"));
185 rightClickMenu.add(selectAllMenuItem);
187 jLabel1.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.jLabel1.text"));
189 jLabel4.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.jLabel4.text"));
191 dateCheckBox.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.dateCheckBox.text"));
192 dateCheckBox.addActionListener(
new java.awt.event.ActionListener() {
193 public void actionPerformed(java.awt.event.ActionEvent evt) {
194 dateCheckBoxActionPerformed(evt);
198 jLabel3.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.jLabel3.text"));
199 jLabel3.setFont(
new java.awt.Font(
"Tahoma", 0, 10));
201 jLabel2.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.jLabel2.text"));
202 jLabel2.setFont(
new java.awt.Font(
"Tahoma", 0, 10));
204 modifiedCheckBox.setSelected(
true);
205 modifiedCheckBox.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.modifiedCheckBox.text"));
206 modifiedCheckBox.addActionListener(
new java.awt.event.ActionListener() {
207 public void actionPerformed(java.awt.event.ActionEvent evt) {
208 modifiedCheckBoxActionPerformed(evt);
212 changedCheckBox.setSelected(
true);
213 changedCheckBox.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.changedCheckBox.text"));
214 changedCheckBox.addActionListener(
new java.awt.event.ActionListener() {
215 public void actionPerformed(java.awt.event.ActionEvent evt) {
216 changedCheckBoxActionPerformed(evt);
220 accessedCheckBox.setSelected(
true);
221 accessedCheckBox.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.accessedCheckBox.text"));
222 accessedCheckBox.addActionListener(
new java.awt.event.ActionListener() {
223 public void actionPerformed(java.awt.event.ActionEvent evt) {
224 accessedCheckBoxActionPerformed(evt);
228 createdCheckBox.setSelected(
true);
229 createdCheckBox.setText(
org.openide.util.NbBundle.getMessage(DateSearchPanel.class,
"DateSearchPanel.createdCheckBox.text"));
230 createdCheckBox.addActionListener(
new java.awt.event.ActionListener() {
231 public void actionPerformed(java.awt.event.ActionEvent evt) {
232 createdCheckBoxActionPerformed(evt);
236 fromDatePicker.addFocusListener(
new java.awt.event.FocusAdapter() {
237 public void focusLost(java.awt.event.FocusEvent evt) {
238 fromDatePickerFocusLost(evt);
241 fromDatePicker.addPropertyChangeListener(
new java.beans.PropertyChangeListener() {
242 public void propertyChange(java.beans.PropertyChangeEvent evt) {
243 fromDatePickerPropertyChange(evt);
247 toDatePicker.addFocusListener(
new java.awt.event.FocusAdapter() {
248 public void focusLost(java.awt.event.FocusEvent evt) {
249 toDatePickerFocusLost(evt);
252 toDatePicker.addPropertyChangeListener(
new java.beans.PropertyChangeListener() {
253 public void propertyChange(java.beans.PropertyChangeEvent evt) {
254 toDatePickerPropertyChange(evt);
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()
264 .addComponent(jLabel2)
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)
278 .addComponent(accessedCheckBox)
279 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
280 .addComponent(createdCheckBox)
281 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
282 .addComponent(changedCheckBox)))
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)
291 .addComponent(toDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
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)
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)))
325 private void dateCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
326 this.setComponentsEnabled();
327 firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
330 private void modifiedCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
331 firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
334 private void accessedCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
335 firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
338 private void createdCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
339 firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
342 private void changedCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
343 firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
346 private void fromDatePickerPropertyChange(java.beans.PropertyChangeEvent evt) {
347 if (evt.getNewValue() instanceof Date) {
348 setFromDate((Date) evt.getNewValue());
352 private void toDatePickerPropertyChange(java.beans.PropertyChangeEvent evt) {
353 if (evt.getNewValue() instanceof Date) {
354 setToDate((Date) evt.getNewValue());
358 private void fromDatePickerFocusLost(java.awt.event.FocusEvent evt) {
360 String fromDateString = this.fromDatePicker.getText();
361 if (!fromDateString.equals(
"")) {
363 Date fromDate = dateFormat.parse(fromDateString);
364 fromDatePicker.setDate(fromDate.toInstant().atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
365 }
catch (ParseException ex) {
371 private void toDatePickerFocusLost(java.awt.event.FocusEvent evt) {
373 String toDateString = this.toDatePicker.getText();
374 if (!toDateString.equals(
"")) {
376 Date toDate = dateFormat.parse(toDateString);
377 toDatePicker.setDate(toDate.toInstant().atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
378 }
catch (ParseException ex) {
390 private void setFromDate(Date date) {
391 String dateStringResult =
"";
394 dateStringResult = dateFormat.format(date);
395 ins = date.toInstant();
398 fromDatePicker.setText(dateStringResult);
400 fromDatePicker.setDate(ins.atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
402 fromDatePicker.setDate(null);
411 private void setToDate(Date date) {
412 String dateStringResult =
"";
415 dateStringResult = dateFormat.format(date);
416 ins = date.toInstant();
418 toDatePicker.setText(dateStringResult);
420 toDatePicker.setDate(ins.atZone(dateFormat.getTimeZone().toZoneId()).toLocalDate());
422 toDatePicker.setDate(null);
426 boolean isValidSearch() {
427 return this.accessedCheckBox.isSelected() ||
428 this.changedCheckBox.isSelected() ||
429 this.createdCheckBox.isSelected() ||
430 this.modifiedCheckBox.isSelected();
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;
452 void addDateChangeListener() {
453 DateChangeListener dcl = (DateChangeEvent event) -> {
454 firePropertyChange(FileSearchPanel.EVENT.CHECKED.toString(), null, null);
457 fromDatePicker.addDateChangeListener(dcl);
458 toDatePicker.addDateChangeListener(dcl);