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