texteditor marker and annotation

来源:互联网 发布:超感知觉 知乎 编辑:程序博客网 时间:2024/05/17 22:49

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                    }                }            }                    }    }}


原创粉丝点击