idea插件之打开ToolWindow并写入内容

今天就给大家介绍一下怎么自定义toolWindow里面的内容。如:创建一个树,可以自定义里面的内容,并且刷新树

1.前面两篇讲解了创建一个按钮,今天来将一下创建一个toolWindow并添加里面的内容

编写插件离不开plugin.xml,在插件中配置自己的toolWindow,此时自己配置的toolWindow已经打开。

<extensions defaultExtensionNs="com.intellij">
  <!-- Add your extensions here -->
  <!-- 自定义控制台输入 -->
  <!--canCloseContents 允许用户关闭-->
  <toolWindow canCloseContents="true" anchor="bottom"
              id="JDCompute Code Lines"
              factoryClass="MyBottomWin">
  </toolWindow>

</extensions>

运行toolWindow,并按下图打开,你会看到右下角有自定义的toolWindow;



2.接下来讲一下点击打开自定义的toolWindow

{
    if (e.getProject() != null) {
        // 将项目对象,ToolWindowid传入,获取控件对象
        ToolWindow toolWindow = ToolWindowManager.getInstance(e.getProject()).getToolWindow("JDCompute Code Lines");
        if (toolWindow != null) {
            // 无论当前状态为关闭/打开,进行强制打开ToolWindow
            toolWindow.show(new Runnable() {
                @Override
                public void run() {

                }
            });
        }
    }

}

从代码中可以看出,涉及到了单例模式以及线程,再传project的时候,小编也是猜的,不知道传什么值,看到网上的一些例子只是传入了文本数据。

这样就可以实现点击的时候打开自定义的toolWindow

3.在toolWindow上写入一棵下拉的树,并可以增加子菜单。

在配置文件中可以看到MyBottomWin这个工厂类,他实现了ToolWindowFactory接口,

@Override
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
   // myToolWindow=toolWindow;
    ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
    Content content = contentFactory.createContent(mainPanel,"", false);
    toolWindow.getContentManager().addContent(content);
}

接口实现以后就可以在重写的方法里面定义自己的面板等。

然后初始化这个类

public MyBottomWin() {
    //定义字体
    Font fnt = new Font("Microsoft YaHei UI", Font.PLAIN, 15);
    //找到字体的资源管理
    FontUIResource fontRes = new FontUIResource(fnt);
    for (Enumeration keys = UIManager.getDefaults().keys(); keys.hasMoreElements(); ) {
        Object key = keys.nextElement();
        Object value = UIManager.get(key);
        if (value instanceof FontUIResource)
            UIManager.put(key, fontRes);
    }
    //定义tree 的根目录
    DefaultMutableTreeNode  root = new DefaultMutableTreeNode("京东集团");
    //定义根节点下面的子节点
    DefaultMutableTreeNode n1 = new DefaultMutableTreeNode("京东研发");
    DefaultMutableTreeNode n2 = new DefaultMutableTreeNode("京东行政");
    DefaultMutableTreeNode n3 = new DefaultMutableTreeNode("京东物流");
    DefaultMutableTreeNode n4 = new DefaultMutableTreeNode("京东金融");
    DefaultMutableTreeNode n5 = new DefaultMutableTreeNode("京东商城");
    DefaultMutableTreeNode n6 = new DefaultMutableTreeNode("京东财务");
    root.add(n1);
    root.add(n2);
    root.add(n3);
    root.add(n4);
    root.add(n5);
    root.add(n6);
    //构造一个treeModel 对象,进行刷新树操作
    dt = new DefaultTreeModel(root);
    tree1 = new JTree(dt);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //得到屏幕的尺寸
    //设置主面板的大小
    mainPanel.setPreferredSize(new Dimension((int)screenSize.getWidth()-50, (int) screenSize.getHeight()/3*2));
    //tree 设置大小
    tree1.setPreferredSize(new Dimension((int)screenSize.getWidth()-50, (int) screenSize.getHeight()/3*2));
   ///构造一个 有滚动条的面板
    scrollPane = new JScrollPane();
    //设置滚动条面板位置
    scrollPane.setPreferredSize(new Dimension((int)screenSize.getWidth()-50, (int) screenSize.getHeight()/3*2-50));
    //tree添加道滚动条面板上
    scrollPane.setViewportView(tree1);
    //将滚动条面板设置哼可见
    scrollPane.setVisible(true);
    //设置滚动条的滚动速度
    scrollPane.getVerticalScrollBar().setUnitIncrement(15);
    //解决闪烁问题
    scrollPane.getVerticalScrollBar().setDoubleBuffered(true);
    mainPanel=new JPanel();
    mainPanel.add(scrollPane);
    tree1.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            if (e.getButton() == e.BUTTON3) { //BUTTON3是鼠标右键
                //获取点击的tree节点
                DefaultMutableTreeNode note = (DefaultMutableTreeNode) tree1.getLastSelectedPathComponent();
                if(note!=null) {
                    addTreeChilden(note);
                }
            }
        }
    });
}

运行的效果


好了,插件的介绍到此为止,有缘再写。

在 IntelliJ IDEA 中,可以通过创建 ToolWindow 来向应用程序添加小窗口。ToolWindow 是一个浮动窗口,可以显示用于特定任务的工具窗格。可以使用 IntelliJ IDEA 的插件 API 来创建 ToolWindow 并添加小窗口。 下面是通过插件 API 在 ToolWindow 中添加小窗口的步骤: 1. 创建一个插件项目并添加插件依赖项。 2. 创建一个类来表示 ToolWindow。 ```java public class MyToolWindowFactory implements ToolWindowFactory { // 实现 ToolWindowFactory 接口 // ... } ``` 3. 在 `createToolWindowContent()` 方法中创建一个 JPanel 来表示 ToolWindow 内容。 ```java public class MyToolWindowFactory implements ToolWindowFactory { public void createToolWindowContent(Project project, ToolWindow toolWindow) { // 创建 JPanel JPanel panel = new JPanel(new BorderLayout()); // 在 JPanel 中添加其他组件 JLabel label = new JLabel("This is my tool window"); panel.add(label, BorderLayout.CENTER); // 将 JPanel 添加到 ToolWindow 中 ContentFactory contentFactory = ContentFactory.SERVICE.getInstance(); Content content = contentFactory.createContent(panel, "", false); toolWindow.getContentManager().addContent(content); } // 实现 ToolWindowFactory 接口的其他方法 // ... } ``` 在 `createToolWindowContent()` 方法中,创建一个 JPanel 并添加其他组件。然后,使用 `ContentFactory` 类创建一个新的 Content 对象,并将 JPanel 添加到该对象中。最后,将 Content 添加到 ToolWindow 的 ContentManager 中。 4. 将 ToolWindow 注册到插件中。 ```java public class MyPlugin implements DumbAware, ApplicationComponent { public void initComponent() { // 注册 ToolWindow ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project); toolWindowManager.registerToolWindow("MyToolWindow", false, ToolWindowAnchor.BOTTOM); } // 实现 ApplicationComponent 接口的其他方法 // ... } ``` 在 `initComponent()` 方法中,使用 `ToolWindowManager` 类将 ToolWindow 注册到 IntelliJ IDEA 中。可以使用 `registerToolWindow()` 方法指定 ToolWindow 的名称、是否可关闭和锚点位置。 这样,就可以在 IntelliJ IDEA 中创建一个 ToolWindow 并向其中添加小窗口了。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值