simplify ComponentWithBrowseButton: remove all action listeners on component disposin...
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Fri, 23 Sep 2016 19:41:05 +0000 (22:41 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Fri, 23 Sep 2016 19:41:05 +0000 (22:41 +0300)
platform/platform-api/src/com/intellij/openapi/ui/ComponentWithBrowseButton.java

index cbd5201134ea9280c50497e5b05eac65c0301be1..a4a24208b4fc0262ba59c25dce9b87fe19c12898 100644 (file)
@@ -26,7 +26,6 @@ import com.intellij.openapi.fileChooser.FileChooserDescriptor;
 import com.intellij.openapi.keymap.KeymapUtil;
 import com.intellij.openapi.project.DumbAwareAction;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.util.SystemInfo;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.util.text.StringUtil;
@@ -36,7 +35,6 @@ import com.intellij.ui.GuiUtils;
 import com.intellij.ui.UIBundle;
 import com.intellij.util.ui.UIUtil;
 import com.intellij.util.ui.accessibility.ScreenReader;
-import com.intellij.util.ui.update.LazyUiDisposable;
 import org.jetbrains.annotations.Nls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -131,42 +129,43 @@ public class ComponentWithBrowseButton<Comp extends JComponent> extends JPanel i
                                       @Nullable Project project,
                                       FileChooserDescriptor fileChooserDescriptor,
                                       TextComponentAccessor<Comp> accessor) {
-    addBrowseFolderListener(title, description, project, fileChooserDescriptor, accessor, true);
+    addActionListener(new BrowseFolderActionListener<>(title, description, this, project, fileChooserDescriptor, accessor));
   }
 
+  /**
+   * @deprecated use {@link #addBrowseFolderListener(String, String, Project, FileChooserDescriptor, TextComponentAccessor)} instead
+   */
   public void addBrowseFolderListener(@Nullable @Nls(capitalization = Nls.Capitalization.Title) String title,
                                       @Nullable @Nls(capitalization = Nls.Capitalization.Sentence) String description,
                                       @Nullable Project project,
                                       FileChooserDescriptor fileChooserDescriptor,
                                       TextComponentAccessor<Comp> accessor, boolean autoRemoveOnHide) {
-    addBrowseFolderListener(project, new BrowseFolderActionListener<>(title, description, this, project, fileChooserDescriptor, accessor), autoRemoveOnHide);
+    addBrowseFolderListener(title, description, project, fileChooserDescriptor, accessor);
   }
 
+  /**
+   * @deprecated use {@link #addActionListener(ActionListener)} instead
+   */
+  @SuppressWarnings("UnusedParameters")
   public void addBrowseFolderListener(@Nullable Project project, final BrowseFolderActionListener<Comp> actionListener) {
-    addBrowseFolderListener(project, actionListener, true);
+    addActionListener(actionListener);
   }
 
+  /**
+   * @deprecated use {@link #addActionListener(ActionListener)} instead
+   */
+  @SuppressWarnings("UnusedParameters")
   public void addBrowseFolderListener(@Nullable Project project, final BrowseFolderActionListener<Comp> actionListener, boolean autoRemoveOnHide) {
-    if (autoRemoveOnHide) {
-      new LazyUiDisposable<ComponentWithBrowseButton<Comp>>(null, this, this) {
-        @Override
-        protected void initialize(@NotNull Disposable parent, @NotNull ComponentWithBrowseButton<Comp> child, @Nullable Project project) {
-          addActionListener(actionListener);
-          Disposer.register(child, new Disposable() {
-            @Override
-            public void dispose() {
-              removeActionListener(actionListener);
-            }
-          });
-        }
-      };
-    } else {
-      addActionListener(actionListener);
-    }
+    addActionListener(actionListener);
   }
 
   @Override
-  public void dispose() { }
+  public void dispose() {
+    ActionListener[] listeners = myBrowseButton.getActionListeners();
+    for (ActionListener listener : listeners) {
+      myBrowseButton.removeActionListener(listener);
+    }
+  }
 
   public FixedSizeButton getButton() {
     return myBrowseButton;