gradle: add CLI-style task execution action
[idea/community.git] / plugins / maven / src / main / java / org / jetbrains / idea / maven / execution / MavenRunnerParametersPanel.java
1 /*
2  * Copyright 2000-2009 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 org.jetbrains.idea.maven.execution;
17
18 import com.intellij.codeInsight.completion.CompletionResultSet;
19 import com.intellij.codeInsight.lookup.LookupElementBuilder;
20 import com.intellij.execution.configurations.ParametersList;
21 import com.intellij.icons.AllIcons;
22 import com.intellij.openapi.externalSystem.service.execution.cmd.ParametersListLexer;
23 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
24 import com.intellij.openapi.project.Project;
25 import com.intellij.openapi.ui.FixedSizeButton;
26 import com.intellij.openapi.ui.LabeledComponent;
27 import com.intellij.openapi.ui.TextFieldWithBrowseButton;
28 import com.intellij.openapi.vfs.VirtualFile;
29 import com.intellij.ui.EditorTextField;
30 import com.intellij.ui.PanelWithAnchor;
31 import com.intellij.ui.components.JBLabel;
32 import com.intellij.util.TextFieldCompletionProvider;
33 import com.intellij.util.execution.ParametersListUtil;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.idea.maven.model.MavenConstants;
36 import org.jetbrains.idea.maven.project.MavenProjectsManager;
37
38 import javax.swing.*;
39 import java.util.LinkedHashMap;
40 import java.util.List;
41 import java.util.Map;
42
43 /**
44  * @author Vladislav.Kaznacheev
45  */
46 public class MavenRunnerParametersPanel implements PanelWithAnchor {
47   private JPanel panel;
48   protected LabeledComponent<TextFieldWithBrowseButton> workingDirComponent;
49   protected LabeledComponent<EditorTextField> goalsComponent;
50   private LabeledComponent<EditorTextField> profilesComponent;
51   private JBLabel myFakeLabel;
52   private JCheckBox myResolveToWorkspaceCheckBox;
53   private FixedSizeButton showProjectTreeButton;
54   private JComponent anchor;
55
56   public MavenRunnerParametersPanel(@NotNull final Project project) {
57     workingDirComponent.getComponent().addBrowseFolderListener(
58       RunnerBundle.message("maven.select.maven.project.file"), "", project,
59       new FileChooserDescriptor(false, true, false, false, false, false) {
60         @Override
61         public boolean isFileSelectable(VirtualFile file) {
62           if (!super.isFileSelectable(file)) return false;
63           return file.findChild(MavenConstants.POM_XML) != null;
64         }
65       });
66
67     if (!project.isDefault()) {
68       TextFieldCompletionProvider profilesCompletionProvider = new TextFieldCompletionProvider(true) {
69         @Override
70         protected final void addCompletionVariants(@NotNull String text, int offset, @NotNull String prefix, @NotNull CompletionResultSet result) {
71           MavenProjectsManager manager = MavenProjectsManager.getInstance(project);
72           for (String profile : manager.getAvailableProfiles()) {
73             result.addElement(LookupElementBuilder.create(ParametersListUtil.join(profile)));
74           }
75         }
76
77         @NotNull
78         @Override
79         protected String getPrefix(@NotNull String currentTextPrefix) {
80           ParametersListLexer lexer = new ParametersListLexer(currentTextPrefix);
81           while (lexer.nextToken()) {
82             if (lexer.getTokenEnd() == currentTextPrefix.length()) {
83               String prefix = lexer.getCurrentToken();
84               if (prefix.startsWith("-") || prefix.startsWith("!")) {
85                 prefix = prefix.substring(1);
86               }
87               return prefix;
88             }
89           }
90
91           return "";
92         }
93       };
94
95       profilesComponent.setComponent(profilesCompletionProvider.createEditor(project));
96
97       goalsComponent.setComponent(new MavenArgumentsCompletionProvider(project).createEditor(project));
98     }
99
100     showProjectTreeButton.setIcon(AllIcons.Actions.Module);
101
102     MavenSelectProjectPopup.attachToWorkingDirectoryField(MavenProjectsManager.getInstance(project),
103                                                           workingDirComponent.getComponent().getTextField(),
104                                                           showProjectTreeButton,
105                                                           goalsComponent.getComponent());
106
107     setAnchor(profilesComponent.getLabel());
108   }
109
110   public JComponent createComponent() {
111     return panel;
112   }
113
114   public void disposeUIResources() {
115   }
116
117   public String getDisplayName() {
118     return RunnerBundle.message("maven.runner.parameters.title");
119   }
120
121   protected void setData(final MavenRunnerParameters data) {
122     data.setWorkingDirPath(workingDirComponent.getComponent().getText());
123     data.setGoals(ParametersListUtil.parse(goalsComponent.getComponent().getText()));
124     data.setResolveToWorkspace(myResolveToWorkspaceCheckBox.isSelected());
125
126     Map<String, Boolean> profilesMap = new LinkedHashMap<String, Boolean>();
127
128     List<String> profiles = ParametersListUtil.parse(profilesComponent.getComponent().getText());
129
130     for (String profile : profiles) {
131       Boolean isEnabled = true;
132       if (profile.startsWith("-") || profile.startsWith("!")) {
133         profile = profile.substring(1);
134         if (profile.isEmpty()) continue;
135
136         isEnabled = false;
137       }
138
139       profilesMap.put(profile, isEnabled);
140     }
141     data.setProfilesMap(profilesMap);
142   }
143
144   protected void getData(final MavenRunnerParameters data) {
145     workingDirComponent.getComponent().setText(data.getWorkingDirPath());
146     goalsComponent.getComponent().setText(ParametersList.join(data.getGoals()));
147     myResolveToWorkspaceCheckBox.setSelected(data.isResolveToWorkspace());
148
149     ParametersList parametersList = new ParametersList();
150
151     for (Map.Entry<String, Boolean> entry : data.getProfilesMap().entrySet()) {
152       String profileName = entry.getKey();
153
154       if (!entry.getValue()) {
155         profileName = '-' + profileName;
156       }
157
158       parametersList.add(profileName);
159     }
160
161     profilesComponent.getComponent().setText(parametersList.getParametersString());
162   }
163
164   @Override
165   public JComponent getAnchor() {
166     return anchor;
167   }
168
169   @Override
170   public void setAnchor(JComponent anchor) {
171     this.anchor = anchor;
172     workingDirComponent.setAnchor(anchor);
173     goalsComponent.setAnchor(anchor);
174     profilesComponent.setAnchor(anchor);
175     myFakeLabel.setAnchor(anchor);
176   }
177 }