Merge branch 'release/5.17.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / TaxonNodeSelectionDialog.java
index e2e864bceb5333cfeadf8bea5f2734f12d3accff..3ac813c44dd7deea7ddfef106f5f5f2363f8552c 100644 (file)
@@ -1,13 +1,17 @@
 /**
- * 
+ *
  */
 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;
@@ -18,172 +22,221 @@ import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Shell;
 
-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;
 
 /**
- * <p>FilteredTaxonNodeSelectionDialog class.</p>
- *
  * @author p.ciardelli
- * @version $Id: $
  */
-public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelectionDialog<TaxonNode> implements SelectionListener{      
-       
-       /**
-        * <p>select</p>
-        *
-        * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
-        * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
-        * @param title a {@link java.lang.String} object.
-        * @param excludeTaxa a {@link java.util.List} object.
-        * @param node a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
-        * @return a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
-        */
-       public static TaxonNode select(Shell shell, ConversationHolder conversation, String title, List<UUID> excludeTaxa, TaxonNode node, Classification classification) {
-               TaxonNodeSelectionDialog dialog = new TaxonNodeSelectionDialog(shell, 
-                               conversation,
+public class TaxonNodeSelectionDialog extends AbstractFilteredCdmResourceSelectionDialog<TaxonNode> implements SelectionListener{
+
+       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,
                                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;  
-
-       /**
-        * <p>Constructor for FilteredTaxonNodeSelectionDialog.</p>
-        *
-        * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
-        * @param title a {@link java.lang.String} object.
-        * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
-        * @param excludeTaxa a {@link java.util.List} object.
-        * @param multi a boolean.
-        * @param node a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
-        */
-       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);
-               
+
+       private Classification selectedClassification;
+       private UUID selectedUuid;
+//     private final Set<UUID> excludeTaxa;
+       private boolean allowClassificationSelection = false;
+
+       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);
-               
-               if(classification != null){
-                       selectedClassification = classification;
-               }
-       }
+//             setDetailsLabelProvider(labelProvider);
+               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);
+       }
 
-       /* (non-Javadoc)
-        * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
-        */
        /** {@inheritDoc} */
        @Override
-       protected Control createExtendedContentArea(Composite parent) {
-               return createClassificationSelectionCombo(parent);
+       protected Control createDialogArea(Composite parent) {
+           Composite container = (Composite) super.createDialogArea(parent);
+               return createClassificationSelectionCombo(container);
        }
-       
+
        /*
         * currently disabled tree selection composite
         */
        private Control createClassificationSelectionCombo(Composite parent){
 //             classifications = CdmStore.getTaxonTreeService().list(null, null, null, null, null);
-               
+
                Composite classificationSelection = new Composite(parent, SWT.NULL);
                classificationSelection.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
-               
+
                GridLayout layout = new GridLayout();
                classificationSelection.setLayout(layout);
-               
+
                Label label = new Label(classificationSelection, SWT.NULL);
                // TODO not working is not really true but leave it here to remind everyone that this is under construction
-               label.setText("Select Classification (experimental)");
+               label.setText("Select Classification");
                classificationSelectionCombo = new Combo(classificationSelection, SWT.BORDER | SWT.READ_ONLY);
                classificationSelectionCombo.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
-               
+
                for(Classification tree : classifications){
                        classificationSelectionCombo.add(tree.getName().getText(), classifications.indexOf(tree));
+                       if (tree.getUuid().equals(selectedUuid)){
+                           selectedClassification = tree;
+                       }
+
                }
-               
+
                classificationSelectionCombo.select(classifications.indexOf(selectedClassification));
-               
+
                // TODO remember last selection
                classificationSelectionCombo.addSelectionListener(this);
-               
-               
-               
+
+
+
                return classificationSelection;
        }
 
-       /** {@inheritDoc} */
        @Override
        protected String getTitle(TaxonNode taxonNode) {
                if(taxonNode != null && taxonNode.getTaxon() != null){
                        return taxonNode.getTaxon().getTitleCache();
                }
-               
+
                return "";
        }
-       
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#getPersistentObject(java.util.UUID)
-        */
-       /** {@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);
+       }
+
+       @Override
+       protected void callService(String pattern) {
+           model = CdmStore.getService(IClassificationService.class)
+                       .getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(
+                                       selectedClassification.getUuid(), limitOfInitialElements, pattern, allowClassificationSelection, true);
        }
 
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.dialogs.AbstractFilteredCdmResourceSelectionDialog#initModel()
-        */
-       /** {@inheritDoc} */
        @Override
-       protected void initModel() {
-               // default to first tree 
-               // TODO this will be problematic and can only be seen as workaround 
-               
-               
-               if(classifications == null){
+       protected void sort() {
+           if(!PreferencesUtil.isSortTaxaByRankAndName()){
+            Collections.sort(model, getItemsComparator());
+        }
+      // otherwise result is already sorted
+    }
+
+    private void fillClassifications() {
+        if(classifications == null){
                        classifications = CdmStore.getService(IClassificationService.class).list(null, null, null, null, null);
-                       selectedClassification = classifications.iterator().next();
+                       Collections.sort(classifications, new Comparator<Classification>() {
+
+                @Override
+                public int compare(Classification o1, Classification o2) {
+                    if (o1.equals(o2)){
+                        return 0;
+                    }
+                    int result = o1.getTitleCache().compareTo(o2.getTitleCache());
+                    if (result == 0){
+                        return o1.getUuid().compareTo(o2.getUuid());
+                    }
+                    return result;
+                }
+            });
+                       if (selectedClassification == null){
+//                         if (this.cdmBaseToBeFiltered.isEmpty()){
+                               selectedClassification = classifications.iterator().next();
+
+//                         } else {
+//                             selectedClassification = this.cdmBaseToBeFiltered.getClassification();
+//                         }
+                       }
                }
-               
-               model = CdmStore.getService(IClassificationService.class).getTaxonNodeUuidAndTitleCacheOfAcceptedTaxaByClassification(selectedClassification);
-       }
+    }
 
-       /** {@inheritDoc} */
        @Override
        protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
                return null;
        }
 
-       /** {@inheritDoc} */
        @Override
-       protected String getNewWizardLinkText() {
+       protected String[] getNewWizardText() {
                return null;
        }
-       
-       /** {@inheritDoc} */
-       public void widgetSelected(SelectionEvent e) {
+
+       @Override
+    public void widgetSelected(SelectionEvent e) {
                selectedClassification = classifications.get(classificationSelectionCombo.getSelectionIndex());
-               
+
 //             refresh();
-               setPattern(null);
+               if (getSearchField().getText() != null){
+                   search();
+               }
        }
 
-       /** {@inheritDoc} */
-       public void widgetDefaultSelected(SelectionEvent e) {}
-
-
+       @Override
+    public void widgetDefaultSelected(SelectionEvent e) {}
 }