How to change the text/icon displayed for each file/directory in project view

I'd like to add new information to each file in the project view (and possibly package view). This may include the directories/package names as well. This information may be in iconic form or it may be text (prefixed or suffixed to the default text).

So, if I have a project view with directory structure

src/
com/
example/
Test.java

I may want to show something like

src/
com/
example
Test

or perhaps an icon before each file to show if it is read-only.

If this is possible, then how is it handled differenly if the option to flatten empty packages is selected? So I may want

com.example
Test

in the package view.

Thanks for any pointers. I've gone through the OpenAPI stuff 3 or 4 times now and no joy.

It will be nice when this is finally documented to a standard that is usable. I'm sure it is a great API but not really usable currently without a lot of wasted time (which will put off so many plugin writers unfortunately).

5 comments

Hello Pete,

You can achieve this by implementing the TreeStructureProvider interface
and replacing the standard file nodes passed to your modify() method with
your custom nodes inherited from the AbstractTreeNode class. Your implementation
of AbstractTreeNode.update() can modify the icons or text of the node by
setting the fields of the PresentationData instance passed to that method.

We understand that the current OpenAPI documentation is not always adequate
for accomplishing complex tasks like this one, but we're constantly working
on improving the documentation.

PH> I'd like to add new information to each file in the project view
PH> (and possibly package view). This may include the
PH> directories/package names as well. This information may be in iconic
PH> form or it may be text (prefixed or suffixed to the default text).
PH>
PH> So, if I have a project view with directory structure
PH>
PH> src/
PH> com/
PH> example/
PH> Test.java
PH> I may want to show something like
PH>
PH> src/
PH> com/
PH> example
PH> Test
PH> or perhaps an icon before each file to show if it is read-only.
PH>
PH> If this is possible, then how is it handled differenly if the option
PH> to flatten empty packages is selected? So I may want
PH>
PH> com.example
PH> Test
PH> in the package view.
PH>
PH> Thanks for any pointers. I've gone through the OpenAPI stuff 3 or 4
PH> times now and no joy.
PH>
PH> It will be nice when this is finally documented to a standard that
PH> is usable. I'm sure it is a great API but not really usable
PH> currently without a lot of wasted time (which will put off so many
PH> plugin writers unfortunately).

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


0

Dmitry

How do you register this new Implementation with IDEA so that the custom code gets called?

0

Here's how I got this to work (with no thanks to the SDK documentation):
I make no reps as to whether this is the best way to achieve this functionality.

Add the following to your plugins META-INF/plugin.xml:

<extensions defaultExtensionNs="com.intellij">
  <iconProvider implementation="com.test.TestIconProvider"/>
</extensions>


then implement this class:

public class TestIconProvider extends IconProvider
{
private final Icon icon;

public TestIconProvider()
{
  this.icon = IconLoader.getIcon( "test_icon.PNG" );
}

@Override
public Icon getIcon( @NotNull PsiElement psiElement, int i )
{
  Language language = psiElement.getLanguage();
  String languageDisplayName = language.getDisplayName();

  if( !languageDisplayName.equals( "XML" ) )
  {
   return null;
  }

  if( psiElement.getText().contains( "<somespecifcxmlstringthatdefinesyourcustomxml" ) )
  {
   return icon;
  }
  else
  {
   return null;
  }
}
}

0

add to the "extensions" section of plugin.xml file:

    <!-- Provides icons for ..... files shown in the Project View -->
    <iconProvider implementation="gw.plugin.ij.MyIconProvider"/>


the class file:

public class MyIconProvider extends IconProvider {
  private Map<String, Icon> icons = new HashMap<String, Icon>();

  public MyIconProvider() {
    icons.put(GosuClassFileType.EXT, GosuIcons.FILE_CLASS);
    icons.put(GosuProgramFileType.EXT, GosuIcons.FILE_PROG);
    icons.put(GosuEnhancementFileType.EXT, GosuIcons.FILE_ENH);
  }

  @Override
  public Icon getIcon(@NotNull PsiElement psiElement, int i) {
    String fileExtension = psiElement.getContainingFile().getVirtualFile().getExtension();
    return icons.get(fileExtension);
  }
}
0

If you can't see your icon in the project structure view, it's important to set the tag priority:

<iconProvider implementation="..." order="first"/>
0

Please sign in to leave a comment.