(四)IntelliJ 插件开发——Dialog(对话框)
2020-03-03 本文已影响0人
秋水畏寒
MyToolWindow 和 MyToolWindowFactory 只是辅助展示dialog wrapper,核心是CustomDialogWrapper
官方文档
https://www.jetbrains.org/intellij/sdk/docs/user_interface_components/dialog_wrapper.html
Github
https://github.com/kungyutucheng/my_gradle_plugin
运行环境
macOS 10.14.5
IntelliJ idea 2019.2.4
MyToolWindow
package com.kungyu.dialog;
import com.intellij.openapi.wm.ToolWindow;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author wengyongcheng
* @since 2020/3/1 10:30 下午
*/
public class MyToolWindow {
private JButton hideButton;
private JButton dialogButton;
private JPanel myToolWindowContent;
public MyToolWindow(ToolWindow toolWindow) {
init();
hideButton.addActionListener(e -> toolWindow.hide(null));
}
private void init() {
dialogButton = new JButton("触发自定义dialog");
dialogButton.addActionListener(e -> {
if (new CustomDialogWrapper().showAndGet()) {
// 监听弹框消失,相当于show 和 getExitCode方法的结合
System.out.println("show and get");
}
});
hideButton = new JButton("取消");
myToolWindowContent = new JPanel();
myToolWindowContent.add(dialogButton);
myToolWindowContent.add(hideButton);
}
public JPanel getContent() {
return myToolWindowContent;
}
}
MyToolWindowFactory
package com.kungyu.dialog;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import org.jetbrains.annotations.NotNull;
/**
* @author wengyongcheng
* @since 2020/3/1 10:31 下午
*/
public class MyToolWindowFactory implements ToolWindowFactory, Condition<Project> {
/**
* 创建 tool window
* @param project
* @param toolWindow
*/
@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
MyToolWindow myToolWindow = new MyToolWindow(toolWindow);
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
Content content = contentFactory.createContent(myToolWindow.getContent(), "自定义tool window", false);
toolWindow.getContentManager().addContent(content);
}
/**
* 控制tool window是否展示
* @param project
* @return
*/
@Override
public boolean value(Project project) {
return true;
}
}
plugin.xml
<extensions defaultExtensionNs="com.intellij">
<!-- secondary:true表示设置在tool window bar最下方 -->
<toolWindow id="customer tool window" anchor="right" factoryClass="com.kungyu.dialog.MyToolWindowFactory" secondary="true"
conditionClass="com.kungyu.dialog.MyToolWindowFactory"/>
</extensions>
CustomDialogWrapper
package com.kungyu.dialog;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
/**
* @author wengyongcheng
* @since 2020/3/2 10:36 下午
*/
public class CustomDialogWrapper extends DialogWrapper {
private JTextField inputTextField;
private CustomOKAction okAction;
private DialogWrapperExitAction exitAction;
public CustomDialogWrapper() {
super(true);
init();
setTitle("自定义dialog");
}
/**
* 创建视图
* @return
*/
@Nullable
@Override
protected JComponent createCenterPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("这就是自定义dialog");
label.setPreferredSize(new Dimension(100,100));
panel.add(label,BorderLayout.CENTER);
inputTextField = new JTextField();
panel.add(inputTextField, BorderLayout.NORTH);
return panel;
}
/**
* 校验数据
* @return 通过必须返回null,不通过返回一个 ValidationInfo 信息
*/
@Nullable
@Override
protected ValidationInfo doValidate() {
String text = inputTextField.getText();
if(StringUtils.isNotBlank(text)) {
return null;
} else {
return new ValidationInfo("校验不通过");
}
}
/**
* 覆盖默认的ok/cancel按钮
* @return
*/
@NotNull
@Override
protected Action[] createActions() {
exitAction = new DialogWrapperExitAction("自定义cancel按钮", CANCEL_EXIT_CODE);
okAction = new CustomOKAction();
// 设置默认的焦点按钮
okAction.putValue(DialogWrapper.DEFAULT_ACTION, true);
return new Action[]{okAction,exitAction};
}
/**
* 自定义 ok Action
*/
protected class CustomOKAction extends DialogWrapperAction {
protected CustomOKAction() {
super("自定义ok按钮");
}
@Override
protected void doAction(ActionEvent e) {
// 点击ok的时候进行数据校验
ValidationInfo validationInfo = doValidate();
if (validationInfo != null) {
Messages.showMessageDialog(validationInfo.message,"校验不通过", Messages.getInformationIcon());
} else {
close(CANCEL_EXIT_CODE);
}
}
}
}
效果
初始样式校验不通过
校验通过dialog消失
默认效果
初始样式 校验不通过校验通过dialog消失