Merge branch 'release/5.17.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / TaxonNodeSelectionDialog.java
index aff448056bc52c62a47b703ed86601f261c76044..3ac813c44dd7deea7ddfef106f5f5f2363f8552c 100644 (file)
@@ -6,10 +6,12 @@ package eu.etaxonomy.taxeditor.ui.dialog.selection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
+import java.util.Set;
 import java.util.UUID;
 
 import org.eclipse.jface.viewers.ILabelProvider;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.layout.GridData;
@@ -19,57 +21,94 @@ import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
-import org.eclipse.swt.widgets.Text;
 
-import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.service.IClassificationService;
+import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
 import eu.etaxonomy.cdm.model.taxon.Classification;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
  * @author p.ciardelli
- * @version $Id: $
  */
 public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelectionDialog<TaxonNode> implements SelectionListener{
 
-       public static TaxonNode select(Shell shell, ConversationHolder conversation, String title, List<UUID> excludeTaxa, TaxonNode node, Classification classification) {
+       public static TaxonNode select(Shell shell, //ConversationHolder conversation,
+               String title, Set<UUID> excludeTaxa, TaxonNode node, UUID classificationUUID, boolean allowSelectClassification) {
                TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
-                               conversation,
+                               //conversation,
                                title,
                                excludeTaxa,
                                false,
                                node,
-                               classification);
+                               classificationUUID, allowSelectClassification);
                return getSelectionFromDialog(dialog);
        }
+       public static TaxonNode select(Shell shell, //ConversationHolder conversation,
+            String title, Set<UUID> excludeTaxa, TaxonNode node, UUID classificationUUID) {
+        TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
+                //conversation,
+                title,
+                excludeTaxa,
+                false,
+                node,
+                classificationUUID);
+        return getSelectionFromDialog(dialog);
+    }
+
+       public static UuidAndTitleCache<TaxonNode> selectUuidAndTitleCache(Shell shell, //ConversationHolder conversation,
+            String title, Set<UUID> excludeTaxa, TaxonNode node, UUID classificationUUID) {
+        TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell,
+                //conversation,
+                title,
+                excludeTaxa,
+                false,
+                node,
+                classificationUUID, false);
+        return getUuidAndTitleCacheSelectionFromDialog(dialog);
+    }
 
        private Combo classificationSelectionCombo;
 
        private List<Classification> classifications;
 
        private Classification selectedClassification;
-       private final List<UUID> excludeTaxa;
+       private UUID selectedUuid;
+//     private final Set<UUID> excludeTaxa;
+       private boolean allowClassificationSelection = false;
 
-       protected TaxonNodeSelectionDialog(Shell shell, ConversationHolder conversation, String title, List<UUID> excludeTaxa, boolean multi, TaxonNode node, Classification classification) {
-               super(shell, conversation, title, multi, TaxonNodeSelectionDialog.class.getCanonicalName(), node);
+       protected TaxonNodeSelectionDialog(Shell shell, //ConversationHolder conversation,
+               String title, Set<UUID> excludeTaxa, boolean multi, TaxonNode node, UUID classificationUUID, boolean allowSelectClassification) {
+               super(shell, //conversation,
+                       title, multi, TaxonNodeSelectionDialog.class.getCanonicalName(), node);
 
                ILabelProvider labelProvider = new FilteredCdmResourceLabelProvider();
 
                setListLabelProvider(labelProvider);
 //             setDetailsLabelProvider(labelProvider);
-               this.excludeTaxa = excludeTaxa;
-               if(classification != null){
-                       selectedClassification = classification;
-               }
-               search();
+               this.cdmBaseToBeFiltered = excludeTaxa;
+               if(classificationUUID != null){
+            selectedUuid = classificationUUID;
+        }
+
+               fillClassifications();
+               this.allowClassificationSelection = allowSelectClassification;
+
+               //createClassificationSelectionCombo(shell);
+       }
+       protected TaxonNodeSelectionDialog(Shell shell, //ConversationHolder conversation,
+            String title, Set<UUID> excludeTaxa, boolean multi, TaxonNode node, UUID classificationUUID) {
+           this(shell, title, excludeTaxa, multi, node, classificationUUID, false);
        }
 
        /** {@inheritDoc} */
        @Override
-       protected Control createExtendedContentArea(Composite parent) {
-               return createClassificationSelectionCombo(parent);
+       protected Control createDialogArea(Composite parent) {
+           Composite container = (Composite) super.createDialogArea(parent);
+               return createClassificationSelectionCombo(container);
        }
 
        /*
@@ -92,6 +131,9 @@ public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelecti
 
                for(Classification tree : classifications){
                        classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
+                       if (tree.getUuid().equals(selectedUuid)){
+                           selectedClassification = tree;
+                       }
 
                }
 
@@ -105,7 +147,6 @@ public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelecti
                return classificationSelection;
        }
 
-       /** {@inheritDoc} */
        @Override
        protected String getTitle(TaxonNode taxonNode) {
                if(taxonNode != null && taxonNode.getTaxon() != null){
@@ -115,26 +156,41 @@ public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelecti
                return "";
        }
 
-       /** {@inheritDoc} */
+       @Override
+    protected SelectionListener getNewWizardButtonSelectionListener(){
+        return new SelectionAdapter() {
+
+            @Override
+            public void widgetSelected(SelectionEvent e) {
+                Classification tree = selectedClassification;
+                setPattern(tree.getRootNode());
+
+            }
+        };
+    }
+
        @Override
        protected TaxonNode getPersistentObject(UUID uuid) {
-               return CdmStore.getService(IClassificationService.class).getTaxonNodeByUuid(uuid);
+               return CdmStore.getService(ITaxonNodeService.class).find(uuid);
        }
 
-       /** {@inheritDoc} */
        @Override
-       protected void search() {
-               // default to first tree
-               // TODO this will be problematic and can only be seen as workaround
-
-           Control control =getSearchField();
-        String pattern = null;
-        if (control != null){
-            pattern = ((Text)control).getText();
-        }
+       protected void callService(String pattern) {
+           model = CdmStore.getService(IClassificationService.class)
+                       .getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(
+                                       selectedClassification.getUuid(), limitOfInitialElements, pattern, allowClassificationSelection, true);
+       }
 
+       @Override
+       protected void sort() {
+           if(!PreferencesUtil.isSortTaxaByRankAndName()){
+            Collections.sort(model, getItemsComparator());
+        }
+      // otherwise result is already sorted
+    }
 
-               if(classifications == null){
+    private void fillClassifications() {
+        if(classifications == null){
                        classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
                        Collections.sort(classifications, new Comparator<Classification>() {
 
@@ -150,43 +206,37 @@ public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelecti
                     return result;
                 }
             });
-                       if (this.cdmBaseToBeFiltered == null){
-                           selectedClassification = classifications.iterator().next();
-                       } else {
-                           selectedClassification = this.cdmBaseToBeFiltered.getClassification();
+                       if (selectedClassification == null){
+//                         if (this.cdmBaseToBeFiltered.isEmpty()){
+                               selectedClassification = classifications.iterator().next();
+
+//                         } else {
+//                             selectedClassification = this.cdmBaseToBeFiltered.getClassification();
+//                         }
                        }
                }
-               if (pattern == null || pattern.equals("?")){
-                   model = CdmStore.getService(IClassificationService.class).getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(selectedClassification.getUuid(),  limitOfInitialElements, pattern);
-               }else{
-                   model = CdmStore.getService(IClassificationService.class).getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(selectedClassification.getUuid(),  null, null);
-               }
-       }
+    }
 
-       /** {@inheritDoc} */
        @Override
        protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
                return null;
        }
 
-       /** {@inheritDoc} */
        @Override
-       protected String getNewWizardLinkText() {
+       protected String[] getNewWizardText() {
                return null;
        }
 
-       /** {@inheritDoc} */
        @Override
     public void widgetSelected(SelectionEvent e) {
                selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
 
 //             refresh();
-               setPattern(null);
+               if (getSearchField().getText() != null){
+                   search();
+               }
        }
 
-       /** {@inheritDoc} */
        @Override
     public void widgetDefaultSelected(SelectionEvent e) {}
-
-
 }