Author: pkluegl Date: Thu Nov 3 16:36:07 2011 New Revision: 1197208 URL: http://svn.apache.org/viewvc?rev=1197208&view=rev Log: UIMA-2273 Reverted and added changes again without new formatting
Modified: uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java Modified: uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java URL: http://svn.apache.org/viewvc/uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java?rev=1197208&r1=1197207&r2=1197208&view=diff ============================================================================== --- uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java (original) +++ uima/uimaj/trunk/uimaj-ep-cas-editor/src/main/java/org/apache/uima/caseditor/editor/AnnotationEditor.java Thu Nov 3 16:36:07 2011 @@ -142,28 +142,29 @@ import org.eclipse.ui.views.contentoutli /** * An editor to annotate text. */ -public final class AnnotationEditor extends StatusTextEditor implements ICasEditor, - ISelectionListener { - - private abstract class AbstractAnnotateAction extends Action implements ISelectionChangedListener { - +public final class AnnotationEditor extends StatusTextEditor implements ICasEditor, ISelectionListener { + + private abstract class AbstractAnnotateAction extends Action + implements ISelectionChangedListener { + public void selectionChanged(SelectionChangedEvent event) { - setEnabled(AnnotationEditor.this.getSelection().y - AnnotationEditor.this.getSelection().x > 0); + setEnabled(AnnotationEditor.this.getSelection().y - + AnnotationEditor.this.getSelection().x > 0); } } - + /** * This action annotates the selected text with a defined tag. */ private class QuickAnnotateAction extends AbstractAnnotateAction { - + private static final String ID = "QuickAnnotate"; - + private StyledText mTextWidget; /** * Initializes a new instance. - * + * * @param textWidget */ QuickAnnotateAction(StyledText textWidget) { @@ -181,7 +182,7 @@ public final class AnnotationEditor exte // get old annotations of current type for this area // if there is something ... the delete them and add Collection<AnnotationFS> oldAnnotations = getAnnotation(getDocument().getCAS(), - getAnnotationMode(), new Span(selection.x, selection.y)); + getAnnotationMode(), new Span(selection.x, selection.y)); if (!oldAnnotations.isEmpty()) { getDocument().removeFeatureStructures(oldAnnotations); @@ -207,14 +208,15 @@ public final class AnnotationEditor exte private class SmartAnnotateAction extends AbstractAnnotateAction { private static final String ID = "Annotate"; - + @Override public void run() { if (isSomethingSelected()) { - QuickTypeSelectionDialog typeDialog = new QuickTypeSelectionDialog(Display.getCurrent() - .getActiveShell(), AnnotationEditor.this); + QuickTypeSelectionDialog typeDialog = + new QuickTypeSelectionDialog(Display.getCurrent().getActiveShell(), + AnnotationEditor.this); typeDialog.open(); } @@ -275,30 +277,29 @@ public final class AnnotationEditor exte * document with the annotations in eclipse. */ private class DocumentListener extends AbstractAnnotationDocumentListener { - + /** * Adds a collection of annotations. - * + * * @param annotations */ @Override public void addedAnnotation(Collection<AnnotationFS> annotations) { - IAnnotationModelExtension annotationModel = (IAnnotationModelExtension) getDocumentProvider() - .getAnnotationModel(getEditorInput()); - + IAnnotationModelExtension annotationModel = (IAnnotationModelExtension) getDocumentProvider().getAnnotationModel(getEditorInput()); + Map<Annotation, Position> addAnnotationMap = new HashMap<Annotation, Position>(); - + for (AnnotationFS annotation : annotations) { - addAnnotationMap.put(new EclipseAnnotationPeer(annotation), - new Position(annotation.getBegin(), annotation.getEnd() - annotation.getBegin())); + addAnnotationMap.put(new EclipseAnnotationPeer(annotation), new Position(annotation.getBegin(), + annotation.getEnd() - annotation.getBegin())); } - + annotationModel.replaceAnnotations(null, addAnnotationMap); } /** * Removes a collection of annotations. - * + * * @param deletedAnnotations */ @Override @@ -312,35 +313,32 @@ public final class AnnotationEditor exte highlight(0, 0); // TODO: only if removed annotation was selected - IAnnotationModelExtension annotationModel = (IAnnotationModelExtension) getDocumentProvider() - .getAnnotationModel(getEditorInput()); - + IAnnotationModelExtension annotationModel = (IAnnotationModelExtension) getDocumentProvider().getAnnotationModel(getEditorInput()); + Annotation removeAnnotations[] = new Annotation[deletedAnnotations.size()]; int removeAnnotationsIndex = 0; for (AnnotationFS annotation : deletedAnnotations) { removeAnnotations[removeAnnotationsIndex++] = new EclipseAnnotationPeer(annotation); } - + annotationModel.replaceAnnotations(removeAnnotations, null); } /** - * + * * @param annotations */ @Override public void updatedAnnotation(Collection<AnnotationFS> annotations) { - - IAnnotationModelExtension annotationModel = (IAnnotationModelExtension) getDocumentProvider() - .getAnnotationModel(getEditorInput()); - + + IAnnotationModelExtension annotationModel = (IAnnotationModelExtension) getDocumentProvider().getAnnotationModel(getEditorInput()); + for (AnnotationFS annotation : annotations) { - annotationModel.modifyAnnotationPosition(new EclipseAnnotationPeer(annotation), + annotationModel.modifyAnnotationPosition(new EclipseAnnotationPeer(annotation), new Position(annotation.getBegin(), annotation.getEnd() - annotation.getBegin())); } - - selectionChanged(getSite().getPage().getActivePart(), - mFeatureStructureSelectionProvider.getSelection()); + + selectionChanged(getSite().getPage().getActivePart(), mFeatureStructureSelectionProvider.getSelection()); } public void changed() { @@ -348,14 +346,15 @@ public final class AnnotationEditor exte syncAnnotations(); } - + public void viewChanged(String oldViewName, String newViewName) { - // TODO: Currently do nothing ... + // TODO: Currently do nothing ... } - + public void casDocumentChanged(ICasDocument oldDocument, ICasDocument newDocument) { // TODO: Currently do nothing ... } + } /** @@ -395,7 +394,8 @@ public final class AnnotationEditor exte // Workaround: It is possible check that is text under the cursor ? try { offset = textWidget.getOffsetAtLocation(new Point(e.x, e.y)); - } catch (IllegalArgumentException e2) { + } + catch (IllegalArgumentException e2) { return; } @@ -404,8 +404,8 @@ public final class AnnotationEditor exte mCandidate = view.get(offset); if (mCandidate != null) { - textWidget.setSelectionRange(mCandidate.getBegin(), - mCandidate.getEnd() - mCandidate.getBegin()); + textWidget.setSelectionRange(mCandidate.getBegin(), mCandidate.getEnd() + - mCandidate.getBegin()); } } } @@ -452,10 +452,12 @@ public final class AnnotationEditor exte // ask document provider for this // getAnnotation must be added to cas document provider - AnnotationStyle style = getAnnotationStyle(eclipseAnnotation.getAnnotationFS().getType()); + AnnotationStyle style = getAnnotationStyle( + eclipseAnnotation.getAnnotationFS().getType()); return style.getLayer(); - } else { + } + else { return 0; } } @@ -475,14 +477,15 @@ public final class AnnotationEditor exte } /** - * This implementation imitates the behavior without the {@link IAnnotationAccessExtension}. + * This implementation imitates the behavior without the + * {@link IAnnotationAccessExtension}. */ public boolean isSubtype(Object annotationType, Object potentialSupertype) { Type type = getDocument().getCAS().getTypeSystem().getType((String) annotationType); - return mShowAnnotationsMenu.getSelectedTypes().contains(type) - || getAnnotationMode().equals(type); + return mShowAnnotationsMenu.getSelectedTypes().contains(type) || + getAnnotationMode().equals(type); } public void paint(Annotation annotation, GC gc, Canvas canvas, Rectangle bounds) { @@ -492,54 +495,54 @@ public final class AnnotationEditor exte // TODO: Move to external class static class CasViewMenu extends ContributionItem { - + private AnnotationEditor casEditor; - + public CasViewMenu(AnnotationEditor casEditor) { this.casEditor = casEditor; - } - + } + @Override public void fill(Menu parentMenu, int index) { - + CAS cas = casEditor.getDocument().getCAS(); - - for (Iterator<CAS> it = cas.getViewIterator(); it.hasNext();) { - + + for (Iterator<CAS> it = cas.getViewIterator(); it.hasNext(); ) { + CAS casView = it.next(); final String viewName = casView.getViewName(); - + final MenuItem actionItem = new MenuItem(parentMenu, SWT.CHECK); actionItem.setText(viewName); - + // TODO: Disable non-text views, check mime-type try { actionItem.setEnabled(cas.getDocumentText() != null); } catch (Throwable t) { // TODO: Not nice, discuss better solution on ml - actionItem.setEnabled(false); + actionItem.setEnabled(false); } - + // TODO: Add support for non text views, editor has - // to display some error message - + // to display some error message + if (cas.getViewName().equals(viewName)) - actionItem.setSelection(true); - + actionItem.setSelection(true); + // TODO: move this to an action actionItem.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { // Trigger only if view is really changed // TODO: Move this check to the document itself ... - if (!casEditor.getDocument().getCAS().getViewName().equals(viewName)) { - casEditor.showView(viewName); + if(!casEditor.getDocument().getCAS().getViewName().equals(viewName)) { + casEditor.showView(viewName); } } }); } } } - + private Type mAnnotationMode; /** @@ -567,13 +570,13 @@ public final class AnnotationEditor exte private DocumentListener mAnnotationSynchronizer; private AnnotationStyleChangeListener mAnnotationStyleListener; - + private Collection<Type> shownAnnotationTypes = new HashSet<Type>(); - + private IPropertyChangeListener preferenceStoreChangeListener; - + private CasDocumentProvider casDocumentProvider; - + /** * Creates an new AnnotationEditor object. */ @@ -582,26 +585,26 @@ public final class AnnotationEditor exte @Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { - casDocumentProvider = CasDocumentProviderFactory.instance().getDocumentProvider(input); - + casDocumentProvider = + CasDocumentProviderFactory.instance().getDocumentProvider(input); + setDocumentProvider(new TextDocumentProvider(casDocumentProvider)); - + super.init(site, input); } - + public CasDocumentProvider getCasDocumentProvider() { return casDocumentProvider; } - + /** * Retrieves annotation editor adapters. - * + * * @param adapter * @return an adapter or null */ @Override - public Object getAdapter(@SuppressWarnings("rawtypes") - Class adapter) { + public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) { if (IContentOutlinePage.class.equals(adapter) && getDocument() != null) { if (mOutlinePage == null) { @@ -609,9 +612,11 @@ public final class AnnotationEditor exte } return mOutlinePage; - } else if (CAS.class.equals(adapter) && getDocument() != null) { + } + else if (CAS.class.equals(adapter) && getDocument() != null) { return getDocument().getCAS(); - } else { + } + else { return super.getAdapter(adapter); } @@ -627,34 +632,20 @@ public final class AnnotationEditor exte mPainter = new AnnotationPainter(sourceViewer, new AnnotationAccess()); sourceViewer.addPainter(mPainter); - + return sourceViewer; } private void setTextSize(int newSize) { Font font = getSourceViewer().getTextWidget().getFont(); - + if (font.getFontData().length > 0) { FontData fd = font.getFontData()[0]; - getSourceViewer().getTextWidget().setFont( - new Font(font.getDevice(), fd.getName(), newSize, fd.getStyle())); + getSourceViewer().getTextWidget().setFont(new Font(font.getDevice(), + fd.getName(), newSize, fd.getStyle())); } } - - /** - * Used to inform about input changes - * - * @param parent - */ - @Override - protected void handleElementContentReplaced() { - super.handleElementContentReplaced(); - ICasDocument oldDocument = getDocument(); - setInput(getEditorInput()); - ICasDocument newDocument = getDocument(); - oldDocument.switchCasDocument(oldDocument, newDocument); - } - + /** * Configures the editor. * @@ -676,9 +667,9 @@ public final class AnnotationEditor exte // that // // Until this can be used we improvise by registering a couple of listeners which - // compute a caret offset change based on events which could change it, + // compute a caret offset change based on events which could change it, // that are key and mouse listeners. - + getSourceViewer().getTextWidget().addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { int newCaretOffset = getSourceViewer().getTextWidget().getCaretOffset(); @@ -711,33 +702,31 @@ public final class AnnotationEditor exte public void mouseUp(MouseEvent e) { int newCaretOffset = getSourceViewer().getTextWidget().getCaretOffset(); - + if (newCaretOffset != mCursorPosition) { mCursorPosition = newCaretOffset; refreshSelection(); - } + } } }); + // UIMA-2242: // When a single character is selected with a double click the selection // changes but the caret does not. // A press on enter now fails to create an annotation. - + // UIMA-2247: // Changed again to ensure that also selection from the find dialog // can be detected - getSourceViewer().getSelectionProvider().addSelectionChangedListener( - new ISelectionChangedListener() { - public void selectionChanged(SelectionChangedEvent event) { - mFeatureStructureSelectionProvider.setSelection(new AnnotationEditorSelection( - (ITextSelection) event.getSelection(), new StructuredSelection( - ModelFeatureStructure.create(getDocument(), - getSelectedAnnotations())))); - } - }); - + getSourceViewer().getSelectionProvider().addSelectionChangedListener(new ISelectionChangedListener() { + public void selectionChanged(SelectionChangedEvent event) { + mFeatureStructureSelectionProvider.setSelection(new AnnotationEditorSelection((ITextSelection) event.getSelection(), + new StructuredSelection(ModelFeatureStructure.create(getDocument(), getSelectedAnnotations())))); + } + }); + DragSource dragSource = new DragSource(getSourceViewer().getTextWidget(), DND.DROP_COPY); Transfer[] types = new Transfer[] { FeatureStructureTransfer.getInstance() }; @@ -750,38 +739,37 @@ public final class AnnotationEditor exte getSourceViewer().setEditable(false); getSite().setSelectionProvider(mFeatureStructureSelectionProvider); - + + // Retrieve font size from preference store, default is 15 IPreferenceStore prefStore = CasEditorPlugin.getDefault().getPreferenceStore(); - int textSize = prefStore - .getInt(AnnotationEditorPreferenceConstants.ANNOTATION_EDITOR_TEXT_SIZE); - + int textSize = prefStore.getInt(AnnotationEditorPreferenceConstants.ANNOTATION_EDITOR_TEXT_SIZE); + if (textSize > 0) { setTextSize(textSize); } - + preferenceStoreChangeListener = (new IPropertyChangeListener() { - + public void propertyChange(PropertyChangeEvent event) { - if (AnnotationEditorPreferenceConstants.ANNOTATION_EDITOR_TEXT_SIZE.equals(event - .getProperty())) { + if (AnnotationEditorPreferenceConstants.ANNOTATION_EDITOR_TEXT_SIZE.equals(event.getProperty())) { Integer textSize = (Integer) event.getNewValue(); - + if (textSize != null && textSize > 0) { setTextSize(textSize); } } } }); - + prefStore.addPropertyChangeListener(preferenceStoreChangeListener); - + // Note: In case the CAS could not be created the editor will be initialized with - // a null document (getDocument() == null). Depending on the error it might be + // a null document (getDocument() == null). Depending on the error it might be // possible to recover and the editor input will be set again. The createPartControl // method might be called with a document or without one, both cases must be supported. - - // Perform the document dependent initialization + + // Perform the document dependent initialization initiallySynchronizeUI(); } @@ -791,7 +779,7 @@ public final class AnnotationEditor exte /** * Checks if the current instance is editable. - * + * * @return false */ @Override @@ -799,50 +787,65 @@ public final class AnnotationEditor exte return false; } + + + /** + * Used to inform about input changes + * + * @param parent + */ + @Override + protected void handleElementContentReplaced() { + super.handleElementContentReplaced(); + ICasDocument oldDocument = getDocument(); + setInput(getEditorInput()); + ICasDocument newDocument = getDocument(); + oldDocument.switchCasDocument(oldDocument, newDocument); + } + @Override protected void doSetInput(IEditorInput input) throws CoreException { - + super.doSetInput(input); - - if (CasEditorPlugin.getDefault().getAndClearShowMigrationDialogFlag()) { + + + if (CasEditorPlugin.getDefault(). + getAndClearShowMigrationDialogFlag()) { getSite().getShell().getDisplay().asyncExec(new Runnable() { - public void run() { - MessageDialog - .openInformation( - getSite().getShell(), - "Cas Editor Project Removed", - "The Cas Editor Project support was removed from this release and" - + " your existing Cas Editor Projects have been migrated. If you still want to use the " - + "Cas Editor Project support please install the Cas Editor 2.3.1 again.\n\n" - + "The Analysis Engine and Cas Consumer run support was also removed and " - + "is replaced by the new Analysis Engine Launch plugin."); - } + public void run() { + MessageDialog.openInformation(getSite().getShell(),"Cas Editor Project Removed", + "The Cas Editor Project support was removed from this release and" + + " your existing Cas Editor Projects have been migrated. If you still want to use the " + + "Cas Editor Project support please install the Cas Editor 2.3.1 again.\n\n" + + "The Analysis Engine and Cas Consumer run support was also removed and " + + "is replaced by the new Analysis Engine Launch plugin."); + } }); } - + + if (getDocument() != null) { - + // Synchronize shown types with the editor - String shownTypesString = getCasDocumentProvider().getSessionPreferenceStore(input) - .getString("LastShownTypes"); - + String shownTypesString = getCasDocumentProvider().getSessionPreferenceStore(input).getString("LastShownTypes"); + String[] shownTypes = shownTypesString.split(";"); - + for (String shownType : shownTypes) { - + Type type = getDocument().getType(shownType); - - // Types can be deleted from the type system but still be marked + + // Types can be deleted from the type system but still be marked // as shown in the .dotCorpus file, in that case the type // name cannot be mapped to a type and should be ignored. - + if (type != null) shownAnnotationTypes.add(type); } - + if (getSourceViewer() != null) { - - // This branch is usually only executed when the + + // This branch is usually only executed when the // input was updated because it could not be opened the // first time trough an error e.g. no type system available // @@ -852,13 +855,13 @@ public final class AnnotationEditor exte // must be synchronized now initiallySynchronizeUI(); - + // Views which fetch content from the editors document still believe // the editor document is not available. // Send a partBroughtToTop event to the views to update their content // on this currently active editor. IWorkbenchPage page = getSite().getWorkbenchWindow().getActivePage(); - + for (IWorkbenchPart view : page.getViews()) { if (view instanceof PageBookView) { ((PageBookView) view).partBroughtToTop(getEditorSite().getPart()); @@ -867,7 +870,7 @@ public final class AnnotationEditor exte } } } - + // The editor status support is abused to display different control than the // text control when displaying text is not possible, e.g. because the sofa // is not a text sofa or not set at all @@ -875,7 +878,7 @@ public final class AnnotationEditor exte protected boolean isErrorStatus(IStatus status) { return super.isErrorStatus(status) || getDocument().getCAS().getDocumentText() == null; } - + /** * Initialized the UI from the freshly set document. * @@ -883,71 +886,73 @@ public final class AnnotationEditor exte */ private void initiallySynchronizeUI() { if (getDocument() != null) { - - mShowAnnotationsMenu = new ShowAnnotationsMenu(getDocument().getCAS().getTypeSystem(), - shownAnnotationTypes); + + mShowAnnotationsMenu = new ShowAnnotationsMenu( + getDocument().getCAS().getTypeSystem(), shownAnnotationTypes); mShowAnnotationsMenu.addListener(new IShowAnnotationsListener() { public void selectionChanged(Collection<Type> selection) { - + // if changes selection is either larger, or smaller // if larger an annotation type was added // if smaller one was removed if (shownAnnotationTypes.size() < selection.size()) { List<Type> clonedCollection = new ArrayList<Type>(selection); clonedCollection.removeAll(shownAnnotationTypes); - + Type addedAnnotationType = clonedCollection.get(0); showAnnotationType(addedAnnotationType, true); - } else if (selection.size() < shownAnnotationTypes.size()) { + } + else if (selection.size() < shownAnnotationTypes.size()) { List<Type> clonedCollection = new ArrayList<Type>(shownAnnotationTypes); clonedCollection.removeAll(selection); - + Type removedAnnotationType = clonedCollection.get(0); showAnnotationType(removedAnnotationType, false); } - + // Repaint after annotations are changed mPainter.paint(IPainter.CONFIGURATION); - + setEditorSessionPreferences(); - + if (mEditorListener != null) { - for (IAnnotationEditorModifyListener listener : mEditorListener) + for (IAnnotationEditorModifyListener listener : mEditorListener) listener.showAnnotationsChanged(selection); } } }); - - IPreferenceStore sessionPreferences = getCasDocumentProvider().getSessionPreferenceStore( - getEditorInput()); - + + IPreferenceStore sessionPreferences = + getCasDocumentProvider().getSessionPreferenceStore(getEditorInput()); + // TODO: Define constants for these settings! - + String lastModeTypeName = sessionPreferences.getString("LastUsedModeType"); - + if (lastModeTypeName.length() == 0) lastModeTypeName = CAS.TYPE_NAME_ANNOTATION; - + setAnnotationMode(getDocument().getType(lastModeTypeName)); - + String lastActiveViewName = sessionPreferences.getString("LastActiveCasViewName"); - + try { // TODO: Missing compatibility check!!! getDocument().getCAS().getView(lastActiveViewName); showView(lastActiveViewName); - } catch (CASRuntimeException e) { + } + catch (CASRuntimeException e) { // ignore, view is not available // Note: Using exceptions for control flow is very bad practice // TODO: Is there a way to check which views are available?! - // Maybe we should iterate over all available views, and then - // check if it is available! + // Maybe we should iterate over all available views, and then + // check if it is available! showView(CAS.NAME_DEFAULT_SOFA); } } } - + @Override protected void editorContextMenuAboutToShow(IMenuManager menu) { super.editorContextMenuAboutToShow(menu); @@ -956,9 +961,8 @@ public final class AnnotationEditor exte menu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, new Separator()); menu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, getAction(SmartAnnotateAction.ID)); menu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, getAction(QuickAnnotateAction.ID)); - menu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, - getAction(ITextEditorActionDefinitionIds.DELETE)); - + menu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, getAction(ITextEditorActionDefinitionIds.DELETE)); + TypeSystem typeSytem = getDocument().getCAS().getTypeSystem(); // mode menu @@ -966,15 +970,14 @@ public final class AnnotationEditor exte menu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, modeMenuManager); ModeMenu modeMenu = new ModeMenu(typeSytem, this); - modeMenu.addListener(new IModeMenuListener() { + modeMenu.addListener(new IModeMenuListener(){ - public void modeChanged(Type newMode) { - IAction actionToExecute = new ChangeModeAction(newMode, newMode.getShortName(), - AnnotationEditor.this); + public void modeChanged(Type newMode) { + IAction actionToExecute = new ChangeModeAction(newMode, newMode.getShortName(), + AnnotationEditor.this); - actionToExecute.run(); - } - }); + actionToExecute.run(); + }}); modeMenuManager.add(modeMenu); @@ -982,7 +985,7 @@ public final class AnnotationEditor exte MenuManager showAnnotationMenu = new MenuManager("Show Annotations"); menu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, showAnnotationMenu); showAnnotationMenu.add(mShowAnnotationsMenu); - + // view menu MenuManager casViewMenuManager = new MenuManager("CAS Views"); menu.appendToGroup(IWorkbenchActionConstants.MB_ADDITIONS, casViewMenuManager); @@ -1005,7 +1008,7 @@ public final class AnnotationEditor exte /** * Returns the current <code>AnnotationDocument</code> of this editor. - * + * * @return current <code>AnnotationDocument</code> */ public ICasDocument getDocument() { @@ -1015,10 +1018,10 @@ public final class AnnotationEditor exte public void reopenEditorWithNewTypeSystem() { setInput(getEditorInput()); } - + /** * Returns the current annotation type. - * + * * @return - current annotation type */ public Type getAnnotationMode() { @@ -1027,7 +1030,7 @@ public final class AnnotationEditor exte /** * Sets the new annotation type. - * + * * @param type */ protected void setAnnotationMode(Type type) { @@ -1051,7 +1054,7 @@ public final class AnnotationEditor exte public Collection<Type> getShownAnnotationTypes() { return Collections.unmodifiableCollection(shownAnnotationTypes); } - + public void setShownAnnotationType(Type type, boolean isShown) { mShowAnnotationsMenu.setSelectedType(type, isShown); } @@ -1068,7 +1071,7 @@ public final class AnnotationEditor exte /** * Retrieves an <code>AnnotationStyle</code> from the underlying storage. - * + * * @param element * @param type * @return @@ -1076,12 +1079,13 @@ public final class AnnotationEditor exte public AnnotationStyle getAnnotationStyle(Type type) { if (type == null) throw new IllegalArgumentException("type parameter must not be null!"); - - IPreferenceStore prefStore = getCasDocumentProvider().getTypeSystemPreferenceStore( - getEditorInput()); - + + IPreferenceStore prefStore = getCasDocumentProvider(). + getTypeSystemPreferenceStore(getEditorInput()); + return AnnotationStyle.getAnnotationStyleFromStore(prefStore, type.getName()); } + /** * Sets an annotation style. @@ -1094,61 +1098,61 @@ public final class AnnotationEditor exte // TODO: Disk must be accessed for every changed annotation style // add a second method which can take all changed styles public void setAnnotationStyle(AnnotationStyle style) { - IPreferenceStore prefStore = getCasDocumentProvider().getTypeSystemPreferenceStore( - getEditorInput()); + IPreferenceStore prefStore = getCasDocumentProvider().getTypeSystemPreferenceStore(getEditorInput()); AnnotationStyle.putAnnotatationStyleToStore(prefStore, style); - + getCasDocumentProvider().saveTypeSystemPreferenceStore(getEditorInput()); } - + /** * Set the shown annotation status of a type. * * @param type - * @param isVisible - * if true the type is shown, if false the type it not shown + * @param isVisible if true the type is shown, if false the type + * it not shown */ private void showAnnotationType(Type type, boolean isVisible) { AnnotationStyle style = getAnnotationStyle(type); - + if (isVisible) { - + IDrawingStrategy strategy = DrawingStyle.createStrategy(style); - + // It might not be possible to create the drawing strategy trough // configuration errors, in this case the drawing strategy will be ignored if (strategy != null) { - + if (style.getStyle().equals(AnnotationStyle.Style.TAG)) { getSourceViewer().getTextWidget().setLineSpacing(13); } - + mPainter.addDrawingStrategy(type.getName(), strategy); mPainter.addAnnotationType(type.getName(), type.getName()); java.awt.Color color = style.getColor(); - mPainter.setAnnotationTypeColor(type.getName(), - new Color(null, color.getRed(), color.getGreen(), color.getBlue())); + mPainter.setAnnotationTypeColor(type.getName(), new Color(null, color.getRed(), + color.getGreen(), color.getBlue())); } - + shownAnnotationTypes.add(type); - } else { + } + else { mPainter.removeAnnotationType(type.getName()); shownAnnotationTypes.remove(type); - + if (style.getStyle().equals(AnnotationStyle.Style.TAG)) { // TODO: only if no more TAG styles are active // How to figure that out ?! boolean isKeepLineSpacing = false; - - for (Type shownType : shownAnnotationTypes) { + + for(Type shownType : shownAnnotationTypes) { AnnotationStyle potentialTagStyle = getAnnotationStyle(shownType); - + if (AnnotationStyle.Style.TAG.equals(potentialTagStyle.getStyle())) { isKeepLineSpacing = true; break; } } - + if (!isKeepLineSpacing) getSourceViewer().getTextWidget().setLineSpacing(0); } @@ -1162,7 +1166,7 @@ public final class AnnotationEditor exte mPainter.removeAllAnnotationTypes(); getSourceViewer().getTextWidget().setLineSpacing(0); - + for (Type displayType : mShowAnnotationsMenu.getSelectedTypes()) { showAnnotationType(displayType, true); } @@ -1179,47 +1183,46 @@ public final class AnnotationEditor exte IAnnotationModel annotationModel = getDocumentProvider().getAnnotationModel(getEditorInput()); ((IAnnotationModelExtension) annotationModel).removeAllAnnotations(); } - + private void syncAnnotations() { - + removeAllAnnotations(); - + // Remove all annotation from the model IAnnotationModel annotationModel = getDocumentProvider().getAnnotationModel(getEditorInput()); ((IAnnotationModelExtension) annotationModel).removeAllAnnotations(); - + // Add all annotation to the model // copy annotations into annotation model final Iterator<AnnotationFS> mAnnotations = getDocument().getCAS().getAnnotationIndex() .iterator(); - + // TODO: Build first a map, and then pass all annotations at once Map annotationsToAdd = new HashMap(); - + while (mAnnotations.hasNext()) { AnnotationFS annotationFS = mAnnotations.next(); - annotationsToAdd - .put(new EclipseAnnotationPeer(annotationFS), new Position(annotationFS.getBegin(), - annotationFS.getEnd() - annotationFS.getBegin())); + annotationsToAdd.put(new EclipseAnnotationPeer(annotationFS), new Position( + annotationFS.getBegin(), annotationFS.getEnd() - annotationFS.getBegin())); } - + ((IAnnotationModelExtension) annotationModel).replaceAnnotations(null, annotationsToAdd); } - + /** * @param listener */ public void addAnnotationListener(IAnnotationEditorModifyListener listener) { mEditorListener.add(listener); } - + public void removeAnnotationListener(IAnnotationEditorModifyListener listener) { mEditorListener.remove(listener); } /** * Returns the selection. - * + * * @return - the selection */ public Point getSelection() { @@ -1228,7 +1231,7 @@ public final class AnnotationEditor exte /** * Highlights the given range in the editor. - * + * * @param start * @param length */ @@ -1249,8 +1252,8 @@ public final class AnnotationEditor exte } if (length != 0) { - mCurrentStyleRange = new StyleRange(start, length, text.getSelectionForeground(), - text.getSelectionBackground()); + mCurrentStyleRange = new StyleRange(start, length, text.getSelectionForeground(), text + .getSelectionBackground()); text.setStyleRange(mCurrentStyleRange); } @@ -1258,9 +1261,9 @@ public final class AnnotationEditor exte /** * Retrieves the currently selected annotation. - * + * * TODO: make this private ??? clients can use selections for this ... - * + * * @return the selected annotations or an empty list */ public List<AnnotationFS> getSelectedAnnotations() { @@ -1295,7 +1298,7 @@ public final class AnnotationEditor exte return selection; } - + /** * Returns the caret position relative to the start of the text. * @@ -1304,59 +1307,59 @@ public final class AnnotationEditor exte public int getCaretOffset() { return getSourceViewer().getTextWidget().getCaretOffset(); } - + public void showView(String viewName) { - + // TODO: Check if set view is compatible .. if not display some message! - + // TODO: Consider to clear selection if this is called in the // selectionChanged method, is that the right place?! - + // Clear all old selections, references FSes of current view mFeatureStructureSelectionProvider.clearSelection(); - + // Move the caret before the first char, otherwise it // might be placed on an index which is out of bounds in // the changed text if (getSourceViewer().getTextWidget().getText().length() > 0) getSourceViewer().getTextWidget().setCaretOffset(0); - + // De-highlight the text in the editor, because the highlight - // method has to remember the highlighted text area + // method has to remember the highlighted text area // After the text changed the offsets might be out of bound highlight(0, 0); - + // Remove all editor annotations // Changing the text with annotations might fail, because the // bounds might be invalid in the new text removeAllAnnotations(); - + // Change the view in the input document // TODO: Add support for this to the interface ((AnnotationDocument) getDocument()).switchView(viewName); - + // Retrieve the new (changed) text document and refresh the source viewer getSourceViewer().setDocument((AnnotationDocument) getDocument(), - getDocumentProvider().getAnnotationModel(getEditorInput())); + getDocumentProvider().getAnnotationModel(getEditorInput())); getSourceViewer().invalidateTextPresentation(); - + // All annotations will be synchronized in the document listener - + // Last opened view should be remembered, in case a new editor is opened setEditorSessionPreferences(); - + // Check if CAS view is compatible, only if compatible the listeners // to update the annotations in the editor can be registered // and the annotations can be synchronized if (!isErrorStatus(getCasDocumentProvider().getStatus(getEditorInput()))) { - + // Synchronize all annotation from the document with // the editor syncAnnotations(); - + // Note: If a change from a compatible view to a compatible view - // occurs there is no need be register the listeners again - + // occurs there is no need be register the listeners again + // Register listener to synchronize annotations between the // editor and the document in case the annotations // change e.g. updated in a view @@ -1364,48 +1367,49 @@ public final class AnnotationEditor exte mAnnotationSynchronizer = new DocumentListener(); getDocument().addChangeListener(mAnnotationSynchronizer); } - + // Register listener to synchronize annotation styles // between multiple open annotation editors if (mAnnotationStyleListener == null) { mAnnotationStyleListener = new AnnotationStyleChangeListener() { - + public void annotationStylesChanged(Collection<AnnotationStyle> styles) { // TODO: Only sync changed types syncAnnotationTypes(); } }; - - getCasDocumentProvider().getTypeSystemPreferenceStore(getEditorInput()) - .addPropertyChangeListener(mAnnotationStyleListener); + + getCasDocumentProvider().getTypeSystemPreferenceStore(getEditorInput()). + addPropertyChangeListener(mAnnotationStyleListener); } - + getSite().getPage().addSelectionListener(this); - } else { + } + else { // if not null ... then unregister ... listener if (mAnnotationSynchronizer != null) { getDocument().removeChangeListener(mAnnotationSynchronizer); mAnnotationSynchronizer = null; } - + if (mAnnotationStyleListener != null) { - getCasDocumentProvider().getTypeSystemPreferenceStore(getEditorInput()) - .removePropertyChangeListener(mAnnotationStyleListener); + getCasDocumentProvider().getTypeSystemPreferenceStore(getEditorInput()). + removePropertyChangeListener(mAnnotationStyleListener); mAnnotationStyleListener = null; } - + getSite().getPage().removeSelectionListener(this); } - + // According to error status toggle between text editor or // status page updatePartControl(getEditorInput()); } - + /** * Text is not editable, cause of the nature of the annotation editor. This does not mean, that * the annotations are not editable. - * + * * @return false */ @Override @@ -1415,52 +1419,53 @@ public final class AnnotationEditor exte /** * Notifies the current instance about selection changes in the workbench. - * + * * @param part * @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 beforeCaret = text.charAt(newCaretOffset -1); char afterCaret = text.charAt(newCaretOffset); - + final int cr = 0x0D; final int lf = 0x0A; if (beforeCaret == cr && afterCaret == lf) { // In case the caret offset is in the middle // of a multiple-char line delimiter place caret // before - newCaretOffset = newCaretOffset - 1; + newCaretOffset = newCaretOffset -1; } } - + // check bounds, if out of text do nothing getSourceViewer().getTextWidget().setCaretOffset(newCaretOffset); getSourceViewer().revealRange(newCaretOffset, 0); - + mFeatureStructureSelectionProvider.setSelection(selection); } - } else { + } + else { // Nothing selected, clear annotation selection highlight(0, 0); } @@ -1473,35 +1478,38 @@ public final class AnnotationEditor exte // getSourceViewer() returns null here ... but why ? return getSourceViewer().getTextWidget().getSelectionCount() != 0; } - + /** - * Set the session data which should be used to initalize the next Cas Editor which is opened. + * Set the session data which should be used to initalize the next Cas Editor which + * is opened. */ private void setEditorSessionPreferences() { - - // TODO: Define constants with prefix for these settings ... so they don't conflict with other - // plugins! - - IPreferenceStore sessionStore = getCasDocumentProvider().getSessionPreferenceStore( - getEditorInput()); - + + // TODO: Define constants with prefix for these settings ... so they don't conflict with other plugins! + + IPreferenceStore sessionStore = getCasDocumentProvider(). + getSessionPreferenceStore(getEditorInput()); + sessionStore.setValue("LastActiveCasViewName", getDocument().getCAS().getViewName()); sessionStore.setValue("LastUsedModeType", getAnnotationMode().getName()); - + StringBuilder shownTypesString = new StringBuilder(); - + for (Type shownType : getShownAnnotationTypes()) { shownTypesString.append(shownType.getName()); shownTypesString.append(";"); } - + sessionStore.setValue("LastShownTypes", shownTypesString.toString()); } /** * Creates custom annotation actions: - * - * Annotate Action Smart Annotate Action Delete Annotations Action Find Annotate Action + * + * Annotate Action + * Smart Annotate Action + * Delete Annotations Action + * Find Annotate Action */ @Override protected void createActions() { @@ -1512,13 +1520,12 @@ public final class AnnotationEditor exte getSite().setSelectionProvider(mFeatureStructureSelectionProvider); // create annotate action - QuickAnnotateAction quickAnnotateAction = new QuickAnnotateAction(getSourceViewer() - .getTextWidget()); + QuickAnnotateAction quickAnnotateAction = new QuickAnnotateAction(getSourceViewer().getTextWidget()); quickAnnotateAction.setActionDefinitionId(QuickAnnotateAction.ID); quickAnnotateAction.setText("Quick Annotate"); setAction(QuickAnnotateAction.ID, quickAnnotateAction); getSite().getSelectionProvider().addSelectionChangedListener(quickAnnotateAction); - + SmartAnnotateAction smartAnnotateAction = new SmartAnnotateAction(); smartAnnotateAction.setActionDefinitionId(SmartAnnotateAction.ID); smartAnnotateAction.setText("Annotate"); @@ -1537,41 +1544,37 @@ public final class AnnotationEditor exte setActionActivationCode(IWorkbenchActionDefinitionIds.DELETE, (char) 0, SWT.CR, SWT.NONE); // create show annotation context editing action - ShowAnnotationContextEditAction annotationContextEditAction = new ShowAnnotationContextEditAction(); + ShowAnnotationContextEditAction annotationContextEditAction = + new ShowAnnotationContextEditAction(); annotationContextEditAction.setActionDefinitionId(ITextEditorActionDefinitionIds.QUICK_ASSIST); setAction(ITextEditorActionDefinitionIds.QUICK_ASSIST, annotationContextEditAction); // Create find annotate action - FindAnnotateAction findAnnotateAction = new FindAnnotateAction(this, getSourceViewer() - .getFindReplaceTarget()); + FindAnnotateAction findAnnotateAction = new FindAnnotateAction(this, getSourceViewer().getFindReplaceTarget()); findAnnotateAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.FIND_REPLACE); setAction(ITextEditorActionConstants.FIND, findAnnotateAction); - + // Lower left side of annotation action - LowerLeftAnnotationSideAction lowerLeftAnnotationSideAction = new LowerLeftAnnotationSideAction( - getDocument()); + LowerLeftAnnotationSideAction lowerLeftAnnotationSideAction = new LowerLeftAnnotationSideAction(getDocument()); lowerLeftAnnotationSideAction.setActionDefinitionId(LowerLeftAnnotationSideAction.ID); setAction(LowerLeftAnnotationSideAction.ID, lowerLeftAnnotationSideAction); getSite().getSelectionProvider().addSelectionChangedListener(lowerLeftAnnotationSideAction); - + // Wide left side of annotation action - WideLeftAnnotationSideAction wideLeftAnnotationSide = new WideLeftAnnotationSideAction( - getDocument()); + WideLeftAnnotationSideAction wideLeftAnnotationSide = new WideLeftAnnotationSideAction(getDocument()); wideLeftAnnotationSide.setActionDefinitionId(WideLeftAnnotationSideAction.ID); setAction(WideLeftAnnotationSideAction.ID, wideLeftAnnotationSide); getSite().getSelectionProvider().addSelectionChangedListener(wideLeftAnnotationSide); - + // Lower right side of annotation - LowerRightAnnotationSideAction lowerRightAnnotationSideAction = new LowerRightAnnotationSideAction( - getDocument()); + LowerRightAnnotationSideAction lowerRightAnnotationSideAction = new LowerRightAnnotationSideAction(getDocument()); lowerRightAnnotationSideAction.setActionDefinitionId(LowerRightAnnotationSideAction.ID); setAction(LowerRightAnnotationSideAction.ID, lowerRightAnnotationSideAction); getSite().getSelectionProvider().addSelectionChangedListener(lowerRightAnnotationSideAction); - + // Wide right side of annotation - WideRightAnnotationSideAction wideRightAnnotationSideAction = new WideRightAnnotationSideAction( - getDocument()); + WideRightAnnotationSideAction wideRightAnnotationSideAction = new WideRightAnnotationSideAction(getDocument()); wideRightAnnotationSideAction.setActionDefinitionId(WideRightAnnotationSideAction.ID); setAction(WideRightAnnotationSideAction.ID, wideRightAnnotationSideAction); getSite().getSelectionProvider().addSelectionChangedListener(wideRightAnnotationSideAction); @@ -1591,15 +1594,13 @@ public final class AnnotationEditor exte } CasDocumentProvider provider = getCasDocumentProvider(); - + if (provider != null) - provider.getTypeSystemPreferenceStore(getEditorInput()).removePropertyChangeListener( - mAnnotationStyleListener); - + provider.getTypeSystemPreferenceStore(getEditorInput()).removePropertyChangeListener(mAnnotationStyleListener); + if (preferenceStoreChangeListener != null) - CasEditorPlugin.getDefault().getPreferenceStore() - .removePropertyChangeListener(preferenceStoreChangeListener); - + CasEditorPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(preferenceStoreChangeListener); + super.dispose(); } @@ -1633,75 +1634,78 @@ public final class AnnotationEditor exte return dirtyParts.toArray(new AnnotationEditor[dirtyParts.size()]); } - + @Override protected Control createStatusControl(Composite parent, IStatus status) { // Type System is missing in non Cas Editor Project case if (status.getCode() == CasDocumentProvider.TYPE_SYSTEM_NOT_AVAILABLE_STATUS_CODE - && getEditorInput() != null) { + && getEditorInput() !=null) { // TODO: Is the null check on the editor input necessary ?! - + // Show a form to select a type system in the document provider, // afterwards the form calls reopenEditorWithNewTypesytem to reopen // the editor on the input return getCasDocumentProvider().createTypeSystemSelectorForm(this, parent, status); - } else if (status.getCode() == IStatus.OK) { - + } + else if (status.getCode() == IStatus.OK) { + // TODO: Figure out which page should be shown // TODO: Implement pages ... // TODO: Each page needs an ability to switch the view back to something else ... - + if (getDocument() != null && getDocument().getCAS().getDocumentText() == null) { - + // TODO: Also display the current view name ... - + Composite noTextComposite = new Composite(parent, SWT.NONE); noTextComposite.setLayout(new GridLayout(1, false)); Label noTextLabel = new Label(noTextComposite, SWT.NONE); noTextLabel.setText("Text sofa is not set!"); - + + Label switchToView = new Label(noTextComposite, SWT.NONE); switchToView.setText("Choose a view to switch to:"); - + final Combo viewSelectionCombo = new Combo(noTextComposite, SWT.READ_ONLY); - + List<String> viewNames = new ArrayList<String>(); - - for (Iterator<CAS> it = getDocument().getCAS().getViewIterator(); it.hasNext();) { + + for (Iterator<CAS> it = getDocument().getCAS().getViewIterator(); it.hasNext(); ) { viewNames.add(it.next().getViewName()); } - + viewSelectionCombo.setItems(viewNames.toArray(new String[viewNames.size()])); - + // Preselect default view, will always be there viewSelectionCombo.select(0); - + Button switchView = new Button(noTextComposite, SWT.PUSH); switchView.setText("Switch"); - + // TODO: Add a combo to select view ... - + switchView.addSelectionListener(new SelectionListener() { - + public void widgetSelected(SelectionEvent e) { // TODO; Switch to selected view in combo ... showView(viewSelectionCombo.getText()); } - + public void widgetDefaultSelected(SelectionEvent e) { } }); - + return noTextComposite; } - + return super.createStatusControl(parent, status); - } else { + } + else { return super.createStatusControl(parent, status); } } - + /** * Retrieves the annotations in the given span. */ @@ -1728,17 +1732,18 @@ public final class AnnotationEditor exte FSMatchConstraint annotatioInSpanConstraint = cf.and(embeddedBegin, embeddedEnd); - FSMatchConstraint annotationInSpanAndStrictTypeConstraint = cf.and(annotatioInSpanConstraint, - strictType); + FSMatchConstraint annotationInSpanAndStrictTypeConstraint = + cf.and(annotatioInSpanConstraint, strictType); FSIndex<AnnotationFS> allAnnotations = cas.getAnnotationIndex(type); - FSIterator<AnnotationFS> annotationInsideSpanIndex = cas.createFilteredIterator( - allAnnotations.iterator(), annotationInSpanAndStrictTypeConstraint); + FSIterator<AnnotationFS> annotationInsideSpanIndex = + cas.createFilteredIterator(allAnnotations.iterator(), + annotationInSpanAndStrictTypeConstraint); return DocumentUimaImpl.fsIteratorToCollection(annotationInsideSpanIndex); } - + /** * Retrieves the view map. */