如何为java方法应用装饰器

时间:2015-12-17 13:20:10

标签: java plugins eclipse-plugin eclipse-rcp decorator

我正在开发一个eclipse插件,它将使用装饰器在java项目中显示一些通知。我能够在.java文件和ICompilation单元中显示前缀和装饰器。但我无法为方法提供装饰器。

我在下面提供了这些片段。

<extension point="org.eclipse.ui.decorators"> 
   <decorator 
     id="com.eclipse.plugin.test.plugin.decorator.decorators" 
     label="Decorators" 
     state="true" 
    class="com.eclipse.plugin.test.ui.plugin.decorator.Decorators"      
    objectClass="org.eclipse.core.resources.IResource" 
    adaptable="true">     
    </decorator> 
</extension>

Decorator.java:

public class Decorators extends LabelProvider implements ILabelDecorator {
private static Images demoImage_ = new Images();
/**
 * Boolean indicator to differenciate decoration at the start or not.. Not
 * used
 */
private static boolean newDecorationRequest_ = false;
private static List initialDecoratorList_ = new Vector();
private static boolean decorateTextLabels_ = true;
private static boolean decorateProject_ = true;

public Decorators() {
    super();
}

/**
 * Get the static instance of DemoDecorator
 * 
 * @return Demo decorator object
 * 
 */
public static Decorators getDemoDecorator() {

    IDecoratorManager decoratorManager = test_automation_plugin.Activator
            .getDefault().getWorkbench().getDecoratorManager();
    System.out.println("dedcorator not enabled" + decoratorManager);
    try {
        decoratorManager
                .setEnabled(
                        "com.eclipse.plugin.test.ui.plugin.decorator.decorators",
                        true);
    } catch (CoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (decoratorManager
            .getEnabled("com.eclipse.plugin.test.ui.plugin.decorator.decorators")) {
        System.out.println("dedcorator enabled");
        return (Decorators) decoratorManager
                .getLabelDecorator("com.eclipse.plugin.test.ui.plugin.decorator.decorators");
    }
    return null;
}

/**
 * Function to decorate the image of the object
 * 
 * @see org.eclipse.jface.viewers.ILabelDecorator#decorateImage(Image,
 *      Object)
 * 
 * @param baseImage
 *            base image of the object
 * @param obj
 *            object to be decorated
 * 
 * @return Image for the object calculated from its persistent properties
 *         null if there is no need of a special decoration for the resource
 */

@Override
public Image decorateImage(Image baseImage, Object object) {

    IResource objectResource;

    objectResource = (IResource) object;

    Vector decoratorImageKeys = new Vector();
    Image image;

    /**
     * Object resource cant be null. Should return the image as null
     */
    if (objectResource == null) {
        return null;
    }

    /**
     * Projects and Folders should not be decorated..
     */
    if (objectResource.getType() == IResource.FOLDER
            | objectResource.getType() == IResource.PROJECT) {
        return null;
    }

    try {

        /**
         * Resource properties have been changed. Find the decorator with
         * which the image should be decorated
         */
        decoratorImageKeys = ResourcePropertiesManager
                .findDecorationImageForResource(objectResource);

        if (!decoratorImageKeys.isEmpty()) {
            image = drawIconImage(baseImage, decoratorImageKeys);
            DecoratorManager.removeResource(objectResource);
            return image;
        }
        /**
         * The resource need not be decorated
         */
        else {
            DecoratorManager.removeResource(objectResource);
            return null;
        }
    } catch (Exception e) {
        Ilogger console = Console.getInstance();
        console.logger("Exception arised:" + e);
    }

    return null;
}

/**
 * @see org.eclipse.jface.viewers.ILabelDecorator#decorateText(String,
 *      Object)
 * 
 * @param label
 *            default label of the object
 * @param obj
 *            object
 * 
 * @return label with which the object should be decorated. If null is
 *         returned, then the object is decorated with default label
 * 
 *         this method is violating sonar qube rules of cyclomatic
 *         complexity but decorators need deep access in the eclipse
 *         platform so it needs some complex code.
 */

@Override
public String decorateText(String label, Object object) {
    IResource objectResource;
    objectResource = (IResource) object;
    String newText = label;

    /**
     * Object resource cant be null. So return with null.
     */
    if (objectResource == null) {
        return null;
    }

    if (objectResource.getType() == objectResource.PROJECT) {

        if (decorateProject_) {
            /**
             * do nothing.
             */
        }
        return null;
    }
    /**
     * Dont decorate the folder
     */
    if (objectResource.getType() == objectResource.FOLDER) {

        return null;
    }

    if (!decorateTextLabels_) {
        return null;
    }

    String prefixValue = ResourcePropertiesManager
            .getPrefix(objectResource);
    String suffixValue = ResourcePropertiesManager
            .getSuffix(objectResource);
    /**
     * setting up the prefx value for the selected element
     */
    if (prefixValue != null && prefixValue.length() != 0) {
        newText = " [ ";
        newText += prefixValue;
        newText += " ] ";
        newText += label;
    }

    /**
     * setting up the suffix value for the selected element.
     */
    if (suffixValue != null && suffixValue.length() != 0) {
        newText += " [ ";
        newText += suffixValue;
        newText += " ]";
    }
    return newText;

}

/**
 * Check whether the decorator for the resource has been changed
 * 
 * @param resource
 *            IResource object
 * 
 * @return true if the resource has been changed false if the resource has
 *         not been changed
 */
public boolean checkForNewDecoratorChange(IResource resource) {

    if (newDecorationRequest_) {
        return DecoratorManager.contains(resource);
    }
    return true;
}

/**
 * Refresh the project. This is used to refresh the label decorators of of
 * the resources.
 * 
 */
public void refresh() {
    System.out.println("ghelooo");
    newDecorationRequest_ = true;
    initialDecoratorList_ = null;

    List resourcesToBeUpdated;

    /**
     * Get the Demo decorator
     */
    Decorators demoDecorator = getDemoDecorator();
    if (demoDecorator == null) {
        return;
    } else {

        resourcesToBeUpdated = DecoratorManager.getSuccessResources();
        /**
         * Fire a label provider changed event to decorate the resources
         * whose image needs to be updated
         */

        demoDecorator.fireLabelEvent(new LabelProviderChangedEvent(
                demoDecorator, resourcesToBeUpdated.toArray()));
    }
}

/**
 * Refresh all the resources in the project
 */
public void refreshAll(boolean displayTextLabel, boolean displayProject) {
    decorateTextLabels_ = displayTextLabel;
    decorateProject_ = displayProject;

    Decorators demoDecorator = getDemoDecorator();
    if (demoDecorator == null) {
        return;
    } else {
        demoDecorator.fireLabelEvent(new LabelProviderChangedEvent(
                demoDecorator));
    }
}

public void refresh(List resourcesToBeUpdated) {
    newDecorationRequest_ = true;
    initialDecoratorList_ = null;

    Decorators demoDecorator = getDemoDecorator();
    if (demoDecorator == null) {
        return;
    } else {
        /**
         * Fire a label provider changed event to decorate the resources
         * whose image needs to be updated
         */

        fireLabelEvent(new LabelProviderChangedEvent(demoDecorator,
                resourcesToBeUpdated.toArray()));
    }
}

/**
 * Fire a Label Change event so that the label decorators are automatically
 * refreshed.
 * 
 * @param event
 *            LabelProviderChangedEvent
 */
private void fireLabelEvent(final LabelProviderChangedEvent event) {
    /**
     * We need to get the thread of execution to fire the label provider
     * changed event , else WSWB complains of thread exception.
     */

    Display.getDefault().asyncExec(new Runnable() {
        public void run() {
            fireLabelProviderChanged(event);
        }
    });
}

/**
 * Function to draw icon image
 * 
 * @param baseImage
 *            base image of the object resource
 * @param decoratorImageKeys
 *            vector of image keys
 * 
 * @return icon image with which the resource is to be decorated
 */
private Image drawIconImage(Image baseImage, Vector decoratorImageKeys) {
    Image image;

    OverlayImageIcon overlayIcon = new OverlayImageIcon(baseImage,
            demoImage_, decoratorImageKeys);
    image = overlayIcon.getImage();
    return image;   }}

当我添加persistance属性时,我无法为方法设置装饰器,因为method.getresource()将再次指向ICompilationUnit。

public static void addPersistentProperty (IResource resource,String localName,String value){
    /**
     *  Get the correct Qualified Name
     */
    QualifiedName qName = PersistentPropertyTypes.getInstance().
            getQualifiedName(localName);

    try{
        resource.setPersistentProperty(qName, value);
    }catch(Exception e){
        Ilogger console=Console.getInstance();  
        console.logger("Exception arised:"+e);
    }
    }

当我使用下面的代码时,它不会修饰它再次装饰ICompilationUnit的方法

  ResourcePropertiesManager.addPersistentProperty  (method.getResource(),"IMethod", "method");
            DecoratorManager.addSuccessResources (method.getResource());

            /**
             *  Refresh the label decorations... 
             *  Change it to DecoratorWithImageCaching if image caching should be used
             */
             Decorators.getDemoDecorator().refresh();

1 个答案:

答案 0 :(得分:0)

这是JDT代码用于方法的覆盖指示符的内容:

<extension
  point="org.eclipse.ui.decorators">
  <decorator
    label="%OverrideIndicatorLabelDecorator.label"
    lightweight="true"
    location="BOTTOM_RIGHT"     
    state="true"
    class="org.eclipse.jdt.ui.OverrideIndicatorLabelDecorator"
    id="org.eclipse.jdt.ui.override.decorator">
    <description>
        %OverrideIndicatorLabelDecorator.description
    </description>
    <enablement>
        <objectClass
            name="org.eclipse.jdt.core.IMethod">
        </objectClass>
    </enablement>
  </decorator>  

注意:这是使用轻量级装饰器,因此您实施ILightweightLabelDecorator