Merge branch 'release/4.3.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / DerivateView.java
index 632d3153003d40695e75497e158151c4564af7c0..c5bcfb75a8c4e0d2bdca90719b15eb0220a50bd3 100644 (file)
@@ -27,6 +27,7 @@ import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.Tree;
 import org.eclipse.ui.IEditorInput;
@@ -60,6 +61,7 @@ import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
 import eu.etaxonomy.taxeditor.model.IPartContentHasMedia;
 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
@@ -95,6 +97,50 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
             "derivationEvents.derivatives.sources"
     });
 
+       private static final int WARN_THRESHOLD = 200;
+
+    private DelaySelection delaySelection = null;
+    /**
+     * This is the monitor for the DelaySelection runnable.
+     * If it is <code>true</code> then it is currently delaying a selection.
+     */
+    private boolean isInDelay;
+
+
+    /**
+     * This class invokes internal_selectionChanged() in a separate thread.
+     * This allows an asynchronous and/or delayed handling of selection changes
+     */
+    private class DelaySelection implements Runnable{
+        private IWorkbenchPart part;
+        private ISelection selection;
+
+        public DelaySelection(IWorkbenchPart part, ISelection selection) {
+            super();
+            this.part = part;
+            this.selection = selection;
+        }
+
+        @Override
+        public void run() {
+            try{
+                selectionChanged_internal(part, selection);
+            }
+            finally{
+                isInDelay = false;
+            }
+        }
+
+        public synchronized void setSelection(ISelection selection) {
+            this.selection = selection;
+        }
+
+        public synchronized void setPart(IWorkbenchPart part) {
+            this.part = part;
+        }
+
+    }
+
        private ConversationHolder conversation;
 
        private TreeViewer viewer;
@@ -202,7 +248,9 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
         control.setMenu(menu);
 
         //init tree
-        updateRootEntities(((DerivateViewEditorInput)getEditorInput()).getDerivativeUuids());
+        Collection<UUID> derivativeUuids = ((DerivateViewEditorInput)getEditorInput()).getDerivativeUuids();
+        checkWarnThreshold(derivativeUuids);
+               updateRootEntities(derivativeUuids);
         //set taxon filter
         derivateSearchCompositeController.setTaxonFilter(((DerivateViewEditorInput) getEditorInput()).getTaxonUuid());
         //reset status bar
@@ -308,9 +356,8 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
         // commit the conversation and start a new transaction immediately
         conversation.commit(true);
 
-        if(CdmStore.getCurrentSessionManager().isRemoting()) {
-            CdmStore.getService(IOccurrenceService.class).merge(new ArrayList<SpecimenOrObservationBase>(rootElements), true);
-        }
+        CdmStore.getService(IOccurrenceService.class).merge(new ArrayList<SpecimenOrObservationBase>(rootElements), true);
+        
         monitor.worked(1);
 
         this.setDirty(false);
@@ -348,7 +395,6 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
 
     @Override
     public void setFocus() {
-        viewer.getControl().setFocus();
         //make sure to bind again if maybe in another view the conversation was unbound
         if(conversation!=null && !conversation.isBound()){
             conversation.bind();
@@ -356,6 +402,7 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
         if(cdmEntitySession != null) {
             cdmEntitySession.bind();
         }
+        derivateSearchCompositeController.setFocusOnSearchField();
     }
 
     @Override
@@ -414,7 +461,9 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
      * Refreshes the derivate hierarchy tree
      */
     public void refreshTree(){
-        viewer.refresh();
+       if(!viewer.getTree().isDisposed()){
+               viewer.refresh();
+       }
     }
 
     //FIXME:Remoting hack to make this work for remoting
@@ -479,10 +528,8 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
             cdmEntitySession.dispose();
         }
     }
-
-    @Override
-    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
-        if(part == this){
+    public void selectionChanged_internal(IWorkbenchPart part, ISelection selection) {
+       if(part == this){
             return;
         }
         if(viewer.getTree().isDisposed()){
@@ -509,12 +556,37 @@ public class DerivateView extends EditorPart implements IPartContentHasFactualDa
                 for (SpecimenOrObservationBase specimenOrObservationBase : fieldUnits) {
                     uuids.add(specimenOrObservationBase.getUuid());
                 }
+                checkWarnThreshold(uuids);
                 updateRootEntities(uuids);
                 setPartName("Derivative Editor: " + selectedTaxon.getName());
             }
         }
     }
 
+    @Override
+    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
+       if(delaySelection==null){
+            delaySelection = new DelaySelection(part, selection);
+        }
+        delaySelection.setPart(part);
+        delaySelection.setSelection(selection);
+        if(!isInDelay){
+            isInDelay = true;
+            Display.getCurrent().asyncExec(delaySelection);
+        }
+    }
+
+
+       private void checkWarnThreshold(Collection<UUID> uuids) {
+               if(uuids!=null && uuids.size()>WARN_THRESHOLD){
+                       MessagingUtils.warningDialog("Performance warning", this.getClass(), String.format("Specimens will not be loaded!\n"
+                                       + "There are %d specimens associated with the current "
+                                       + "selection. If you really want to show all of them in the specimen editor please "
+                                       + "use the taxon filter in the search bar.", uuids.size()));
+                       uuids.clear();
+               }
+       }
+
     public TreeViewer getViewer() {
         return viewer;
     }