added project path validation to NewProjectDialog (PY-1749)
authorDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Wed, 22 Sep 2010 17:53:40 +0000 (21:53 +0400)
committerDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Wed, 22 Sep 2010 17:59:46 +0000 (21:59 +0400)
platform/platform-impl/src/com/intellij/platform/DirectoryProjectGenerator.java
platform/platform-impl/src/com/intellij/platform/NewDirectoryProjectDialog.java

index 788f24baed3f5c604d406e801569ed412da5a9c4..4202e17b46f6dbc97c1ef83c6ec937249877aa2a 100644 (file)
  */
 package com.intellij.platform;
 
+import com.intellij.facet.ui.ValidationResult;
 import com.intellij.openapi.extensions.ExtensionPointName;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.progress.ProcessCanceledException;
 import org.jetbrains.annotations.Nls;
+import org.jetbrains.annotations.NotNull;
 
 /**
  * @author yole
@@ -33,4 +35,7 @@ public interface DirectoryProjectGenerator<T> {
   T showGenerationSettings(final VirtualFile baseDir) throws ProcessCanceledException;
 
   void generateProject(final Project project, final VirtualFile baseDir, final T settings);
+
+  @NotNull
+  ValidationResult validate(@NotNull String baseDirPath);
 }
index dc72bceffbe575da9cfc4ef9f2ce9846f956db76..edfeec61bfba9c193e63841901085c9ed340ccb9 100644 (file)
@@ -15,6 +15,9 @@
  */
 package com.intellij.platform;
 
+import com.intellij.facet.ui.FacetEditorValidator;
+import com.intellij.facet.ui.FacetValidatorsManager;
+import com.intellij.facet.ui.ValidationResult;
 import com.intellij.ide.GeneralSettings;
 import com.intellij.openapi.application.ApplicationNamesInfo;
 import com.intellij.openapi.extensions.Extensions;
@@ -34,6 +37,8 @@ import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
 import javax.swing.event.DocumentEvent;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.io.File;
 
 /**
@@ -61,16 +66,17 @@ public class NewDirectoryProjectDialog extends DialogWrapper {
 
     FileChooserDescriptor descriptor = new FileChooserDescriptor(false, true, false, false, false, false);
     ComponentWithBrowseButton.BrowseFolderActionListener<JTextField> listener =
-        new ComponentWithBrowseButton.BrowseFolderActionListener<JTextField>("Select Location for Project Directory", "", myLocationField, project,
-                                                                             descriptor,
-                                                                             TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT) {
-
-          protected void onFileChoosen(VirtualFile chosenFile) {
-            super.onFileChoosen(chosenFile);
-            myBaseDir = chosenFile.getPath();
-            myLocationField.setText(new File(chosenFile.getPath(), myProjectNameTextField.getText()).toString());
-          }
-        };
+      new ComponentWithBrowseButton.BrowseFolderActionListener<JTextField>("Select Location for Project Directory", "", myLocationField,
+                                                                           project,
+                                                                           descriptor,
+                                                                           TextComponentAccessor.TEXT_FIELD_WHOLE_TEXT) {
+
+        protected void onFileChoosen(VirtualFile chosenFile) {
+          super.onFileChoosen(chosenFile);
+          myBaseDir = chosenFile.getPath();
+          myLocationField.setText(new File(chosenFile.getPath(), myProjectNameTextField.getText()).toString());
+        }
+      };
     myLocationField.addActionListener(listener);
 
     myProjectNameTextField.getDocument().addDocumentListener(new DocumentAdapter() {
@@ -78,6 +84,13 @@ public class NewDirectoryProjectDialog extends DialogWrapper {
         myLocationField.setText(new File(myBaseDir, myProjectNameTextField.getText()).getPath());
       }
     });
+
+    myProjectNameTextField.getDocument().addDocumentListener(new DocumentAdapter() {
+      @Override
+      protected void textChanged(DocumentEvent e) {
+
+      }
+    });
     myProjectNameTextField.selectAll();
 
     final DirectoryProjectGenerator[] generators = Extensions.getExtensions(DirectoryProjectGenerator.EP_NAME);
@@ -102,12 +115,65 @@ public class NewDirectoryProjectDialog extends DialogWrapper {
             append("Empty project", SimpleTextAttributes.REGULAR_ATTRIBUTES);
           }
           else {
-            DirectoryProjectGenerator generator = (DirectoryProjectGenerator) value;
+            DirectoryProjectGenerator generator = (DirectoryProjectGenerator)value;
             append(generator.getName(), SimpleTextAttributes.REGULAR_ATTRIBUTES);
           }
         }
       });
     }
+
+    registerValidators(new FacetValidatorsManager() {
+      public void registerValidator(FacetEditorValidator validator, JComponent... componentsToWatch) {
+      }
+
+      public void validate() {
+        doValidate();
+      }
+    });
+  }
+
+  private void doValidate() {
+    String projectName = myProjectNameTextField.getText();
+    if (projectName.trim().isEmpty()) {
+      setOKActionEnabled(false);
+      setErrorText("Project name can't be empty");
+      return;
+    }
+    DirectoryProjectGenerator generator = getProjectGenerator();
+    if (generator != null) {
+      String baseDirPath = myLocationField.getTextField().getText();
+      ValidationResult validationResult = generator.validate(baseDirPath);
+      if (!validationResult.isOk()) {
+        setOKActionEnabled(false);
+        setErrorText(validationResult.getErrorMessage());
+        return;
+      }
+    }
+    setOKActionEnabled(true);
+    setErrorText(null);
+  }
+
+  private void registerValidators(final FacetValidatorsManager validatorsManager) {
+    validateOnTextChange(validatorsManager, myLocationField.getTextField());
+    validateOnSelectionChange(validatorsManager, myProjectTypeComboBox);
+  }
+
+  private static void validateOnSelectionChange(final FacetValidatorsManager validatorsManager, final JComboBox projectNameTextField) {
+    projectNameTextField.addActionListener(new ActionListener() {
+      @Override
+      public void actionPerformed(ActionEvent e) {
+        validatorsManager.validate();
+      }
+    });
+  }
+
+  private static void validateOnTextChange(final FacetValidatorsManager validatorsManager, final JTextField textField) {
+    textField.getDocument().addDocumentListener(new DocumentAdapter() {
+      @Override
+      protected void textChanged(DocumentEvent e) {
+        validatorsManager.validate();
+      }
+    });
   }
 
   public static String getBaseDir() {