Project

General

Profile

« Previous | Next » 

Revision fb259956

Added by Patrick Plitzner over 5 years ago

ref #7869 Run ontology search in background task

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPresenter.java
16 16
import java.util.List;
17 17
import java.util.stream.Collectors;
18 18

  
19
import org.eclipse.core.runtime.IProgressMonitor;
20
import org.eclipse.core.runtime.IStatus;
21
import org.eclipse.core.runtime.Status;
22
import org.eclipse.core.runtime.jobs.Job;
23
import org.eclipse.e4.ui.di.UISynchronize;
19 24
import org.eclipse.jface.util.LocalSelectionTransfer;
20 25
import org.eclipse.jface.viewers.ISelection;
21 26
import org.eclipse.jface.viewers.IStructuredSelection;
......
56 61

  
57 62
    private GfBioTerminologyImportComposite composite;
58 63
    private Collection<TerminologyWrapper> selectedOntologies = new ArrayList<>();
64
    private UISynchronize sync;
59 65

  
60
    public GfBioTerminologyImportPresenter(GfBioTerminologyImportComposite composite) {
66
    public GfBioTerminologyImportPresenter(GfBioTerminologyImportComposite composite, UISynchronize sync) {
61 67
        super();
62 68
        this.composite = composite;
69
        this.sync = sync;
63 70
        composite.getBtnSearch().addSelectionListener(new SelectionAdapter() {
64 71
            @Override
65 72
            public void widgetSelected(SelectionEvent e) {
......
194 201
        if(searchString.equals(TXT_SEARCH_DEFAULT)){
195 202
            return;
196 203
        }
197
        if(selectedOntologies.isEmpty()){
198
            MessagingUtils.confirmDialog("Long running operation", "You have not selected any specific ontology. This search may take a long time.\nDo you want to continue?");
199
        }
200
        String response = new RequestSearch(searchString, selectedOntologies).request();
201
        if(response==null){
202
            MessagingUtils.informationDialog(NO_CONNECTION_TITLE, NO_CONNECTION_MESSAGE);
204
        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?")){
203 205
            return;
204 206
        }
205
        List<OntologyTermWrapper> wrapperList = new ArrayList<>();
206
        wrapperList = TermParser.parse(response);
207
        if(wrapperList.isEmpty()){
208
            MessagingUtils.informationDialog(Messages.GfBioTerminologyImportPresenter_NO_RESULTS_TITLE, Messages.GfBioTerminologyImportPresenter_NO_RESULTS_MESSAGE);
209
        }
210
        //sort list
211
        Collections.sort(wrapperList, new Comparator<OntologyTermWrapper>() {
207
        Job searchJob = new Job("Search Ontologies") {
208

  
212 209
            @Override
213
            public int compare(OntologyTermWrapper o1, OntologyTermWrapper o2) {
214
                //remove wildcards from search string
215
                String trimmedSearch = searchString.replace("*", "");
216
                String label1 = o1.getLabel();
217
                String label2 = o2.getLabel();
218
                //1. search string at the beginning
219
                if(label1.startsWith(trimmedSearch)){
220
                    if(!label2.startsWith(trimmedSearch)){
221
                        return -1;
222
                    }
223
                    else{
224
                        return label1.compareTo(label2);
225
                    }
210
            protected IStatus run(IProgressMonitor monitor) {
211
                String response = new RequestSearch(searchString, selectedOntologies).request();
212
                if(response==null){
213
                    MessagingUtils.informationDialog(NO_CONNECTION_TITLE, NO_CONNECTION_MESSAGE);
214
                    return Status.CANCEL_STATUS;
215
                }
216
                final List<OntologyTermWrapper> wrapperList = new ArrayList<>();
217
                wrapperList.addAll(TermParser.parse(response));
218
                if(wrapperList.isEmpty()){
219
                    MessagingUtils.informationDialog(Messages.GfBioTerminologyImportPresenter_NO_RESULTS_TITLE, Messages.GfBioTerminologyImportPresenter_NO_RESULTS_MESSAGE);
226 220
                }
227
                else if(label2.startsWith(trimmedSearch)){
228
                    return 1;
221
                //sort list
222
                Collections.sort(wrapperList, new TermWrapperComparator(searchString));
223
                sync.syncExec(() -> {
224
                    composite.getTreeTermHierarchy().setInput(wrapperList);
225
                    composite.getLblResultCount().setText(Integer.toString(wrapperList.size()));
226
                });
227
                return Status.OK_STATUS;
228
            }
229
        };
230
       searchJob.schedule();
231
    }
232

  
233
    private final class TermWrapperComparator implements Comparator<OntologyTermWrapper> {
234
        private final String searchString;
235

  
236
        public TermWrapperComparator(String searchString) {
237
            super();
238
            this.searchString = searchString;
239
        }
240

  
241
        @Override
242
        public int compare(OntologyTermWrapper o1, OntologyTermWrapper o2) {
243
            //remove wildcards from search string
244
            String trimmedSearch = searchString.replace("*", "");
245
            String label1 = o1.getLabel();
246
            String label2 = o2.getLabel();
247
            //1. search string at the beginning
248
            if(label1.startsWith(trimmedSearch)){
249
                if(!label2.startsWith(trimmedSearch)){
250
                    return -1;
229 251
                }
230
                //2. label that contains search string
231
                if(label1.contains(trimmedSearch)){
232
                    if(!label2.contains(trimmedSearch)){
233
                        return -1;
234
                    }
252
                else{
253
                    return label1.compareTo(label2);
235 254
                }
236
                else if(label2.contains(trimmedSearch)){
237
                    return 1;
255
            }
256
            else if(label2.startsWith(trimmedSearch)){
257
                return 1;
258
            }
259
            //2. label that contains search string
260
            if(label1.contains(trimmedSearch)){
261
                if(!label2.contains(trimmedSearch)){
262
                    return -1;
238 263
                }
239
                return label1.compareTo(label2);
240 264
            }
241
        });
242
        composite.getTreeTermHierarchy().setInput(wrapperList);
243

  
244
        composite.getLblResultCount().setText(Integer.toString(wrapperList.size()));
265
            else if(label2.contains(trimmedSearch)){
266
                return 1;
267
            }
268
            return label1.compareTo(label2);
269
        }
245 270
    }
246 271

  
247 272
}

Also available in: Unified diff