Communicate between two Plugins through MessageBus?
Hi,
Is it possible to send messages between Plugins?
This is what ive tried: I Implemented this Class on every Plugin. The #1 trigger() and #2 receive via listen() (Theoretically ;-) )
public class Notification {
/**
* Notify
* @param title
* @param message
*/
public static void notify(String title, String message) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Notifications.Bus.notify(new com.intellij.notification.Notification("pra", title + ".", message + ".", NotificationType.INFORMATION));
}
});
}
/**
* Listen to Messages
* @param project
*/
public static void listen(Project project) {
project.getMessageBus().connect().subscribe(ChangeActionNotifier.CHANGE_ACTION_TOPIC, new ChangeActionNotifier() {
@Override
public void doAction(String context) {
// Action...
Notification.notify("test", context);
}
});
}
/**
* Trigger Messages
* @param context
* @param project
*/
public static void trigger(String context, Project project) {
ChangeActionNotifier publisher = project.getMessageBus().syncPublisher(ChangeActionNotifier.CHANGE_ACTION_TOPIC);
publisher.doAction(context);
}
}
/**
* Interface
*/
interface ChangeActionNotifier {
Topic<ChangeActionNotifier> CHANGE_ACTION_TOPIC = Topic.create("custom name", ChangeActionNotifier.class);
void doAction(String context);
}
请先登录再写评论。
Yes, of course it's possible provided that the topic is visible from both plugins.
Sorry i dont understand you.
Ive Implemented the "Notification"-Class in both Plugins. this is my understanding of visible in both Plugins.
Plugin#1 send a message and i expect a "catch" when i listen in the Plugin#2
...Within once Plugin the Implemantation works...
...Or what do YOU mean?
ChangeActionNotifier should be the same class in both plugins. If it's implemented in both of them, it'll be loaded with different class loaders, and there will be two different classes. So one plugin should probably depend on the other, and the class should be declared in the latter one.
Hmmm.. What i have to do exactly?
(Sorry, but im Java beginner)
Yes, "depends" tag should be sufficient.
No Success :-/
>> So one plugin should probably depend on the other, and the class should be declared in the latter one.
How do i declare classes in this case?
The classes should be public if other plugins need to access them. Otherwise there's nothing special about them.
No Success at all....
Ive broken down a test plugin - please have a look here
https://bitbucket.org/comodmw/test-notifications/src/8a2a93c7db05a16e91cdbce6f734f9671e4523b5/src/myToolWindow/MyToolWindowFactory.java?at=pluginB&fileviewer=file-view-default
You can switch between the two Plugins "PluginA" and "PluginB" (Actually the same with the one difference that one depends the other)
This Plugin comes with a simple tool window and ONE button to fire the Notification to the other Plugin or in other words "all Listeners"
What do im missing?
You have the same class in both plugins, so it's loaded by each plugin separately, and each plugin only sees its own class. Please try removing Notification from pluginB.
When i delte the Class, how can i call the trigger??
Ive tried now to Rename the PluginB-Classes: https://bitbucket.org/comodmw/test-notifications/commits/0bd688f7ef656f94b9bfb64903ee7dca0bd3db5b
Is it in another way possible to include the whole PluginA as dependency lib with their sources to get access?, but this was not the idea of the post.
You can use pluginA classes from pluginB. Those classes should be available as dependency during compilation, of course.