ref #7518 Pre-load and select previously selected element
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 15 Aug 2018 15:49:30 +0000 (17:49 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 15 Aug 2018 15:49:30 +0000 (17:49 +0200)
eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/input/AbstractBulkEditorInput.java

index 503b8714b9feedef8e45af3a9f483d3054e51251..61bd5a1bf87c75cf66c0dd72167b9f795f18c222 100644 (file)
@@ -17,6 +17,7 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.UUID;
+import java.util.stream.Collectors;
 
 import org.eclipse.core.runtime.ICoreRunnable;
 import org.eclipse.core.runtime.jobs.Job;
@@ -173,7 +174,6 @@ public abstract class AbstractBulkEditorInput<T extends CdmBase> extends CdmEnti
            model.clear();
 
                if(getEntityUuid() != null){
-
                        T entity = loadEntity(getEntityUuid());
                        model.add(entity);
                }
@@ -202,30 +202,32 @@ public abstract class AbstractBulkEditorInput<T extends CdmBase> extends CdmEnti
                    monitor.beginTask(jobLabel, totalWork);
                    int pageNumber = 0;
                    List<T> entities;
+
+                   //load previously selected element
+                   UUID selectedUuid = null;
+                   if(selection!=null && selection.getFirstElement() instanceof CdmBase){
+                       selectedUuid = ((CdmBase) selection.getFirstElement()).getUuid();
+                       T entity = loadEntity(selectedUuid);
+                       model.add(entity);
+                   }
+
                 do {
                     if (monitor.isCanceled()) {
                         break;
                     }
                     configurator.setPageNumber(pageNumber);
                     entities = listEntities(configurator);
-                    /*
-                     * IMPORTANT!
-                     * Entities have to be loaded into the main session because they are
-                     * loaded in a parallel asynchronous thread
-                     */
-                    getCdmEntitySession().load(entities, true);
-
-                    model.addAll(entities);
-
-                    //select entity when it is loaded
-                    if(selection!=null && model.containsAll(selection.toList())){
-                        EventUtility.postAsyncEvent(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED, selection);
-                    }
+
+                    addToModel(entities, selectedUuid);
+
                     pageNumber++;
                     monitor.worked(pageSize);
                     long workedLong = pageSize*pageNumber;
                     int loadedCount =  workedLong>Integer.MAX_VALUE?Integer.MAX_VALUE:(int)workedLong;
                     monitor.setTaskName(String.format(Messages.AbstractBulkEditorInput_LOADED, loadedCount, totalWork, getName()));
+
+                    //Update selection
+                    EventUtility.postAsyncEvent(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED, selection);
                 } while (!entities.isEmpty());
                    monitor.done();
                    EventUtility.postAsyncEvent(WorkbenchEventConstants.BULK_EDITOR_SEARCH_FINISHED, selection);
@@ -234,6 +236,22 @@ public abstract class AbstractBulkEditorInput<T extends CdmBase> extends CdmEnti
                }
        }
 
+       private void addToModel(List<T> entities, UUID selectedUuid){
+           //filter pre-loaded previously selected element
+           if(selectedUuid!=null){
+               entities = entities.stream().filter(entity->!entity.getUuid().equals(selectedUuid)).collect(Collectors.toList());
+           }
+           /*
+         * IMPORTANT!
+         * Entities have to be loaded into the main session because they are
+         * loaded in a parallel asynchronous thread
+         */
+        if(getCdmEntitySession()!=null){//is null when closing the bulk editor during loading
+            getCdmEntitySession().load(entities, true);
+        }
+        model.addAll(entities);
+       }
+
        public boolean isMergingEnabled() {
                return false;
        }