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
.ExecutionBundle
;
20 import com
.intellij
.execution
.Executor
;
21 import com
.intellij
.execution
.ExecutorRegistry
;
22 import com
.intellij
.execution
.RunnerRegistry
;
23 import com
.intellij
.execution
.configurations
.RunConfiguration
;
24 import com
.intellij
.execution
.runners
.ProgramRunner
;
25 import com
.intellij
.openapi
.options
.*;
26 import com
.intellij
.openapi
.util
.Disposer
;
27 import com
.intellij
.openapi
.util
.JDOMExternalizable
;
28 import com
.intellij
.openapi
.util
.Pair
;
29 import com
.intellij
.ui
.ColoredListCellRenderer
;
30 import com
.intellij
.ui
.ListScrollingUtil
;
31 import com
.intellij
.ui
.SimpleTextAttributes
;
32 import com
.intellij
.util
.containers
.Convertor
;
33 import org
.jetbrains
.annotations
.NonNls
;
34 import org
.jetbrains
.annotations
.NotNull
;
37 import javax
.swing
.event
.ListSelectionEvent
;
38 import javax
.swing
.event
.ListSelectionListener
;
40 import java
.util
.ArrayList
;
41 import java
.util
.Collection
;
42 import java
.util
.List
;
47 class ConfigurationSettingsEditor
extends CompositeSettingsEditor
<RunnerAndConfigurationSettingsImpl
> {
48 private final ArrayList
<SettingsEditor
<RunnerAndConfigurationSettingsImpl
>> myRunnerEditors
=
49 new ArrayList
<SettingsEditor
<RunnerAndConfigurationSettingsImpl
>>();
50 private RunnersEditorComponent myRunnersComponent
;
51 private final RunConfiguration myConfiguration
;
52 private final SettingsEditor
<RunConfiguration
> myConfigurationEditor
;
53 private SettingsEditorGroup
<RunnerAndConfigurationSettingsImpl
> myCompound
;
55 public CompositeSettingsBuilder
<RunnerAndConfigurationSettingsImpl
> getBuilder() {
57 return new GroupSettingsBuilder
<RunnerAndConfigurationSettingsImpl
>(myCompound
);
61 if (myCompound
== null) {
62 myCompound
= new SettingsEditorGroup
<RunnerAndConfigurationSettingsImpl
>();
63 Disposer
.register(this, myCompound
);
64 if (myConfigurationEditor
instanceof SettingsEditorGroup
) {
65 SettingsEditorGroup
<RunConfiguration
> group
= (SettingsEditorGroup
<RunConfiguration
>)myConfigurationEditor
;
66 List
<Pair
<String
, SettingsEditor
<RunConfiguration
>>> editors
= group
.getEditors();
67 for (Pair
<String
, SettingsEditor
<RunConfiguration
>> pair
: editors
) {
68 myCompound
.addEditor(pair
.getFirst(), new ConfigToSettingsWrapper(pair
.getSecond()));
72 myCompound
.addEditor(ExecutionBundle
.message("run.configuration.configuration.tab.title"),
73 new ConfigToSettingsWrapper(myConfigurationEditor
));
77 myRunnersComponent
= new RunnersEditorComponent();
78 ProgramRunner
[] runners
= RunnerRegistry
.getInstance().getRegisteredRunners();
80 final Executor
[] executors
= ExecutorRegistry
.getInstance().getRegisteredExecutors();
81 for (final Executor executor
: executors
) {
82 for (ProgramRunner runner
: runners
) {
83 if (runner
.canRun(executor
.getId(), myConfiguration
)) {
84 JComponent perRunnerSettings
= createCompositePerRunnerSettings(executor
, runner
);
85 if (perRunnerSettings
!= null) {
86 myRunnersComponent
.addExecutorComponent(executor
, perRunnerSettings
);
92 if (myRunnerEditors
.size() > 0) {
93 myCompound
.addEditor(ExecutionBundle
.message("run.configuration.startup.connection.rab.title"),
94 new CompositeSettingsEditor
<RunnerAndConfigurationSettingsImpl
>(getFactory()) {
95 public CompositeSettingsBuilder
<RunnerAndConfigurationSettingsImpl
> getBuilder() {
96 return new CompositeSettingsBuilder
<RunnerAndConfigurationSettingsImpl
>() {
97 public Collection
<SettingsEditor
<RunnerAndConfigurationSettingsImpl
>> getEditors() {
98 return myRunnerEditors
;
101 public JComponent
createCompoundEditor() {
102 return myRunnersComponent
.getComponent();
111 private JComponent
createCompositePerRunnerSettings(final Executor executor
, final ProgramRunner runner
) {
112 final SettingsEditor
<JDOMExternalizable
> configEditor
= myConfiguration
.getRunnerSettingsEditor(runner
);
113 SettingsEditor
<JDOMExternalizable
> runnerEditor
;
116 runnerEditor
= runner
.getSettingsEditor(executor
, myConfiguration
);
118 catch (AbstractMethodError error
) {
119 // this is stub code for plugin copatibility!
123 if (configEditor
== null && runnerEditor
== null) return null;
124 SettingsEditor
<RunnerAndConfigurationSettingsImpl
> wrappedConfigEditor
= null;
125 SettingsEditor
<RunnerAndConfigurationSettingsImpl
> wrappedRunEditor
= null;
126 if (configEditor
!= null) {
127 wrappedConfigEditor
= new SettingsEditorWrapper
<RunnerAndConfigurationSettingsImpl
, JDOMExternalizable
>(configEditor
,
128 new Convertor
<RunnerAndConfigurationSettingsImpl
, JDOMExternalizable
>() {
129 public JDOMExternalizable
convert(RunnerAndConfigurationSettingsImpl configurationSettings
) {
130 return configurationSettings
.getConfigurationSettings(runner
).getSettings();
133 myRunnerEditors
.add(wrappedConfigEditor
);
134 Disposer
.register(this, wrappedConfigEditor
);
137 if (runnerEditor
!= null) {
138 wrappedRunEditor
= new SettingsEditorWrapper
<RunnerAndConfigurationSettingsImpl
, JDOMExternalizable
>(runnerEditor
,
139 new Convertor
<RunnerAndConfigurationSettingsImpl
, JDOMExternalizable
>() {
140 public JDOMExternalizable
convert(RunnerAndConfigurationSettingsImpl configurationSettings
) {
141 return configurationSettings
.getRunnerSettings(runner
).getData();
144 myRunnerEditors
.add(wrappedRunEditor
);
145 Disposer
.register(this, wrappedRunEditor
);
148 if (wrappedRunEditor
!= null && wrappedConfigEditor
!= null) {
149 JPanel panel
= new JPanel(new BorderLayout());
150 panel
.add(wrappedConfigEditor
.getComponent(), BorderLayout
.CENTER
);
151 panel
.add(wrappedRunEditor
.getComponent(), BorderLayout
.SOUTH
);
155 if (wrappedRunEditor
!= null) return wrappedRunEditor
.getComponent();
156 return wrappedConfigEditor
.getComponent();
159 public ConfigurationSettingsEditor(RunnerAndConfigurationSettingsImpl settings
) {
160 super(settings
.createFactory());
161 myConfigurationEditor
= (SettingsEditor
<RunConfiguration
>)settings
.getConfiguration().getConfigurationEditor();
162 Disposer
.register(this, myConfigurationEditor
);
163 myConfiguration
= settings
.getConfiguration();
166 public RunnerAndConfigurationSettingsImpl
getSnapshot() throws ConfigurationException
{
167 RunnerAndConfigurationSettingsImpl settings
= getFactory().create();
168 settings
.setName(myConfiguration
.getName());
169 if (myConfigurationEditor
instanceof CheckableRunConfigurationEditor
) {
170 ((CheckableRunConfigurationEditor
)myConfigurationEditor
).checkEditorData(settings
.getConfiguration());
178 private static class RunnersEditorComponent
{
179 @NonNls private static final String NO_RUNNER_COMPONENT
= "<NO RUNNER LABEL>";
181 private JList myRunnersList
;
182 private JPanel myRunnerPanel
;
183 private final CardLayout myLayout
= new CardLayout();
184 private final DefaultListModel myListModel
= new DefaultListModel();
185 private final JLabel myNoRunner
= new JLabel(ExecutionBundle
.message("run.configuration.norunner.selected.label"));
186 private JPanel myRunnersPanel
;
188 public RunnersEditorComponent() {
189 myRunnerPanel
.setLayout(myLayout
);
190 myRunnerPanel
.add(myNoRunner
, NO_RUNNER_COMPONENT
);
191 myRunnersList
.setModel(myListModel
);
192 myRunnersList
.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
193 public void valueChanged(ListSelectionEvent e
) {
194 updateRunnerComponent();
197 updateRunnerComponent();
198 myRunnersList
.setCellRenderer(new ColoredListCellRenderer() {
199 protected void customizeCellRenderer(JList list
, Object value
, int index
, boolean selected
, boolean hasFocus
) {
200 Executor executor
= (Executor
)value
;
201 setIcon(executor
.getIcon());
202 append(executor
.getId(), SimpleTextAttributes
.REGULAR_ATTRIBUTES
);
207 private void updateRunnerComponent() {
208 Executor executor
= (Executor
)myRunnersList
.getSelectedValue();
209 myLayout
.show(myRunnerPanel
, executor
!= null ? executor
.getId() : NO_RUNNER_COMPONENT
);
210 myRunnersPanel
.revalidate();
213 public void addExecutorComponent(Executor executor
, JComponent component
) {
214 myRunnerPanel
.add(component
, executor
.getId());
215 myListModel
.addElement(executor
);
216 ListScrollingUtil
.ensureSelectionExists(myRunnersList
);
219 public JComponent
getComponent() {
220 return myRunnersPanel
;
224 private static class ConfigToSettingsWrapper
extends SettingsEditor
<RunnerAndConfigurationSettingsImpl
> {
225 private final SettingsEditor
<RunConfiguration
> myConfigEditor
;
227 public ConfigToSettingsWrapper(SettingsEditor
<RunConfiguration
> configEditor
) {
228 myConfigEditor
= configEditor
;
231 public void resetEditorFrom(RunnerAndConfigurationSettingsImpl configurationSettings
) {
232 myConfigEditor
.resetFrom(configurationSettings
.getConfiguration());
235 public void applyEditorTo(RunnerAndConfigurationSettingsImpl configurationSettings
) throws ConfigurationException
{
236 myConfigEditor
.applyTo(configurationSettings
.getConfiguration());
240 public JComponent
createEditor() {
241 return myConfigEditor
.getComponent();
244 public void disposeEditor() {
245 Disposer
.dispose(myConfigEditor
);