Autopsy  3.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 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.Exceptions;
37 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.getCaseParentDirTextField().setText(lastBaseDirectory);
174  createdDirectory = (String) settings.getProperty("createdDirectory"); //NON-NLS
175  if (createdDirectory != null && !createdDirectory.equals("")) {
176  logger.log(Level.INFO, "Deleting a case dir in readSettings(): " + createdDirectory); //NON-NLS
177  Case.deleteCaseDirectory(new File(createdDirectory));
178  }
179  } catch (Exception e) {
180  logger.log(Level.WARNING, "Could not read wizard settings in NewCaseWizardPanel1, ", e); //NON-NLS
181  }
182  }
183 
193  @Override
194  public void storeSettings(WizardDescriptor settings) {
195  settings.putProperty("caseName", getComponent().getCaseName()); //NON-NLS
196  settings.putProperty("caseParentDir", getComponent().getCaseParentDir()); //NON-NLS
197  settings.putProperty("createdDirectory", createdDirectory); //NON-NLS
198  ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, PROP_BASECASE, getComponent().getCaseParentDir());
199  }
200 
201  @Override
202  public void validate() throws WizardValidationException {
203  String caseName = getComponent().getCaseName();
204  String caseParentDir = getComponent().getCaseParentDir();
205  String caseDirPath = caseParentDir + caseName;
206 
207  // check if case Name contain one of this following symbol:
208  // \ / : * ? " < > |
209  if (!Case.isValidName(caseName)) {
210  String errorMsg = NbBundle
211  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.invalidSymbols");
212  validationError(errorMsg);
213  } else {
214  // check if the directory exist
215  if (new File(caseDirPath).exists()) {
216  // throw a warning to enter new data or delete the existing directory
217  String errorMsg = NbBundle
218  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.dirExists", caseDirPath);
219  validationError(errorMsg);
220  } else {
221  // check if the "base" directory path is absolute
222  File baseDir = new File(caseParentDir);
223  if (baseDir.isAbsolute()) {
224  // when the base directory doesn't exist
225  if (!baseDir.exists()) {
226  // get confirmation to create directory
227  String confMsg = NbBundle
228  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.confMsg.createDir.msg",
229  caseParentDir);
230  NotifyDescriptor d2 = new NotifyDescriptor.Confirmation(confMsg,
231  NbBundle.getMessage(this.getClass(),
232  "NewCaseWizardPanel1.validate.confMsg.createDir.title"),
233  NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE);
234  d2.setValue(NotifyDescriptor.NO_OPTION);
235 
236  Object res2 = DialogDisplayer.getDefault().notify(d2);
237  if (res2 != null && res2 == DialogDescriptor.YES_OPTION) {
238  // if user say yes
239  try {
240  createDirectory(caseDirPath);
241  } catch (Exception ex) {
242  String errorMsg = NbBundle.getMessage(this.getClass(),
243  "NewCaseWizardPanel1.validate.errMsg.cantCreateParDir.msg",
244  caseParentDir);
245  logger.log(Level.WARNING, errorMsg, ex);
246  validationError(errorMsg);
247  }
248  }
249  if (res2 != null && res2 == DialogDescriptor.NO_OPTION) {
250  // if user say no
251  validationError(NbBundle.getMessage(this.getClass(),
252  "NewCaseWizardPanel1.validate.errMsg.prevCreateBaseDir.msg",
253  caseDirPath) );
254  }
255  } else {
256  try {
257  createDirectory(caseDirPath);
258  } catch (Exception ex) {
259  String errorMsg = NbBundle
260  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.cantCreateDir");
261  logger.log(Level.WARNING, errorMsg, ex);
262  validationError(errorMsg);
263  }
264  }
265  } else {
266  // throw a notification
267  String errorMsg = NbBundle
268  .getMessage(this.getClass(), "NewCaseWizardPanel1.validate.errMsg.invalidBaseDir.msg");
269  validationError(errorMsg);
270  }
271  }
272  }
273  }
274 
275  private void validationError(String errorMsg) throws WizardValidationException {
276  throw new WizardValidationException(this.getComponent(), errorMsg, null);
277  }
278 
279  /*
280  * create the directory and create a new case
281  */
282  private void createDirectory(final String caseDirPath) throws WizardValidationException {
283  // try to create the directory with the case name in the choosen parent directory
284  boolean success = false;
285  try {
286  Case.createCaseDirectory(caseDirPath);
287  success = true;
288  } catch (CaseActionException ex) {
289  logger.log(Level.SEVERE, "Could not createDirectory for the case, ", ex); //NON-NLS
290  }
291 
292  // check if the directory is successfully created
293  if (!success) {
294 
295  // delete the folder if we already created the folder and the error shows up
296  if (new File(caseDirPath).exists()) {
297  Case.deleteCaseDirectory(new File(caseDirPath));
298  }
299 
300  String errorMsg = NbBundle.getMessage(this.getClass(),
301  "NewCaseWizardPanel1.createDir.errMsg.cantCreateDir.msg");
302 
303  validationError(errorMsg);
304 
305  } // the new case directory is successfully created
306  else {
307  createdDirectory = caseDirPath;
308  // try to close Startup window if there's one
309  try {
310  StartupWindowProvider.getInstance().close();
311  } catch (Exception ex) {
312  logger.log(Level.WARNING, "Startup window didn't close as expected.", ex); //NON-NLS
313 
314  }
315 
316  }
317  }
318 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.