Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ImageFilePanel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2016 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.casemodule;
20 
21 import java.io.File;
22 import java.util.Calendar;
23 import java.util.List;
24 import java.util.SimpleTimeZone;
25 import java.util.TimeZone;
26 import javax.swing.JFileChooser;
27 import javax.swing.event.DocumentEvent;
28 import javax.swing.event.DocumentListener;
29 import javax.swing.JPanel;
30 import javax.swing.filechooser.FileFilter;
31 
32 import org.openide.util.NbBundle;
36 import java.util.logging.Level;
40 
44 public class ImageFilePanel extends JPanel implements DocumentListener {
45 
46  private final String PROP_LASTIMAGE_PATH = "LBL_LastImage_PATH"; //NON-NLS
47  private static final Logger logger = Logger.getLogger(ImageFilePanel.class.getName());
48  private final JFileChooser fc = new JFileChooser();
49 
50  // Externally supplied name is used to store settings
51  private final String contextName;
52 
61  private ImageFilePanel(String context, List<FileFilter> fileChooserFilters) {
63  fc.setDragEnabled(false);
64  fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
65  fc.setMultiSelectionEnabled(false);
66 
67  errorLabel.setVisible(false);
68 
69  boolean firstFilter = true;
70  for (FileFilter filter : fileChooserFilters) {
71  if (firstFilter) { // set the first on the list as the default selection
72  fc.setFileFilter(filter);
73  firstFilter = false;
74  } else {
75  fc.addChoosableFileFilter(filter);
76  }
77  }
78 
79  this.contextName = context;
80 
81  }
82 
87  public static synchronized ImageFilePanel createInstance(String context, List<FileFilter> fileChooserFilters) {
88 
89  ImageFilePanel instance = new ImageFilePanel(context, fileChooserFilters);
90  instance.postInit();
91  instance.createTimeZoneList();
92 
93  return instance;
94  }
95 
96  //post-constructor initialization to properly initialize listener support
97  //without leaking references of uninitialized objects
98  private void postInit() {
99  pathTextField.getDocument().addDocumentListener(this);
100  }
101 
107  @SuppressWarnings("unchecked")
108  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
109  private void initComponents() {
110 
111  pathLabel = new javax.swing.JLabel();
112  browseButton = new javax.swing.JButton();
113  pathTextField = new javax.swing.JTextField();
114  timeZoneLabel = new javax.swing.JLabel();
115  timeZoneComboBox = new javax.swing.JComboBox<String>();
116  noFatOrphansCheckbox = new javax.swing.JCheckBox();
117  descLabel = new javax.swing.JLabel();
118  errorLabel = new javax.swing.JLabel();
119 
120  setMinimumSize(new java.awt.Dimension(0, 65));
121  setPreferredSize(new java.awt.Dimension(403, 65));
122 
123  org.openide.awt.Mnemonics.setLocalizedText(pathLabel, org.openide.util.NbBundle.getMessage(ImageFilePanel.class, "ImageFilePanel.pathLabel.text")); // NOI18N
124 
125  org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(ImageFilePanel.class, "ImageFilePanel.browseButton.text")); // NOI18N
126  browseButton.addActionListener(new java.awt.event.ActionListener() {
127  public void actionPerformed(java.awt.event.ActionEvent evt) {
129  }
130  });
131 
132  pathTextField.setText(org.openide.util.NbBundle.getMessage(ImageFilePanel.class, "ImageFilePanel.pathTextField.text")); // NOI18N
133 
134  org.openide.awt.Mnemonics.setLocalizedText(timeZoneLabel, org.openide.util.NbBundle.getMessage(ImageFilePanel.class, "ImageFilePanel.timeZoneLabel.text")); // NOI18N
135 
136  timeZoneComboBox.setMaximumRowCount(30);
137 
138  org.openide.awt.Mnemonics.setLocalizedText(noFatOrphansCheckbox, org.openide.util.NbBundle.getMessage(ImageFilePanel.class, "ImageFilePanel.noFatOrphansCheckbox.text")); // NOI18N
139  noFatOrphansCheckbox.setToolTipText(org.openide.util.NbBundle.getMessage(ImageFilePanel.class, "ImageFilePanel.noFatOrphansCheckbox.toolTipText")); // NOI18N
140 
141  org.openide.awt.Mnemonics.setLocalizedText(descLabel, org.openide.util.NbBundle.getMessage(ImageFilePanel.class, "ImageFilePanel.descLabel.text")); // NOI18N
142 
143  errorLabel.setForeground(new java.awt.Color(255, 0, 0));
144  org.openide.awt.Mnemonics.setLocalizedText(errorLabel, org.openide.util.NbBundle.getMessage(ImageFilePanel.class, "ImageFilePanel.errorLabel.text")); // NOI18N
145 
146  javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
147  this.setLayout(layout);
148  layout.setHorizontalGroup(
149  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
150  .addGroup(layout.createSequentialGroup()
151  .addComponent(pathTextField)
152  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
153  .addComponent(browseButton)
154  .addGap(2, 2, 2))
155  .addGroup(layout.createSequentialGroup()
156  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
157  .addGroup(layout.createSequentialGroup()
158  .addComponent(timeZoneLabel)
159  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
160  .addComponent(timeZoneComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 215, javax.swing.GroupLayout.PREFERRED_SIZE))
161  .addComponent(pathLabel)
162  .addComponent(noFatOrphansCheckbox)
163  .addGroup(layout.createSequentialGroup()
164  .addGap(21, 21, 21)
165  .addComponent(descLabel))
166  .addComponent(errorLabel))
167  .addGap(0, 20, Short.MAX_VALUE))
168  );
169  layout.setVerticalGroup(
170  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
171  .addGroup(layout.createSequentialGroup()
172  .addComponent(pathLabel)
173  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
174  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
175  .addComponent(browseButton)
176  .addComponent(pathTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
177  .addGap(3, 3, 3)
178  .addComponent(errorLabel)
179  .addGap(1, 1, 1)
180  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
181  .addComponent(timeZoneLabel)
182  .addComponent(timeZoneComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
183  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
184  .addComponent(noFatOrphansCheckbox)
185  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
186  .addComponent(descLabel)
187  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
188  );
189  }// </editor-fold>//GEN-END:initComponents
190  @SuppressWarnings("deprecation")
191  private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
192  String oldText = pathTextField.getText();
193  // set the current directory of the FileChooser if the ImagePath Field is valid
194  File currentDir = new File(oldText);
195  if (currentDir.exists()) {
196  fc.setCurrentDirectory(currentDir);
197  }
198 
199  int retval = fc.showOpenDialog(this);
200  if (retval == JFileChooser.APPROVE_OPTION) {
201  String path = fc.getSelectedFile().getPath();
202  pathTextField.setText(path);
203  }
204 
205  try {
206  firePropertyChange(DataSourceProcessor.DSP_PANEL_EVENT.FOCUS_NEXT.toString(), false, true);
207  } catch (Exception e) {
208  logger.log(Level.SEVERE, "ImageFilePanel listener threw exception", e); //NON-NLS
209  MessageNotifyUtil.Notify.show(NbBundle.getMessage(this.getClass(), "ImageFilePanel.moduleErr"),
210  NbBundle.getMessage(this.getClass(), "ImageFilePanel.moduleErr.msg"),
212  }
213  }//GEN-LAST:event_browseButtonActionPerformed
214 
215  // Variables declaration - do not modify//GEN-BEGIN:variables
216  private javax.swing.JButton browseButton;
217  private javax.swing.JLabel descLabel;
218  private javax.swing.JLabel errorLabel;
219  private javax.swing.JCheckBox noFatOrphansCheckbox;
220  private javax.swing.JLabel pathLabel;
221  private javax.swing.JTextField pathTextField;
222  private javax.swing.JComboBox<String> timeZoneComboBox;
223  private javax.swing.JLabel timeZoneLabel;
224  // End of variables declaration//GEN-END:variables
225 
231  public String getContentPaths() {
232  return pathTextField.getText();
233  }
234 
239  public void setContentPath(String s) {
240  pathTextField.setText(s);
241  }
242 
243  public String getTimeZone() {
244  String tz = timeZoneComboBox.getSelectedItem().toString();
245  return tz.substring(tz.indexOf(")") + 2).trim();
246  }
247 
248  public boolean getNoFatOrphans() {
249  return noFatOrphansCheckbox.isSelected();
250  }
251 
252  public void reset() {
253  //reset the UI elements to default
254  pathTextField.setText(null);
255  }
256 
262  public boolean validatePanel() {
263  errorLabel.setVisible(false);
264  String path = getContentPaths();
265  if (path == null || path.isEmpty()) {
266  return false;
267  }
268 
269  // display warning if there is one (but don't disable "next" button)
270  warnIfPathIsInvalid(path);
271 
272  boolean isExist = new File(path).isFile();
273  boolean isPhysicalDrive = DriveUtils.isPhysicalDrive(path);
274  boolean isPartition = DriveUtils.isPartition(path);
275 
276  return (isExist || isPhysicalDrive || isPartition);
277  }
278 
285  private void warnIfPathIsInvalid(String path) {
287  errorLabel.setVisible(true);
288  errorLabel.setText(NbBundle.getMessage(this.getClass(), "DataSourceOnCDriveError.text"));
289  }
290  }
291 
292  public void storeSettings() {
293  String imagePathName = getContentPaths();
294  if (null != imagePathName) {
295  String imagePath = imagePathName.substring(0, imagePathName.lastIndexOf(File.separator) + 1);
296  ModuleSettings.setConfigSetting(contextName, PROP_LASTIMAGE_PATH, imagePath);
297  }
298  }
299 
300  public void readSettings() {
301  String lastImagePath = ModuleSettings.getConfigSetting(contextName, PROP_LASTIMAGE_PATH);
302  if (null != lastImagePath) {
303  if (!lastImagePath.isEmpty()) {
304  pathTextField.setText(lastImagePath);
305  }
306  }
307  }
308 
313  public void createTimeZoneList() {
314  // load and add all timezone
315  String[] ids = SimpleTimeZone.getAvailableIDs();
316  for (String id : ids) {
317  TimeZone zone = TimeZone.getTimeZone(id);
318  int offset = zone.getRawOffset() / 1000;
319  int hour = offset / 3600;
320  int minutes = (offset % 3600) / 60;
321  String item = String.format("(GMT%+d:%02d) %s", hour, minutes, id); //NON-NLS
322 
323  /*
324  * DateFormat dfm = new SimpleDateFormat("z");
325  * dfm.setTimeZone(zone); boolean hasDaylight =
326  * zone.useDaylightTime(); String first = dfm.format(new Date(2010,
327  * 1, 1)); String second = dfm.format(new Date(2011, 6, 6)); int mid
328  * = hour * -1; String result = first + Integer.toString(mid);
329  * if(hasDaylight){ result = result + second; }
330  * timeZoneComboBox.addItem(item + " (" + result + ")");
331  */
332  timeZoneComboBox.addItem(item);
333  }
334  // get the current timezone
335  TimeZone thisTimeZone = Calendar.getInstance().getTimeZone();
336  int thisOffset = thisTimeZone.getRawOffset() / 1000;
337  int thisHour = thisOffset / 3600;
338  int thisMinutes = (thisOffset % 3600) / 60;
339  String formatted = String.format("(GMT%+d:%02d) %s", thisHour, thisMinutes, thisTimeZone.getID()); //NON-NLS
340 
341  // set the selected timezone
342  timeZoneComboBox.setSelectedItem(formatted);
343  }
344 
352  @Override
353  public void insertUpdate(DocumentEvent e) {
354 
355  try {
356  firePropertyChange(DataSourceProcessor.DSP_PANEL_EVENT.UPDATE_UI.toString(), false, true);
357  } catch (Exception ee) {
358  logger.log(Level.SEVERE, "ImageFilePanel listener threw exception", ee); //NON-NLS
359  MessageNotifyUtil.Notify.show(NbBundle.getMessage(this.getClass(), "ImageFilePanel.moduleErr"),
360  NbBundle.getMessage(this.getClass(), "ImageFilePanel.moduleErr.msg"),
362  }
363  }
364 
365  @Override
366  public void removeUpdate(DocumentEvent e) {
367  try {
368  firePropertyChange(DataSourceProcessor.DSP_PANEL_EVENT.UPDATE_UI.toString(), false, true);
369  } catch (Exception ee) {
370  logger.log(Level.SEVERE, "ImageFilePanel listener threw exception", ee); //NON-NLS
371  MessageNotifyUtil.Notify.show(NbBundle.getMessage(this.getClass(), "ImageFilePanel.moduleErr"),
372  NbBundle.getMessage(this.getClass(), "ImageFilePanel.moduleErr.msg"),
374  }
375  }
376 
377  @Override
378  public void changedUpdate(DocumentEvent e) {
379 
380  try {
381  firePropertyChange(DataSourceProcessor.DSP_PANEL_EVENT.UPDATE_UI.toString(), false, true);
382  } catch (Exception ee) {
383  logger.log(Level.SEVERE, "ImageFilePanel listener threw exception", ee); //NON-NLS
384  MessageNotifyUtil.Notify.show(NbBundle.getMessage(this.getClass(), "ImageFilePanel.moduleErr"),
385  NbBundle.getMessage(this.getClass(), "ImageFilePanel.moduleErr.msg"),
387  }
388  }
389 
393  public void select() {
394  pathTextField.requestFocusInWindow();
395  }
396 }
static boolean isPhysicalDrive(String path)
Definition: DriveUtils.java:43
static synchronized ImageFilePanel createInstance(String context, List< FileFilter > fileChooserFilters)
static boolean isValid(String path, Case.CaseType caseType)
void browseButtonActionPerformed(java.awt.event.ActionEvent evt)
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
ImageFilePanel(String context, List< FileFilter > fileChooserFilters)
javax.swing.JComboBox< String > timeZoneComboBox
static String getConfigSetting(String moduleName, String settingName)
static boolean isPartition(String path)
Definition: DriveUtils.java:54
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
static void show(String title, String message, MessageType type, ActionListener actionListener)

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