Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / annotatedlineeditor / AnnotatedLineEditor.java
index 90653125629cf16939dc2d07deb0a7de5837a20a..340f67627b619aaee7580960a4129255381f60b3 100644 (file)
@@ -1,12 +1,11 @@
-// $Id$
 /**
-* Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
-* http://www.e-taxonomy.eu
-* 
-* The contents of this file are subject to the Mozilla Public License Version 1.1
-* See LICENSE.TXT at the top of this package for the full license terms.
-*/
+ * Copyright (C) 2007 EDIT
+ * European Distributed Institute of Taxonomy
+ * http://www.e-taxonomy.eu
+ *
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
 package eu.etaxonomy.taxeditor.annotatedlineeditor;
 
 import java.util.Iterator;
@@ -19,6 +18,7 @@ import org.eclipse.jface.text.source.ISourceViewer;
 import org.eclipse.jface.text.source.IVerticalRuler;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.editors.text.TextEditor;
 import org.eclipse.ui.texteditor.IDocumentProvider;
 
@@ -28,6 +28,7 @@ import eu.etaxonomy.cdm.api.service.IOccurrenceService;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
+import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
@@ -45,286 +46,300 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
  * @version 1.0
  */
 public class AnnotatedLineEditor extends TextEditor implements IConversationEnabled, IPostOperationEnabled {
-       
-       private ConversationHolder conversation;
-       
-       private IEntityPersistenceService persistenceService;
-       protected ILineDisplayStrategy lineDisplayStrategy;
-       
-       
-       /**
-        * <p>Constructor for AnnotatedLineEditor.</p>
-        *
-        * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
-        */
-       public AnnotatedLineEditor(ConversationHolder conversation) {
-               this.conversation = conversation;
-       }
-       
-       /* (non-Javadoc)
-        * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
-        */
-       /** {@inheritDoc} */
-       @Override
-       protected void doSetInput(IEditorInput input) throws CoreException {
-
-               AnnotatedLineDocumentProvider provider = new AnnotatedLineDocumentProvider(input);
-               
-               provider.setLineDisplayStrategy(lineDisplayStrategy, input);
-               setDocumentProvider(provider);
-               
-               super.doSetInput(input);        
-       }
-               
-       /**
-        * <p>Setter for the field <code>persistenceService</code>.</p>
-        *
-        * @param persistenceService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
-        */
-       protected void setPersistenceService(
-                       IEntityPersistenceService persistenceService) {
-               this.persistenceService = persistenceService;   
-       }
-       
-       /**
-        * <p>Getter for the field <code>persistenceService</code>.</p>
-        *
-        * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
-        */
-       protected IEntityPersistenceService getPersistenceService() {
-               return persistenceService;      
-       }
-
-       /**
-        * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
-        *
-        * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
-        */
-       protected void setLineDisplayStrategy(
-                       ILineDisplayStrategy lineDisplayStrategy) {
-               this.lineDisplayStrategy = lineDisplayStrategy;         
-       }
-       
-       /** {@inheritDoc} */
-       @Override
-       protected ISourceViewer createSourceViewer(Composite parent,
-                       IVerticalRuler ruler, int styles) {
-               
-               fAnnotationAccess= getAnnotationAccess();
-               fOverviewRuler= createOverviewRuler(getSharedColors()); 
-               LineSelectionViewer viewer = new LineSelectionViewer(parent, ruler, getOverviewRuler(),  
-                                               isOverviewRulerVisible(), styles);
-//                                             isOverviewRulerVisible(), styles | SWT.WRAP);
-               getSourceViewerDecorationSupport(viewer);
-                               
-               return viewer;
-       }
-       
-       /**
-        * Create an annotated line with an "empty" entity, i.e. using the editor
-        * input's default entity type and a zero-length title cache.
-        *
-        * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
-        */
-       public LineAnnotation createAnnotatedLineNewObject() {
-               
-               // Create new object
-               
-               AnnotatedLineDocumentProvider documentProvider = (AnnotatedLineDocumentProvider) getDocumentProvider();
-               IEntityCreator entityCreator = documentProvider.getEntityCreator(getEditorInput());
-               Object entity = entityCreator.createEntity(null);
-
-               LineAnnotation annotation = createAnnotatedLine(entity);
-               if (annotation != null) {
-                       annotation.markAsNew(true);
-               }
-               return annotation;
-       }
-       
-       /**
-        * Create an annotated line, first creating an entity of type "key" - this key
-        * must be recognized by the editor's entity creator.
-        *
-        * @param key a {@link java.lang.Object} object.
-        * @param titleCache a {@link java.lang.String} object.
-        * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
-        */
-       public LineAnnotation createAnnotatedLineNewObject(Object key, String titleCache) {
-               
-               
-
-               // Create new object
-               Object entity = ((AnnotatedLineDocumentProvider) getDocumentProvider()).
-                                                       getEntityCreator(getEditorInput()).createEntity(key, titleCache);
-               // checks if the creator also saves the entity (in a different conversation), in which case 
-               // we need to bind back this editors conversation
-               // we also need to reload the entity because the conversation used to
-               // to create / save the entity may have been closed
-               if(entity != null && ((AnnotatedLineDocumentProvider) getDocumentProvider()).getEntityCreator(getEditorInput()).savesEntity()) {                
-                       getConversationHolder().bind();
-                       Object object = CdmStore.getService(IOccurrenceService.class).load(((CdmBase)entity).getUuid());                        
-                       entity = HibernateProxyHelper.deproxy(object);                  
-               }
-               
-               if(entity == null) {
-                       return null;
-               }
-               LineAnnotation annotation = createAnnotatedLine(entity);
-               if (annotation != null) {
-                       annotation.markAsNew(true);
-               }
-
-               return annotation;
-               
-       }
-       
-         @Override
-         public boolean isDirty() {
-               return super.isDirty();           
-         }
-       
-       /**
-        * Creates an annotated line at the end of the document. The annotation contains the entity.
-        *
-        * @param entity a {@link java.lang.Object} object.
-        * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
-        */
-       public LineAnnotation createAnnotatedLine(Object entity) {
-               
-               IEditorInput input = getEditorInput();
-               AnnotatedLineDocumentProvider provider = (AnnotatedLineDocumentProvider) getDocumentProvider();
-               
-               LineAnnotation annotation = null;
-               try {
-                       annotation = provider.createAnnotatedLine(input, entity);
-
-                       // Jump to new line
-                       IAnnotationModel model = provider.getAnnotationModel(input);
-                       if(model != null){
-                               int start= model.getPosition(annotation).getOffset();
-                               selectAndReveal(start, 0);
-                       }
-                       
-               } catch (BadLocationException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
-               }               
-               return annotation;
-       }
-       
-       /**
-        * <p>removeAnnotatedLine</p>
-        *
-        * @param lineno a int.
-        */
-       public void removeAnnotatedLine(int lineno) {
-               ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(lineno);
-       }
-
-       /**
-        * <p>removeAnnotatedLine</p>
-        *
-        * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
-        */
-       public void removeAnnotatedLine(LineAnnotation annotation) {
-               ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(annotation);
-       }
-       
-       /* (non-Javadoc)
-        * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
-        */
-       /** {@inheritDoc} */
-       @Override
-       public void doSave(IProgressMonitor progressMonitor) {
-               if (getConversationHolder() != null) {
-                       if( ! getConversationHolder().isBound()){
-                               getConversationHolder().bind();
-                       }
-                       super.doSave(progressMonitor);
-                       getConversationHolder().commit(true);
-               } else {
-                       super.doSave(progressMonitor);  
-               }
-               firePropertyChange(PROP_DIRTY);
-       }
-       
-       /* (non-Javadoc)
-        * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
-        */
-       /** {@inheritDoc} */
-       @Override
-       public void setFocus() {
-               super.setFocus();
-               if (getConversationHolder() != null) {
-                       getConversationHolder().bind();
-               }
-               // TODO pass focus to underlying widgets
-       }
-
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
-        */
-       /**
-        * <p>getConversationHolder</p>
-        *
-        * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
-        */
-       public ConversationHolder getConversationHolder() {
-               return conversation;
-       }
-
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
-        */
-       /** {@inheritDoc} */
-       public void update(CdmDataChangeMap changeEvents) {}
-
-       /**
-        * Refreshes text in all lines.
-        */
-       protected void refreshLineDisplay() {
-               IDocumentProvider provider = getDocumentProvider();
-               IEditorInput input = getEditorInput();
-               IAnnotationModel model = provider.getAnnotationModel(input);
-               Iterator iter = getDocumentProvider().getAnnotationModel(getEditorInput()).getAnnotationIterator();
-               while (iter.hasNext()) {
-                       Object next = iter.next();
-                       if (next instanceof LineAnnotation) {
-                               LineAnnotation annotation = (LineAnnotation) next;
-                               ((AnnotatedLineDocumentProvider) getDocumentProvider()).
-                                               updateLineFromAnnotation(annotation);
-                       }
-               }
-       }
-       
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
-        */
-       /** {@inheritDoc} */
-       public boolean postOperation(CdmBase objectAffectedByOperation) {
-               refreshLineDisplay();
-               
-               return true;
-       }
-       
-       /* (non-Javadoc)
-        * @see org.eclipse.ui.editors.text.TextEditor#dispose()
-        */
-       /** {@inheritDoc} */
-       @Override
-       public void dispose() {
-               super.dispose();
-               conversation.close();
-       }
-
-       /**
-        * <p>onComplete</p>
-        *
-        * @return a boolean.
-        */
-       public boolean onComplete() {
-               // TODO Auto-generated method stub
-               return false;
-       }
+
+    protected ConversationHolder conversation;
+
+    private IEntityPersistenceService persistenceService;
+    protected ILineDisplayStrategy lineDisplayStrategy;
+
+
+    /**
+     * <p>Constructor for AnnotatedLineEditor.</p>
+     *
+     * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
+     */
+    public AnnotatedLineEditor(ConversationHolder conversation) {
+        this.conversation = conversation;
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ui.editors.text.TextEditor#doSetInput(org.eclipse.ui.IEditorInput)
+     */
+    /** {@inheritDoc} */
+    @Override
+    protected void doSetInput(IEditorInput input) throws CoreException {
+
+        AnnotatedLineDocumentProvider provider = new AnnotatedLineDocumentProvider(input);
+        
+        provider.setLineDisplayStrategy(lineDisplayStrategy, input);
+        setDocumentProvider(provider);
+
+        super.doSetInput(input);
+    }
+
+    /**
+     * <p>Setter for the field <code>persistenceService</code>.</p>
+     *
+     * @param persistenceService a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
+     */
+    protected void setPersistenceService(
+            IEntityPersistenceService persistenceService) {
+        this.persistenceService = persistenceService;
+    }
+
+    /**
+     * <p>Getter for the field <code>persistenceService</code>.</p>
+     *
+     * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService} object.
+     */
+    protected IEntityPersistenceService getPersistenceService() {
+        return persistenceService;
+    }
+
+    /**
+     * <p>Setter for the field <code>lineDisplayStrategy</code>.</p>
+     *
+     * @param lineDisplayStrategy a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.ILineDisplayStrategy} object.
+     */
+    protected void setLineDisplayStrategy(
+            ILineDisplayStrategy lineDisplayStrategy) {
+        this.lineDisplayStrategy = lineDisplayStrategy;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    protected ISourceViewer createSourceViewer(Composite parent,
+            IVerticalRuler ruler, int styles) {
+
+        fAnnotationAccess= getAnnotationAccess();
+        fOverviewRuler= createOverviewRuler(getSharedColors());
+        LineSelectionViewer viewer = new LineSelectionViewer(parent, ruler, getOverviewRuler(),
+                isOverviewRulerVisible(), styles);
+        //                                             isOverviewRulerVisible(), styles | SWT.WRAP);
+        getSourceViewerDecorationSupport(viewer);
+
+        return viewer;
+    }
+
+    /**
+     * Create an annotated line with an "empty" entity, i.e. using the editor
+     * input's default entity type and a zero-length title cache.
+     *
+     * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
+     */
+    public LineAnnotation createAnnotatedLineNewObject() {
+
+        // Create new object
+
+        AnnotatedLineDocumentProvider documentProvider = (AnnotatedLineDocumentProvider) getDocumentProvider();
+        IEntityCreator entityCreator = documentProvider.getEntityCreator(getEditorInput());
+        Object entity = entityCreator.createEntity(null);
+
+        LineAnnotation annotation = createAnnotatedLine(entity);
+        if (annotation != null) {
+            annotation.markAsNew(true);
+        }
+        return annotation;
+    }
+
+    /**
+     * Create an annotated line, first creating an entity of type "key" - this key
+     * must be recognized by the editor's entity creator.
+     *
+     * @param key a {@link java.lang.Object} object.
+     * @param titleCache a {@link java.lang.String} object.
+     * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
+     */
+    public LineAnnotation createAnnotatedLineNewObject(Object key, String titleCache) {
+
+
+
+        // Create new object
+        Object entity = ((AnnotatedLineDocumentProvider) getDocumentProvider()).
+                getEntityCreator(getEditorInput()).createEntity(key, titleCache);
+        // checks if the creator also saves the entity (in a different conversation), in which case
+        // we need to bind back this editors conversation
+        // we also need to reload the entity because the conversation used to
+        // to create / save the entity may have been closed
+        if(entity != null && ((AnnotatedLineDocumentProvider) getDocumentProvider()).getEntityCreator(getEditorInput()).savesEntity()) {
+            getConversationHolder().bind();
+            //FIXME: why do we use IOccurrenceService here? is this generic?
+            //because this method is only invoked by the handler which is used in the
+            //specimen bulk editor. This is unsafe and should be refactored!
+            Object object = CdmStore.getService(IOccurrenceService.class).load(((CdmBase)entity).getUuid());
+            entity = HibernateProxyHelper.deproxy(object);
+        }
+
+        if(entity == null) {
+            return null;
+        }
+        LineAnnotation annotation = createAnnotatedLine(entity);
+        if (annotation != null) {
+            annotation.markAsNew(true);
+        }
+
+        return annotation;
+
+    }
+
+    @Override
+    public boolean isDirty() {
+        return super.isDirty();
+    }
+
+    /**
+     * Creates an annotated line at the end of the document. The annotation contains the entity.
+     *
+     * @param entity a {@link java.lang.Object} object.
+     * @return a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
+     */
+    public LineAnnotation createAnnotatedLine(Object entity) {
+
+        IEditorInput input = getEditorInput();
+        this.conversation.close();
+        this.conversation = ((AbstractBulkEditorInput)input).getConversation();
+        AnnotatedLineDocumentProvider provider = (AnnotatedLineDocumentProvider) getDocumentProvider();
+
+        LineAnnotation annotation = null;
+        try {
+            annotation = provider.createAnnotatedLine(input, entity);
+
+            // Jump to new line
+            IAnnotationModel model = provider.getAnnotationModel(input);
+            if(model != null){
+                int start= model.getPosition(annotation).getOffset();
+                selectAndReveal(start, 0);
+            }
+
+        } catch (BadLocationException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        return annotation;
+    }
+
+    /**
+     * <p>removeAnnotatedLine</p>
+     *
+     * @param lineno a int.
+     */
+    public void removeAnnotatedLine(int lineno) {
+        ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(lineno);
+    }
+
+    /**
+     * <p>removeAnnotatedLine</p>
+     *
+     * @param annotation a {@link eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation} object.
+     */
+    public void removeAnnotatedLine(LineAnnotation annotation) {
+        ((AnnotatedLineDocumentProvider) getDocumentProvider()).removeAnnotatedLine(annotation);
+        
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ui.texteditor.AbstractTextEditor#doSave(org.eclipse.core.runtime.IProgressMonitor)
+     */
+    /** {@inheritDoc} */
+    @Override
+    public void doSave(IProgressMonitor progressMonitor) {
+        if (getConversationHolder() != null) {
+            if( ! getConversationHolder().isBound()){
+                getConversationHolder().bind();
+            }
+            super.doSave(progressMonitor);
+            getConversationHolder().commit(true);
+        } else {
+            super.doSave(progressMonitor);
+        }
+        firePropertyChange(PROP_DIRTY);
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ui.texteditor.AbstractTextEditor#setFocus()
+     */
+    /** {@inheritDoc} */
+    @Override
+    public void setFocus() {
+        super.setFocus();
+        if (getConversationHolder() != null) {
+            getConversationHolder().bind();
+        }
+        ((AbstractBulkEditorInput)getEditorInput()).bind();
+        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
+        // TODO pass focus to underlying widgets
+    }
+
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
+     */
+    /**
+     * <p>getConversationHolder</p>
+     *
+     * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
+     */
+    @Override
+    public ConversationHolder getConversationHolder() {
+        return conversation;
+    }
+
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
+     */
+    /** {@inheritDoc} */
+    @Override
+    public void update(CdmDataChangeMap changeEvents) {}
+
+    /**
+     * Refreshes text in all lines.
+     */
+    protected void refreshLineDisplay() {
+        IDocumentProvider provider = getDocumentProvider();
+        IEditorInput input = getEditorInput();
+        IAnnotationModel model = provider.getAnnotationModel(input);
+        Iterator iter = getDocumentProvider().getAnnotationModel(getEditorInput()).getAnnotationIterator();
+        while (iter.hasNext()) {
+            Object next = iter.next();
+            if (next instanceof LineAnnotation) {
+                LineAnnotation annotation = (LineAnnotation) next;
+                ((AnnotatedLineDocumentProvider) getDocumentProvider()).
+                updateLineFromAnnotation(annotation);
+            }
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
+     */
+    /** {@inheritDoc} */
+    @Override
+    public boolean postOperation(CdmBase objectAffectedByOperation) {
+        refreshLineDisplay();
+
+        return true;
+    }
+
+    /* (non-Javadoc)
+     * @see org.eclipse.ui.editors.text.TextEditor#dispose()
+     */
+    /** {@inheritDoc} */
+    @Override
+    public void dispose() {
+        super.dispose();
+        conversation.close();
+        ((AbstractBulkEditorInput)getEditorInput()).dispose();
+    }
+
+    /**
+     * <p>onComplete</p>
+     *
+     * @return a boolean.
+     */
+    @Override
+    public boolean onComplete() {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
 
 }