下面列出了怎么用org.eclipse.jface.text.IPainter的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Synchronizes all annotations with the eclipse annotation painter.
*/
public void syncAnnotationTypes() {
mPainter.removeAllAnnotationTypes();
getSourceViewer().getTextWidget().setLineSpacing(0);
for (Type displayType : mShowAnnotationsMenu.getSelectedTypes()) {
showAnnotationType(displayType, true);
}
if (!mShowAnnotationsMenu.getSelectedTypes().contains(getAnnotationMode())) {
showAnnotationType(getAnnotationMode(), true);
}
mPainter.paint(IPainter.CONFIGURATION);
}
/**
* Initialize foreground, background color, current line highlight from the
* current theme if needed.
*
*/
private void applyThemeEditorIfNeeded() {
if (!initializeViewerColors) {
StyledText styledText = viewer.getTextWidget();
((ITheme) tokenProvider).initializeViewerColors(styledText);
initializeViewerColors = true;
}
if (updateTextDecorations) {
return;
}
try {
// Ugly code to update "current line highlight" :
// - get the PaintManager from the ITextViewer with reflection.
// - get the list of IPainter of PaintManager with reflection
// - loop for IPainter to retrieve CursorLinePainter which manages "current line
// highlight".
PaintManager paintManager = ClassHelper.getFieldValue(viewer, "fPaintManager", TextViewer.class);
if (paintManager == null) {
return;
}
List<IPainter> painters = ClassHelper.getFieldValue(paintManager, "fPainters", PaintManager.class);
if (painters == null) {
return;
}
for (IPainter painter : painters) {
if (painter instanceof CursorLinePainter) {
// Update current line highlight
Color background = tokenProvider.getEditorCurrentLineHighlight();
if (background != null) {
((CursorLinePainter) painter).setHighlightColor(background);
}
updateTextDecorations = true;
}
}
} catch (Exception e) {
TMUIPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TMUIPlugin.PLUGIN_ID, e.getMessage(), e));
}
}
/**
* Sets the given document as the indents' model input and updates
* the model accordingly.
*
* @param document the models's new input document, <code>null</code> if none
* @param painter the painter to be notified about model changes, <code>null</code> if none
*/
public void applyToDocument(IDocument document, IPainter painter, StyledText widget) {
deactivate();
textWidget = widget;
documentPainter = painter;
listenedDocument = document;
listenedDocument.addDocumentListener(documentListener);
requestUpdate();
}
@Override
public void paint(int reason) {
IDocument document= fSourceViewer.getDocument();
if (document == null) {
deactivate(false);
return;
}
Point selection= fSourceViewer.getSelectedRange();
if (selection.y > 0) {
deactivate(true);
return;
}
IRegion pair= fMatcher.match(document, selection.x);
if (pair == null) {
deactivate(true);
return;
}
if (fIsActive) {
if (IPainter.CONFIGURATION == reason) {
// redraw current highlighting
handleDrawRequest(null);
} else if (pair.getOffset() != fPairPosition.getOffset() ||
pair.getLength() != fPairPosition.getLength() ||
fMatcher.getAnchor() != fAnchor || fMatcher.getMatchFlags() != fMatchFlags)
{
// otherwise only do something if position is different
// remove old highlighting
handleDrawRequest(null);
// update position
updatePos(pair);
// apply new highlighting
handleDrawRequest(null);
}
} else {
fIsActive = true;
updatePos(pair);
fTextWidget.addPaintListener(this);
fPaintPositionManager.managePosition(fPairPosition);
handleDrawRequest(null);
}
}
/**
* Schedules the update of indent guides model to be run. The job is added to a
* queue of waiting jobs, and will be run when it arrives at the beginning
* of the queue.
*/
protected void requestUpdate() {
Job job = new Job(Messages.UpdateIndentGuides) {
@Override
protected IStatus run(IProgressMonitor monitor) {
IDocument document = listenedDocument;
if (document != null) {
try {
final int[] tabSizeNah = new int[1];
tabSizeNah[0] = -1;
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
if (!textWidget.isDisposed()){
tabSizeNah[0] = textWidget.getTabs(); //#^(@!! gui thread expected
}
}
});
if (tabSizeNah[0] == -1) { // Widget is disposed
return Status.OK_STATUS;
}
CharSequence chars = document.get();
assumeIndents(IndentsParser.buildDescriptors(chars, tabSizeNah[0], eolPrefix));
} catch (Exception e) {
LogHelper.logError(e);
}
if (documentPainter != null) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
IPainter painter = documentPainter;
if (painter != null)
painter.paint(IPainter.CONFIGURATION);
}
});
}
}
return Status.OK_STATUS;
}
};
job.schedule();
}
public void addPainter(IPainter painter) {
viewer.addPainter(painter);
}
public void removePainter(IPainter painter) {
viewer.removePainter(painter);
}