class of tree node's value in Package view

Hi,

I've discovered that when trying to find what elements the user has
currently selected in the package view, using code like this:

DataContext context = DataManager.getInstance().getDataContext();
Navigatable[] navigatables = (Navigatable[])
context.getData(DataConstants.NAVIGATABLE_ARRAY);
for (int i = 0; i < navigatables.length; i++) {
Navigatable navigatable = navigatables+;
if (navigatable instanceof AbstractTreeNode) {
Object value = ((AbstractTreeNode) navigatable).getValue();
/* test value for implementing various OpenAPI interfaces,
do something with the value */
}
}

The values (in the package view only; in other views they are more
friendly objects) have the following class:

com.intellij.ide.projectView.impl.nodes.PackageElement

Using reflection, I have explored this class and discovered that its
public interface looks something like this:

package com.intellij.ide.projectView.impl.nodes;
class PackageElement {
public PsiPackage getPackage();
public Module getModule();
public boolean isLibraryElement();
}

This is really useful, as I think it's the only way to get accurate
selection information out of the package view. The packages displayed
in the package view have their displayed contents limited to the module
they are displayed underneath, so knowing which module they are
underneath is important to process the selection properly in a way that
will make sense to the user, for example, for a menu action being
designed for a plugin. But unfortunately, this interface is not
published in OpenAPI (this is a bad thing). The other DataContext
options I have explored, including VIRTUAL_FILE_ARRAY or even the nearly
undocumented "psi.Element.array" are not satisfactory, because they
don't include the information about the Module that is selected, and
instead return values which represent the package(s) across all Modules
in the project.

So, to get the data present in PackageElement, which is not part of
OpenAPI, do I have to use reflection? Or fake out the compiler somehow
to make it think it knows the definition of that class and the interface
I've "documented" above? Or is there something I'm missing and there's
an official way to get to this data?

Thanks,

David Beattie
Software Engineer
Parasoft Corporation

1 comment

By the way, my current workaround is to compile directly against idea.jar so I can get the definition of the class in
question. This seems risky to me though?

--David

David Beattie wrote:

Hi,

I've discovered that when trying to find what elements the user has
currently selected in the package view, using code like this:

DataContext context = DataManager.getInstance().getDataContext();
Navigatable[] navigatables = (Navigatable[])
context.getData(DataConstants.NAVIGATABLE_ARRAY);
for (int i = 0; i < navigatables.length; i++) {
Navigatable navigatable = navigatables+;
if (navigatable instanceof AbstractTreeNode) {
Object value = ((AbstractTreeNode) navigatable).getValue();
/* test value for implementing various OpenAPI interfaces,
do something with the value */
}
}

The values (in the package view only; in other views they are more
friendly objects) have the following class:

com.intellij.ide.projectView.impl.nodes.PackageElement

Using reflection, I have explored this class and discovered that its
public interface looks something like this:

package com.intellij.ide.projectView.impl.nodes;
class PackageElement {
public PsiPackage getPackage();
public Module getModule();
public boolean isLibraryElement();
}

This is really useful, as I think it's the only way to get accurate
selection information out of the package view. The packages displayed
in the package view have their displayed contents limited to the module
they are displayed underneath, so knowing which module they are
underneath is important to process the selection properly in a way that
will make sense to the user, for example, for a menu action being
designed for a plugin. But unfortunately, this interface is not
published in OpenAPI (this is a bad thing). The other DataContext
options I have explored, including VIRTUAL_FILE_ARRAY or even the nearly
undocumented "psi.Element.array" are not satisfactory, because they
don't include the information about the Module that is selected, and
instead return values which represent the package(s) across all Modules
in the project.

So, to get the data present in PackageElement, which is not part of
OpenAPI, do I have to use reflection? Or fake out the compiler somehow
to make it think it knows the definition of that class and the interface
I've "documented" above? Or is there something I'm missing and there's
an official way to get to this data?

Thanks,

David Beattie
Software Engineer
Parasoft Corporation

0

Please sign in to leave a comment.