texteditor marker and annotation

http://liugang594.iteye.com/blog/795749

http://stackoverflow.com/questions/2888207/eclipse-plugin-custom-icon-for-a-marker


http://help.eclipse.org/indigo/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/editors_annotations.htm


http://www.cnblogs.com/yinger/archive/2011/11/21/2257058.html

https://www.ibm.com/developerworks/cn/opensource/os-cn-ecljtf5/

   <extension
         id="wang.andy.validation.groovy.problemmarker"
         name="Groovy syntax problem"
         point="org.eclipse.core.resources.markers">
      <persistent
            value="true">
      </persistent>
      <super
            type="org.eclipse.core.resources.problemmarker">
      </super>
   </extension>
   <extension
         id="wang.andy.validation.groovy.textmarker"
         name="Groovy syntax problem"
         point="org.eclipse.core.resources.markers">
      <persistent
            value="true">
      </persistent>
      <super
            type="org.eclipse.core.resources.textmarker">
      </super>
   </extension>
   <extension
         point="org.eclipse.ui.ide.markerImageProviders">
      <imageprovider
            icon="icons/error.gif"
            id="wang.andy.validation.groovy.textmarker.imageprovider"
            markertype="wang.andy.validation.groovy.textmarker">
      </imageprovider>
   </extension>
   <extension
         point="org.eclipse.ui.editors.annotationTypes">
      <type
            markerSeverity="1"
            markerType="wang.andy.validation.groovy.textmarker"
            name="wang.andy.validation.texteditor.warning"
            super="org.eclipse.ui.workbench.texteditor.warning">
      </type>
      <type
            markerSeverity="2"
            markerType="wang.andy.validation.groovy.problemmarker"
            name="wang.andy.validation.texteditor.error"
            super="org.eclipse.ui.workbench.texteditor.error">
      </type>
   </extension>
   <extension
         point="org.eclipse.ui.editors.markerAnnotationSpecification">
      <specification  
         annotationType="wang.andy.validation.texteditor.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="wang.andy.validation.texteditor.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>  
   </extension>


一个是IFile类型的实例(用来执行marking),一个是IDocument 类型的实例(用于确定插入文档中的marker的位置)。

  public IMarker addProblemMarker(String type, String message,
            String location, int severity, int priority)
    {
        if (null != resource)
        {
            try
            {//type是 String GROOVY_PROBLEM_MARKER_ID = "wang.andy.validation.groovy.problemmarker";
                IMarker marker = resource.createMarker(type);
                marker.setAttribute(IMarker.MESSAGE, message);
                marker.setAttribute(IMarker.LOCATION, location);
                marker.setAttribute(IMarker.SEVERITY, severity);
                marker.setAttribute(IMarker.PRIORITY, priority);
                return marker;

            }
            catch (CoreException e)
            {
                e.printStackTrace();
            }
        }
        return null;

    }


    public IMarker addTextMarker(String type, String message, String location,
            int severity, int priority)
    {
        if (null != resource)
        {
            try
            {//type是 String GROOVY_PROBLEM_MARKER_ID = "wang.andy.validation.groovy.textmarker";
                IMarker textmarker = resource.createMarker(type);
                textmarker.setAttribute(IMarker.MESSAGE, message);
                textmarker.setAttribute(IMarker.LOCATION, location);
                textmarker.setAttribute(IMarker.SEVERITY, severity);
                textmarker.setAttribute(IMarker.PRIORITY, priority);
                return textmarker;
            }
            catch (CoreException e)
            {
                e.printStackTrace();
            }
        }
        return null;
    }


    public void addTextAnnotation(IMarker textmarker, ITextEditor editor)
    {
        if (editor instanceof MappingTextEditor)
        {

            IDocumentProvider documentProvider = editor.getDocumentProvider();
            //            documentProvider = new ExpressionTextDocumentProvider();
            ITextSelection textSelection = (ITextSelection)editor.getSelectionProvider()
                    .getSelection();
            if (!textSelection.isEmpty())
            {
                //                IAnnotationModel model = getAnnotationModel(editor);
                IAnnotationModel annotationModel = documentProvider.getAnnotationModel(editor.getEditorInput());
                //                annotationModel = new ExpressionAnnotationModel(resource);
                if (annotationModel != null)
                {
                    
                    int start = textSelection.getStartLine();
                    int end = textSelection.getEndLine();
                    
                    //annotations
                    SimpleMarkerAnnotation ma = new SimpleMarkerAnnotation(
                            GROOVY_TEXT_ANNOTATION_ERROR_ID, marker);
                    //GROOVY_TEXT_ANNOTATION_ERROR_ID = "wang.andy.validation.texteditor.error";
                    try
                    {
                        IDocument document = editor.getDocumentProvider()
                                .getDocument(editor.getEditorInput());
                        int offset = document.getLineOffset(start);
                        int endOffset = document.getLineOffset(end);
                        Position position = new Position(offset, endOffset
                                - offset);
                        
                        //Finally add the new annotation to the model
                        annotationModel.connect(document);
                        
                        annotationModel.addAnnotation(ma, position);
                        
                        annotationModel.disconnect(document);
                    }
                    catch (BadLocationException x)
                    {
                        // ignore
                    }
                }
            }
            
        }
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值