19 package org.sleuthkit.autopsy.filesearch;
21 import java.awt.Component;
22 import java.awt.event.ActionListener;
23 import java.beans.PropertyChangeEvent;
24 import java.beans.PropertyChangeListener;
25 import java.text.DateFormat;
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Calendar;
30 import java.util.Date;
31 import java.util.Iterator;
32 import java.util.List;
34 import java.util.SimpleTimeZone;
35 import java.util.TimeZone;
36 import javax.swing.JLabel;
37 import javax.swing.JList;
38 import javax.swing.JSeparator;
39 import javax.swing.ListCellRenderer;
40 import javax.swing.SwingUtilities;
41 import javax.swing.border.EmptyBorder;
42 import org.openide.util.NbBundle;
50 class DateSearchFilter
extends AbstractFileSearchFilter<DateSearchPanel> {
52 private static final String NONE_SELECTED_MESSAGE = NbBundle.getMessage(DateSearchFilter.class,
"DateSearchFilter.noneSelectedMsg.text");
53 private static final DateFormat DATE_FORMAT =
new SimpleDateFormat(
"MM/dd/yyyy");
54 private static final String SEPARATOR =
"SEPARATOR";
60 this(
new DateSearchPanel(DATE_FORMAT, DateSearchFilter.createTimeZoneList()));
63 private DateSearchFilter(DateSearchPanel panel) {
65 Case.addPropertyChangeListener(this.
new CasePropertyChangeListener());
69 public boolean isEnabled() {
70 return this.getComponent().getDateCheckBox().isSelected();
74 public String getPredicate() throws FilterValidationException {
75 String query =
"NULL";
76 DateSearchPanel panel = this.getComponent();
79 String tz = this.getComponent().getTimeZoneComboBox().getSelectedItem().toString();
80 String tzID = tz.substring(tz.indexOf(
" ") + 1);
81 TimeZone selectedTZ = TimeZone.getTimeZone(tzID);
85 String startDateValue = panel.getDateFromTextField().getText();
86 Calendar startDate = null;
88 DateFormat sdf =
new SimpleDateFormat(
"MM/dd/yyyy");
89 sdf.setTimeZone(selectedTZ);
90 Date temp = sdf.parse(startDateValue);
92 startDate = Calendar.getInstance(
new SimpleTimeZone(0,
"GMT"));
93 startDate.setTime(temp);
94 }
catch (ParseException ex) {
97 if (!startDateValue.isEmpty()) {
98 if (startDate != null) {
99 fromDate = startDate.getTimeInMillis() / 1000;
104 String endDateValue = panel.getDateToTextField().getText();
105 Calendar endDate = null;
107 DateFormat sdf =
new SimpleDateFormat(
"MM/dd/yyyy");
108 sdf.setTimeZone(selectedTZ);
109 Date temp2 = sdf.parse(endDateValue);
111 endDate = Calendar.getInstance(
new SimpleTimeZone(0,
"GMT"));
112 endDate.setTime(temp2);
113 endDate.set(Calendar.HOUR, endDate.get(Calendar.HOUR) + 24);
114 }
catch (ParseException ex) {
117 if (!endDateValue.isEmpty()) {
118 if (endDate != null) {
119 toDate = endDate.getTimeInMillis() / 1000;
124 if (fromDate > toDate) {
130 final boolean modifiedChecked = panel.getModifiedCheckBox().isSelected();
131 final boolean changedChecked = panel.getChangedCheckBox().isSelected();
132 final boolean accessedChecked = panel.getAccessedCheckBox().isSelected();
133 final boolean createdChecked = panel.getCreatedCheckBox().isSelected();
135 if (modifiedChecked || changedChecked || accessedChecked || createdChecked) {
137 if (modifiedChecked) {
138 query +=
" OR (mtime BETWEEN " + fromDate +
" AND " + toDate +
")";
141 if (changedChecked) {
142 query +=
" OR (ctime BETWEEN " + fromDate +
" AND " + toDate +
")";
145 if (accessedChecked) {
146 query +=
" OR (atime BETWEEN " + fromDate +
" AND " + toDate +
")";
149 if (createdChecked) {
150 query +=
" OR (crtime BETWEEN " + fromDate +
" AND " + toDate +
")";
154 throw new FilterValidationException(NONE_SELECTED_MESSAGE);
161 private void updateTimeZoneList() {
162 this.getComponent().setTimeZones(DateSearchFilter.createTimeZoneList());
165 private static List<String> createTimeZoneList() {
167 List<String> timeZones =
new ArrayList<>();
171 Case currentCase = Case.getCurrentCase();
173 Set<TimeZone> caseTimeZones = currentCase.getTimeZones();
174 Iterator<TimeZone> iterator = caseTimeZones.iterator();
175 while (iterator.hasNext()) {
176 TimeZone zone = iterator.next();
177 int offset = zone.getRawOffset() / 1000;
178 int hour = offset / 3600;
179 int minutes = (offset % 3600) / 60;
180 String item = String.format(
"(GMT%+d:%02d) %s", hour, minutes, zone.getID());
184 if (caseTimeZones.size() > 0) {
185 timeZones.add(SEPARATOR);
189 String[] ids = SimpleTimeZone.getAvailableIDs();
190 for (String
id : ids) {
191 TimeZone zone = TimeZone.getTimeZone(
id);
192 int offset = zone.getRawOffset() / 1000;
193 int hour = offset / 3600;
194 int minutes = (offset % 3600) / 60;
195 String item = String.format(
"(GMT%+d:%02d) %s", hour, minutes,
id);
198 }
catch (IllegalStateException ex) {
206 public void addActionListener(ActionListener l) {
207 getComponent().addActionListener(l);
211 public boolean isValid() {
212 return this.getComponent().isValidSearch();
218 static class ComboBoxRenderer
extends JLabel implements ListCellRenderer<String> {
220 JSeparator separator;
224 setBorder(
new EmptyBorder(1, 1, 1, 1));
225 separator =
new JSeparator(JSeparator.HORIZONTAL);
229 public Component getListCellRendererComponent(JList<? extends String> list, String value,
int index,
boolean isSelected,
boolean cellHasFocus) {
230 String str = (value == null) ?
"" : value;
231 if (SEPARATOR.equals(str)) {
235 setBackground(list.getSelectionBackground());
236 setForeground(list.getSelectionForeground());
238 setBackground(list.getBackground());
239 setForeground(list.getForeground());
241 setFont(list.getFont());
251 switch (
Case.
Events.valueOf(evt.getPropertyName())) {
253 Object newValue = evt.getNewValue();
254 if (null != newValue) {
258 SwingUtilities.invokeLater(DateSearchFilter.this::updateTimeZoneList);
261 case DATA_SOURCE_ADDED:
262 case DATA_SOURCE_DELETED:
271 SwingUtilities.invokeLater(DateSearchFilter.this::updateTimeZoneList);
272 }
catch (IllegalStateException notUsed) {
void propertyChange(PropertyChangeEvent evt)
static Case getCurrentCase()