gpt4 book ai didi

java - 如何为java方法应用装饰器

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:54 25 4
gpt4 key购买 nike

我正在开发一个 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>

装饰器.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; }}

当我添加持久性属性时,我无法设置方法的装饰器,因为 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();

最佳答案

这是 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

关于java - 如何为java方法应用装饰器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34335631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com