IDEA-CR-10973 Fix excess calls to checkValid() in Python new project wizard
authorMikhail Golubev <mikhail.golubev@jetbrains.com>
Thu, 16 Jun 2016 09:49:24 +0000 (12:49 +0300)
committerMikhail Golubev <mikhail.golubev@jetbrains.com>
Thu, 23 Jun 2016 14:40:56 +0000 (17:40 +0300)
python/ide/src/com/jetbrains/python/PythonSdkChooserCombo.java
python/ide/src/com/jetbrains/python/newProject/actions/ProjectSpecificSettingsStep.java

index 7c9335ba62bd7db589265ff0d1c2deae39c61635..bd132745ff050b491d5f09604a65a9c2a0704bc7 100644 (file)
@@ -20,16 +20,15 @@ import com.intellij.openapi.options.ConfigurationException;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.projectRoots.Sdk;
 import com.intellij.openapi.roots.ui.configuration.projectRoot.ProjectSdksModel;
+import com.intellij.openapi.ui.ComboBox;
 import com.intellij.openapi.util.Condition;
 import com.intellij.ui.CollectionComboBoxModel;
 import com.intellij.ui.ComboboxWithBrowseButton;
-import com.intellij.util.NullableConsumer;
 import com.intellij.util.containers.ContainerUtil;
 import com.jetbrains.python.configuration.PyConfigurableInterpreterList;
 import com.jetbrains.python.sdk.PySdkListCellRenderer;
 import com.jetbrains.python.sdk.PySdkService;
 import com.jetbrains.python.sdk.PythonSdkDetailsStep;
-import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
 import java.awt.event.ActionEvent;
@@ -41,17 +40,12 @@ import java.util.List;
  */
 public class PythonSdkChooserCombo extends ComboboxWithBrowseButton {
   private final List<ActionListener> myChangedListeners = ContainerUtil.createLockFreeCopyOnWriteList();
-  private static final Logger LOG = Logger.getInstance("#com.jetbrains.python.PythonSdkChooserCombo");
+  private static final Logger LOG = Logger.getInstance(PythonSdkChooserCombo.class);
 
   @SuppressWarnings("unchecked")
   public PythonSdkChooserCombo(final Project project, List<Sdk> sdks, final Condition<Sdk> acceptableSdkCondition) {
-    Sdk initialSelection = null;
-    for (Sdk sdk : sdks) {
-      if (acceptableSdkCondition.value(sdk)) {
-        initialSelection = sdk;
-        break;
-      }
-    }
+    super(new ComboBox<>());
+    final Sdk initialSelection = ContainerUtil.find(sdks, acceptableSdkCondition);
     final JComboBox comboBox = getComboBox();
     comboBox.setModel(new CollectionComboBoxModel(sdks, initialSelection));
     comboBox.setRenderer(new PySdkListCellRenderer(true));
index 93ee06c1a79646fa12de85724728bd636c284c6b..1bdc5359402e3261c087e251d1c9a0c8cb36ac0f 100644 (file)
@@ -28,7 +28,9 @@ import com.intellij.platform.DirectoryProjectGenerator;
 import com.intellij.ui.DocumentAdapter;
 import com.intellij.ui.HideableDecorator;
 import com.intellij.util.NullableConsumer;
+import com.intellij.util.PathUtil;
 import com.intellij.util.ui.UIUtil;
+import com.intellij.util.ui.update.UiNotifyConnector;
 import com.jetbrains.python.PythonSdkChooserCombo;
 import com.jetbrains.python.configuration.PyConfigurableInterpreterList;
 import com.jetbrains.python.configuration.VirtualEnvProjectFilter;
@@ -46,9 +48,7 @@ import javax.swing.event.DocumentEvent;
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
-import java.beans.PropertyChangeEvent;
-import java.beans.PropertyChangeListener;
-import java.io.File;
+import java.awt.event.ItemEvent;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -117,20 +117,28 @@ public class ProjectSpecificSettingsStep extends ProjectSettingsStepBase impleme
   protected void registerValidators() {
     super.registerValidators();
     if (myProjectGenerator instanceof PythonProjectGenerator && !((PythonProjectGenerator)myProjectGenerator).hideInterpreter()) {
-      mySdkCombo.getComboBox().addPropertyChangeListener(new PropertyChangeListener() {
+      myLocationField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
         @Override
-        public void propertyChange(PropertyChangeEvent event) {
-          checkValid();
+        protected void textChanged(DocumentEvent e) {
+          final String path = myLocationField.getText().trim();
+          ((PythonProjectGenerator)myProjectGenerator).locationChanged(PathUtil.getFileName(path));
         }
       });
-      final ActionListener listener = new ActionListener() {
-        @Override
-        public void actionPerformed(ActionEvent event) {
+      
+      mySdkCombo.getComboBox().addItemListener(e -> {
+        if (e.getStateChange() == ItemEvent.SELECTED) {
           checkValid();
         }
-      };
-      mySdkCombo.getComboBox().addActionListener(listener);
-      mySdkCombo.addActionListener(listener);
+      });
+      UiNotifyConnector.doWhenFirstShown(mySdkCombo, this::checkValid);
+    }
+  }
+  
+  @Override
+  protected void initGeneratorListeners() {
+    super.initGeneratorListeners();
+    if (myProjectGenerator instanceof PythonProjectGenerator) {
+      ((PythonProjectGenerator)myProjectGenerator).addSettingsStateListener(this::checkValid);
     }
   }
 
@@ -277,33 +285,4 @@ public class ProjectSpecificSettingsStep extends ProjectSettingsStepBase impleme
 
     return LabeledComponent.create(mySdkCombo, "Interpreter", BorderLayout.WEST);
   }
-
-  @Override
-  protected void initGeneratorListeners() {
-    super.initGeneratorListeners();
-    if (myProjectGenerator instanceof PythonProjectGenerator) {
-      ((PythonProjectGenerator)myProjectGenerator).addSettingsStateListener(new PythonProjectGenerator.SettingsListener() {
-        @Override
-        public void stateChanged() {
-          checkValid();
-        }
-      });
-      
-      myErrorLabel.addMouseListener(((PythonProjectGenerator)myProjectGenerator).getErrorLabelMouseListener());
-    }
-    myLocationField.getTextField().getDocument().addDocumentListener(new DocumentAdapter() {
-      @Override
-      protected void textChanged(DocumentEvent e) {
-        if (myProjectGenerator instanceof PythonProjectGenerator) {
-          String path = myLocationField.getText().trim();
-          path = StringUtil.trimEnd(path, File.separator);
-          int ind = path.lastIndexOf(File.separator);
-          if (ind != -1) {
-            String projectName = path.substring(ind + 1, path.length());
-            ((PythonProjectGenerator)myProjectGenerator).locationChanged(projectName);
-          }
-        }
-      }
-    });
-  }
 }