1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| int a; //indice du début de mon texte à surligner
int l; //longueur de mon texte à surligner
IEditorPart editor = IDE.openEditor(myWorkbenchPage, myFfile, true);
EditorPart part = (EditorPart)editor;
Color bColor = new Color(null, 144, 162, 219); //un bleu par exemple
ISourceViewer source = null;
StyledText text = null;
if(part instanceof CompilationUnitEditor) { //éditeur JAVA
CompilationUnitEditor cue = (CompilationUnitEditor)editor;
source = cue.getViewer();
text = source.getTextWidget();
}
else if(part instanceof CEditor) { //editeur C
CEditor edit = (CEditor)part;
source = edit.getCSourceViewer();
text = source.getTextWidget();
}
if(text != null) {
//on récupère les StyleRange existants et on change le background.
//ne pas créer de nouveau StyleRange permet de ne rien écraser
StyleRange[] sr = text.getStyleRanges(a, l);
for(int g=0; g<sr.length; g++) {
sr[g].background = bColor;
text.setStyleRange(sr[g]);
}
//on place le curseur à la fin de ce qu'on vient de colorer...
ISelection select = new TextSelection(a+l, 0);
editor.getEditorSite().getSelectionProvider().setSelection(select);
} |
Partager