commit fb259956acc274fc8a1f5a08e4ebf874c70c4e9d
Author: Patrick Plitzner <p.plitzner@bgbm.org>
Date:   Fri Oct 26 08:47:00 2018 +0200

    ref #7869 Run ontology search in background task

diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPart.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPart.java
index 327d21344..4fa6ffd49 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPart.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPart.java
@@ -9,7 +9,9 @@
 package eu.etaxonomy.taxeditor.view.webimport.termimport;
 
 import javax.annotation.PostConstruct;
+import javax.inject.Inject;
 
+import org.eclipse.e4.ui.di.UISynchronize;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
 
@@ -20,10 +22,13 @@ import org.eclipse.swt.widgets.Composite;
  */
 public class GfBioTerminologyImportPart {
 
+    @Inject
+    private UISynchronize sync;
+
     @SuppressWarnings("unused")
     @PostConstruct
     public void create(Composite parent){
         GfBioTerminologyImportComposite composite = new GfBioTerminologyImportComposite(parent, SWT.NONE);
-        new GfBioTerminologyImportPresenter(composite);
+        new GfBioTerminologyImportPresenter(composite, sync);
     }
 }
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPresenter.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPresenter.java
index 3f8e2cdd5..ff70c3064 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPresenter.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPresenter.java
@@ -16,6 +16,11 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.stream.Collectors;
 
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.e4.ui.di.UISynchronize;
 import org.eclipse.jface.util.LocalSelectionTransfer;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
@@ -56,10 +61,12 @@ public class GfBioTerminologyImportPresenter {
 
     private GfBioTerminologyImportComposite composite;
     private Collection<TerminologyWrapper> selectedOntologies = new ArrayList<>();
+    private UISynchronize sync;
 
-    public GfBioTerminologyImportPresenter(GfBioTerminologyImportComposite composite) {
+    public GfBioTerminologyImportPresenter(GfBioTerminologyImportComposite composite, UISynchronize sync) {
         super();
         this.composite = composite;
+        this.sync = sync;
         composite.getBtnSearch().addSelectionListener(new SelectionAdapter() {
             @Override
             public void widgetSelected(SelectionEvent e) {
@@ -194,54 +201,72 @@ public class GfBioTerminologyImportPresenter {
         if(searchString.equals(TXT_SEARCH_DEFAULT)){
             return;
         }
-        if(selectedOntologies.isEmpty()){
-            MessagingUtils.confirmDialog("Long running operation", "You have not selected any specific ontology. This search may take a long time.\nDo you want to continue?");
-        }
-        String response = new RequestSearch(searchString, selectedOntologies).request();
-        if(response==null){
-            MessagingUtils.informationDialog(NO_CONNECTION_TITLE, NO_CONNECTION_MESSAGE);
+        if(selectedOntologies.isEmpty() && !MessagingUtils.confirmDialog("Long running operation", "You have not selected any specific ontology. This search may take a long time.\n\nDo you want to continue?")){
             return;
         }
-        List<OntologyTermWrapper> wrapperList = new ArrayList<>();
-        wrapperList = TermParser.parse(response);
-        if(wrapperList.isEmpty()){
-            MessagingUtils.informationDialog(Messages.GfBioTerminologyImportPresenter_NO_RESULTS_TITLE, Messages.GfBioTerminologyImportPresenter_NO_RESULTS_MESSAGE);
-        }
-        //sort list
-        Collections.sort(wrapperList, new Comparator<OntologyTermWrapper>() {
+        Job searchJob = new Job("Search Ontologies") {
+
             @Override
-            public int compare(OntologyTermWrapper o1, OntologyTermWrapper o2) {
-                //remove wildcards from search string
-                String trimmedSearch = searchString.replace("*", "");
-                String label1 = o1.getLabel();
-                String label2 = o2.getLabel();
-                //1. search string at the beginning
-                if(label1.startsWith(trimmedSearch)){
-                    if(!label2.startsWith(trimmedSearch)){
-                        return -1;
-                    }
-                    else{
-                        return label1.compareTo(label2);
-                    }
+            protected IStatus run(IProgressMonitor monitor) {
+                String response = new RequestSearch(searchString, selectedOntologies).request();
+                if(response==null){
+                    MessagingUtils.informationDialog(NO_CONNECTION_TITLE, NO_CONNECTION_MESSAGE);
+                    return Status.CANCEL_STATUS;
+                }
+                final List<OntologyTermWrapper> wrapperList = new ArrayList<>();
+                wrapperList.addAll(TermParser.parse(response));
+                if(wrapperList.isEmpty()){
+                    MessagingUtils.informationDialog(Messages.GfBioTerminologyImportPresenter_NO_RESULTS_TITLE, Messages.GfBioTerminologyImportPresenter_NO_RESULTS_MESSAGE);
                 }
-                else if(label2.startsWith(trimmedSearch)){
-                    return 1;
+                //sort list
+                Collections.sort(wrapperList, new TermWrapperComparator(searchString));
+                sync.syncExec(() -> {
+                    composite.getTreeTermHierarchy().setInput(wrapperList);
+                    composite.getLblResultCount().setText(Integer.toString(wrapperList.size()));
+                });
+                return Status.OK_STATUS;
+            }
+        };
+       searchJob.schedule();
+    }
+
+    private final class TermWrapperComparator implements Comparator<OntologyTermWrapper> {
+        private final String searchString;
+
+        public TermWrapperComparator(String searchString) {
+            super();
+            this.searchString = searchString;
+        }
+
+        @Override
+        public int compare(OntologyTermWrapper o1, OntologyTermWrapper o2) {
+            //remove wildcards from search string
+            String trimmedSearch = searchString.replace("*", "");
+            String label1 = o1.getLabel();
+            String label2 = o2.getLabel();
+            //1. search string at the beginning
+            if(label1.startsWith(trimmedSearch)){
+                if(!label2.startsWith(trimmedSearch)){
+                    return -1;
                 }
-                //2. label that contains search string
-                if(label1.contains(trimmedSearch)){
-                    if(!label2.contains(trimmedSearch)){
-                        return -1;
-                    }
+                else{
+                    return label1.compareTo(label2);
                 }
-                else if(label2.contains(trimmedSearch)){
-                    return 1;
+            }
+            else if(label2.startsWith(trimmedSearch)){
+                return 1;
+            }
+            //2. label that contains search string
+            if(label1.contains(trimmedSearch)){
+                if(!label2.contains(trimmedSearch)){
+                    return -1;
                 }
-                return label1.compareTo(label2);
             }
-        });
-        composite.getTreeTermHierarchy().setInput(wrapperList);
-
-        composite.getLblResultCount().setText(Integer.toString(wrapperList.size()));
+            else if(label2.contains(trimmedSearch)){
+                return 1;
+            }
+            return label1.compareTo(label2);
+        }
     }
 
 }
