Project view - file type icon customization

How do I set a different icon for a particular file type that has specific content?

Specifically, I'm trying to set a custom icon for an in-house XML file (similar to how the IDEA plugin icon is different than a regular XML file icon).
The file typically is located at a known place in the directory structure and has a known name, but ideally Id like to be able to check for the existence of an in-house XML element (so any *.xml file with the right contents will have the custom icon)

After a Google search, it looks like IconProvider has something to do with it, but I didnt find anything about how to register this new IconProvider with IDEA and I didnt see anything about checking the contents of the file.

What are the specifics for the icon (size, format and number of colors)?

Regards

Nick

2 comments
Comment actions Permalink

Hello Nick,

Yes, IconProvider is the right API in this case. The implementation is registered
as an extension in the iconProvider extension point. You need to check whether
the PSI element passed to your IconProvider is an XmlFile, and if it is,
use the XML PSI to check if you need to change the icon.

The icons are 16x16, the format and number of colors doesn't matter.

How do I set a different icon for a particular file type that has
specific content?

Specifically, I'm trying to set a custom icon for an in-house XML file
(similar to how the IDEA plugin icon is different than a regular XML
file icon). The file typically is located at a known place in the
directory structure and has a known name, but ideally Id like to be
able to check for the existence of an in-house XML element (so any
*.xml file with the right contents will have the custom icon)

After a Google search, it looks like IconProvider has something to do
with it, but I didnt find anything about how to register this new
IconProvider with IDEA and I didnt see anything about checking the
contents of the file.

What are the specifics for the icon (size, format and number of
colors)?

Regards

Nick

---
Original message URL:
http://www.jetbrains.net/devnet/message/5256092#5256092

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


0
Comment actions Permalink

For everybody who likes example code, here is some that works for my plugin:

package net.jangaroo.ide.idea.exml;

import com.intellij.ide.IconProvider;
import com.intellij.openapi.util.IconLoader;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

import javax.swing.Icon;

/**
* Provides a custom icon for *.exml files.
*/
public class ExmlIconProvider extends IconProvider {

  private static final String EXML_ICON_URL = "/net/jangaroo/EXML-logo-16x16.png";

  public Icon getIcon(@NotNull PsiElement psiElement, int flags) {
    PsiFile containingFile = psiElement.getContainingFile();
    if (containingFile != null && containingFile.getName().endsWith(".exml")) {
      return IconLoader.getIcon(EXML_ICON_URL);
    }
    return null;
  }

}


The icon has to be packaged as a resource into the plugin jar.

plugin.xml fragment:

  <extensions defaultExtensionNs="com.intellij">
    <iconProvider implementation="net.jangaroo.ide.idea.exml.ExmlIconProvider"/>
  </extensions>
1

Please sign in to leave a comment.