cleanup: use CUSTOM_COMPONENT_PROPERTY property
authorGregory.Shrago <gregory.shrago@jetbrains.com>
Tue, 24 Feb 2015 18:43:23 +0000 (21:43 +0300)
committerGregory.Shrago <gregory.shrago@jetbrains.com>
Tue, 24 Feb 2015 18:45:02 +0000 (21:45 +0300)
platform/lang-impl/src/com/intellij/execution/actions/RunConfigurationsComboBoxAction.java
plugins/tasks/tasks-core/src/com/intellij/tasks/actions/SwitchTaskCombo.java

index dee3d78b4f69ff1d8351e7a1c3295f1bd5618fa2..50d3ac732482eaedaa52c29e12aa371574a32140 100644 (file)
@@ -22,62 +22,46 @@ import com.intellij.icons.AllIcons;
 import com.intellij.ide.DataManager;
 import com.intellij.openapi.actionSystem.*;
 import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
-import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.project.DumbAware;
 import com.intellij.openapi.project.DumbService;
 import com.intellij.openapi.project.IndexNotReadyException;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.util.Key;
-import com.intellij.openapi.wm.IdeFrame;
 import com.intellij.ui.SizedIcon;
 import com.intellij.util.ui.EmptyIcon;
-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.Iterator;
 import java.util.List;
 import java.util.Map;
 
 public class RunConfigurationsComboBoxAction extends ComboBoxAction implements DumbAware {
-  private static final Logger LOG = Logger.getInstance("#com.intellij.execution.actions.RunConfigurationAction");
-  private static final Key<ComboBoxButton> BUTTON_KEY = Key.create("COMBOBOX_BUTTON");
 
   public static final Icon CHECKED_ICON = new SizedIcon(AllIcons.Actions.Checked, 16, 16);
   public static final Icon CHECKED_SELECTED_ICON = new SizedIcon(AllIcons.Actions.Checked_selected, 16, 16);
   public static final Icon EMPTY_ICON = EmptyIcon.ICON_16;
 
   @Override
-  public void actionPerformed(final AnActionEvent e) {
-    final IdeFrame ideFrame = findFrame(e.getData(PlatformDataKeys.CONTEXT_COMPONENT));
-    final ComboBoxButton button = (ComboBoxButton)ideFrame.getComponent().getRootPane().getClientProperty(BUTTON_KEY);
+  public void actionPerformed(AnActionEvent e) {
+    ComboBoxButton button = (ComboBoxButton)e.getPresentation().getClientProperty(CUSTOM_COMPONENT_PROPERTY);
     if (button == null || !button.isShowing()) return;
     button.showPopup();
   }
 
-  private static IdeFrame findFrame(final Component component) {
-    return UIUtil.getParentOfType(IdeFrame.class, component);
-  }
-
   @Override
-  public void update(final AnActionEvent e) {
-    final Presentation presentation = e.getPresentation();
-    final Project project = e.getData(CommonDataKeys.PROJECT);
+  public void update(AnActionEvent e) {
+    Presentation presentation = e.getPresentation();
+    ComboBoxButton button = (ComboBoxButton)presentation.getClientProperty(CUSTOM_COMPONENT_PROPERTY);
+    Project project = e.getData(CommonDataKeys.PROJECT);
     if (ActionPlaces.isMainMenuOrActionSearch(e.getPlace())) {
       presentation.setDescription(ExecutionBundle.message("choose.run.configuration.action.description"));
-      presentation.setEnabled(findFrame(e.getData(PlatformDataKeys.CONTEXT_COMPONENT)) != null);
+      presentation.setEnabled(button != null);
       return;
     }
 
     try {
       if (project == null || project.isDisposed() || !project.isInitialized()) {
-        //if (ProjectManager.getInstance().getOpenProjects().length > 0) {
-        //  // do nothing if frame is not active
-        //  return;
-        //}
-
         updateButton(null, null, null, presentation);
         presentation.setEnabled(false);
       }
@@ -135,31 +119,9 @@ public class RunConfigurationsComboBoxAction extends ComboBoxAction implements D
 
   @Override
   public JComponent createCustomComponent(final Presentation presentation) {
-    final ComboBoxButton comboBoxButton = new ComboBoxButton(presentation) {
-      @Override
-      public void addNotify() {
-        super.addNotify();    //To change body of overriden methods use Options | File Templates.;
-        final IdeFrame frame = findFrame(this);
-        LOG.assertTrue(frame != null);
-        frame.getComponent().getRootPane().putClientProperty(BUTTON_KEY, this);
-      }
-
-      @Override
-      public void removeNotify() {
-        final IdeFrame frame = findFrame(this);
-        LOG.assertTrue(frame != null);
-        frame.getComponent().getRootPane().putClientProperty(BUTTON_KEY, null);
-        presentation.setIcon(null);
-        setIcon(null);
-        super.removeNotify();
-      }
-    };
-
-    final JPanel panel = new JPanel(new BorderLayout());
-    panel.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
-    panel.add(comboBoxButton);
-    panel.setOpaque(false);
-    return panel;
+    ComboBoxButton button = new ComboBoxButton(presentation);
+    button.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
+    return button;
   }
 
 
index 622c47dc552cbc386d5a5c655f207c5dde2f1134..0e6d3f3c91873c440102984cbdd84d93bbcea79e 100644 (file)
 
 package com.intellij.tasks.actions;
 
-import com.intellij.ide.DataManager;
-import com.intellij.openapi.actionSystem.*;
+import com.intellij.openapi.actionSystem.AnActionEvent;
+import com.intellij.openapi.actionSystem.CommonDataKeys;
+import com.intellij.openapi.actionSystem.DefaultActionGroup;
+import com.intellij.openapi.actionSystem.Presentation;
 import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
 import com.intellij.openapi.project.DumbAware;
 import com.intellij.openapi.project.Project;
-import com.intellij.openapi.ui.popup.JBPopup;
 import com.intellij.openapi.util.Comparing;
-import com.intellij.openapi.util.Key;
 import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.openapi.wm.IdeFrame;
-import com.intellij.openapi.wm.impl.IdeFrameImpl;
 import com.intellij.tasks.LocalTask;
 import com.intellij.tasks.TaskManager;
 import com.intellij.tasks.config.TaskSettings;
-import com.intellij.util.IJSwingUtilities;
-import com.intellij.util.ui.UIUtil;
 import org.jetbrains.annotations.NotNull;
 
 import javax.swing.*;
@@ -41,34 +37,17 @@ import javax.swing.*;
  */
 public class SwitchTaskCombo extends ComboBoxAction implements DumbAware {
 
-  private static final Key<ComboBoxButton> BUTTON_KEY = Key.create("SWITCH_TASK_BUTTON");
-
   @Override
   public void actionPerformed(AnActionEvent e) {
-    final IdeFrameImpl ideFrame = findFrame(e);
-    final ComboBoxButton button = (ComboBoxButton)ideFrame.getRootPane().getClientProperty(BUTTON_KEY);
+    ComboBoxButton button = (ComboBoxButton)e.getPresentation().getClientProperty(CUSTOM_COMPONENT_PROPERTY);
     if (button == null || !button.isShowing()) return;
     button.showPopup();
   }
 
-  private static IdeFrameImpl findFrame(AnActionEvent e) {
-    return IJSwingUtilities.findParentOfType(e.getData(PlatformDataKeys.CONTEXT_COMPONENT), IdeFrameImpl.class);
-  }
-
-  public JComponent createCustomComponent(final Presentation presentation) {
-    return new ComboBoxButton(presentation) {
-      public void addNotify() {
-        super.addNotify();
-        final IdeFrame frame = UIUtil.getParentOfType(IdeFrame.class, this);
-        assert frame != null;
-        frame.getComponent().getRootPane().putClientProperty(BUTTON_KEY, this);
-      }
-
-      @Override
-      protected JBPopup createPopup(Runnable onDispose) {
-        return SwitchTaskAction.createPopup(DataManager.getInstance().getDataContext(this), onDispose, false);
-      }
-    };
+  public JComponent createCustomComponent(Presentation presentation) {
+    ComboBoxButton button = new ComboBoxButton(presentation);
+    button.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 2));
+    return button;
   }
 
   @NotNull
@@ -79,9 +58,10 @@ public class SwitchTaskCombo extends ComboBoxAction implements DumbAware {
 
   @Override
   public void update(AnActionEvent e) {
-    Project project = e.getData(CommonDataKeys.PROJECT);
     Presentation presentation = e.getPresentation();
-    if (project == null || project.isDisposed() || (ActionPlaces.isMainMenuOrActionSearch(e.getPlace()) && findFrame(e) == null)) {
+    Project project = e.getData(CommonDataKeys.PROJECT);
+    ComboBoxButton button = (ComboBoxButton)presentation.getClientProperty(CUSTOM_COMPONENT_PROPERTY);
+    if (project == null || project.isDisposed() || button == null) {
       presentation.setEnabled(false);
       presentation.setText("");
       presentation.setIcon(null);