* @param selection
*/
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
if (selection instanceof StructuredSelection) {
AnnotationSelection annotations = new AnnotationSelection((StructuredSelection) selection);
// only process these selection if the annotations belong
// to the current editor instance
if (getSite().getPage().getActiveEditor() == this) {
if (!annotations.isEmpty()) {
highlight(annotations.getFirst().getBegin(), annotations.getLast().getEnd()
- annotations.getFirst().getBegin());
// move caret to new position when selected outside of the editor
if (AnnotationEditor.this != part) {
// Note: The caret cannot be placed between line delimiters
// See bug UIMA-1470
int newCaretOffset = annotations.getLast().getEnd();
String text = getSourceViewer().getTextWidget().getText();
if (newCaretOffset > 0 && newCaretOffset < text.length()) {
char beforeCaret = text.charAt(newCaretOffset -1);
char afterCaret = text.charAt(newCaretOffset);