官方文档
https://www.jetbrains.org/intellij/sdk/docs/tutorials/run_configurations.html
Github
https://github.com/kungyutucheng/my_gradle_plugin
运行环境
macOS 10.14.5
IntelliJ idea 2019.2.4
效果
Demo
1、注册ConfigurationType
<extensions defaultExtensionNs="com.intellij">
<configurationType implementation="com.kungyu.run.configuration.DemoConfigurationType"/>
</extensions>
2、创建DemoConfigurationType,使之实现ConfigurationType接口
package com.kungyu.run.configuration;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.execution.configurations.ConfigurationType;
import com.intellij.icons.AllIcons;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
/**
* @author wengyongcheng
* @since 2020/3/10 4:59 下午
*/
public class DemoConfigurationType implements ConfigurationType {
@NotNull
@Override
public String getDisplayName() {
return "DemoRunConfiguration";
}
@Nls
@Override
public String getConfigurationTypeDescription() {
return "Demo Run Configuration Desc";
}
@Override
public Icon getIcon() {
return AllIcons.General.Information;
}
@NotNull
@Override
public String getId() {
return "DemoRunConfiguration";
}
@Override
public ConfigurationFactory[] getConfigurationFactories() {
return new ConfigurationFactory[]{new DemoConfigurationFactory(this)};
}
}
3、创建DemoConfigurationFactory,使之继承ConfigurationFactory
package com.kungyu.run.configuration;
import com.intellij.execution.configurations.ConfigurationFactory;
import com.intellij.execution.configurations.ConfigurationType;
import com.intellij.execution.configurations.RunConfiguration;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
/**
* @author wengyongcheng
* @since 2020/3/10 5:01 下午
*/
public class DemoConfigurationFactory extends ConfigurationFactory {
private static final String FACTORY_NAME = "Demo configuration factory";
protected DemoConfigurationFactory(@NotNull ConfigurationType type) {
super(type);
}
@NotNull
@Override
public RunConfiguration createTemplateConfiguration(@NotNull Project project) {
return new DemoRunConfiguration(project,this,"Demo");
}
@NotNull
@Override
public String getName() {
return FACTORY_NAME;
}
}
4、创建DemoRunConfiguration,使之继承RunConfigurationBase
package com.kungyu.run.configuration;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.Executor;
import com.intellij.execution.configurations.*;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author wengyongcheng
* @since 2020/3/10 5:03 下午
*/
public class DemoRunConfiguration extends RunConfigurationBase {
protected DemoRunConfiguration(@NotNull Project project, @Nullable ConfigurationFactory factory, @Nullable String name) {
super(project, factory, name);
}
@NotNull
@Override
public SettingsEditor<? extends RunConfiguration> getConfigurationEditor() {
return new DemoSettingsEditor();
}
@Override
public void checkConfiguration() throws RuntimeConfigurationException {
}
@Nullable
@Override
public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment environment) throws ExecutionException {
return null;
}
}
5、创建UI Form
需先安装UI Designer插件,也可以不安装,直接在java代码中实例化组件(缺陷是样式调整比较繁琐),下文与官网保持一致采用UI Designer插件
6、完善DemoSettingsEditor
package com.kungyu.run.configuration;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
/**
* @author wengyongcheng
* @since 2020/3/11 11:29 上午
*/
public class DemoSettingsEditor extends SettingsEditor<DemoRunConfiguration> {
private JPanel panel;
private LabeledComponent<TextFieldWithBrowseButton> scriptName;
private void createUIComponents() {
scriptName = new LabeledComponent<>();
scriptName.setComponent(new TextFieldWithBrowseButton());
}
@Override
protected void resetEditorFrom(@NotNull DemoRunConfiguration s) {
}
@Override
protected void applyEditorTo(@NotNull DemoRunConfiguration s) throws ConfigurationException {
}
@NotNull
@Override
protected JComponent createEditor() {
return panel;
}
}
其中,panel
和scriptName
的实例化代码交由UI Designer Plugin在编译期间生成,所以无需增加对应的代码