Fix: dispose errors appcode/142.4546
authorNikolay Mikhaylov <nikolay.mikhaylov@gmail.com>
Thu, 3 Sep 2015 11:40:28 +0000 (14:40 +0300)
committerNikolay Mikhaylov <nikolay.mikhaylov@gmail.com>
Thu, 3 Sep 2015 11:58:48 +0000 (14:58 +0300)
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryAddLibraryAction.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryLibraryDescription.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryLibraryProperties.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryLibrarySupport.java [new file with mode: 0644]
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryLibrarySupportInModuleConfigurable.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryLibraryWithDescriptionEditor.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryUtils.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/propertiesEditor/RepositoryLibraryPropertiesDialog.java [new file with mode: 0644]
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/propertiesEditor/RepositoryLibraryPropertiesEditor.form [moved from plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryLibraryPropertiesEditor.form with 98% similarity]
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/propertiesEditor/RepositoryLibraryPropertiesEditor.java [moved from plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryLibraryPropertiesEditor.java with 73% similarity]
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/propertiesEditor/RepositoryLibraryPropertiesModel.java [new file with mode: 0644]

index 69de53ea1f376cf05ee653a14de22c10bcccc17a..1da01a531c6f7e2458c5d19c52c924b023f617ef 100644 (file)
@@ -11,6 +11,8 @@ import com.intellij.psi.PsiFile;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.idea.maven.project.ProjectBundle;
+import org.jetbrains.idea.maven.utils.library.propertiesEditor.RepositoryLibraryPropertiesDialog;
+import org.jetbrains.idea.maven.utils.library.propertiesEditor.RepositoryLibraryPropertiesModel;
 
 public class RepositoryAddLibraryAction extends IntentionAndQuickFixAction {
   private final Module module;
@@ -35,13 +37,23 @@ public class RepositoryAddLibraryAction extends IntentionAndQuickFixAction {
 
   @Override
   public void applyFix(@NotNull Project project, PsiFile file, @Nullable Editor editor) {
-    RepositoryLibrarySupportInModuleConfigurable
-      mavenSupport = new RepositoryLibrarySupportInModuleConfigurable(module.getProject(), libraryDescription);
-    if (mavenSupport.showEditorAndGet()) {
-      IdeaModifiableModelsProvider modifiableModelsProvider = new IdeaModifiableModelsProvider();
-      final ModifiableRootModel modifiableModel = modifiableModelsProvider.getModuleModifiableModel(module);
-      assert modifiableModel != null;
-      mavenSupport.addSupport(
+    RepositoryLibraryPropertiesModel model = new RepositoryLibraryPropertiesModel(
+      RepositoryUtils.DefaultVersionId,
+      false,
+      false);
+    RepositoryLibraryPropertiesDialog dialog = new RepositoryLibraryPropertiesDialog(
+      project,
+      model,
+      libraryDescription,
+      false);
+    if (!dialog.showAndGet()) {
+      return;
+    }
+    IdeaModifiableModelsProvider modifiableModelsProvider = new IdeaModifiableModelsProvider();
+    final ModifiableRootModel modifiableModel = modifiableModelsProvider.getModuleModifiableModel(module);
+    RepositoryLibrarySupport librarySupport = new RepositoryLibrarySupport(project, libraryDescription, model);
+    assert modifiableModel != null;
+    librarySupport.addSupport(
         module,
         modifiableModel,
         modifiableModelsProvider);
@@ -51,6 +63,5 @@ public class RepositoryAddLibraryAction extends IntentionAndQuickFixAction {
           modifiableModel.commit();
         }
       });
-    }
   }
 }
index 68d66a84eaac76144c33f2a3f4324ebbe9263f5a..764a9f9c91d47342568b331b1537d5c4ef5bb546 100644 (file)
@@ -99,7 +99,7 @@ public abstract class RepositoryLibraryDescription {
   }
 
   @NotNull
-  List<MavenRepositoryInfo> getRemoteRepositories() {
+  public List<MavenRepositoryInfo> getRemoteRepositories() {
     return defaultRemoteRepositories;
   }
 
index 705601cf8b673ea6115704fb737eb3eae4cf128f..4aa1d4d5d026edc0e7036503878c5ced87a639ca 100644 (file)
@@ -86,7 +86,7 @@ public class RepositoryLibraryProperties extends LibraryProperties<RepositoryLib
     }
   }
 
-  String getGroupId() {
+  public String getGroupId() {
     return groupId;
   }
 
diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryLibrarySupport.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/RepositoryLibrarySupport.java
new file mode 100644 (file)
index 0000000..5e29eed
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2000-2015 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.idea.maven.utils.library;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.module.Module;
+import com.intellij.openapi.progress.ProgressIndicator;
+import com.intellij.openapi.progress.ProgressManager;
+import com.intellij.openapi.progress.Task;
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.roots.*;
+import com.intellij.openapi.roots.impl.libraries.LibraryEx;
+import com.intellij.openapi.roots.libraries.Library;
+import com.intellij.openapi.roots.libraries.LibraryProperties;
+import com.intellij.openapi.roots.libraries.LibraryTable;
+import com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil;
+import com.intellij.openapi.util.Comparing;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.idea.maven.utils.library.propertiesEditor.RepositoryLibraryPropertiesModel;
+
+import java.util.Arrays;
+
+public class RepositoryLibrarySupport {
+  @NotNull private Project project;
+  @NotNull private RepositoryLibraryPropertiesModel model;
+  @NotNull private RepositoryLibraryDescription libraryDescription;
+
+  public RepositoryLibrarySupport(@NotNull Project project,
+                                  @NotNull RepositoryLibraryDescription libraryDescription,
+                                  @NotNull RepositoryLibraryPropertiesModel model) {
+    this.project = project;
+    this.libraryDescription = libraryDescription;
+    this.model = model;
+  }
+
+  public void addSupport(@NotNull Module module,
+                         @NotNull final ModifiableRootModel rootModel,
+                         @NotNull ModifiableModelsProvider modifiableModelsProvider) {
+    LibraryTable.ModifiableModel modifiableModel = modifiableModelsProvider.getLibraryTableModifiableModel(module.getProject());
+
+    Library library = Iterables.find(Arrays.asList(modifiableModel.getLibraries()), new Predicate<Library>() {
+      @Override
+      public boolean apply(@Nullable Library library) {
+        return isLibraryEqualsToSelected(library);
+      }
+    }, null);
+    if (library == null) {
+      library = createNewLibrary(module, modifiableModel);
+    }
+    final DependencyScope dependencyScope = LibraryDependencyScopeSuggester.getDefaultScope(library);
+    final ModifiableRootModel moduleModifiableModel = modifiableModelsProvider.getModuleModifiableModel(module);
+    try {
+      LibraryOrderEntry foundEntry =
+        (LibraryOrderEntry)Iterables.find(Arrays.asList(moduleModifiableModel.getOrderEntries()), new Predicate<OrderEntry>() {
+          @Override
+          public boolean apply(@Nullable OrderEntry entry) {
+            return entry instanceof LibraryOrderEntry
+                   && ((LibraryOrderEntry)entry).getScope() == dependencyScope
+                   && isLibraryEqualsToSelected(((LibraryOrderEntry)entry).getLibrary());
+          }
+        }, null);
+      if (foundEntry == null) {
+        rootModel.addLibraryEntry(library).setScope(dependencyScope);
+      }
+    }
+    finally {
+      moduleModifiableModel.dispose();
+    }
+  }
+
+  private LibraryEx createNewLibrary(@NotNull final Module module, final LibraryTable.ModifiableModel modifiableModel) {
+    RepositoryLibraryProperties libraryProperties = new RepositoryLibraryProperties(
+      libraryDescription.getGroupId(),
+      libraryDescription.getArtifactId(),
+      model.getVersion());
+    final LibraryEx library = (LibraryEx)modifiableModel.createLibrary(
+      LibraryEditingUtil.suggestNewLibraryName(modifiableModel, RepositoryLibraryType.getInstance().getDescription(libraryProperties)),
+      RepositoryLibraryType.REPOSITORY_LIBRARY_KIND);
+    RepositoryLibraryProperties realLibraryProperties = (RepositoryLibraryProperties)library.getProperties();
+    realLibraryProperties.setMavenId(libraryProperties.getMavenId());
+
+    ApplicationManager.getApplication().runWriteAction(new Runnable() {
+      @Override
+      public void run() {
+        modifiableModel.commit();
+      }
+    });
+    Task task = new Task.Backgroundable(project, "Maven", false) {
+      public void run(@NotNull ProgressIndicator indicator) {
+        RepositoryUtils.loadDependencies(
+          indicator,
+          module.getProject(),
+          library,
+          model.isDownloadSources(),
+          model.isDownloadJavaDocs());
+      }
+    };
+    ProgressManager.getInstance().run(task);
+
+    return library;
+  }
+
+  private boolean isLibraryEqualsToSelected(Library library) {
+    if (!(library instanceof LibraryEx)) {
+      return false;
+    }
+
+    LibraryEx libraryEx = (LibraryEx)library;
+    if (!RepositoryLibraryType.REPOSITORY_LIBRARY_KIND.equals(libraryEx.getKind())) {
+      return false;
+    }
+
+    LibraryProperties libraryProperties = libraryEx.getProperties();
+    if (libraryProperties == null || !(libraryProperties instanceof RepositoryLibraryProperties)) {
+      return false;
+    }
+    RepositoryLibraryProperties repositoryLibraryProperties = (RepositoryLibraryProperties)libraryProperties;
+    RepositoryLibraryDescription description = RepositoryLibraryDescription.findDescription(repositoryLibraryProperties);
+
+    if (!description.equals(libraryDescription)) {
+      return false;
+    }
+
+    return Comparing.equal(repositoryLibraryProperties.getVersion(), model.getVersion());
+  }
+}
index c8db44a8f26443d2ac35260904dea82d41af456b..80bfec6ab70277d340a1ea8eb71a3dca59bb4b70 100644 (file)
@@ -1,37 +1,30 @@
 package org.jetbrains.idea.maven.utils.library;
 
-import com.google.common.base.Predicate;
-import com.google.common.collect.Iterables;
 import com.intellij.framework.addSupport.FrameworkSupportInModuleConfigurable;
-import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.module.Module;
-import com.intellij.openapi.progress.ProgressIndicator;
-import com.intellij.openapi.progress.ProgressManager;
-import com.intellij.openapi.progress.Task;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.roots.*;
-import com.intellij.openapi.roots.impl.libraries.LibraryEx;
-import com.intellij.openapi.roots.libraries.Library;
-import com.intellij.openapi.roots.libraries.LibraryProperties;
-import com.intellij.openapi.roots.libraries.LibraryTable;
-import com.intellij.openapi.roots.ui.configuration.libraries.LibraryEditingUtil;
-import com.intellij.openapi.util.Comparing;
+import com.intellij.openapi.project.ProjectManager;
+import com.intellij.openapi.roots.ModifiableModelsProvider;
+import com.intellij.openapi.roots.ModifiableRootModel;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
-import org.jetbrains.idea.maven.project.ProjectBundle;
+import org.jetbrains.idea.maven.utils.library.propertiesEditor.RepositoryLibraryPropertiesEditor;
+import org.jetbrains.idea.maven.utils.library.propertiesEditor.RepositoryLibraryPropertiesModel;
 
 import javax.swing.*;
-import java.util.Arrays;
 
 public class RepositoryLibrarySupportInModuleConfigurable extends FrameworkSupportInModuleConfigurable {
   @NotNull private final RepositoryLibraryDescription libraryDescription;
   @Nullable private final Project project;
   private RepositoryLibraryPropertiesEditor editor;
+  private RepositoryLibraryPropertiesModel model;
 
   public RepositoryLibrarySupportInModuleConfigurable(@Nullable Project project, @NotNull RepositoryLibraryDescription libraryDescription) {
     this.libraryDescription = libraryDescription;
     this.project = project;
-    editor = new RepositoryLibraryPropertiesEditor(project, false, false, libraryDescription.createDefaultProperties());
+    RepositoryLibraryProperties defaultProperties = libraryDescription.createDefaultProperties();
+    this.model = new RepositoryLibraryPropertiesModel(defaultProperties.getVersion(), false, false);
+    editor = new RepositoryLibraryPropertiesEditor(project, model, libraryDescription);
   }
 
   @Nullable
@@ -40,91 +33,14 @@ public class RepositoryLibrarySupportInModuleConfigurable extends FrameworkSuppo
     return editor.getMainPanel();
   }
 
-  public boolean showEditorAndGet() {
-    editor.setTitle(ProjectBundle.message("maven.add.library.support", libraryDescription.getDisplayName()));
-    return editor.showAndGet();
-  }
-
   @Override
   public void addSupport(@NotNull Module module,
-                         @NotNull final ModifiableRootModel rootModel,
+                         @NotNull ModifiableRootModel rootModel,
                          @NotNull ModifiableModelsProvider modifiableModelsProvider) {
-    LibraryTable.ModifiableModel modifiableModel = modifiableModelsProvider.getLibraryTableModifiableModel(module.getProject());
-
-    Library library = Iterables.find(Arrays.asList(modifiableModel.getLibraries()), new Predicate<Library>() {
-      @Override
-      public boolean apply(@Nullable Library library) {
-        return isLibraryEqualsToSelected(library);
-      }
-    }, null);
-    if (library == null) {
-      library = createNewLibrary(module, modifiableModel);
-    }
-    final DependencyScope dependencyScope = LibraryDependencyScopeSuggester.getDefaultScope(library);
-    final ModifiableRootModel moduleModifiableModel = modifiableModelsProvider.getModuleModifiableModel(module);
-    LibraryOrderEntry foundEntry =
-      (LibraryOrderEntry)Iterables.find(Arrays.asList(moduleModifiableModel.getOrderEntries()), new Predicate<OrderEntry>() {
-        @Override
-        public boolean apply(@Nullable OrderEntry entry) {
-          return entry instanceof LibraryOrderEntry
-                 && ((LibraryOrderEntry)entry).getScope() == dependencyScope
-                 && isLibraryEqualsToSelected(((LibraryOrderEntry)entry).getLibrary());
-        }
-      }, null);
-    if (foundEntry == null) {
-      rootModel.addLibraryEntry(library).setScope(dependencyScope);
-    }
-  }
-
-  private LibraryEx createNewLibrary(@NotNull final Module module, final LibraryTable.ModifiableModel modifiableModel) {
-    final LibraryEx library = (LibraryEx)modifiableModel.createLibrary(
-      LibraryEditingUtil.suggestNewLibraryName(modifiableModel, RepositoryLibraryType.getInstance().getDescription(editor.getProperties())),
-      RepositoryLibraryType.REPOSITORY_LIBRARY_KIND);
-    RepositoryLibraryProperties libraryProperties = (RepositoryLibraryProperties)library.getProperties();
-    libraryProperties.setMavenId(editor.getProperties().getMavenId());
-
-    ApplicationManager.getApplication().runWriteAction(new Runnable() {
-      @Override
-      public void run() {
-        modifiableModel.commit();
-      }
-    });
-    Task task = new Task.Backgroundable(project, "Maven", false) {
-      public void run(@NotNull ProgressIndicator indicator) {
-        RepositoryUtils.loadDependencies(
-          indicator,
-          module.getProject(),
-          library,
-          editor.downloadSources(),
-          editor.downloadJavaDocs());
-      }
-    };
-    ProgressManager.getInstance().run(task);
-
-    return library;
-  }
-
-  private boolean isLibraryEqualsToSelected(Library library) {
-    if (!(library instanceof LibraryEx)) {
-      return false;
-    }
-
-    LibraryEx libraryEx = (LibraryEx)library;
-    if (!RepositoryLibraryType.REPOSITORY_LIBRARY_KIND.equals(libraryEx.getKind())) {
-      return false;
-    }
-
-    LibraryProperties libraryProperties = libraryEx.getProperties();
-    if (libraryProperties == null || !(libraryProperties instanceof RepositoryLibraryProperties)) {
-      return false;
-    }
-    RepositoryLibraryProperties repositoryLibraryProperties = (RepositoryLibraryProperties)libraryProperties;
-    RepositoryLibraryDescription description = RepositoryLibraryDescription.findDescription(repositoryLibraryProperties);
-
-    if (!description.equals(libraryDescription)) {
-      return false;
-    }
-
-    return Comparing.equal(repositoryLibraryProperties.getVersion(), editor.getSelectedVersion());
+    RepositoryLibrarySupport librarySupport = new RepositoryLibrarySupport(
+      project == null ? ProjectManager.getInstance().getDefaultProject() : project,
+      libraryDescription,
+      model);
+    librarySupport.addSupport(module, rootModel, modifiableModelsProvider);
   }
 }
index ea31507cc5cf98c7ee59d269e93ca299cff684fd..49d276e13f912fb6e5aefedf1bd616c33cb89559 100644 (file)
@@ -3,6 +3,8 @@ package org.jetbrains.idea.maven.utils.library;
 import com.intellij.openapi.roots.libraries.ui.LibraryEditorComponent;
 import com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryPropertiesEditorBase;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.idea.maven.utils.library.propertiesEditor.RepositoryLibraryPropertiesDialog;
+import org.jetbrains.idea.maven.utils.library.propertiesEditor.RepositoryLibraryPropertiesModel;
 
 public class RepositoryLibraryWithDescriptionEditor
   extends LibraryPropertiesEditorBase<RepositoryLibraryProperties, RepositoryLibraryType> {
@@ -21,24 +23,27 @@ public class RepositoryLibraryWithDescriptionEditor
     //String oldVersion = properties.getVersion();
     boolean wasGeneratedName =
       RepositoryLibraryType.getInstance().getDescription(properties).equals(myEditorComponent.getLibraryEditor().getName());
-    RepositoryLibraryPropertiesEditor editor = new RepositoryLibraryPropertiesEditor(
-      myEditorComponent.getProject(),
+    RepositoryLibraryPropertiesModel model = new RepositoryLibraryPropertiesModel(
+      properties.getVersion(),
       RepositoryUtils.libraryHasSources(myEditorComponent.getLibraryEditor()),
-      RepositoryUtils.libraryHasJavaDocs(myEditorComponent.getLibraryEditor()),
-      properties);
-    editor.setTitle(RepositoryLibraryDescription.findDescription(properties).getDisplayName());
-    if (!editor.showAndGet()) {
+      RepositoryUtils.libraryHasJavaDocs(myEditorComponent.getLibraryEditor()));
+    RepositoryLibraryPropertiesDialog dialog = new RepositoryLibraryPropertiesDialog(
+      myEditorComponent.getProject(),
+      model,
+      RepositoryLibraryDescription.findDescription(properties),
+      true);
+    if (!dialog.showAndGet()) {
       return;
     }
-    myEditorComponent.getProperties().loadState(editor.getProperties());
+    myEditorComponent.getProperties().changeVersion(model.getVersion());
     if (wasGeneratedName) {
       myEditorComponent.renameLibrary(RepositoryLibraryType.getInstance().getDescription(properties));
     }
     myEditorComponent.getLibraryEditor().removeAllRoots();
     myEditorComponent.getLibraryEditor().addRoots(RepositoryUtils.download(
       myEditorComponent.getProject(),
-      editor.downloadSources(),
-      editor.downloadJavaDocs(),
+      model.isDownloadSources(),
+      model.isDownloadJavaDocs(),
       properties));
   }
 }
index 926928f45b1fb09ebe1941146dca07b8073a1d24..caedaa20425ce3a12e24eaef39634aa4c4d6927b 100644 (file)
@@ -52,6 +52,7 @@ public class RepositoryUtils {
   @NotNull static public final String LatestVersionId = "LATEST";
   @NotNull static public final String ReleaseVersionId = "RELEASE";
   @NotNull static public final String SnapshotVersionSuffix = "-SNAPSHOT";
+  @NotNull static public final String DefaultVersionId = ReleaseVersionId;
 
   public static void downloadAsync(final Project project,
                                    final boolean downloadSources,
diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/propertiesEditor/RepositoryLibraryPropertiesDialog.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/propertiesEditor/RepositoryLibraryPropertiesDialog.java
new file mode 100644 (file)
index 0000000..ce4ea48
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2015 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.idea.maven.utils.library.propertiesEditor;
+
+import com.intellij.openapi.project.Project;
+import com.intellij.openapi.ui.DialogWrapper;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.idea.maven.utils.library.RepositoryLibraryDescription;
+
+import javax.swing.*;
+
+public class RepositoryLibraryPropertiesDialog extends DialogWrapper {
+  private RepositoryLibraryPropertiesEditor propertiesEditor;
+  private RepositoryLibraryPropertiesModel model;
+
+  public RepositoryLibraryPropertiesDialog(@Nullable Project project,
+                                           RepositoryLibraryPropertiesModel model,
+                                           RepositoryLibraryDescription description,
+                                           final boolean changesRequired) {
+    super(project);
+    this.model = model;
+    propertiesEditor =
+      new RepositoryLibraryPropertiesEditor(project, model, description, new RepositoryLibraryPropertiesEditor.ModelChangeListener() {
+        @Override
+        public void onChange(RepositoryLibraryPropertiesEditor editor) {
+          setOKActionEnabled(editor.isValid() && (!changesRequired || editor.hasChanges()));
+        }
+      });
+    setTitle(description.getDisplayName());
+    init();
+  }
+
+  @Nullable
+  @Override
+  protected JComponent createCenterPanel() {
+    return propertiesEditor.getMainPanel();
+  }
+}
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.jetbrains.idea.maven.utils.library.RepositoryLibraryPropertiesEditor">
+<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.jetbrains.idea.maven.utils.library.propertiesEditor.RepositoryLibraryPropertiesEditor">
   <grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="4" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
     <margin top="0" left="0" bottom="0" right="0"/>
     <constraints>
@@ -61,7 +61,7 @@
             <properties>
               <inheritsPopupMenu value="true"/>
               <labelFor value="3957d"/>
-              <text value="Loading available versions..."/>
+              <text value="&amp;Loading available versions..."/>
             </properties>
           </component>
           <hspacer id="baffe">
@@ -1,4 +1,4 @@
-package org.jetbrains.idea.maven.utils.library;
+package org.jetbrains.idea.maven.utils.library.propertiesEditor;
 
 import com.google.common.base.Strings;
 import com.intellij.openapi.application.ApplicationManager;
@@ -9,15 +9,16 @@ import com.intellij.openapi.progress.Task;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.project.ProjectManager;
 import com.intellij.openapi.ui.ComboBox;
-import com.intellij.openapi.ui.DialogWrapper;
 import com.intellij.openapi.util.Condition;
-import com.intellij.openapi.util.Disposer;
 import com.intellij.ui.CollectionComboBoxModel;
 import com.intellij.ui.components.JBCheckBox;
 import com.intellij.util.containers.JBIterable;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.idea.maven.project.ProjectBundle;
+import org.jetbrains.idea.maven.utils.library.RepositoryAttachHandler;
+import org.jetbrains.idea.maven.utils.library.RepositoryLibraryDescription;
+import org.jetbrains.idea.maven.utils.library.RepositoryUtils;
 
 import javax.swing.*;
 import javax.swing.event.ChangeEvent;
@@ -29,17 +30,13 @@ import java.awt.event.ItemListener;
 import java.util.Arrays;
 import java.util.List;
 
-public class RepositoryLibraryPropertiesEditor extends DialogWrapper {
+public class RepositoryLibraryPropertiesEditor {
   @NotNull private final Project project;
   State currentState;
   List<String> versions;
   private VersionKind versionKind;
-  private RepositoryLibraryProperties initialProperties;
-  private RepositoryLibraryProperties properties;
-  private boolean initialDownloadSources;
-  private boolean downloadSources;
-  private boolean initialDownloadJavaDocs;
-  private boolean downloadJavaDocs;
+  private RepositoryLibraryPropertiesModel initialModel;
+  private RepositoryLibraryPropertiesModel model;
   private RepositoryLibraryDescription repositoryLibraryDescription;
   private ComboBox versionKindSelector;
   private ComboBox versionSelector;
@@ -51,27 +48,40 @@ public class RepositoryLibraryPropertiesEditor extends DialogWrapper {
   private JBCheckBox downloadSourcesCheckBox;
   private JBCheckBox downloadJavaDocsCheckBox;
 
+  @NotNull private ModelChangeListener onChangeListener;
+
+  public interface ModelChangeListener {
+    void onChange(RepositoryLibraryPropertiesEditor editor);
+  }
+
   public RepositoryLibraryPropertiesEditor(@Nullable Project project,
-                                           boolean downloadSources,
-                                           boolean downloadJavaDocs,
-                                           @NotNull RepositoryLibraryProperties properties) {
-    super(project);
-    this.downloadJavaDocs = this.initialDownloadJavaDocs = downloadJavaDocs;
-    this.downloadSources = this.initialDownloadSources = downloadSources;
+                                           RepositoryLibraryPropertiesModel model,
+                                           RepositoryLibraryDescription description) {
+    this(project, model, description, new ModelChangeListener() {
+      @Override
+      public void onChange(RepositoryLibraryPropertiesEditor editor) {
+
+      }
+    });
+  }
+
+
+  public RepositoryLibraryPropertiesEditor(@Nullable Project project,
+                                           RepositoryLibraryPropertiesModel model,
+                                           RepositoryLibraryDescription description,
+                                           @NotNull ModelChangeListener onChangeListener) {
+    this.initialModel = model.clone();
+    this.model = model;
     this.project = project == null ? ProjectManager.getInstance().getDefaultProject() : project;
-    this.initialProperties = properties;
-    this.properties = new RepositoryLibraryProperties();
-    this.properties.loadState(properties);
-    repositoryLibraryDescription = RepositoryLibraryDescription.findDescription(properties);
+    repositoryLibraryDescription = description;
     myReloadButton.addActionListener(new ActionListener() {
       @Override
       public void actionPerformed(ActionEvent e) {
         reloadVersionsAsync();
       }
     });
+    this.onChangeListener = onChangeListener;
     reloadVersionsAsync();
-
-    super.init();
   }
 
   private static VersionKind getVersionKind(String version) {
@@ -130,22 +140,15 @@ public class RepositoryLibraryPropertiesEditor extends DialogWrapper {
         }
       }
     });
-    setSelectedVersionKind(getVersionKind(properties.getVersion()));
-  }
-
-  private void checkOkButtonState() {
-    setOKActionEnabled(currentState == State.Loaded
-                       && (!properties.equals(initialProperties)
-                           || downloadSources != initialDownloadSources
-                           || downloadJavaDocs != initialDownloadJavaDocs));
+    setSelectedVersionKind(getVersionKind(model.getVersion()));
   }
 
   private void versionKindChanged() {
     versionSelector.setEnabled(versionKind == VersionKind.Select);
-    properties.changeVersion(getSelectedVersion());
-    int selection = getSelection(properties.getVersion(), versions);
+    model.setVersion(getSelectedVersion());
+    int selection = getSelection(model.getVersion(), versions);
     versionSelector.setSelectedIndex(selection);
-    checkOkButtonState();
+    onChangeListener.onChange(this);
   }
 
   private VersionKind getSelectedVersionKind() {
@@ -184,7 +187,7 @@ public class RepositoryLibraryPropertiesEditor extends DialogWrapper {
     versionPanel.setVisible(state == State.Loaded);
     failedToLoadPanel.setVisible(state == State.FailedToLoad);
     loadingPanel.setVisible(state == State.Loading);
-    checkOkButtonState();
+    onChangeListener.onChange(this);
   }
 
   private void reloadVersionsAsync() {
@@ -197,8 +200,8 @@ public class RepositoryLibraryPropertiesEditor extends DialogWrapper {
         try {
           List<String> versions = RepositoryAttachHandler.retrieveVersions(
             project,
-            properties.getGroupId(),
-            properties.getArtifactId(),
+            repositoryLibraryDescription.getGroupId(),
+            repositoryLibraryDescription.getArtifactId(),
             repositoryLibraryDescription.getRemoteRepositories());
           versionsLoaded(versions);
         }
@@ -211,7 +214,7 @@ public class RepositoryLibraryPropertiesEditor extends DialogWrapper {
   }
 
   private void initVersionsPanel() {
-    final int selection = getSelection(properties.getVersion(), versions);
+    final int selection = getSelection(model.getVersion(), versions);
     CollectionComboBoxModel<String> versionSelectorModel = new CollectionComboBoxModel<String>(versions);
     //noinspection unchecked
     versionSelector.setModel(versionSelectorModel);
@@ -221,28 +224,29 @@ public class RepositoryLibraryPropertiesEditor extends DialogWrapper {
     versionSelector.addItemListener(new ItemListener() {
       @Override
       public void itemStateChanged(ItemEvent e) {
-        properties.changeVersion(getSelectedVersion());
-        checkOkButtonState();
+        model.setVersion(getSelectedVersion());
+        onChangeListener.onChange(RepositoryLibraryPropertiesEditor.this);
       }
     });
-    downloadSourcesCheckBox.setSelected(downloadSources);
+    downloadSourcesCheckBox.setSelected(model.isDownloadSources());
     downloadSourcesCheckBox.addChangeListener(new ChangeListener() {
       @Override
       public void stateChanged(ChangeEvent e) {
-        downloadSources = downloadSourcesCheckBox.isSelected();
-        checkOkButtonState();
+        model.setDownloadSources(downloadSourcesCheckBox.isSelected());
+        onChangeListener.onChange(RepositoryLibraryPropertiesEditor.this);
       }
     });
-    downloadJavaDocsCheckBox.setSelected(downloadJavaDocs);
+    downloadJavaDocsCheckBox.setSelected(model.isDownloadJavaDocs());
     downloadJavaDocsCheckBox.addChangeListener(new ChangeListener() {
       @Override
       public void stateChanged(ChangeEvent e) {
-        downloadJavaDocs = downloadJavaDocsCheckBox.isSelected();
-        checkOkButtonState();
+        model.setDownloadJavaDocs(downloadJavaDocsCheckBox.isSelected());
+        onChangeListener.onChange(RepositoryLibraryPropertiesEditor.this);
       }
     });
   }
 
+
   private void versionsLoaded(final List<String> versions) {
     this.versions = versions;
     if (versions == null || versions.isEmpty()) {
@@ -255,12 +259,7 @@ public class RepositoryLibraryPropertiesEditor extends DialogWrapper {
       public void run() {
         initVersionsPanel();
       }
-    }, ModalityState.any(), new Condition() {
-      @Override
-      public boolean value(Object o) {
-        return Disposer.isDisposed(myDisposable);
-      }
-    });
+    }, ModalityState.any());
   }
 
   private void versionsFailedToLoad() {
@@ -269,12 +268,7 @@ public class RepositoryLibraryPropertiesEditor extends DialogWrapper {
       public void run() {
         setState(State.FailedToLoad);
       }
-    }, ModalityState.any(), new Condition() {
-      @Override
-      public boolean value(Object o) {
-        return Disposer.isDisposed(myDisposable);
-      }
-    });
+    }, ModalityState.any());
   }
 
   public String getSelectedVersion() {
@@ -291,26 +285,16 @@ public class RepositoryLibraryPropertiesEditor extends DialogWrapper {
     return null;
   }
 
-  public RepositoryLibraryProperties getProperties() {
-    return properties;
-  }
-
-  public boolean downloadSources() {
-    return downloadSources;
-  }
-
-  public boolean downloadJavaDocs() {
-    return downloadJavaDocs;
-  }
-
   public JPanel getMainPanel() {
     return mainPanel;
   }
 
-  @Nullable
-  @Override
-  protected JComponent createCenterPanel() {
-    return mainPanel;
+  public boolean isValid() {
+    return currentState == State.Loaded;
+  }
+
+  public boolean hasChanges() {
+    return !model.equals(initialModel);
   }
 
   private enum VersionKind {
diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/propertiesEditor/RepositoryLibraryPropertiesModel.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/library/propertiesEditor/RepositoryLibraryPropertiesModel.java
new file mode 100644 (file)
index 0000000..bfd5160
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2000-2015 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jetbrains.idea.maven.utils.library.propertiesEditor;
+
+import com.google.common.base.Strings;
+
+public class RepositoryLibraryPropertiesModel {
+  private String version;
+  private boolean downloadSources;
+  private boolean downloadJavaDocs;
+
+  public RepositoryLibraryPropertiesModel(String version, boolean downloadSources, boolean downloadJavaDocs) {
+    this.version = version;
+    this.downloadSources = downloadSources;
+    this.downloadJavaDocs = downloadJavaDocs;
+  }
+
+  public RepositoryLibraryPropertiesModel clone() {
+    return new RepositoryLibraryPropertiesModel(version, downloadSources, downloadJavaDocs);
+  }
+
+  public boolean isValid() {
+    return !Strings.isNullOrEmpty(version);
+  }
+
+  public boolean isDownloadSources() {
+    return downloadSources;
+  }
+
+  public void setDownloadSources(boolean downloadSources) {
+    this.downloadSources = downloadSources;
+  }
+
+  public boolean isDownloadJavaDocs() {
+    return downloadJavaDocs;
+  }
+
+  public void setDownloadJavaDocs(boolean downloadJavaDocs) {
+    this.downloadJavaDocs = downloadJavaDocs;
+  }
+
+  public String getVersion() {
+    return version;
+  }
+
+  public void setVersion(String version) {
+    this.version = version;
+  }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) return true;
+    if (o == null || getClass() != o.getClass()) return false;
+
+    RepositoryLibraryPropertiesModel model = (RepositoryLibraryPropertiesModel)o;
+
+    if (downloadSources != model.downloadSources) return false;
+    if (downloadJavaDocs != model.downloadJavaDocs) return false;
+    if (version != null ? !version.equals(model.version) : model.version != null) return false;
+
+    return true;
+  }
+
+  @Override
+  public int hashCode() {
+    int result = (downloadSources ? 1 : 0);
+    result = 31 * result + (downloadJavaDocs ? 1 : 0);
+    result = 31 * result + (version != null ? version.hashCode() : 0);
+    return result;
+  }
+}