代码源地址:
Github
参考链接:
Plugin Extension Points in IntelliJ SDK Docs
本文主要实践一下ToolWindow
这个Sample
.
intellij 编辑器下载:
idea
-
新建工程
-
Gradle build工程
-
首次搞Gradle 会download
ideaUI-2020.3
这压缩包 500来兆。
Build 完成之后,来导入 源代码Github
再Gralde build 完成就可以看到
Run,
可以在setting
里看到已经自动安装了plugin
那么,在编辑器的最下面看看我们的Sample. 点击My Calender.
下面就是运行结果图:
过程中可能遇到的问题:
- Gradle 编译出错。而错误日志是乱码的情况
解:
错误日志:
类文件具有错误的版本 55.0, 应为 52.0
这个解决方法也非常简单:
把项目使用的Java jdk 更换成新版本即可。
项目中重点的是plugin.xml
,这里存放了idea plugin 所有配置属性。
<id>
项目唯一id
<name>
name
<vendor>
作者
<description>
简介
<depends>com.intellij.modules.platform</depends>
依赖
toolWindow plugin 配置
<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<toolWindow id="My Calendar" secondary="true" icon="AllIcons.General.Modified" anchor="right"
factoryClass="com.arjun.demo.MyToolWindowFactory"/>
</extensions>
<actions>
<!-- Add your actions here -->
</actions>
MyToolWindowFactory.java
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;
public class MyToolWindowFactory implements ToolWindowFactory {
/**
* Create the tool window content.
*
* @param project current project
* @param toolWindow current tool window
*/
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(), "test_ddd", false);
toolWindow.getContentManager().addContent(content);
}
}
MyToolWindow.form
对应绑定MyToolWindow.java
其中 JButton
之类的组件也就是Java Swing方面的内容了。
public class MyToolWindow {
private JButton refreshToolWindowButton;
private JButton hideToolWindowButton;
private JLabel currentDate;
private JLabel currentTime;
private JLabel timeZone;
private JPanel myToolWindowContent;
public MyToolWindow(ToolWindow toolWindow) {
hideToolWindowButton.addActionListener(e -> toolWindow.hide(null));
refreshToolWindowButton.addActionListener(e -> currentDateTime());
this.currentDateTime();
this.other();
}
private void other() {
String gid = "gid";
Notification nf = new Notification(gid
, new ImageIcon(getClass().getResource("/toolWindow/Calendar-icon.png")
), NotificationType.INFORMATION);
nf.setContent("This is my notification");
Notifications.Bus.notify(nf);
}
public void currentDateTime() {
// Get current date and time
Calendar instance = Calendar.getInstance();
currentDate.setText(
instance.get(Calendar.DAY_OF_MONTH) + "/"
+ (instance.get(Calendar.MONTH) + 1) + "/"
+ instance.get(Calendar.YEAR)
);
currentDate.setIcon(new ImageIcon(getClass().getResource("/toolWindow/Calendar-icon.png")));
int min = instance.get(Calendar.MINUTE);
String strMin = min < 10 ? "0" + min : String.valueOf(min);
currentTime.setText(instance.get(Calendar.HOUR_OF_DAY) + ":" + strMin);
currentTime.setIcon(new ImageIcon(getClass().getResource("/toolWindow/Time-icon.png")));
// Get time zone
long gmt_Offset = instance.get(Calendar.ZONE_OFFSET); // offset from GMT in milliseconds
String str_gmt_Offset = String.valueOf(gmt_Offset / 3600000);
str_gmt_Offset = (gmt_Offset > 0) ? "GMT + " + str_gmt_Offset : "GMT - " + str_gmt_Offset;
timeZone.setText(str_gmt_Offset);
timeZone.setIcon(new ImageIcon(getClass().getResource("/toolWindow/Time-zone-icon.png")));
}
public JPanel getContent() {
return myToolWindowContent;
}
}
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ ( T | o ) ( Y | o | u | t | h | , | T | o ) ( S | i | m | p | l | e | ! ) \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/