Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
NewCaseWizardPanel1.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011 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.HashSet;
23 import java.util.Iterator;
24 import java.util.Set;
25 import java.util.logging.Level;
26 
27 import org.openide.util.NbBundle;
29 import javax.swing.event.ChangeEvent;
30 import javax.swing.event.ChangeListener;
31 import org.openide.DialogDescriptor;
32 import org.openide.DialogDisplayer;
33 import org.openide.NotifyDescriptor;
34 import org.openide.WizardDescriptor;
35 import org.openide.WizardValidationException;
36 import org.openide.util.HelpCtx;
39 
45 class NewCaseWizardPanel1 implements WizardDescriptor.ValidatingPanel<WizardDescriptor> {
46 
51  private NewCaseVisualPanel1 component;
52  private Boolean isFinish = false;
53  private static String createdDirectory;
54  private static final String PROP_BASECASE = "LBL_BaseCase_PATH"; //NON-NLS
55  private static final Logger logger = Logger.getLogger(NewCaseWizardPanel1.class.getName());
56 
65  @Override
66  public NewCaseVisualPanel1 getComponent() {
67  if (component == null) {
68  component = new NewCaseVisualPanel1(this);
69  }
70  return component;
71  }
72 
79  @Override
80  public HelpCtx getHelp() {
81  // Show no Help button for this panel:
82  return HelpCtx.DEFAULT_HELP;
83  // If you have context help:
84  // return new HelpCtx(SampleWizardPanel1.class);
85  }
86 
94  @Override
95  public boolean isValid() {
96  // If it is always OK to press Next or Finish, then:
97  return isFinish;
98  // If it depends on some condition (form filled out...), then:
99  // return someCondition();
100  // and when this condition changes (last form field filled in...) then:
101  // fireChangeEvent();
102  // and uncomment the complicated stuff below.
103  }
104  private final Set<ChangeListener> listeners = new HashSet<ChangeListener>(1); // or can use ChangeSupport in NB 6.0
105 
111  @Override
112  public final void addChangeListener(ChangeListener l) {
113  synchronized (listeners) {
114  listeners.add(l);
115  }
116  }
117 
123  @Override
124  public final void removeChangeListener(ChangeListener l) {
125  synchronized (listeners) {
126  listeners.remove(l);
127  }
128  }
129 
134  protected final void fireChangeEvent() {
135  Iterator<ChangeListener> it;
136  synchronized (listeners) {
137  it = new HashSet<ChangeListener>(listeners).iterator();
138  }
139  ChangeEvent ev = new ChangeEvent(this);
140  while (it.hasNext()) {
141  it.next().stateChanged(ev);
142  }
143  }
144 
151  public void setIsFinish(Boolean isFinish) {
152  this.isFinish = isFinish;
153  fireChangeEvent();
154  }
155 
156  // You can use a settings object to keep track of state. Normally the
157  // settings object will be the WizardDescriptor, so you can use
158  // WizardDescriptor.getProperty & putProperty to store information entered
159  // by the user.
168  @Override
169  public void readSettings(WizardDescriptor settings) {
170  NewCaseVisualPanel1 component = getComponent();
171  try {
172  String lastBaseDirectory = ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, PROP_BASECASE);
173  component.setCaseParentDir(lastBaseDirectory);
174  component.readSettings();
175  createdDirectory = (String) settings.getProperty("createdDirectory"); //NON-NLS
176  if (createdDirectory != null && !createdDirectory.equals("")) {
177  logger.log(Level.INFO, "Deleting a case dir in readSettings(): " + createdDirectory); //NON-NLS
178  Case.deleteCaseDirectory(new File(createdDirectory));
179  }
180  } catch (Exception e) {
181  logger.log(Level.WARNING, "Could not read wizard settings in NewCaseWizardPanel1, ", e); //NON-NLS
182  }
183  }
184 
194  @Override
195  public void storeSettings(WizardDescriptor settings) {
196  CaseType caseType = getComponent().getCaseType();
197  settings.putProperty("caseName", getComponent().getCaseName()); //NON-NLS
198  settings.putProperty("caseParentDir", getComponent().getCaseParentDir()); //NON-NLS
199  settings.putProperty("createdDirectory", createdDirectory); //NON-NLS
200  settings.putProperty("caseType", caseType.ordinal()); //NON-NLS
201  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, ModuleSettings.CURRENT_CASE_TYPE, caseType.toString());
202  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, PROP_BASECASE, getComponent().getCaseParentDir());
203  }
204 
205  @Override
206  public void validate() throws WizardValidationException {
207  String caseName = getComponent().getCaseName();
208  String caseParentDir = getComponent().getCaseParentDir();
209  String caseDirPath = caseParentDir + caseName;
210 
211  // check if case Name contain one of this following symbol:
212  // \ / : * ? " < > |
213  if (!Case.isValidName(caseName)) {
214  String errorMsg = NbBundle
215  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.invalidSymbols");
216  validationError(errorMsg);
217  } else {
218  // check if the directory exist
219  if (new File(caseDirPath).exists()) {
220  // throw a warning to enter new data or delete the existing directory
221  String errorMsg = NbBundle
222  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.dirExists", caseDirPath);
223  validationError(errorMsg);
224  } else {
225  // check if the "base" directory path is absolute
226  File baseDir = new File(caseParentDir);
227  if (baseDir.isAbsolute()) {
228  // when the base directory doesn't exist
229  if (!baseDir.exists()) {
230  // get confirmation to create directory
231  String confMsg = NbBundle
232  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.confMsg.createDir.msg",
233  caseParentDir);
234  NotifyDescriptor d2 = new NotifyDescriptor.Confirmation(confMsg,
235  NbBundle.getMessage(this.getClass(),
236  "NewCaseWizardPanel1.validate.confMsg.createDir.title"),
237  NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE);
238  d2.setValue(NotifyDescriptor.NO_OPTION);
239 
240  Object res2 = DialogDisplayer.getDefault().notify(d2);
241  if (res2 != null && res2 == DialogDescriptor.YES_OPTION) {
242  // if user says yes
243  try {
244  createDirectory(caseDirPath, getComponent().getCaseType());
245  } catch (Exception ex) {
246  String errorMsg = NbBundle.getMessage(this.getClass(),
247  "NewCaseWizardPanel1.validate.errMsg.cantCreateParDir.msg",
248  caseParentDir);
249  logger.log(Level.WARNING, errorMsg, ex);
250  validationError(errorMsg);
251  }
252  }
253  if (res2 != null && res2 == DialogDescriptor.NO_OPTION) {
254  // if user says no
255  validationError(NbBundle.getMessage(this.getClass(),
256  "NewCaseWizardPanel1.validate.errMsg.prevCreateBaseDir.msg",
257  caseDirPath));
258  }
259  } else {
260  try {
261  createDirectory(caseDirPath, getComponent().getCaseType());
262  } catch (Exception ex) {
263  String errorMsg = NbBundle
264  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.cantCreateDir");
265  logger.log(Level.WARNING, errorMsg, ex);
266  validationError(errorMsg);
267  }
268  }
269  } else {
270  // throw a notification
271  String errorMsg = NbBundle
272  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.invalidBaseDir.msg");
273  validationError(errorMsg);
274  }
275  }
276  }
277  }
278 
279  private void validationError(String errorMsg) throws WizardValidationException {
280  throw new WizardValidationException(this.getComponent(), errorMsg, null);
281  }
282 
283  /*
284  * create the directory and create a new case
285  */
286  private void createDirectory(final String caseDirPath, CaseType caseType) throws WizardValidationException {
287  // try to create the directory with the case name in the chosen parent directory
288  boolean success = false;
289  try {
290  Case.createCaseDirectory(caseDirPath, caseType);
291  success = true;
292  } catch (CaseActionException ex) {
293  logger.log(Level.SEVERE, "Could not createDirectory for the case, ", ex); //NON-NLS
294  }
295 
296  // check if the directory is successfully created
297  if (!success) {
298 
299  // delete the folder if we already created the folder and the error shows up
300  if (new File(caseDirPath).exists()) {
301  Case.deleteCaseDirectory(new File(caseDirPath));
302  }
303 
304  String errorMsg = NbBundle.getMessage(this.getClass(),
305  "NewCaseWizardPanel1.createDir.errMsg.cantCreateDir.msg");
306 
307  validationError(errorMsg);
308 
309  } // the new case directory is successfully created
310  else {
311  createdDirectory = caseDirPath;
312  // try to close Startup window if there's one
313  try {
314  StartupWindowProvider.getInstance().close();
315  } catch (Exception ex) {
316  logger.log(Level.WARNING, "Startup window didn't close as expected.", ex); //NON-NLS
317 
318  }
319 
320  }
321  }
322 }

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.