action button updater rubymine/112.620
authorKonstantin Bulenkov <kb@jetbrains.com>
Fri, 27 Jan 2012 14:59:55 +0000 (15:59 +0100)
committerKonstantin Bulenkov <kb@jetbrains.com>
Fri, 27 Jan 2012 15:01:37 +0000 (16:01 +0100)
platform/platform-api/src/com/intellij/ui/AnActionButton.java
platform/platform-api/src/com/intellij/ui/AnActionButtonUpdater.java [new file with mode: 0644]

index 7ecf3b2af4771a3871e57f428bab54641e0a6d79..99c74f1ea744de42a224bcb62ea0a0d76522bf37 100644 (file)
@@ -18,10 +18,13 @@ package com.intellij.ui;
 import com.intellij.openapi.actionSystem.*;
 import com.intellij.ui.awt.RelativePoint;
 import com.intellij.util.ui.UIUtil;
+import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
 import java.awt.*;
+import java.util.HashSet;
+import java.util.Set;
 
 /**
  * @author Konstantin Bulenkov
@@ -32,6 +35,7 @@ public abstract class AnActionButton extends AnAction implements ShortcutProvide
   private ShortcutSet myShortcut;
   private AnAction myAction = null;
   private JComponent myContextComponent;
+  private Set<AnActionButtonUpdater> myUpdaters;
 
   public AnActionButton(String text) {
     super(text);
@@ -92,12 +96,20 @@ public abstract class AnActionButton extends AnAction implements ShortcutProvide
   public final void update(AnActionEvent e) {
     boolean myActionVisible = true;
     boolean myActionEnabled = true;
-    if (myAction != null) {
+    if (myAction != null) {      
       myAction.update(e);
-      myActionEnabled = myAction.getTemplatePresentation().isEnabled();
-      myActionVisible = myAction.getTemplatePresentation().isVisible();
+      myActionEnabled = e.getPresentation().isEnabled();
+      myActionVisible = e.getPresentation().isVisible();
+    }
+    boolean enabled = isEnabled() && isContextComponentOk() && myActionEnabled;
+    if (enabled) {
+      for (AnActionButtonUpdater updater : myUpdaters) {
+        if (!updater.isEnabled(e)) {
+          enabled = false;
+          break;
+        }
+      }
     }
-    final boolean enabled = isEnabled() && isContextComponentOk() && myActionEnabled;
     e.getPresentation().setEnabled(enabled);
     e.getPresentation().setVisible(isVisible() && myActionVisible);
 
@@ -105,6 +117,13 @@ public abstract class AnActionButton extends AnAction implements ShortcutProvide
       updateButton(e);
     }
   }
+  
+  public final void addCustomUpdater(@NotNull AnActionButtonUpdater updater) {
+    if (myUpdaters == null) {
+      myUpdaters = new HashSet<AnActionButtonUpdater>();
+    }
+    myUpdaters.add(updater);
+  }
 
   public void updateButton(AnActionEvent e) {
   }
diff --git a/platform/platform-api/src/com/intellij/ui/AnActionButtonUpdater.java b/platform/platform-api/src/com/intellij/ui/AnActionButtonUpdater.java
new file mode 100644 (file)
index 0000000..bc9cc43
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2000-2012 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 com.intellij.ui;
+
+import com.intellij.openapi.actionSystem.AnActionEvent;
+
+/**
+ * @author Konstantin Bulenkov
+ */
+public interface AnActionButtonUpdater {
+  boolean isEnabled(AnActionEvent e);
+}