[groovy] script configuration: support alternative JRE, rearrange UI (IDEA-135061)

This commit is contained in:
Daniil Ovchinnikov
2016-11-23 17:05:52 +03:00
parent fcadcf7820
commit 0ca03ffb2f
4 changed files with 145 additions and 153 deletions
@@ -139,7 +139,7 @@ public class JavaParametersUtil {
parameters.configureByProject(project, classPathType, createProjectJdk(project, jreHome));
}
private static Sdk createModuleJdk(final Module module, boolean productionOnly, @Nullable String jreHome) throws CantRunException {
public static Sdk createModuleJdk(final Module module, boolean productionOnly, @Nullable String jreHome) throws CantRunException {
return jreHome == null ? JavaParameters.getValidJdkToRunModule(module, productionOnly) : createAlternativeJdk(jreHome);
}
@@ -1,104 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.jetbrains.plugins.groovy.runner.GroovyRunConfigurationEditor">
<grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="9" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="9" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="329"/>
<xy x="20" y="20" width="500" height="354"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<vspacer id="41261">
<component id="dec50" class="com.intellij.openapi.ui.LabeledComponent" binding="myScriptPathComponent" default-binding="true">
<constraints>
<grid row="8" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<componentClass value="com.intellij.openapi.ui.TextFieldWithBrowseButton"/>
<labelLocation value="West"/>
<text value="Script path"/>
<verifyInputWhenFocusTarget value="false"/>
</properties>
</component>
<component id="2fdc8" class="com.intellij.execution.ui.CommonJavaParametersPanel" binding="myCommonJavaParametersPanel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<vspacer id="e3ad0">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="10"/>
</grid>
</constraints>
</vspacer>
<component id="3b65f" class="com.intellij.ui.RawCommandLineEditor" binding="myVMParameters">
<component id="9b09" class="com.intellij.openapi.ui.LabeledComponent" binding="myModulesComboBoxComponent">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="7" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="3d033" class="com.intellij.ui.RawCommandLineEditor" binding="myParameters" default-binding="true">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="7" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<grid id="27781" binding="scriptPathPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<grid id="4852" binding="workDirPanel" layout-manager="BorderLayout" hgap="0" vgap="0">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
<component id="39213" class="com.intellij.execution.configuration.EnvironmentVariablesComponent" binding="myEnvVariables">
<constraints>
<grid row="4" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="7" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<componentClass value="com.intellij.application.options.ModulesComboBox"/>
<labelLocation value="West"/>
<text value="Module"/>
</properties>
</component>
<component id="5f62b" class="com.intellij.ui.components.JBLabel" binding="myScriptParametersLabel">
<component id="9cee8" class="com.intellij.execution.ui.JrePathEditor" binding="myJrePathEditor">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Script parameters:"/>
</properties>
</component>
<component id="c5c05" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="VM options:"/>
</properties>
</component>
<component id="86e25" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="d1e7d"/>
<text value="Module:"/>
</properties>
</component>
<component id="d1e7d" class="com.intellij.application.options.ModulesComboBox" binding="myModulesBox">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="5194" class="javax.swing.JLabel">
<vspacer id="f5fd3">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="10"/>
</grid>
</constraints>
<properties>
<text value="Script path:"/>
</properties>
</component>
<component id="ae9d1" class="javax.swing.JLabel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Working directory:"/>
</properties>
</component>
</vspacer>
<component id="c934c" class="javax.swing.JCheckBox" binding="myDebugCB" default-binding="true">
<constraints>
<grid row="6" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<selected value="false"/>
@@ -107,12 +66,17 @@
</component>
<component id="62ce7" class="javax.swing.JCheckBox" binding="myAddClasspathCB">
<constraints>
<grid row="7" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Add module &amp;classpath to the runner"/>
</properties>
</component>
<vspacer id="41261">
<constraints>
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
</children>
</grid>
</form>
@@ -17,116 +17,123 @@
package org.jetbrains.plugins.groovy.runner;
import com.intellij.application.options.ModulesComboBox;
import com.intellij.execution.configuration.EnvironmentVariablesComponent;
import com.intellij.ide.util.BrowseFilesListener;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.execution.ui.CommonJavaParametersPanel;
import com.intellij.execution.ui.DefaultJreSelector.SdkFromModuleDependencies;
import com.intellij.execution.ui.JrePathEditor;
import com.intellij.execution.util.ScriptFileUtil;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.FieldPanel;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.PanelWithAnchor;
import com.intellij.ui.RawCommandLineEditor;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.ui.UIUtil;
import kotlin.jvm.functions.Function0;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.GroovyFileType;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.DocumentEvent;
public class GroovyRunConfigurationEditor extends SettingsEditor<GroovyScriptRunConfiguration> implements PanelWithAnchor {
private ModulesComboBox myModulesBox;
private JPanel myMainPanel;
private RawCommandLineEditor myVMParameters;
private RawCommandLineEditor myParameters;
private JPanel scriptPathPanel;
private JPanel workDirPanel;
private LabeledComponent<TextFieldWithBrowseButton> myScriptPathComponent;
private CommonJavaParametersPanel myCommonJavaParametersPanel;
private LabeledComponent<ModulesComboBox> myModulesComboBoxComponent;
private JrePathEditor myJrePathEditor;
private JCheckBox myDebugCB;
private EnvironmentVariablesComponent myEnvVariables;
private JBLabel myScriptParametersLabel;
private JCheckBox myAddClasspathCB;
private final JTextField scriptPathField;
private final JTextField workDirField;
private JComponent anchor;
public GroovyRunConfigurationEditor() {
private JComponent myAnchor;
scriptPathField = new JTextField();
final BrowseFilesListener scriptBrowseListener = new BrowseFilesListener(scriptPathField,
"Script Path",
"Specify path to script",
new FileChooserDescriptor(true, false, false, false, false, false) {
@Override
public boolean isFileSelectable(VirtualFile file) {
return file.getFileType() == GroovyFileType.GROOVY_FILE_TYPE;
public GroovyRunConfigurationEditor(@NotNull Project project) {
final TextFieldWithBrowseButton scriptPath = myScriptPathComponent.getComponent();
scriptPath.addBrowseFolderListener(
"Script Path", "Specify path to script", project,
FileChooserDescriptorFactory.createSingleFileDescriptor(GroovyFileType.GROOVY_FILE_TYPE)
);
final ModulesComboBox modulesComboBox = myModulesComboBoxComponent.getComponent();
modulesComboBox.addActionListener(e -> myCommonJavaParametersPanel.setModuleContext(modulesComboBox.getSelectedModule()));
ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
Function0<Boolean> productionOnly = () -> {
VirtualFile script = ScriptFileUtil.findScriptFileByPath(scriptPath.getText());
return script != null && !fileIndex.isInTestSourceContent(script);
};
myJrePathEditor.setDefaultJreSelector(new SdkFromModuleDependencies(modulesComboBox, productionOnly) {
@Override
public void addChangeListener(@NotNull Runnable listener) {
super.addChangeListener(listener);
scriptPath.getChildComponent().getDocument().addDocumentListener(
new DocumentAdapter() {
@Override
protected void textChanged(DocumentEvent e) {
listener.run();
}
}
});
final FieldPanel scriptFieldPanel = new FieldPanel(scriptPathField, null, null, scriptBrowseListener, null);
scriptPathPanel.setLayout(new BorderLayout());
scriptPathPanel.add(scriptFieldPanel, BorderLayout.CENTER);
workDirField = new JTextField();
final BrowseFilesListener workDirBrowseFilesListener = new BrowseFilesListener(workDirField,
"Working directory",
"Specify working directory",
BrowseFilesListener.SINGLE_DIRECTORY_DESCRIPTOR);
final FieldPanel workDirFieldPanel = new FieldPanel(workDirField, null, null, workDirBrowseFilesListener, null);
workDirPanel.setLayout(new BorderLayout());
workDirPanel.add(workDirFieldPanel, BorderLayout.CENTER);
setAnchor(myEnvVariables.getLabel());
);
}
});
myAnchor = UIUtil.mergeComponentsWithAnchor(
myScriptPathComponent,
myCommonJavaParametersPanel,
myModulesComboBoxComponent,
myJrePathEditor
);
}
@Override
public void resetEditorFrom(@NotNull GroovyScriptRunConfiguration configuration) {
myVMParameters.setDialogCaption("VM Options");
myVMParameters.setText(configuration.getVMParameters());
myScriptPathComponent.getComponent().setText(configuration.getScriptPath());
myCommonJavaParametersPanel.reset(configuration);
myParameters.setDialogCaption("Script Parameters");
myParameters.setText(configuration.getProgramParameters());
myModulesComboBoxComponent.getComponent().setModules(configuration.getValidModules());
myModulesComboBoxComponent.getComponent().setSelectedModule(configuration.getConfigurationModule().getModule());
myJrePathEditor.setPathOrName(configuration.getAlternativeJrePath(), configuration.isAlternativeJrePathEnabled());
scriptPathField.setText(configuration.getScriptPath());
workDirField.setText(configuration.getWorkingDirectory());
myDebugCB.setEnabled(true);
myDebugCB.setSelected(configuration.isDebugEnabled());
myAddClasspathCB.setSelected(configuration.isAddClasspathToTheRunner());
myModulesBox.setModules(configuration.getValidModules());
myModulesBox.setSelectedModule(configuration.getConfigurationModule().getModule());
myEnvVariables.setEnvs(configuration.getEnvs());
}
@Override
public void applyEditorTo(@NotNull GroovyScriptRunConfiguration configuration) throws ConfigurationException {
configuration.setModule(myModulesBox.getSelectedModule());
configuration.setVMParameters(myVMParameters.getText());
configuration.setScriptPath(myScriptPathComponent.getComponent().getText().trim());
myCommonJavaParametersPanel.applyTo(configuration);
configuration.setModule(myModulesComboBoxComponent.getComponent().getSelectedModule());
configuration.setAlternativeJrePathEnabled(myJrePathEditor.isAlternativeJreSelected());
configuration.setAlternativeJrePath(myJrePathEditor.getJrePathOrName());
configuration.setDebugEnabled(myDebugCB.isSelected());
configuration.setAddClasspathToTheRunner(myAddClasspathCB.isSelected());
configuration.setProgramParameters(myParameters.getText());
configuration.setScriptPath(scriptPathField.getText().trim());
configuration.setWorkingDirectory(workDirField.getText().trim());
configuration.setEnvs(myEnvVariables.getEnvs());
}
@Override
@NotNull
public JComponent createEditor() {
myDebugCB.setEnabled(true);
myDebugCB.setSelected(false);
return myMainPanel;
}
@Override
public JComponent getAnchor() {
return anchor;
return myAnchor;
}
@Override
public void setAnchor(JComponent anchor) {
this.anchor = anchor;
myScriptParametersLabel.setAnchor(anchor);
myEnvVariables.setAnchor(anchor);
myAnchor = anchor;
myScriptPathComponent.setAnchor(anchor);
myCommonJavaParametersPanel.setAnchor(anchor);
myModulesComboBoxComponent.setAnchor(anchor);
myJrePathEditor.setAnchor(anchor);
}
}
@@ -24,6 +24,7 @@ import com.intellij.execution.process.OSProcessHandler;
import com.intellij.execution.process.ProcessAdapter;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.util.JavaParametersUtil;
import com.intellij.execution.util.ProgramParametersUtil;
import com.intellij.execution.util.ScriptFileUtil;
import com.intellij.openapi.application.ApplicationManager;
@@ -38,6 +39,7 @@ import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.JDOMExternalizer;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
@@ -82,6 +84,9 @@ public class GroovyScriptRunConfiguration extends ModuleBasedConfiguration<RunCo
private final Map<String, String> envs = new LinkedHashMap<>();
public boolean passParentEnv = true;
private boolean myAlternativeJrePathEnabled;
private @Nullable String myAlternativeJrePath;
public GroovyScriptRunConfiguration(final String name, final Project project, final ConfigurationFactory factory) {
super(name, new RunConfigurationModule(project), factory);
workDir = PathUtil.getLocalPath(project.getBaseDir());
@@ -142,6 +147,9 @@ public class GroovyScriptRunConfiguration extends ModuleBasedConfiguration<RunCo
isAddClasspathToTheRunner = Boolean.parseBoolean(JDOMExternalizer.readString(element, "addClasspath"));
envs.clear();
JDOMExternalizer.readMap(element, envs, null, "env");
myAlternativeJrePathEnabled = JDOMExternalizer.readBoolean(element, "alternativeJrePathEnabled");
myAlternativeJrePath = JDOMExternalizer.readString(element, "alternativeJrePath");
}
@Override
@@ -155,6 +163,11 @@ public class GroovyScriptRunConfiguration extends ModuleBasedConfiguration<RunCo
JDOMExternalizer.write(element, "debug", isDebugEnabled);
if (isAddClasspathToTheRunner) JDOMExternalizer.write(element, "addClasspath", true);
JDOMExternalizer.writeMap(element, envs, null, "env");
if (myAlternativeJrePathEnabled) {
JDOMExternalizer.write(element, "alternativeJrePathEnabled", true);
if (StringUtil.isNotEmpty(myAlternativeJrePath)) JDOMExternalizer.write(element, "alternativeJrePath", myAlternativeJrePath);
}
}
@Override
@@ -189,7 +202,14 @@ public class GroovyScriptRunConfiguration extends ModuleBasedConfiguration<RunCo
protected JavaParameters createJavaParameters() throws ExecutionException {
final Module module = getModule();
final boolean tests = ProjectRootManager.getInstance(getProject()).getFileIndex().isInTestSourceContent(scriptFile);
JavaParameters params = createJavaParametersWithSdk(module);
String jrePath = isAlternativeJrePathEnabled() ? getAlternativeJrePath() : null;
JavaParameters params = new JavaParameters();
params.setUseClasspathJar(true);
params.setDefaultCharset(getProject());
params.setJdk(
module == null ? JavaParametersUtil.createProjectJdk(getProject(), jrePath)
: JavaParametersUtil.createModuleJdk(module, !tests, jrePath)
);
ProgramParametersUtil.configureConfiguration(params, GroovyScriptRunConfiguration.this);
scriptRunner.configureCommandLine(params, module, tests, scriptFile, GroovyScriptRunConfiguration.this);
@@ -267,7 +287,7 @@ public class GroovyScriptRunConfiguration extends ModuleBasedConfiguration<RunCo
@Override
@NotNull
public SettingsEditor<? extends RunConfiguration> getConfigurationEditor() {
return new GroovyRunConfigurationEditor();
return new GroovyRunConfigurationEditor(getProject());
}
@Override
@@ -297,6 +317,7 @@ public class GroovyScriptRunConfiguration extends ModuleBasedConfiguration<RunCo
else {
throw new RuntimeConfigurationWarning(GroovyBundle.message("script.file.is.not.groovy.file"));
}
JavaParametersUtil.checkAlternativeJRE(this);
}
@Override
@@ -311,23 +332,23 @@ public class GroovyScriptRunConfiguration extends ModuleBasedConfiguration<RunCo
@Override
public boolean isAlternativeJrePathEnabled() {
return false;
return myAlternativeJrePathEnabled;
}
@Override
public void setAlternativeJrePathEnabled(boolean enabled) {
throw new UnsupportedOperationException();
public void setAlternativeJrePathEnabled(boolean alternativeJrePathEnabled) {
myAlternativeJrePathEnabled = alternativeJrePathEnabled;
}
@Nullable
@Override
public String getAlternativeJrePath() {
throw new UnsupportedOperationException();
return myAlternativeJrePath;
}
@Override
public void setAlternativeJrePath(String path) {
throw new UnsupportedOperationException();
public void setAlternativeJrePath(@Nullable String alternativeJrePath) {
myAlternativeJrePath = alternativeJrePath;
}
@Override