hide upload panel if there's no providers for stored before-launch task configs ...
[fedora-idea.git] / platform / lang-impl / src / com / intellij / execution / impl / ConfigurationSettingsEditorWrapper.java
blob290eacfa15182a22be2f17b67d118fa8188e0fe9
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.execution.impl;
19 import com.intellij.execution.BeforeRunTask;
20 import com.intellij.execution.BeforeRunTaskProvider;
21 import com.intellij.execution.configurations.RunConfiguration;
22 import com.intellij.execution.configurations.UnknownRunConfiguration;
23 import com.intellij.ide.DataManager;
24 import com.intellij.ide.impl.TypeSafeDataProviderAdapter;
25 import com.intellij.openapi.actionSystem.DataKey;
26 import com.intellij.openapi.actionSystem.DataSink;
27 import com.intellij.openapi.actionSystem.TypeSafeDataProvider;
28 import com.intellij.openapi.extensions.Extensions;
29 import com.intellij.openapi.options.ConfigurationException;
30 import com.intellij.openapi.options.SettingsEditor;
31 import com.intellij.openapi.ui.FixedSizeButton;
32 import com.intellij.openapi.util.Disposer;
33 import com.intellij.openapi.util.Key;
34 import gnu.trove.THashMap;
35 import org.jetbrains.annotations.NotNull;
37 import javax.swing.*;
38 import java.awt.*;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.util.*;
42 import java.util.List;
44 /**
45 * User: anna
46 * Date: 27-Mar-2006
48 public class ConfigurationSettingsEditorWrapper extends SettingsEditor<RunnerAndConfigurationSettingsImpl> {
49 public static DataKey<ConfigurationSettingsEditorWrapper> CONFIGURATION_EDITOR_KEY = DataKey.create("ConfigurationSettingsEditor");
50 private JPanel myComponentPlace;
51 private JCheckBox myCbStoreProjectConfiguration;
52 private JPanel myWholePanel;
54 private JPanel myStepsPanel;
55 private Map<Key<? extends BeforeRunTask>, BeforeRunTask> myStepsBeforeLaunch;
56 private Map<Key<? extends BeforeRunTask>, StepBeforeLaunchRow> myStepBeforeLaunchRows = new THashMap<Key<? extends BeforeRunTask>, StepBeforeLaunchRow>();
58 private boolean myStoreProjectConfiguration;
60 private final ConfigurationSettingsEditor myEditor;
62 public ConfigurationSettingsEditorWrapper(final RunnerAndConfigurationSettingsImpl settings) {
63 myEditor = new ConfigurationSettingsEditor(settings);
64 Disposer.register(this, myEditor);
65 doReset(settings);
68 private void doReset(RunnerAndConfigurationSettingsImpl settings) {
69 final RunConfiguration runConfiguration = settings.getConfiguration();
70 final RunManagerImpl runManager = RunManagerImpl.getInstanceImpl(runConfiguration.getProject());
72 myStepsBeforeLaunch = runManager.getBeforeRunTasks(runConfiguration);
73 myStepBeforeLaunchRows.clear();
75 final BeforeRunTaskProvider<BeforeRunTask>[] providers = Extensions.getExtensions(BeforeRunTaskProvider.EXTENSION_POINT_NAME,
76 runConfiguration.getProject());
77 myStepsPanel.removeAll();
78 if (providers.length == 0 || runConfiguration instanceof UnknownRunConfiguration) {
79 myStepsPanel.setVisible(false);
81 else {
82 List<StepBeforeLaunchRow> stepsRows = new ArrayList<StepBeforeLaunchRow>();
83 for (BeforeRunTaskProvider<BeforeRunTask> provider : providers) {
84 final BeforeRunTask task = myStepsBeforeLaunch.get(provider.getId());
85 if (task != null) {
86 final StepBeforeLaunchRow stepRow = new StepBeforeLaunchRow(runConfiguration, provider, task);
87 myStepBeforeLaunchRows.put(provider.getId(), stepRow);
88 stepsRows.add(stepRow);
92 if (!stepsRows.isEmpty()) {
93 myStepsPanel.setLayout(new GridLayout(stepsRows.size(), 1));
94 for (StepBeforeLaunchRow stepRow : stepsRows) {
95 myStepsPanel.add(stepRow);
98 else {
99 myStepsPanel.setVisible(false);
103 myStoreProjectConfiguration = runManager.isConfigurationShared(settings);
104 myCbStoreProjectConfiguration.setEnabled(!(runConfiguration instanceof UnknownRunConfiguration));
105 myCbStoreProjectConfiguration.setSelected(myStoreProjectConfiguration);
106 myCbStoreProjectConfiguration.addActionListener(new ActionListener() {
107 public void actionPerformed(ActionEvent e) {
108 myStoreProjectConfiguration = myCbStoreProjectConfiguration.isSelected();
113 @NotNull
114 protected JComponent createEditor() {
115 myComponentPlace.setLayout(new GridBagLayout());
116 myComponentPlace.add(myEditor.getComponent(), new GridBagConstraints(0,0,1,1,1.0,1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0,0));
117 myComponentPlace.doLayout();
118 myWholePanel.putClientProperty(DataManager.CLIENT_PROPERTY_DATA_PROVIDER, new TypeSafeDataProviderAdapter(new MyDataProvider()));
119 return myWholePanel;
122 public void updateBeforeRunTaskPanel(@NotNull Key<? extends BeforeRunTask> key) {
123 myStepBeforeLaunchRows.get(key).update(myStepsBeforeLaunch.get(key));
126 protected void disposeEditor() {
129 public void resetEditorFrom(final RunnerAndConfigurationSettingsImpl settings) {
130 myEditor.resetEditorFrom(settings);
131 doReset(settings);
134 public void applyEditorTo(final RunnerAndConfigurationSettingsImpl settings) throws ConfigurationException {
135 myEditor.applyEditorTo(settings);
136 doApply(settings);
139 public RunnerAndConfigurationSettingsImpl getSnapshot() throws ConfigurationException {
140 RunnerAndConfigurationSettingsImpl result = myEditor.getSnapshot();
141 doApply(result);
142 return result;
145 private void doApply(final RunnerAndConfigurationSettingsImpl settings) {
146 final RunConfiguration runConfiguration = settings.getConfiguration();
147 final RunManagerImpl runManager = RunManagerImpl.getInstanceImpl(runConfiguration.getProject());
148 runManager.setBeforeRunTasks(runConfiguration, myStepsBeforeLaunch);
149 runManager.shareConfiguration(runConfiguration, myStoreProjectConfiguration);
152 public Map<Key<? extends BeforeRunTask>, BeforeRunTask> getStepsBeforeLaunch() {
153 return Collections.unmodifiableMap(myStepsBeforeLaunch);
156 public boolean isStoreProjectConfiguration() {
157 return myStoreProjectConfiguration;
160 private class StepBeforeLaunchRow extends JPanel {
161 private final JCheckBox myCheckBox;
162 private FixedSizeButton myButton;
163 private final RunConfiguration myRunConfiguration;
164 private final BeforeRunTaskProvider<BeforeRunTask> myProvider;
166 public StepBeforeLaunchRow(final RunConfiguration runConfiguration, final BeforeRunTaskProvider<BeforeRunTask> provider,
167 final BeforeRunTask beforeRunTask) {
168 super(new GridBagLayout());
169 myRunConfiguration = runConfiguration;
170 myProvider = provider;
171 final boolean isChecked = beforeRunTask.isEnabled();
172 myCheckBox = new JCheckBox(provider.getDescription(runConfiguration, beforeRunTask), isChecked);
173 GridBagConstraints gc = new GridBagConstraints(GridBagConstraints.RELATIVE, 0 , 1, 1, 0, 1, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0);
174 add(myCheckBox, gc);
175 gc.weightx = 1;
176 if (provider.hasConfigurationButton()) {
177 myButton = new FixedSizeButton(20);
178 add(myButton, gc);
180 myButton.addActionListener(new ActionListener() {
181 public void actionPerformed(ActionEvent e) {
182 if (provider.configureTask(runConfiguration, beforeRunTask)) {
183 myCheckBox.setText(provider.getDescription(runConfiguration, beforeRunTask));
184 fireEditorStateChanged();
189 else {
190 add(Box.createHorizontalBox(), gc);
192 enableSettings(beforeRunTask);
193 myCheckBox.addActionListener(new ActionListener() {
194 public void actionPerformed(ActionEvent e) {
195 beforeRunTask.setEnabled(myCheckBox.isSelected());
196 enableSettings(beforeRunTask);
201 private void enableSettings(final BeforeRunTask task) {
202 if (myButton != null) {
203 myButton.setEnabled(myCheckBox.isSelected());
205 myCheckBox.setText(myProvider.getDescription(myRunConfiguration, task));
208 public void update(BeforeRunTask task) {
209 myCheckBox.setSelected(task.isEnabled());
210 enableSettings(task);
214 private class MyDataProvider implements TypeSafeDataProvider {
215 public void calcData(DataKey key, DataSink sink) {
216 if (key.equals(CONFIGURATION_EDITOR_KEY)) {
217 sink.put(CONFIGURATION_EDITOR_KEY, ConfigurationSettingsEditorWrapper.this);