Since this is linked in the official plugin development FAQ and the getDataContext method is deprecated, I have to ask again: How do I get the currently opened project?
I need this to store and save projectspecific stuff via PropertiesComponent.getInstance(Project project).
Yea thats where I get it from when an action is performed. But I need to get it from my application component (in response to an event from a web server).
Yea thats where I get it from when an action is performed. But I need to get it from my application component (in response to an event from a web server).
Then what do you need the project for? To display some UI?
-- Dmitry Jemerov Software Developer JetBrains, Inc. http://www.jetbrains.com/ "Develop with Pleasure!"
In my plugin I have an action that opens a console window. The console window opens on a per IDEA frame basis, so it kind of lives in the project.
I also have a web server built into the plugin. The webserver is shared between IDEA instances and therefor lives on an application level. It listens for messages from web applications and prints those to the console windows.
When a message comes into the webserver I want to print it to the active IDEA instance only. I could not do this because I had no way of getting hold of the active project. The way I do this now is to print it to all the console windows in all IDEA instances. I store the projects->consoles in a hashmap so I cant find all of them. Actually I'm quite happy with this approach but it would still be nice to know how to get hold of the currently active project/IDEA instance.
However I get errors at DataManager.getInstance() because I'm trying to do this from within JUnit tests and it can't find the application. What other methods for getting the Project.
Since this is linked in the official plugin development FAQ and the getDataContext method is deprecated, I have to ask again:
How do I get the currently opened project?
I need this to store and save projectspecific stuff via PropertiesComponent.getInstance(Project project).
You should be able to hold of it using the DataManager:
DataContext dataContext = DataManager.getInstance().getDataContext();
Project project = (Project) dataContext.getData(DataConstants.PROJECT);
- Steve
getDataContextFromFocus is also deprecated.
Now
works (in Kotlin).
@Brian Faris you code works, thanks a lot!
Hello Johan,
From where? Usually you can get it from DataContext (DataConstants.PROJECT).
--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Yea thats where I get it from when an action is performed. But I need to get it from my application component (in response to an event from a web server).
Hello Johan,
Then what do you need the project for? To display some UI?
--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Maybe I should explain myself better. :)
In my plugin I have an action that opens a console window. The console window opens on a per IDEA frame basis, so it kind of lives in the project.
I also have a web server built into the plugin. The webserver is shared between IDEA instances and therefor lives on an application level. It listens for messages from web applications and prints those to the console windows.
When a message comes into the webserver I want to print it to the active IDEA instance only. I could not do this because I had no way of getting hold of the active project.
The way I do this now is to print it to all the console windows in all IDEA instances. I store the projects->consoles in a hashmap so I cant find all of them. Actually I'm quite happy with this approach but it would still be nice to know how to get hold of the currently active project/IDEA instance.
DataConstants interface seems to be deprecated now.
The proper way is
DataContext dataContext = DataManager.getInstance().getDataContext();
Project project = DataKeys.PROJECT.getData(dataContext);
Please, tell me if I'm wrong.
I would really like an updated response to this since the answers are all deprecated.
One method that seems like it should work is:
All variants are null if the class is inherited from ProjectComponent :(