在做的項目里,需要一個這樣的顯示效果.
以前從來沒有做過,也不知道該用什么關(guān)鍵字來搜索.
于是,只好用最原始的辦法,看Eclipse的源代碼.最后一些跟decorator相關(guān)的類引起了我的注意,還發(fā)現(xiàn)有一個相關(guān)的LabelProvider.于是我用這個詞作為關(guān)鍵字搜索到了下面這篇文章,覺得十分有用,所以暫存到這里.
原文標題: org.eclipse.ui.decorators得用法
<extension point="org.eclipse.ui.decorators">
<decorator icon="icons/warning_small.gif" id="cnooc.rcp.decorator.node.warning" label="Data Warning Decorator" lightweight="true" location="BOTTOM_LEFT" state="true">
<enablement>
<and>
<objectClass "com.eplat.cnooc.rcp.node.INode"/>
<objectState name="hasWarning" value="true"/>
</and>
</enablement>
</decorator>
<decorator icon="icons/error_small.gif" id="cnooc.rcp.decorator.node.error" label="Data Error Decorator" lightweight="true" location="BOTTOM_LEFT" state="true">
<enablement>
<and>
<objectClass "com.eplat.cnooc.rcp.node.INode"/>
<objectState name="hasError" value="true"/>
</and>
</enablement>
</decorator>
</extension>
INode是TreeViewer里面節(jié)點得對象.
Viewer設(shè)置LabelProvider時需要如下:
viewer.setLabelProvider(new DecoratingLabelProvider(new ViewLabelProvider(),
Activator.getDefault().getWorkbench().getDecoratorManager().getLabelDecorator()));
寫了這個以后還沒完, 需要讓INode實現(xiàn)IActionFilter接口. eclipse得API中說要么實現(xiàn)IActionFilter接口, 要么實現(xiàn)IAdapter接口, 如果實現(xiàn)后者得話, 系統(tǒng)會調(diào)用getAdapter()方法. 不過我還是選擇前者.

/** *//**
* (non-Javadoc)
*
* @see org.eclipse.ui.IActionFilter#testAttribute(java.lang.Object, java.lang.String,java.lang.String)
*/

public boolean testAttribute(Object target, String name, String value)
{

if (name.equals("hasWarning"))
{
return !getProblems().hasError() && getProblems().hasWarning();

} else if (name.equals("hasError"))
{
return getProblems().hasError();
}
return false;
}
意思應(yīng)該比較明顯得, 如果有warning并且沒有error得時候warning得decorator生效. 如果有error則error得decorator生效. name參數(shù)對應(yīng)得就是objectState得name參數(shù). value參數(shù)對應(yīng)得objectState得value參數(shù). target參數(shù)就是viewer中得節(jié)點對象, 不過由于INode實現(xiàn)了IActionFilter接口, 因此這里得target就是this了.
一般來說需要判斷得就是根據(jù)name獲取得值是否等于value, 等于返回true, 否則返回false. 不過這里我不需要判斷這個了, 直接根據(jù)當(dāng)前狀態(tài)返回就好了.
按照eclipse得原理, 理論上應(yīng)該INode變化以后viewer就會跟著變化, 但是我實驗下來有時候好有時候不好.
因此在Editor得verify方法里增加了下面得邏輯:

/** *//**
* 校驗數(shù)據(jù)
*
* @return
*/

private boolean verify()
{
//校驗數(shù)據(jù)
//不管是否有error或者warning都需要通知向?qū)渌⑿乱幌鹿?jié)點.
ExplorerView view = (ExplorerView) getSite().getPage().findView(ExplorerView.class.getName());
view.refresh((INode) getEditorInput());

return true;
}
現(xiàn)在好了, 只要執(zhí)行verify方法, viewer就會刷新對應(yīng)得節(jié)點, 以達到顯示左下方小圖標得目的.
原文地址: http://blog.csdn.net/bradwoo8621/archive/2007/05/11/1604738.aspx