19 package org.sleuthkit.autopsy.casemodule;
 
   22 import java.util.HashSet;
 
   23 import java.util.Iterator;
 
   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;
 
   43 class NewCaseWizardPanel1 
implements WizardDescriptor.ValidatingPanel<WizardDescriptor> {
 
   45     private static final Logger logger = Logger.getLogger(NewCaseWizardPanel1.class.getName());
 
   46     private static final String PROP_BASECASE = 
"LBL_BaseCase_PATH"; 
 
   47     private static String createdDirectory;
 
   48     private final Set<ChangeListener> listeners = 
new HashSet<>(1);
 
   49     private NewCaseVisualPanel1 component;
 
   50     private boolean isFinish;
 
   58     public NewCaseVisualPanel1 getComponent() {
 
   59         if (component == null) {
 
   60             component = 
new NewCaseVisualPanel1(
this);
 
   72     public HelpCtx getHelp() {
 
   76         return HelpCtx.DEFAULT_HELP;
 
   87     public boolean isValid() {
 
   97     public final void addChangeListener(ChangeListener listener) {
 
   98         synchronized (listeners) {
 
   99             listeners.add(listener);
 
  109     public final void removeChangeListener(ChangeListener listener) {
 
  110         synchronized (listeners) {
 
  111             listeners.remove(listener);
 
  118     protected final void fireChangeEvent() {
 
  119         Iterator<ChangeListener> it;
 
  120         synchronized (listeners) {
 
  121             it = 
new HashSet<>(listeners).iterator();
 
  123         ChangeEvent ev = 
new ChangeEvent(
this);
 
  124         while (it.hasNext()) {
 
  125             it.next().stateChanged(ev);
 
  135     public void setIsFinish(Boolean isFinish) {
 
  136         this.isFinish = isFinish;
 
  149     public void readSettings(WizardDescriptor settings) {
 
  150         NewCaseVisualPanel1 panel = getComponent();
 
  152             String lastBaseDirectory = ModuleSettings.getConfigSetting(ModuleSettings.MAIN_SETTINGS, PROP_BASECASE);
 
  153             panel.setCaseParentDir(lastBaseDirectory);
 
  154             panel.readSettings();
 
  155             createdDirectory = (String) settings.getProperty(
"createdDirectory"); 
 
  156             if (createdDirectory != null && !createdDirectory.isEmpty()) {
 
  157                 logger.log(Level.INFO, 
"Deleting a case dir in readSettings(): {0}", createdDirectory); 
 
  158                 FileUtil.deleteDir(
new File(createdDirectory));
 
  160         } 
catch (Exception e) {
 
  161             logger.log(Level.WARNING, 
"Could not read wizard settings in NewCaseWizardPanel1, ", e); 
 
  175     public void storeSettings(WizardDescriptor settings) {
 
  176         CaseType caseType = getComponent().getCaseType();
 
  177         settings.putProperty(
"caseName", getComponent().getCaseName()); 
 
  178         settings.putProperty(
"caseParentDir", getComponent().getCaseParentDir()); 
 
  179         settings.putProperty(
"createdDirectory", createdDirectory); 
 
  180         settings.putProperty(
"caseType", caseType.ordinal()); 
 
  181         ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, ModuleSettings.CURRENT_CASE_TYPE, caseType.toString());
 
  182         ModuleSettings.setConfigSetting(ModuleSettings.MAIN_SETTINGS, PROP_BASECASE, getComponent().getCaseParentDir());
 
  186     public void validate() throws WizardValidationException {
 
  192         String caseName = getComponent().getCaseName();
 
  193         if (!Case.isValidName(caseName)) {
 
  194             String errorMsg = NbBundle
 
  195                     .getMessage(this.getClass(), 
"NewCaseWizardPanel1.validate.errMsg.invalidSymbols");
 
  196             validationError(errorMsg);
 
  199             String caseParentDir = getComponent().getCaseParentDir();
 
  200             String caseDirPath = caseParentDir + caseName;
 
  203             if (
new File(caseDirPath).exists()) {
 
  205                 String errorMsg = NbBundle
 
  206                         .getMessage(this.getClass(), 
"NewCaseWizardPanel1.validate.errMsg.dirExists", caseDirPath);
 
  207                 validationError(errorMsg);
 
  210                 File baseDir = 
new File(caseParentDir);
 
  211                 if (baseDir.isAbsolute()) {
 
  213                     if (!baseDir.exists()) {
 
  215                         String confMsg = NbBundle
 
  216                                 .getMessage(this.getClass(), 
"NewCaseWizardPanel1.validate.confMsg.createDir.msg",
 
  218                         NotifyDescriptor d2 = 
new NotifyDescriptor.Confirmation(confMsg,
 
  219                                 NbBundle.getMessage(
this.getClass(),
 
  220                                         "NewCaseWizardPanel1.validate.confMsg.createDir.title"),
 
  221                                 NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE);
 
  222                         d2.setValue(NotifyDescriptor.NO_OPTION);
 
  224                         Object res2 = DialogDisplayer.getDefault().notify(d2);
 
  225                         if (res2 != null && res2 == DialogDescriptor.YES_OPTION) {
 
  228                                 createDirectory(caseDirPath, getComponent().getCaseType());
 
  229                             } 
catch (WizardValidationException ex) {
 
  230                                 String errorMsg = NbBundle.getMessage(this.getClass(),
 
  231                                         "NewCaseWizardPanel1.validate.errMsg.cantCreateParDir.msg",
 
  233                                 logger.log(Level.WARNING, errorMsg, ex);
 
  234                                 validationError(errorMsg);
 
  237                         if (res2 != null && res2 == DialogDescriptor.NO_OPTION) {
 
  239                             validationError(NbBundle.getMessage(
this.getClass(),
 
  240                                     "NewCaseWizardPanel1.validate.errMsg.prevCreateBaseDir.msg",
 
  245                             createDirectory(caseDirPath, getComponent().getCaseType());
 
  246                         } 
catch (WizardValidationException ex) {
 
  247                             String errorMsg = NbBundle
 
  248                                     .getMessage(this.getClass(), 
"NewCaseWizardPanel1.validate.errMsg.cantCreateDir");
 
  249                             logger.log(Level.WARNING, errorMsg, ex);
 
  250                             validationError(errorMsg);
 
  255                     String errorMsg = NbBundle
 
  256                             .getMessage(this.getClass(), 
"NewCaseWizardPanel1.validate.errMsg.invalidBaseDir.msg");
 
  257                     validationError(errorMsg);
 
  263     private void validationError(String errorMsg) 
throws WizardValidationException {
 
  264         throw new WizardValidationException(this.getComponent(), errorMsg, null);
 
  270     private void createDirectory(
final String caseDirPath, CaseType caseType) 
throws WizardValidationException {
 
  272         boolean success = 
false;
 
  274             Case.createCaseDirectory(caseDirPath, caseType);
 
  276         } 
catch (CaseActionException ex) {
 
  277             logger.log(Level.SEVERE, 
"Could not createDirectory for the case, ", ex); 
 
  284             if (
new File(caseDirPath).exists()) {
 
  285                 FileUtil.deleteDir(
new File(caseDirPath));
 
  288             String errorMsg = NbBundle.getMessage(this.getClass(),
 
  289                     "NewCaseWizardPanel1.createDir.errMsg.cantCreateDir.msg");
 
  291             validationError(errorMsg);
 
  295             createdDirectory = caseDirPath;
 
  298                 StartupWindowProvider.getInstance().close();
 
  299             } 
catch (Exception ex) {
 
  300                 logger.log(Level.WARNING, 
"Startup window didn't close as expected.", ex);