---
title: "eclipse平台如何实用Marker来实现Problem View的输出"
author: "min12605"
date: "2009-04-13T10:09:07Z"
tags: []
slug: "eclipse-marker-problem-view"
draft: false
lastmod: "2009-04-13T10:12:43Z"
id: 366369
url: "https://dengmin.iteye.com/blog/366369"
---

最近，需要使用eclipse平台中的problem view来输出一些项目中的问题报警，现将搜集到的这方面的内容保存在这里。

[http://blog.zijinshi.cn/delphi/archives/20/](http://blog.zijinshi.cn/delphi/archives/20/) 写道

Marker在BIRT中的使用

概要  
　　用过BIRT的人应该知道，在进行报表设计的时候，如果当前打开的报表中存在错误，那么在Problems视图中就会列出相关的错误信息。当用户点击其中一条信息的时候，系统就会定位到错误的位置。  
　　本文将介绍BIRT中是如何实现此功能的。

什么是marker？  
　　如果要在Problem视图中显示错误信息，并实现错误定位功能，就需要用到marker。  
　　什么是marker呢？简单的说，他就是一个信息记录，在这个记录里，可能保存了问题的信息(message)、位置(location)、严重程度(priority)等等。  
　　我们知道，基于Eclipse的应用程序可能由很多plugin组成，它们往往比较复杂。而每个plugin都不得不管理自己的资源，并做好错误信息（或 其他信息）与用户之间的交互工作。为了辅助plugin的作者实现这样的功能，并使其符合使用者的习惯，Eclipse平台提供了一种机制：通过 marker来管理这些信息。 marker可以用在Problems、Tasks、Bookmarks等视图中，当然，只要愿意，它也可以用在你自己定义的视图中。

Marker的相关知识  
　　Marker的类型和属性都是可以扩展的。我们可以看看接口 org.eclipse.core.resources.IMarker，它定义了一些 marker相关的类型和属性常量。在实际的使用过程中，我们要实现这个接口。如果给一个marker，我们从它本身得到的相关信息包括id、类型、属性值等等，除此之外，我们还可以给它自定义属性。  
　　Maker的属性是用属性名唯一标识的，每个属性名对应一个属性值。属性值只能是三种数据类型：Boolean、Integer、String。  
　　至于如何定义新的marker，可以参见Eclipse帮助文件，它讲述得很详细。

BIRT中的实现  
　　在BIRT中，我们没有定义新的marker，它的类型为IMarker.PROBLEM，各种属性（IMarker.MESSAGE、IMarker.LINE\_NUMBER、IMarker.LOCATION、IMarker.SEVERITY），都是IMarker中预定义的。

1.创建marker  
　　创建marker使用的函数是IResouce.createMarker(type);这儿，我们使用的type是IMarker.PROBLEM。

在IDEMultiPageReportEditor.refreshMark()函数中，我们先把原来的marker从file中删掉。代码如下：

file.deleteMarkers(IMarker.PROBLEM,true,IResource.DEPTH\_INFINITE);

接着从ReportDesignHandle得到所有的Warning和Error信息，存放到List中。

List list = reportDesignHandle.getErrorList( );  
int errorListSize = list.size( );  
list.addAll( reportDesignHandle.getWarningList( ) );

最后，遍历List，并创建marker，把List中的信息加到marker中去。

for ( int i = 0, m = list.size( ); i &lt; m; i++ )  
{  
ErrorDetail errorDetail = (ErrorDetail) list.get( i );  
IMarker marker = file.createMarker( IMarker.PROBLEM );  
Map&lt;String, Object&gt; attrib = new HashMap&lt;String, Object&gt;( );  
// The first part is from error list, the other is from warning  
// list  
if ( i &lt; errorListSize )  
{  
attrib.put( IMarker.SEVERITY, IMarker.SEVERITY\_ERROR );  
}  
else  
{  
attrib.put( IMarker.SEVERITY, IMarker.SEVERITY\_WARNING );  
}  
attrib.put( IMarker.MESSAGE, errorDetail.getMessage( ) );  
attrib.put( IMarker.LINE\_NUMBER, errorDetail.getLineNo( ) );  
attrib.put( IMarker.LOCATION, errorDetail.getTagName( ) );  
if ( errorDetail.getElement( ) != null  
&& errorDetail.getElement( ).getID( ) != 0 )  
{  
attrib.put( ELEMENT\_ID,  
new Integer( (int) errorDetail.getElement( )  
.getID( ) ) );  
}  
// set all attributes together to reduce notification events  
marker.setAttributes( attrib );  
}

2.显示marker  
　　我们先把这些Marker从IResouce中读出来，仍然是通过id获取marker.使用的函数是IResource.findMarkers(type, true, IResource.DEPTH\_INFINITE)，它的函数返回值是一个IMarker数组。然后再把这个数组中的内容显示到ProblemView中去。  
　　由于我们使用的marker type是IMarker.PROBLEM，ProblemView会完成相应的工作，对此，我们不需要做任何工作。

3.定位marker  
　　当用户双击Problems视图中的marker的时候。如果该marker对应layout中的某个element，那么 layout中的这个 element就会被选中，并高亮现实；如果对应masterpage中的某个element，那么就会转到masterpage中，并选中此 element；如果marker信息中在layout和masterpage中找不到对应的element，那么它就转到XML Source中，高亮选中错误的行。

　　当用户双击选中的marker的时候，IDE会通过editorPart.getAdapter(class)得到当前EditorPart的IGotoMarker地实现，并调用IGotoMarker.()实现错误定位的功能。  
　　因此，我们需要IDEMultiPageReportEditor.getAdapter(class)中加入如下代码：

public Object getAdapter( Class type )  
{  
…………  
if ( type == IGotoMarker.class )  
{  
return new BIRTGotoMarker( this );  
}  
…………  
}

这儿BIRTGotoMarker是IGotoMarker接口的一个实现。BIRTGotoMarker中，我们需要实现public void gotoMarker( IMarker marker )这个函数就可以。

　　Marker在BIRT中的使用的情况大致如此， 更详细的信息，大家可以参见BIRT的源代码。

BIRT更多信息  
　　Eclipse BIRT，即Business Intelligence and Reporting Tools，中文译作商业智能与报表工具，是Eclipse开源项目之一。它是一种基于Eclipse的可视化Java报表设计工具。更多BIRT相关的信息，可以访问安讯中国的官方网站：http://www.actuatechina.com。

　　Eclipse源代码可以通过CVS获取：  
　　　　Host:dev.eclipse.org  
　　　　Path:/cvsroot/birt  
　　　　Connection type:pserver  
　　　　User:anonymous  
　　　　Password:(空)

参考资料  
1.[Eclipse Corner Article:Mark My Word by Dejan Glozic, IBM and Jeff McAffer, OTI](http://www.eclipse.org/articles/Article-Mark%20My%20Words/mark-my-words.html)  
2.[Eclipse Platform API Specification](http://help.eclipse.org/ganymede/index.jsp)