Passing data to an Action


I have my ToolWindowFactory: 

class MyToolWindowFactory implements com.intellij.openapi.wm.ToolWindowFactory

In which I have some data, could be a String or an Object

I want to be able to retrieve it at the time my action will execute:

public class RefreshAction extends AnAction {

public void actionPerformed(@NotNull AnActionEvent e) {
// Get data here

In my ToolWindowFactory I have tried to inject data into the `DataContext`:

DataContext dataContext = DataManager.getInstance().getDataContext(toolWindow.getComponent());
DataManager.getInstance().saveInDataContext(dataContext, MyKey, MyData);

However this is not working.
I have also tried to use DataContext in the action itself:

public void actionPerformed(@NotNull AnActionEvent e) {
DataContext dataContext = DataManager.getInstance().getDataContext();
Object hello = DataManager.getInstance().loadFromDataContext(dataContext, DataKeys.LATEST_ERRORS_STATE_SUBJECT);

This did not work either.

Finally I have tried to implement DataProvider:

public class MyToolWindowFactory implements com.intellij.openapi.wm.ToolWindowFactory, DataProvider

And override the method to return some dummy data:

public @Nullable Object getData(@NotNull String dataId) {
return "hello";

This method doesn't even get called.

Now I am quite sure I am missing something and doing something wrong, however there is basically no documentation or examples on this topic, so I am really improvising things.

Is what I want to achieve even possible?



DataProvider must be implemented by corresponding UI Component, not Toolwindow/Factory. See com.intellij.ui.FinderRecursivePanel#getData as sample implementation.


Ok, in the end I took a different simpler approach.

I have just decided to register the action in code rather than via plugin.xml:

MyAction myAction = new MyAction();
ActionManager.getInstance().registerAction("myActionId", myAction);

and then I have created the action class

public class RefreshAction extends AnAction {
private String myParam;

public void actionPerformed(@NotNull AnActionEvent e) {
// Here I can retreive my param or whatever value I set

public void setMyParam(String myParam) {
this.myParam = myParam;

This way I can pass anything to my action


Please sign in to leave a comment.