目录
③CodeChronoSettingsConfigurable
插件可以创建和存储自定义设置,以使用IntelliJ平台持久性模型的方式捕获其配置。
设置可以影响不同级别的范围。本文档介绍在项目和应用程序(或全局、IDE)级别添加自定义设置
一.添加配置界面
1、添加三个必要文件
CodeChronoSettings :持久化配置类
CodeChronoSettingsComponent :组件类,用于构建对话框面板,界面UI。
CodeChronoSettingsConfigurable:定义配置面板,将界面UI注入设置中,并重写保存、重置、是否修改等方法
2、各文件详解及代码示例
①CodeChronoSettings.java
- 实现接口PersistentStateComponent
@State
:指定了组件的名称(name = "codechrono"
)和存储配置的XML文件名(storages = @Storage("codechrono-settings.xml")
)。@Service
:声明这是一个服务组件,级别为APP
,意味着它在IDEA应用程序级别上可用。
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
/**
* @author Codechrono
* 持久化插件配置文件
*/
@State(
name = "codechrono",
storages = @Storage("codechrono-settings.xml")
)
//如果没有使用轻型服务 @Service,则必须在plugin.xml文件中将持久化数据类声明为ServiceEP
@Service(Service.Level.APP)
public final class CodeChronoSettings implements PersistentStateComponent<CodeChronoSettings.State> {
public static class State {
@NonNls
public int days = 14;
public String fielRegx = ".*\\.(txt|sql)$";
public String projectRegx = "demo$|demo$";
}
private State myState = new State();
public static CodeChronoSettings getInstance() {
return ApplicationManager.getApplication().getService(CodeChronoSettings.class);
}
/**
* 点击OK的时候会执行该方法,用于获取当前的状态
* 该方法返回的状态会被保存到对应的文件中
* @return
*/
@Override
public State getState() {
return myState;
}
/**
* 该方法会在插件启动的时候执行,用于加载状态
*/
@Override
public void loadState(@NotNull State state) {
myState = state;
}
}
②CodeChronoSettingsComponent
- 初始化UI组件,添加监听器,并获取主面板
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder;
import javax.swing.*;
/**
* @author codechrono
* 组件类,用于构建和配置设置对话框中的面板。
*/
public class CodeChronoSettingsComponent {
private final JPanel mainPanel;
private final JBTextField fileRegxText = new JBTextField();
private final JBTextField projectRegxText = new JBTextField();
private final JRadioButton day14RButton = new JRadioButton("14天");
private final JRadioButton day30RButton = new JRadioButton("30天");
private final JRadioButton dayCustomizeRButton = new JRadioButton("自定义天数");
private final IntegerTextField inputCustomizeDayText = new IntegerTextField();
private final ButtonGroup group = new ButtonGroup();
public CodeChronoSettingsComponent() {
initUIComponents();
addListeners();
mainPanel = getMainPanel();
}
/**
* 获取首选的焦点组件
*/
public JComponent getPreferredFocusedComponent() {
return fileRegxText;
}
public JPanel getPanel() {
return mainPanel;
}
public void reset(CodeChronoSettings.State state) {
// 重置组件状态的代
fileRegxText.setText(state.fielRegx);
projectRegxText.setText(state.projectRegx);
if (state.days == 14) {
group.setSelected(day14RButton.getModel(), true);
inputCustomizeDayText.setEnabled(false);
} else if (state.days == 30) {
group.setSelected(day30RButton.getModel(), true);
inputCustomizeDayText.setEnabled(false);
} else {
group.setSelected(dayCustomizeRButton.getModel(), true);
inputCustomizeDayText.setText(String.valueOf(state.days));
inputCustomizeDayText.setEnabled(true);
}
}
public void apply(CodeChronoSettings.State state) {
// 这里可以添加应用组件状态的代码,例如:
state.fielRegx = fileRegxText.getText();
state.projectRegx = projectRegxText.getText();
state.days = currentDays();
CodeChronoSettings.getInstance().loadState(state);
}
/**
* 控件值是否为默认值
*
* @param state
* @return
*/
public boolean isDefault(CodeChronoSettings.State state) {
if (fileRegxText.getText().equals(state.fielRegx) && projectRegxText.getText().equals(state.projectRegx)
&& currentDays() == state.days) {
return true;
}
return false;
}
/**
* 初始化组件样式
*/
private void initUIComponents() {
group.add(day14RButton);
group.add(day30RButton);
group.add(dayCustomizeRButton);
inputCustomizeDayText.setEnabled(false);
group.setSelected(day14RButton.getModel(), true);
fileRegxText.setToolTipText("请输入正则表达式,例如:\\.(txt|sql)$");
projectRegxText.setToolTipText("请输入正则表达式,例如:demo$|demo$");
}
private JPanel getMainPanel() {
return FormBuilder.createFormBuilder()
.addLabeledComponent(new JBLabel("文件过滤:"), fileRegxText, 1, false)
.addLabeledComponent(new JBLabel("项目过滤:"), projectRegxText, 1, false)
.addLabeledComponent(new JBLabel("本地数据存储天数:"), day14RButton, 1, false)
.addLabeledComponent(new JBLabel(""), day30RButton, 1, false)
.addLabeledComponent(new JBLabel(""), dayCustomizeRButton, 1, false)
.addLabeledComponent(new JBLabel("自定义天数:"), inputCustomizeDayText, 1, false)
.addComponentFillVertically(new JPanel(), 0)
.getPanel();
}
/**
* 组件添加监听事件
*/
public void addListeners() {
dayCustomizeRButton.addActionListener(e -> dayRbuttonChangeAction());
day14RButton.addActionListener(e -> dayRbuttonChangeAction());
day30RButton.addActionListener(e -> dayRbuttonChangeAction());
}
/**
* 自定义天数按钮事件处理
*/
private void dayRbuttonChangeAction() {
if (dayCustomizeRButton.isSelected()) {
inputCustomizeDayText.setEnabled(true);
} else {
inputCustomizeDayText.setEnabled(false);
}
}
/**
* 当前选择的天数
*/
private int currentDays() {
int days = 14;
if (day14RButton.isSelected()) {
days = 14;
} else if (day30RButton.isSelected()) {
days = 30;
} else if (dayCustomizeRButton.isSelected()) {
days = Integer.parseInt(inputCustomizeDayText.getText());
}
return days;
}
}
③CodeChronoSettingsConfigurable
- 实现接口Configurable
package com.codechrono.idea.plugin.config;
import com.intellij.openapi.options.Configurable;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.util.Objects;
/**
* @author codechrono
* Configurable 用于在 IDEA 设置中展示一个配置面板,它实现了 Configurable 接口。
*/
final class CodeChronoSettingsConfigurable implements Configurable {
private CodeChronoSettingsComponent codeChronoSettingsComponent;
@Nls(capitalization = Nls.Capitalization.Title)
@Override
public String getDisplayName() {
return " CodeChrono";
}
/**
* 获取首选聚焦组件。
* @return
*/
@Override
public JComponent getPreferredFocusedComponent() {
return codeChronoSettingsComponent.getPreferredFocusedComponent();
}
@Nullable
@Override
public JComponent createComponent() {
codeChronoSettingsComponent = new CodeChronoSettingsComponent();
return codeChronoSettingsComponent.getPanel();
}
/**
* 检查设置是否被修改。
* @return
*/
@Override
public boolean isModified() {
CodeChronoSettings.State state =
Objects.requireNonNull(CodeChronoSettings.getInstance().getState());
return !codeChronoSettingsComponent.isDefault(state);
}
@Override
public void apply() {
CodeChronoSettings.State state =
Objects.requireNonNull(CodeChronoSettings.getInstance().getState());
codeChronoSettingsComponent.apply( state);
}
@Override
public void reset() {
CodeChronoSettings.State state =
Objects.requireNonNull(CodeChronoSettings.getInstance().getState());
codeChronoSettingsComponent.reset(state);
}
@Override
public void disposeUIResources() {
codeChronoSettingsComponent = null;
}
}
3、配置界面所在位置
plugin.xml文件 extensions节点
<extensions defaultExtensionNs="com.intellij">
<!-- 配置界面 -->
<!-- parentId配置界面在哪个菜单下,instance指定配置界面的类-->
<applicationConfigurable parentId="tools" instance="com.codechrono.idea.plugin.config.CodeChronoSettingsConfigurable"
id="CodeChrono.Config"
displayName="CodeChrono"/>
</extensions>
4、国际化支持
二.使用配置参数
private static CodeChronoSettings.State state = Objects.requireNonNull(CodeChronoSettings.getInstance().getState());
/**
* 清除历史数据,保留最近14天数据
*/
private static void clearHistory() {
long timestamp = System.currentTimeMillis() - (long) state.days * 24 * 60 * 60 * 1000;
editRecordInterface.deleteByDate(timestamp);
}