more temporary run configs
[fedora-idea.git] / lang-impl / src / com / intellij / execution / actions / RunConfigurationAction.java
blobd5186adc36d144c6a0015ee362a57f3b75a9f6d2
1 package com.intellij.execution.actions;
3 import com.intellij.execution.*;
4 import com.intellij.execution.configurations.ConfigurationType;
5 import com.intellij.execution.configurations.RunConfiguration;
6 import com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl;
7 import com.intellij.ide.DataManager;
8 import com.intellij.openapi.actionSystem.*;
9 import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
10 import com.intellij.openapi.diagnostic.Logger;
11 import com.intellij.openapi.project.Project;
12 import com.intellij.openapi.project.IndexNotReadyException;
13 import com.intellij.openapi.project.DumbAware;
14 import com.intellij.openapi.util.IconLoader;
15 import com.intellij.openapi.util.Key;
16 import com.intellij.openapi.wm.impl.IdeFrameImpl;
17 import com.intellij.util.IJSwingUtilities;
18 import org.jetbrains.annotations.NotNull;
19 import org.jetbrains.annotations.Nullable;
21 import javax.swing.*;
22 import java.awt.*;
24 public class RunConfigurationAction extends ComboBoxAction implements DumbAware {
25 private static final Logger LOG = Logger.getInstance("#com.intellij.execution.actions.RunConfigurationAction");
26 private static final Key<ComboBoxAction.ComboBoxButton> BUTTON_KEY = Key.create("COMBOBOX_BUTTON");
28 public void actionPerformed(final AnActionEvent e) {
29 final IdeFrameImpl ideFrame = findFrame(e.getData(PlatformDataKeys.CONTEXT_COMPONENT));
30 final ComboBoxAction.ComboBoxButton button = (ComboBoxAction.ComboBoxButton)ideFrame.getRootPane().getClientProperty(BUTTON_KEY);
31 if (button == null || !button.isShowing()) return;
32 button.showPopup();
35 private static IdeFrameImpl findFrame(final Component component) {
36 return IJSwingUtilities.findParentOfType(component, IdeFrameImpl.class);
39 public void update(final AnActionEvent e) {
40 final Presentation presentation = e.getPresentation();
41 final Project project = e.getData(PlatformDataKeys.PROJECT);
42 if (ActionPlaces.MAIN_MENU.equals(e.getPlace())) {
43 presentation.setDescription(ExecutionBundle.message("choose.run.configuration.action.description"));
44 presentation.setEnabled(findFrame(e.getData(PlatformDataKeys.CONTEXT_COMPONENT)) != null);
45 return;
48 try {
49 if (project == null || project.isDisposed()) {
50 //if (ProjectManager.getInstance().getOpenProjects().length > 0) {
51 // // do nothing if frame is not active
52 // return;
53 //}
55 updateButton(null, null, presentation);
56 presentation.setEnabled(false);
58 else {
59 final RunManagerEx runManager = RunManagerEx.getInstanceEx(project);
60 RunnerAndConfigurationSettings selected = runManager.getSelectedConfiguration();
61 updateButton(selected, project, presentation);
62 presentation.setEnabled(true);
65 catch (IndexNotReadyException e1) {
66 presentation.setEnabled(false);
70 private static void updateButton(final @Nullable RunnerAndConfigurationSettings settings, final @Nullable Project project,
71 final @NotNull Presentation presentation) {
72 if (project != null && settings != null) {
73 presentation.setText(settings.getName(), false);
74 setConfigurationIcon(presentation, settings, project);
76 else {
77 presentation.setText(""); // IDEA-21657
78 presentation.setIcon(null);
82 private static void setConfigurationIcon(final Presentation presentation, final RunnerAndConfigurationSettings settings, final Project project) {
83 try {
84 presentation.setIcon(ExecutionUtil.getConfigurationIcon(project, settings));
86 catch (IndexNotReadyException ignored) {
90 public JComponent createCustomComponent(final Presentation presentation) {
91 return new ComboBoxButton(presentation) {
92 protected void updateButtonSize() {
93 super.updateButtonSize();
94 final Dimension preferredSize = getPreferredSize();
95 final int width = preferredSize.width;
96 final int height = preferredSize.height;
97 if (width > height * 15) {
98 setPreferredSize(new Dimension(height * 15, height));
102 public void addNotify() {
103 super.addNotify(); //To change body of overriden methods use Options | File Templates.;
104 final IdeFrameImpl frame = findFrame(this);
105 LOG.assertTrue(frame != null);
106 frame.getRootPane().putClientProperty(BUTTON_KEY, this);
112 @NotNull
113 protected DefaultActionGroup createPopupActionGroup(final JComponent button) {
114 final DefaultActionGroup allActionsGroup = new DefaultActionGroup();
115 final Project project = PlatformDataKeys.PROJECT.getData(DataManager.getInstance().getDataContext(button));
116 if (project != null) {
117 final RunManagerEx runManager = RunManagerEx.getInstanceEx(project);
119 allActionsGroup.add(ActionManager.getInstance().getAction(IdeActions.ACTION_EDIT_RUN_CONFIGURATIONS));
120 allActionsGroup.add(new SaveTemporaryAction());
121 allActionsGroup.addSeparator();
123 final ConfigurationType[] types = runManager.getConfigurationFactories();
124 for (ConfigurationType type : types) {
125 final DefaultActionGroup actionGroup = new DefaultActionGroup();
126 final RunnerAndConfigurationSettingsImpl[] configurations = runManager.getConfigurationSettings(type);
127 for (final RunnerAndConfigurationSettingsImpl configuration : configurations) {
128 //if (runManager.canRunConfiguration(configuration)) {
129 final MenuAction action = new MenuAction(configuration, project);
130 actionGroup.add(action);
133 allActionsGroup.add(actionGroup);
134 allActionsGroup.addSeparator();
137 return allActionsGroup;
140 private static class SaveTemporaryAction extends AnAction {
141 public SaveTemporaryAction() {
142 Presentation presentation = getTemplatePresentation();
143 presentation.setIcon(IconLoader.getIcon("/runConfigurations/saveTempConfig.png"));
146 public void actionPerformed(final AnActionEvent e) {
147 final Project project = e.getData(PlatformDataKeys.PROJECT);
148 if (project != null) {
149 final RunManager runManager = RunManager.getInstance(project);
150 runManager.makeStable(chooseTempConfiguration(project));
154 public void update(final AnActionEvent e) {
155 final Presentation presentation = e.getPresentation();
156 final Project project = e.getData(PlatformDataKeys.PROJECT);
157 if (project == null) {
158 disable(presentation);
159 return;
161 RunConfiguration configuration = chooseTempConfiguration(project);
162 if (configuration == null) {
163 disable(presentation);
164 } else {
165 presentation.setText(ExecutionBundle.message("save.temporary.run.configuration.action.name", configuration.getName()));
166 presentation.setDescription(presentation.getText());
167 presentation.setVisible(true);
168 presentation.setEnabled(true);
172 private static void disable(final Presentation presentation) {
173 presentation.setEnabled(false);
174 presentation.setVisible(false);
177 @Nullable
178 private static RunConfiguration chooseTempConfiguration(Project project) {
179 final RunConfiguration[] tempConfigurations = RunManager.getInstance(project).getTempConfigurations();
180 if (tempConfigurations.length > 0) {
181 RunnerAndConfigurationSettings selectedConfiguration = RunManager.getInstance(project).getSelectedConfiguration();
182 if (selectedConfiguration == null || !selectedConfiguration.isTemporary()) {
183 return tempConfigurations[0];
185 return selectedConfiguration.getConfiguration();
187 return null;
191 private static class MenuAction extends AnAction {
192 private final RunnerAndConfigurationSettingsImpl myConfiguration;
193 private final Project myProject;
195 public MenuAction(final RunnerAndConfigurationSettingsImpl configuration, final Project project) {
196 myConfiguration = configuration;
197 myProject = project;
198 String name = configuration.getName();
199 if (name == null || name.length() == 0) {
200 name = " ";
202 final Presentation presentation = getTemplatePresentation();
203 presentation.setText(name, false);
204 presentation.setDescription("Start "+configuration.getType().getConfigurationTypeDescription()+" '"+name+"'");
205 updateIcon(presentation);
208 private void updateIcon(final Presentation presentation) {
209 setConfigurationIcon(presentation, myConfiguration, myProject);
212 public void actionPerformed(final AnActionEvent e){
213 RunManagerEx.getInstanceEx(myProject).setActiveConfiguration(myConfiguration);
214 updateButton(myConfiguration, myProject, e.getPresentation());
217 public void update(final AnActionEvent e) {
218 super.update(e);
219 updateIcon(e.getPresentation());