Fix session problems with remoting
authorPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 26 Oct 2015 15:58:17 +0000 (16:58 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 26 Oct 2015 15:58:17 +0000 (16:58 +0100)
eu.etaxonomy.taxeditor.editor/plugin.xml
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/DerivateView.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/AbstractCdmViewPart.java

index 1d1c2e147da5e220184f06b28fb2a1b059270a22..18537cde89e5ac3a515ffb0ab9d3835095d875ed 100644 (file)
                icon="icons/synced.gif"
                label="Link with Editor"
                style="toggle">
+            <visibleWhen
+                  checkEnabled="false">
+               <reference
+                     definitionId="isCdmStoreConnected">
+               </reference>
+            </visibleWhen>
          </command>
       </menuContribution>
    </extension>
index 33acf9faefae9cf401d5029470aa204fb2094155..7414396f153c6b2fb0dcc0530d863507f476d67f 100644 (file)
@@ -31,6 +31,7 @@ import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.Tree;
 import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IMemento;
 import org.eclipse.ui.ISaveablePart2;
 import org.eclipse.ui.IWorkbenchPart;
 
@@ -47,6 +48,7 @@ import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.Messages;
 import eu.etaxonomy.taxeditor.editor.view.derivate.searchFilter.DerivateSearchCompositeController;
+import eu.etaxonomy.taxeditor.model.IContextListener;
 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
 import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
@@ -62,7 +64,7 @@ import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
  *
  */
 public class DerivateView extends AbstractCdmViewPart implements IPartContentHasFactualData, ISaveablePart2,
-        IPartContentHasDetails, IPartContentHasSupplementalData, IPartContentHasMedia {
+        IPartContentHasDetails, IPartContentHasSupplementalData, IPartContentHasMedia, IContextListener {
     public static final String ID = "eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView"; //$NON-NLS-1$
 
     public static final String YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION = Messages.DerivateView_YOU_NEED_TO_SAVE;
@@ -125,6 +127,13 @@ public class DerivateView extends AbstractCdmViewPart implements IPartContentHas
      * Default constructor
      */
     public DerivateView() {
+        init();
+    }
+
+    /**
+     *
+     */
+    private void init() {
         this.derivateToRootEntityMap = new HashMap<SpecimenOrObservationBase<?>, SpecimenOrObservationBase<?>>();
         this.rootElements = new HashSet<SpecimenOrObservationBase<?>>();
 
@@ -134,6 +143,8 @@ public class DerivateView extends AbstractCdmViewPart implements IPartContentHas
         if (CdmStore.isActive()) {
             cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
         }
+        //listen to context changes
+        CdmStore.getContextManager().addContextListener(this);
     }
 
     @Override
@@ -195,6 +206,19 @@ public class DerivateView extends AbstractCdmViewPart implements IPartContentHas
             if (!conversation.isBound()) {
                 conversation.bind();
             }
+            /*
+             * If the active session is not the session of the Derivate Editor then we will
+             * save it, bind temporarily to our session and rebind to the original session.
+             * This happens e.g. if a selection change happens in the taxon editor and
+             * "Link with editor" is enabled. The selection change event and thus the
+             * loading in updateRootEntities() happens in the session of the taxon
+             * editor.
+             */
+            ICdmEntitySession previousCdmEntitySession = CdmStore.getCurrentSessionManager().getActiveSession();
+            if(cdmEntitySession != null) {
+                cdmEntitySession.bind();
+            }
+
             if(derivativeUuids!=null){
                 this.derivateToRootEntityMap = new HashMap<SpecimenOrObservationBase<?>, SpecimenOrObservationBase<?>>();
                 this.rootElements = new HashSet<SpecimenOrObservationBase<?>>();
@@ -219,6 +243,7 @@ public class DerivateView extends AbstractCdmViewPart implements IPartContentHas
             }
             viewer.setInput(rootElements);
             refreshTree();
+            previousCdmEntitySession.bind();
         }
     }
 
@@ -250,10 +275,6 @@ public class DerivateView extends AbstractCdmViewPart implements IPartContentHas
     public void doSaveAs() {
     }
 
-    public Set<SpecimenOrObservationBase<?>> getRootElements() {
-        return rootElements;
-    }
-
     @Override
     public String getTitleToolTip() {
         return Messages.DerivateView_DERIVATIVE_EDITOR;
@@ -280,7 +301,7 @@ public class DerivateView extends AbstractCdmViewPart implements IPartContentHas
     public void setFocus() {
         viewer.getControl().setFocus();
         //make sure to bind again if maybe in another view the conversation was unbound
-        if(!conversation.isBound()){
+        if(conversation!=null && !conversation.isBound()){
             conversation.bind();
         }
         if(cdmEntitySession != null) {
@@ -450,6 +471,14 @@ public class DerivateView extends AbstractCdmViewPart implements IPartContentHas
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public List<SpecimenOrObservationBase<?>> getRootEntities() {
+        return new ArrayList<SpecimenOrObservationBase<?>>(rootElements);
+    }
+
     @Override
     public void createViewer(Composite parent) {
         // TODO Auto-generated method stub
@@ -468,7 +497,47 @@ public class DerivateView extends AbstractCdmViewPart implements IPartContentHas
 
     public void toggleListenToSelectionChange() {
         listenToSelectionChange = !listenToSelectionChange;
-        derivateSearchCompositeController.setEnabled(listenToSelectionChange);
+        derivateSearchCompositeController.setEnabled(!listenToSelectionChange);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void contextStop(IMemento memento, IProgressMonitor monitor) {
+        derivateSearchCompositeController.setEnabled(false);
+        viewer.setInput(null);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void contextStart(IMemento memento, IProgressMonitor monitor) {
+        init();
+        derivateSearchCompositeController.setEnabled(!listenToSelectionChange);
+        refreshTree();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void contextRefresh(IProgressMonitor monitor) {
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
     }
 
 }
index 27be236e307398262fb4612ca8fef58a28b159ef..f9cedbdb8dd9de570e31e1d1243aa24c7d4b8341 100644 (file)
@@ -186,7 +186,7 @@ public abstract class AbstractCdmViewPart extends ViewPart implements ISelection
     }
 
     @Override
-    public  List<CdmBase> getRootEntities() {
+    public  List<? extends CdmBase> getRootEntities() {
         return Arrays.asList((CdmBase)getViewer().getInput());
     }