4e7a87e1d53af5b6efe38ea59d6a996e4416e716
[idea/community.git] / platform / external-system-impl / src / com / intellij / openapi / externalSystem / service / execution / ExternalSystemTaskSettingsControl.java
1 /*
2  * Copyright 2000-2013 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.openapi.externalSystem.service.execution;
17
18 import com.intellij.openapi.externalSystem.ExternalSystemManager;
19 import com.intellij.openapi.externalSystem.ExternalSystemUiAware;
20 import com.intellij.openapi.externalSystem.model.ProjectSystemId;
21 import com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings;
22 import com.intellij.openapi.externalSystem.service.ui.ExternalProjectPathField;
23 import com.intellij.openapi.externalSystem.util.*;
24 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
25 import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
26 import com.intellij.openapi.options.ConfigurationException;
27 import com.intellij.openapi.project.Project;
28 import com.intellij.openapi.util.Comparing;
29 import com.intellij.openapi.util.text.StringUtil;
30 import com.intellij.ui.RawCommandLineEditor;
31 import com.intellij.ui.components.JBLabel;
32 import com.intellij.ui.components.JBTextField;
33 import com.intellij.util.ui.GridBag;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36
37 import java.awt.*;
38
39 import static com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil.normalizePath;
40
41 /**
42  * @author Denis Zhdanov
43  * @since 23.05.13 18:46
44  */
45 public class ExternalSystemTaskSettingsControl implements ExternalSystemSettingsControl<ExternalSystemTaskExecutionSettings> {
46
47   @NotNull private final ProjectSystemId myExternalSystemId;
48   @NotNull private final Project myProject;
49
50   @SuppressWarnings("FieldCanBeLocal") // Used via reflection at showUi() and disposeResources()
51   private JBLabel myProjectPathLabel;
52   private ExternalProjectPathField myProjectPathField;
53   @SuppressWarnings("FieldCanBeLocal") // Used via reflection at showUi() and disposeResources()
54   private JBLabel myTasksLabel;
55   private JBTextField myTasksTextField;
56   @SuppressWarnings("FieldCanBeLocal") // Used via reflection at showUi() and disposeResources()
57   private JBLabel myVmOptionsLabel;
58   private RawCommandLineEditor myVmOptionsEditor;
59   @SuppressWarnings("FieldCanBeLocal") // Used via reflection at showUi() and disposeResources()
60   private JBLabel myScriptParametersLabel;
61   private RawCommandLineEditor myScriptParametersEditor;
62
63   @Nullable private ExternalSystemTaskExecutionSettings myOriginalSettings;
64
65   public ExternalSystemTaskSettingsControl(@NotNull Project project, @NotNull ProjectSystemId externalSystemId) {
66     myProject = project;
67     myExternalSystemId = externalSystemId;
68   }
69
70   public void setOriginalSettings(@Nullable ExternalSystemTaskExecutionSettings originalSettings) {
71     myOriginalSettings = originalSettings;
72   }
73
74   @Override
75   public void fillUi(@NotNull final PaintAwarePanel canvas, int indentLevel) {
76     myProjectPathLabel = new JBLabel(ExternalSystemBundle.message(
77       "run.configuration.settings.label.project", myExternalSystemId.getReadableName()
78     ));
79     ExternalSystemManager<?, ?, ?, ?, ?> manager = ExternalSystemApiUtil.getManager(myExternalSystemId);
80     FileChooserDescriptor projectPathChooserDescriptor = null;
81     if (manager instanceof ExternalSystemUiAware) {
82       projectPathChooserDescriptor = ((ExternalSystemUiAware)manager).getExternalProjectConfigDescriptor();
83     }
84     if (projectPathChooserDescriptor == null) {
85       projectPathChooserDescriptor = FileChooserDescriptorFactory.createSingleLocalFileDescriptor();
86     }
87     String title = ExternalSystemBundle.message("settings.label.select.project", myExternalSystemId.getReadableName());
88     myProjectPathField = new ExternalProjectPathField(myProject, myExternalSystemId, projectPathChooserDescriptor, title) {
89       @Override
90       public Dimension getPreferredSize() {
91         return myVmOptionsEditor == null ? super.getPreferredSize() : myVmOptionsEditor.getTextField().getPreferredSize();
92       }
93     };
94     canvas.add(myProjectPathLabel, ExternalSystemUiUtil.getLabelConstraints(0));
95     canvas.add(myProjectPathField, ExternalSystemUiUtil.getFillLineConstraints(0));
96
97     myTasksLabel = new JBLabel(ExternalSystemBundle.message("run.configuration.settings.label.tasks"));
98     myTasksTextField = new JBTextField(ExternalSystemConstants.TEXT_FIELD_WIDTH_IN_COLUMNS);
99     canvas.add(myTasksLabel, ExternalSystemUiUtil.getLabelConstraints(0));
100     GridBag c = ExternalSystemUiUtil.getFillLineConstraints(0);
101     c.insets.right = myProjectPathField.getButton().getPreferredSize().width + 8 /* street magic, sorry */;
102     canvas.add(myTasksTextField, c);
103
104     myVmOptionsLabel = new JBLabel(ExternalSystemBundle.message("run.configuration.settings.label.vmoptions"));
105     myVmOptionsEditor = new RawCommandLineEditor();
106     myVmOptionsEditor.setDialogCaption(ExternalSystemBundle.message("run.configuration.settings.label.vmoptions"));
107     canvas.add(myVmOptionsLabel, ExternalSystemUiUtil.getLabelConstraints(0));
108     canvas.add(myVmOptionsEditor, ExternalSystemUiUtil.getFillLineConstraints(0));
109     myScriptParametersLabel = new JBLabel(ExternalSystemBundle.message("run.configuration.settings.label.script.parameters"));
110     myScriptParametersEditor = new RawCommandLineEditor();
111     myScriptParametersEditor.setDialogCaption(ExternalSystemBundle.message("run.configuration.settings.label.script.parameters"));
112     canvas.add(myScriptParametersLabel, ExternalSystemUiUtil.getLabelConstraints(0));
113     canvas.add(myScriptParametersEditor, ExternalSystemUiUtil.getFillLineConstraints(0));
114   }
115
116   @Override
117   public void reset() {
118     myProjectPathField.setText("");
119     myTasksTextField.setText("");
120     myVmOptionsEditor.setText("");
121     myScriptParametersEditor.setText("");
122     showUi(true);
123
124     if (myOriginalSettings == null) {
125       return;
126     }
127
128     String path = myOriginalSettings.getExternalProjectPath();
129     if (StringUtil.isEmpty(path)) {
130       path = "";
131     }
132     myProjectPathField.setText(path);
133     myTasksTextField.setText(StringUtil.join(myOriginalSettings.getTaskNames(), " "));
134     myVmOptionsEditor.setText(myOriginalSettings.getVmOptions());
135     myScriptParametersEditor.setText(myOriginalSettings.getScriptParameters());
136   }
137
138   @Override
139   public boolean isModified() {
140     if (myOriginalSettings == null) {
141       return false;
142     }
143
144     return !Comparing.equal(normalizePath(myProjectPathField.getText()),
145                             normalizePath(myOriginalSettings.getExternalProjectPath()))
146            || !Comparing.equal(normalizePath(myTasksTextField.getText()),
147                                normalizePath(StringUtil.join(myOriginalSettings.getTaskNames(), " ")))
148            || !Comparing.equal(normalizePath(myVmOptionsEditor.getText()),
149                                normalizePath(myOriginalSettings.getVmOptions()))
150            || !Comparing.equal(normalizePath(myScriptParametersEditor.getText()),
151                                normalizePath(myOriginalSettings.getScriptParameters()));
152   }
153
154   @Override
155   public void apply(@NotNull ExternalSystemTaskExecutionSettings settings) {
156     String projectPath = myProjectPathField.getText();
157     settings.setExternalProjectPath(projectPath);
158     settings.setTaskNames(StringUtil.split(myTasksTextField.getText(), " "));
159     settings.setVmOptions(myVmOptionsEditor.getText());
160     settings.setScriptParameters(myScriptParametersEditor.getText());
161   }
162
163   @Override
164   public boolean validate(@NotNull ExternalSystemTaskExecutionSettings settings) throws ConfigurationException {
165     String projectPath = myProjectPathField.getText();
166     if (myOriginalSettings == null) {
167       throw new ConfigurationException(String.format(
168         "Can't store external task settings into run configuration. Reason: target run configuration is undefined. Tasks: '%s', " +
169         "external project: '%s', vm options: '%s', script parameters: '%s'",
170         myTasksTextField.getText(), projectPath, myVmOptionsEditor.getText(), myScriptParametersEditor.getText()
171       ));
172     }
173     return true;
174   }
175
176   @Override
177   public void disposeUIResources() {
178     ExternalSystemUiUtil.disposeUi(this);
179   }
180
181   @Override
182   public void showUi(boolean show) {
183     ExternalSystemUiUtil.showUi(this, show);
184   }
185 }