(三)IntelliJ 插件开发——Tool Windows(工

2020-03-03  本文已影响0人  秋水畏寒

官网文档

https://www.jetbrains.org/intellij/sdk/docs/user_interface_components/tool_windows.html

Github

https://github.com/kungyutucheng/my_gradle_plugin

运行环境

macOS 10.14.5
IntelliJ idea 2019.2.4

定义

maven

实现类似maven截图这样的自定义工具窗口,同一时刻只能展示一个Tool Window

demo

package com.kungyu.toolwindow;

import com.intellij.openapi.project.Project;
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 {
    @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);

    }
}

package com.kungyu.toolwindow;

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 JLabel datetimeLabel;

    private JPanel myToolWindowContent;

    public MyToolWindow(ToolWindow toolWindow) {

        init();

        hideButton.addActionListener(e -> toolWindow.hide(null));
    }

    private void init() {
        datetimeLabel = new JLabel();
        datetimeLabel.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

        hideButton = new JButton("取消");

        myToolWindowContent = new JPanel();
        myToolWindowContent.add(datetimeLabel);
        myToolWindowContent.add(hideButton);


    }

    public JPanel getContent() {
        return myToolWindowContent;
    }

}

<extensions defaultExtensionNs="com.intellij">
    <!-- secondary:true表示设置在tool window bar最下方 -->
    <toolWindow id="customer tool window" anchor="right" factoryClass="com.kungyu.toolwindow.MyToolWindowFactory" secondary="true"/>
</extensions>
效果图

动态展示tool window

package com.kungyu.toolwindow;

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 false;
    }
}
<extensions defaultExtensionNs="com.intellij">
    <!-- secondary:true表示设置在tool window bar最下方 -->
    <toolWindow id="customer tool window" anchor="right" factoryClass="com.kungyu.toolwindow.MyToolWindowFactory" secondary="true"
        conditionClass="com.kungyu.toolwindow.MyToolWindowFactory"/>
</extensions>
上一篇下一篇

猜你喜欢

热点阅读