rcp(插件开发)org.eclipse.ui.decorators 使用

org.eclipse.ui.decorators这个扩展点可以为对应的节点添加不同的图标显示。

 

使用方式都差不多,以下就转载一下使用方式:

1、添加扩展点 org.eclipse.ui.decorators

2、修改plugin.xml

   <extension
         point="org.eclipse.ui.decorators">
      <decorator
            id="my.ui.decorator"
            label="IFolder Decorator"
            state="true"
           class="my.ui.decorators.LabelDecorator">
         <enablement>
            <objectClass name="org.eclipse.core.resources.IFolder"/>
         </enablement>
      </decorator>
   </extension>

添加扩展点后,xml里有很多属性,为了单独对指定的文件夹起作用,删掉其他属性,并增加class属性;

enablement的意思是对什么起作用,这里是对文件夹IFolder起作用;

3、my.ui.decorators.LabelDecorator类源码

package my.ui.decorators;

import org.eclipse.core.internal.resources.Folder;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

import my.ui.BabaowgtkitUIPlugIn;
/**
* 更改图标
* ClassName:LabelDecorator
 */
@SuppressWarnings({ "restriction", "unused" })
public class LabelDecoratorimplementsILabelDecorator{
/**
  *
  * (non-Javadoc)
  * @see org.eclipse.jface.viewers.ILabelDecorator#decorateImage(org.eclipse.swt.graphics.Image, java.lang.Object)
  */
public Image decorateImage(Image image, Object element) {
  //更改src文件夹图标
  if (element instanceof Folder
    && ((Folder) element).getName().equals("src")) {
   return MyUIPlugIn.getImage("icons/packagefolder_obj.gif");
  }
  returnnull;
}

public String decorateText(String text, Object element) {
  // TODO Auto-generated method stub
  return null;
}

public void addListener(ILabelProviderListener listener) {
  // TODO Auto-generated method stub
 
}

public void dispose() {
  // TODO Auto-generated method stub
 
}

public boolean isLabelProperty(Object element, String property) {
  // TODO Auto-generated method stub
  return false;
}

public void removeListener(ILabelProviderListener listener) {
  // TODO Auto-generated method stub
 
}
}

上面用到了MyUIPlugIn.getImage的方法,下面贴出源码,目的就是取项目目录下的图标文件;

MyUIPlugIn是建立插件项目时自动生成的,继承了 AbstractUIPlugin;

public static ImageDescriptor getImageDescriptor(String path) {
  return imageDescriptorFromPlugin(PLUGIN_ID, path);
}

public static Image getImage(String path){
  return getImageDescriptor(path).createImage();
}

PLUGIN_ID是插件项目的ID;

 

 

 

你可能感兴趣的:(Decorator)