Autopsy  4.1
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-2017 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 import javax.swing.event.ChangeEvent;
27 import javax.swing.event.ChangeListener;
28 import org.openide.DialogDescriptor;
29 import org.openide.DialogDisplayer;
30 import org.openide.NotifyDescriptor;
31 import org.openide.WizardDescriptor;
32 import org.openide.WizardValidationException;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
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<>(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<>(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 panel = getComponent();
171  try {
172  String lastBaseDirectory = ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, PROP_BASECASE);
173  panel.setCaseParentDir(lastBaseDirectory);
174  panel.readSettings();
175  createdDirectory = (String) settings.getProperty("createdDirectory"); //NON-NLS
176  if (createdDirectory != null && !createdDirectory.isEmpty()) {
177  logger.log(Level.INFO, "Deleting a case dir in readSettings(): {0}", createdDirectory); //NON-NLS
178  FileUtil.deleteDir(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  /*
208  * Check whether or not the case name is valid. To be valid, the case
209  * name must not contain any characters that are not allowed in file
210  * names, since it will be used as the name of the case directory.
211  */
212  String caseName = getComponent().getCaseName();
213  if (!Case.isValidName(caseName)) {
214  String errorMsg = NbBundle
215  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.invalidSymbols");
216  validationError(errorMsg);
217  } else {
218 
219  String caseParentDir = getComponent().getCaseParentDir();
220  String caseDirPath = caseParentDir + caseName;
221 
222  // check if the directory exist
223  if (new File(caseDirPath).exists()) {
224  // throw a warning to enter new data or delete the existing directory
225  String errorMsg = NbBundle
226  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.dirExists", caseDirPath);
227  validationError(errorMsg);
228  } else {
229  // check if the "base" directory path is absolute
230  File baseDir = new File(caseParentDir);
231  if (baseDir.isAbsolute()) {
232  // when the base directory doesn't exist
233  if (!baseDir.exists()) {
234  // get confirmation to create directory
235  String confMsg = NbBundle
236  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.confMsg.createDir.msg",
237  caseParentDir);
238  NotifyDescriptor d2 = new NotifyDescriptor.Confirmation(confMsg,
239  NbBundle.getMessage(this.getClass(),
240  "NewCaseWizardPanel1.validate.confMsg.createDir.title"),
241  NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE);
242  d2.setValue(NotifyDescriptor.NO_OPTION);
243 
244  Object res2 = DialogDisplayer.getDefault().notify(d2);
245  if (res2 != null && res2 == DialogDescriptor.YES_OPTION) {
246  // if user says yes
247  try {
248  createDirectory(caseDirPath, getComponent().getCaseType());
249  } catch (WizardValidationException ex) {
250  String errorMsg = NbBundle.getMessage(this.getClass(),
251  "NewCaseWizardPanel1.validate.errMsg.cantCreateParDir.msg",
252  caseParentDir);
253  logger.log(Level.WARNING, errorMsg, ex);
254  validationError(errorMsg);
255  }
256  }
257  if (res2 != null && res2 == DialogDescriptor.NO_OPTION) {
258  // if user says no
259  validationError(NbBundle.getMessage(this.getClass(),
260  "NewCaseWizardPanel1.validate.errMsg.prevCreateBaseDir.msg",
261  caseDirPath));
262  }
263  } else {
264  try {
265  createDirectory(caseDirPath, getComponent().getCaseType());
266  } catch (WizardValidationException ex) {
267  String errorMsg = NbBundle
268  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.cantCreateDir");
269  logger.log(Level.WARNING, errorMsg, ex);
270  validationError(errorMsg);
271  }
272  }
273  } else {
274  // throw a notification
275  String errorMsg = NbBundle
276  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.invalidBaseDir.msg");
277  validationError(errorMsg);
278  }
279  }
280  }
281  }
282 
283  private void validationError(String errorMsg) throws WizardValidationException {
284  throw new WizardValidationException(this.getComponent(), errorMsg, null);
285  }
286 
287  /*
288  * create the directory and create a new case
289  */
290  private void createDirectory(final String caseDirPath, CaseType caseType) throws WizardValidationException {
291  // try to create the directory with the case name in the chosen parent directory
292  boolean success = false;
293  try {
294  Case.createCaseDirectory(caseDirPath, caseType);
295  success = true;
296  } catch (CaseActionException ex) {
297  logger.log(Level.SEVERE, "Could not createDirectory for the case, ", ex); //NON-NLS
298  }
299 
300  // check if the directory is successfully created
301  if (!success) {
302 
303  // delete the folder if we already created the folder and the error shows up
304  if (new File(caseDirPath).exists()) {
305  FileUtil.deleteDir(new File(caseDirPath));
306  }
307 
308  String errorMsg = NbBundle.getMessage(this.getClass(),
309  "NewCaseWizardPanel1.createDir.errMsg.cantCreateDir.msg");
310 
311  validationError(errorMsg);
312 
313  } // the new case directory is successfully created
314  else {
315  createdDirectory = caseDirPath;
316  // try to close Startup window if there's one
317  try {
318  StartupWindowProvider.getInstance().close();
319  } catch (Exception ex) {
320  logger.log(Level.WARNING, "Startup window didn't close as expected.", ex); //NON-NLS
321 
322  }
323  }
324  }
325 }

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.