private class DefineFoldingRegionAction extends TextEditorAction { public DefineFoldingRegionAction(ResourceBundle bundle, String prefix, ITextEditor editor) { super(bundle, prefix, editor); } private IAnnotationModel getAnnotationModel(ITextEditor editor) { return (IAnnotationModel)editor.getAdapter(ProjectionAnnotationModel.class); } /* * @see org.eclipse.jface.action.Action#run() */ public void run() { ITextEditor editor= getTextEditor(); ISelection selection= editor.getSelectionProvider().getSelection(); if (selection instanceof ITextSelection) { ITextSelection textSelection= (ITextSelection)selection; if (!textSelection.isEmpty()) { IAnnotationModel model= getAnnotationModel(editor); if (model != null) { int start= textSelection.getStartLine(); int end= textSelection.getEndLine(); try { IDocument document= editor.getDocumentProvider().getDocument(editor.getEditorInput()); int offset= document.getLineOffset(start); int endOffset= document.getLineOffset(end) + document.getLineLength(end); // bug fix Iterator e= model.getAnnotationIterator(); // added while (e.hasNext()) { // added ProjectionAnnotation annotation= (ProjectionAnnotation)e.next(); // added if (model.getPosition(annotation).getOffset() == offset) // added model.removeAnnotation(annotation); // added } // added if (end > start) { // added Position position= new Position(offset, endOffset - offset); model.addAnnotation(new ProjectionAnnotation(), position); } // added } catch (BadLocationException x) { // ignore } } } } } }