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

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