Getting active project?

Since you can have many IDEA frames open I need a way to get hold of the currently open project. How can I do this?

3
12 comments
Avatar
Permanently deleted user

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).

4
Project[] projects = ProjectManager.getInstance().getOpenProjects();
Project activeProject = null;
for (Project project : projects) {
Window window = WindowManager.getInstance().suggestParentWindow(project);
if (window != null && window.isActive()) {
activeProject = project;
}
}
2
Avatar
Permanently deleted user

You should be able to hold of it using the DataManager:

DataContext dataContext = DataManager.getInstance().getDataContext();
Project project = (Project) dataContext.getData(DataConstants.PROJECT);

- Steve

1

getDataContextFromFocus is also deprecated.

Now

val dataContext = DataManager.getInstance().dataContextFromFocusAsync.blockingGet(2000)
val project = dataContext?.getData(DataKeys.PROJECT)

works (in Kotlin).

 

1

@Brian Faris you code works, thanks a lot!

1
Avatar
Permanently deleted user

Hello Johan,

Since you can have many IDEA frames open I need a way to get hold of
the currently open project. How can I do this?


From where? Usually you can get it from DataContext (DataConstants.PROJECT).

--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Avatar
Permanently deleted user

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).

0
Avatar
Permanently deleted user

Hello Johan,

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!"


0
Avatar
Permanently deleted user

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.

0
Avatar
Permanently deleted user

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.

0
Avatar
Permanently deleted user

I would really like an updated response to this since the answers are all deprecated.

One method that seems like it should work is:

DataContext dataContext = DataManager.getInstance().getDataContextFromFocus().getResult();
this.project = DataKeys.PROJECT.getData(dataContext);

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.

 

0

All variants are null if the class is inherited from ProjectComponent :(

0

Please sign in to leave a comment.