`
liugang594
  • 浏览: 978543 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

给Editor加上Annotation

阅读更多

要自定义Annotation,需要实现以下扩展点:

 

“org.eclipse.ui.editors.annotationTypes” 和 “org.eclipse.ui.editors.markerAnnotationSpecification”

 

内容可以按照自己需要的随便填,不行的话可以参考java里的扩展方式。

 

例如:

 

   <extension
         point="org.eclipse.ui.editors.annotationTypes">
      <type
            markerSeverity="2"
            markerType="org.eclipse.core.resources.problemmarker"
            name="com.windriver.liugang.EmbededEditor.error"
            super="org.eclipse.ui.workbench.texteditor.error">
      </type>
      <type
            markerSeverity="2"
            markerType="org.eclipse.core.resources.problemmarker"
            name="com.windriver.liugang.EmbededEditor.warning"
            super="org.eclipse.ui.workbench.texteditor.warning">
      </type>
      <type
            markerSeverity="2"
            markerType="org.eclipse.core.resources.problemmarker"
            name="com.windriver.liugang.EmbededEditor.host"
            super="org.eclipse.ui.workbench.texteditor.info">
      </type>
   </extension>
   <extension
         point="org.eclipse.ui.editors.markerAnnotationSpecification">
      <specification
            annotationType="com.windriver.liugang.EmbededEditor.error"
            colorPreferenceKey="errorColorKey"
            colorPreferenceValue="255,0,0"
            contributesToHeader="true"
            highlightPreferenceKey="errorHightlightKey"
            highlightPreferenceValue="true"
            icon="icons/error.gif"
            includeOnPreferencePage="true"
            isGoToNextNavigationTarget="true"
            isGoToNextNavigationTargetKey="Error_isOccurrenceGoToNextNavigationTarget"
            isGoToPreviousNavigationTarget="true"
            isGoToPreviousNavigationTargetKey="Error_isOccurrenceGoToPreviousNavigationTarget"
            label="ERROR"
            overviewRulerPreferenceKey="errorOverviewKey"
            overviewRulerPreferenceValue="true"
            presentationLayer="4"
            quickFixIcon="icons/error.gif"
            showInNextPrevDropdownToolbarAction="true"
            showInNextPrevDropdownToolbarActionKey="Error_showOccurrenceInNextPrevDropdownToolbarAction"
            symbolicIcon="error"
            textPreferenceKey="errorTextKey"
            textPreferenceValue="true"
            textStylePreferenceKey="occurrenceTextStyle"
            textStylePreferenceValue="NONE"
            verticalRulerPreferenceKey="errorVerticalKey"
            verticalRulerPreferenceValue="true">
      </specification>
      <specification
            annotationType="com.windriver.liugang.EmbededEditor.warning"
            colorPreferenceKey="warningColorKey"
            colorPreferenceValue="255,255,0"
            contributesToHeader="true"
            highlightPreferenceKey="warningHightlightKey"
            highlightPreferenceValue="true"
            icon="icons/warning.gif"
            includeOnPreferencePage="true"
            isGoToNextNavigationTarget="true"
            isGoToNextNavigationTargetKey="Warning_isOccurrenceGoToNextNavigationTarget"
            isGoToPreviousNavigationTarget="true"
            isGoToPreviousNavigationTargetKey="Warning_isOccurrenceGoToPreviousNavigationTarget"
            label="WARNING"
            overviewRulerPreferenceKey="warningOverviewKey"
            overviewRulerPreferenceValue="true"
            presentationLayer="4"
            quickFixIcon="icons/warning.gif"
            showInNextPrevDropdownToolbarAction="true"
            showInNextPrevDropdownToolbarActionKey="Warning_showOccurrenceInNextPrevDropdownToolbarAction"
            symbolicIcon="warning"
            textPreferenceKey="warningTextKey"
            textPreferenceValue="true"
            textStylePreferenceKey="occurrenceTextStyle"
            textStylePreferenceValue="NONE"
            verticalRulerPreferenceKey="warningVerticalKey"
            verticalRulerPreferenceValue="true">
      </specification>
      <specification
            annotationType="com.windriver.liugang.EmbededEditor.host"
            colorPreferenceKey="hostColorKey"
            colorPreferenceValue="255,0,255"
            contributesToHeader="true"
            highlightPreferenceKey="hostHightlightKey"
            highlightPreferenceValue="true"
            icon="icons/host.gif"
            includeOnPreferencePage="true"
            isGoToNextNavigationTarget="true"
            isGoToNextNavigationTargetKey="Host_isOccurrenceGoToNextNavigationTarget"
            isGoToPreviousNavigationTarget="true"
            isGoToPreviousNavigationTargetKey="Host_isOccurrenceGoToPreviousNavigationTarget"
            label="HOST"
            overviewRulerPreferenceKey="hostOverviewKey"
            overviewRulerPreferenceValue="true"
            presentationLayer="4"
            quickFixIcon="icons/host.gif"
            showInNextPrevDropdownToolbarAction="true"
            showInNextPrevDropdownToolbarActionKey="Host_showOccurrenceInNextPrevDropdownToolbarAction"
            symbolicIcon="info"
            textPreferenceKey="hostTextKey"
            textPreferenceValue="true"
            textStylePreferenceKey="occurrenceTextStyle"
            textStylePreferenceValue="NONE"
            verticalRulerPreferenceKey="hostVerticalKey"
            verticalRulerPreferenceValue="true">
      </specification>
   </extension>

 这样就自定义了三种类型的Annotation。

 

有了Annotation后,需要做的就是怎么把Annotation加到对应的Editor上去。

每个Text类型的Editor,都应该有一个DocumentProvider之类的东西与之对应。其中DocumentProvider中会有以下两个方法:

com.windriver.liugang.embedededitor.ui.EmbededDocumentProvider.createAnnotationModel(Object)

com.windriver.liugang.embedededitor.ui.EmbededDocumentProvider.createDocument(Object)

 

其中createAnnotationModel()里返回的AnnotationModel就可以用于管理Annotation了,它有addAnnotation()和removeAnnotation()方法。

 

每个被加到这个AnnotationModel里的Annotation都会被显示出来。

理论上来说,每次Document的内容有变化的时候,都需要更新这个AnnotationModel,最简单的方式就是:

 

document.addDocumentListener(this);

 给这个document加一个DocumentListener,每当有内容变化时发出一个通知。然后在监听事件里更新Annotation。

例如:

 

	public void documentChanged(DocumentEvent event) {
		IDocument document = event.getDocument();
		try {
			refreshAnnotations(document);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

 具体的refreshAnnotation()方法的实现就需要根据自己的需求了。例如上面我已经创建了三种类型的annotation了,下面我想如果一行的内容是空则显示一个error类型的;如果含有"java"字样,则显示warning类型的;要是含有"liugang"字样,则显示host类型的:

	private void refreshAnnotations(IDocument document)
			throws BadLocationException {
		if (embededAnnotation == null) {
			return;
		}
		int numberOfLines = document.getNumberOfLines();
		embededAnnotation.removeAllAnnotations();
		for (int i = 0; i < numberOfLines; i++) {
			int lineLength = document.getLineLength(i);
			int lineOffset = document.getLineOffset(i);
			String lineContents = document.get(lineOffset, lineLength);
			if (lineContents.trim().equals("")) {
				Annotation annotation = new Annotation(
						"com.windriver.liugang.EmbededEditor.error", true,
						"Line content can not be null");
				embededAnnotation.addAnnotation(annotation, new Position(
						lineOffset, lineLength));
			} else if (lineContents.contains("java")) {
				Annotation annotation = new Annotation(
						"com.windriver.liugang.EmbededEditor.warning", true,
						"Java is a keyword of Java");
				embededAnnotation.addAnnotation(annotation, new Position(
						lineOffset, lineLength));
			} else if (lineContents.contains("liugang")) {
				Annotation annotation = new Annotation(
						"com.windriver.liugang.EmbededEditor.host", true,
						"The line contains host name");
				embededAnnotation.addAnnotation(annotation, new Position(
						lineOffset, lineLength));
			}
		}

	}
 

最后如果显示结果为:

  结果

 

  • 大小: 4.4 KB
分享到:
评论
3 楼 zxjlwt 2016-03-02  
学习了
素人派
http://surenpi.com
2 楼 zxjlwt 2015-08-17  
学习了。

http://surenpi.com
1 楼 GirasoleY 2012-11-13  
LZ这里AnnotationModel中的connect()disconnect()等几个重写的方法是怎么实现的呢。
刚接触eclipse插件开发,对于这些方法不熟悉不知道可以提供一下源码或者给个思路呢?

相关推荐

Global site tag (gtag.js) - Google Analytics