Application level components
- 级别:idea 应用级别组件,在 idea 启动完成时即开始工作
- 使用:有些教程中会说实现
ApplicationComponent
接口,但是该接口在新版中已经弃用,需要使用initComponent()
方法则可以通过MessageBus
来监听容器初始化完成的事件做相应的操作,需要使用disposeComponent()
方法则可以通过实现Disposable
接口来实现。这里我们用一个简单的方法来演示一下,实现BaseComponent
接口。 - samples
public class MyApplicationComponent implements BaseComponent {
private final String COMPONENT_NAME = "ZhangkaiT1ApplicationName";
@NotNull
public static MyApplicationComponent getInstance() {
return ApplicationManager.getApplication().getComponent(MyApplicationComponent.class);
}
@Override
public void initComponent() {
NotificationGroup group = new NotificationGroup("MyPluginNotification.UpdateCheck", NotificationDisplayType.BALLOON, true);
Notification notification = group.createNotification("test", MessageType.INFO);
Notifications.Bus.notify(notification);
}
@Override
public void disposeComponent() {
}
@NotNull
@Override
public String getComponentName() {
return COMPONENT_NAME;
}
}
在 plugin.xml
中添加
<application-components>
<component>
<implementation-class>dev.zhangkai.t1.action.components.MyApplicationComponent</implementation-class>
</component>
</application-components>
上面的代码会在 idea 启动时生效
Project level components
- 级别:项目级别,在项目打开时生效
- 使用:通过实现
ProjectComponent
- samples:
public class MyProjectComponent implements ProjectComponent {
private final String COMPONENT_NAME = "ZhangkaiT1ProjectComponent";
private final int VERSION = 100;
private final OkHttpClient client = new OkHttpClient();
private final Request versionRequest = new Request.Builder().get().url("http://127.0.0.1:9000/version").build();
private NotificationGroup group = new NotificationGroup("MyPluginNotification.UpdateCheck", NotificationDisplayType.BALLOON, true);
private Project project;
protected MyProjectComponent(@NotNull Project project) {
this.project = project;
}
/**
* 简单的插件升级检测
*/
@Override
public void projectOpened() {
Call call = client.newCall(versionRequest);
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
notifyMessage("please check your network connection", NotificationType.ERROR);
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
@NotNull String body = response.body().string();
int version = Integer.valueOf(body);
if (version > VERSION) {
notifyMessage("your plugin need update", NotificationType.INFORMATION);
}
}
});
}
@Override
public void projectClosed() {
}
@NotNull
@Override
public String getComponentName() {
return COMPONENT_NAME;
}
private void notifyMessage(String message, NotificationType type) {
Notification notification = group.createNotification("MyPlugin", "update check", message, type);
Notifications.Bus.notify(notification, project);
}
}
服务端是用go写的一个简单的接口,模拟返回当前最新版本号
func main() {
http.HandleFunc("/version", pluginHandler)
log.Fatal(http.ListenAndServe(":9000", nil))
}
func pluginHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "100")
}
实现的效果
Module level components
模块级别的组件,类似于前面的,各位看官自己尝试一下就会用了,不多赘述。