我找到了答案。
问题是
overviewRulerPreferenceValue = “真”
在plugin.xml中失踪。如果有人想尝试,我会给出完整的代码。
package sourceviewsample;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.jface.text.source.CompositeRuler;
import org.eclipse.jface.text.source.IAnnotationAccess;
import org.eclipse.jface.text.source.IOverviewRuler;
import org.eclipse.jface.text.source.ISharedTextColors;
import org.eclipse.jface.text.source.LineNumberRulerColumn;
import org.eclipse.jface.text.source.OverviewRuler;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.internal.editors.text.EditorsPlugin;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.texteditor.AnnotationPreference;
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
/**
* An example of using a SourceViewer with Annotation support outside of the
* TextEditor class. This annotations can be configured in the preferences if
* the markerAnnotationSpecification is setup in the plugin.xml.
*
* To execute this, run as an Eclipse Application and then open a file using
* Open with.. -> Other, and select Sample Editor. You will see the text that
* comes in this example and the highlight.
*/
public class SampleEditor extends ViewPart
{
public static final String ANNO_TYPE = "com.mycompany.element";
public static final String ANNO_KEY_HIGHLIGHT = "annotateElemHighlight";
public static final String ANNO_KEY_OVERVIEW = "annotateElemOverviewRuler";
public static final String ANNO_KEY_VERTICAL = "annotateElemVertialRuler";
public static final String ANNO_KEY_TEXT = "annotateElemText";
public static final String ANNO_KEY_COLOR = "annotateElemColor";
protected SourceViewer _sourceViewer;
protected SourceViewerDecorationSupport _sds;
protected IDocument _document;
protected AnnotationModel _annotationModel;
protected String _docString = "this\nis\na\ntest\ndocument";
public SampleEditor()
{
super();
}
public void createPartControl(Composite parent)
{
int VERTICAL_RULER_WIDTH = 12;
int styles = SWT.V_SCROLL
| SWT.H_SCROLL
| SWT.MULTI
| SWT.BORDER
| SWT.FULL_SELECTION;
ISharedTextColors sharedColors = EditorsPlugin.getDefault()
.getSharedTextColors();
IAnnotationAccess iaccess = new IAnnotationAccess() {
@Override
public boolean isTemporary(Annotation annotation) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isMultiLine(Annotation annotation) {
// TODO Auto-generated method stub
return false;
}
@Override
public Object getType(Annotation annotation) {
// TODO Auto-generated method stub
return ANNO_TYPE;
}
};
IOverviewRuler overviewRuler = new OverviewRuler(iaccess,
VERTICAL_RULER_WIDTH,
sharedColors);
CompositeRuler ruler = new CompositeRuler(VERTICAL_RULER_WIDTH);
_document = new Document();
_document.set(_docString);
_annotationModel = new AnnotationModel();
_annotationModel.connect(_document);
_sourceViewer = new SourceViewer(parent,
ruler,
overviewRuler,
true,
styles);
_sourceViewer.configure(new SourceViewerConfiguration());
_sds = new SourceViewerDecorationSupport(_sourceViewer,
overviewRuler,
null,
sharedColors);
AnnotationPreference ap = new AnnotationPreference();
ap.setColorPreferenceKey(ANNO_KEY_COLOR);
ap.setColorPreferenceValue(new RGB(150, 200, 250));
ap.setHighlightPreferenceKey(ANNO_KEY_HIGHLIGHT);
ap.setVerticalRulerPreferenceKey(ANNO_KEY_VERTICAL);
ap.setOverviewRulerPreferenceKey(ANNO_KEY_OVERVIEW);
ap.setOverviewRulerPreferenceValue(true);
ap.setTextPreferenceKey(ANNO_KEY_TEXT);
ap.setAnnotationType(ANNO_TYPE);
_sds.setAnnotationPreference(ap);
_sds.install(EditorsPlugin.getDefault().getPreferenceStore());
_sourceViewer.setDocument(_document, _annotationModel);
_sourceViewer.getControl().setLayoutData(new GridData(SWT.FILL,
SWT.FILL,
true,
true));
ruler.addDecorator(0, new LineNumberRulerColumn());
overviewRuler.addAnnotationType(ANNO_TYPE);
overviewRuler.getControl().setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
Annotation annotation = new Annotation(false);
annotation.setType(ANNO_TYPE);
Position position = new Position(0, 4);
_annotationModel.addAnnotation(annotation, position);
try {
_document.addPosition(position);
} catch (BadLocationException e) {
e.printStackTrace();
}
overviewRuler.setModel(_annotationModel);
}
public void dispose()
{
// The _sourceViewer goes away automatically when the editor goes
// away because it's hooked to the controls
_sds.dispose();
}
//
// This stuff below is just needed to make the EditorPart happy
//
public void doSave(IProgressMonitor monitor)
{
}
public void doSaveAs()
{
}
// public void init(IEditorSite site, IEditorInput input)
// throws PartInitException
// {
// setSite(site);
// setInput(input);
// }
//
// public boolean isDirty()
// {
// return false;
// }
public boolean isSaveAsAllowed()
{
return false;
}
public void setFocus()
{
}
}
这里是plugin.xml中以及
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.2"?>
<plugin>
<extension
point="org.eclipse.ui.editors.annotationTypes">
<type
markerSeverity="0"
name="com.mycompany.element">
</type>
</extension>
<extension
point="org.eclipse.ui.editors.markerAnnotationSpecification">
<specification
annotationType="com.mycompany.element"
colorPreferenceKey="annotateElemColor"
colorPreferenceValue="255,255,0"
highlightPreferenceKey="annotateElemHighlight"
highlightPreferenceValue="true"
includeOnPreferencePage="true"
label="Sample Annotation"
overviewRulerPreferenceKey="annotateElemOverviewRuler"
overviewRulerPreferenceValue="true"
textPreferenceKey="annotateElemText"
verticalRulerPreferenceKey="annotateElemVerticalRuler"
verticalRulerPreferenceValue="true">
</specification>
</extension>
<extension
point="org.eclipse.ui.views">
<view id="sourceviewsample.SampleEditor"
name="Source Viewer"
class="sourceviewsample.SampleEditor"/>
</extension>
</plugin>
我认为可能的代码希望你定义与'org.eclipse.ui.editors.annotationTypes'和'组织注释。 eclipse.ui.editors.markerAnnotationSpecification'扩展点。 –
您添加了注释类型,但您是否添加了注释? – nitind
是的,我也添加了注释:)我认为这些扩展点只有在使用标记时才需要。请纠正我,如果我错了 – user1223879