Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / AbstractFilteredCdmResourceSelectionDialog.java
index fba0ce6c3ee5ad90283aaea259c60e6070a82ad0..a6ec94c170de6d86d423beb86ca5a7e69a7f1988 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2007 EDIT
 * European Distributed Institute of Taxonomy
 
 package eu.etaxonomy.taxeditor.ui.dialog.selection;
 
-import java.lang.reflect.Field;
 import java.text.Collator;
 import java.util.Comparator;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 import java.util.UUID;
@@ -39,7 +38,6 @@ import org.eclipse.swt.widgets.Link;
 import org.eclipse.swt.widgets.Shell;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.IMemento;
-import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
 
 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
@@ -62,13 +60,14 @@ import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
  * @version 1.0
  */
 public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmBase> extends
-               FilteredItemsSelectionDialog implements IConversationEnabled {
+               CdmFilteredItemsSelectionDialog implements IConversationEnabled {
 
        private final ConversationHolder conversation;
 
        protected List<UuidAndTitleCache<T>> model;
        private final Set<T> transientCdmObjects = new HashSet<T>();
        private final String settings;
+       protected final int limitOfInitialElements = 100;
 
        protected T cdmBaseToBeFiltered;
 
@@ -98,9 +97,9 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                initModel();
                shell.setCursor(cursor);
                String objectTitle = getTitle(cdmObject);
-               if (objectTitle != null) {
-                       setInitialPattern(objectTitle);
-               }
+//             if (objectTitle != null) {
+//                     setInitialPattern(objectTitle);
+//             }
 
                setListLabelProvider(createListLabelProvider());
                setDetailsLabelProvider(createDetailsLabelProvider());
@@ -223,15 +222,10 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
        /** {@inheritDoc} */
        @Override
        public void refresh() {
-               initModel();
-               filterExcludedObjects();
                super.refresh();
        }
 
-       /**
-        * <p>initModel</p>
-        */
-       abstract protected void initModel();
+
 
        /* (non-Javadoc)
         * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
@@ -281,19 +275,19 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                // and all the clever caching prevents the content provider from knowing that the model has changed
                // I am aware, that this is a hack, but the FilteredSelectionDialog API does not offer a convenient
                // way to solve the problem.
-               try {
-                       Field lastCompletedFilter = this.getClass().getSuperclass().getSuperclass().getDeclaredField("lastCompletedFilter");
-                       lastCompletedFilter.setAccessible(true);
-                       lastCompletedFilter.set(this, null);
-               } catch (SecurityException e) {
-                       MessagingUtils.error(getClass(), e);
-               } catch (NoSuchFieldException e) {
-                       MessagingUtils.error(getClass(), e);
-               } catch (IllegalArgumentException e) {
-                       MessagingUtils.error(getClass(), e);
-               } catch (IllegalAccessException e) {
-                       MessagingUtils.error(getClass(), e);
-               }
+//             try {
+//                     Field lastCompletedFilter = this.getClass().getSuperclass().getSuperclass().getDeclaredField("lastCompletedFilter");
+//                     lastCompletedFilter.setAccessible(true);
+//                     lastCompletedFilter.set(this, null);
+//             } catch (SecurityException e) {
+//                     MessagingUtils.error(getClass(), e);
+//             } catch (NoSuchFieldException e) {
+//                     MessagingUtils.error(getClass(), e);
+//             } catch (IllegalArgumentException e) {
+//                     MessagingUtils.error(getClass(), e);
+//             } catch (IllegalAccessException e) {
+//                     MessagingUtils.error(getClass(), e);
+//             }
 
                // this also is not the nicest way to do it.
                // I am still amazed, that FilteredSelectionDialog does not offer any methods to change its data
@@ -302,6 +296,8 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                ((Text) getPatternControl()).setText(pattern);
        }
 
+
+
        /* (non-Javadoc)
        * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
        */
@@ -313,8 +309,14 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                try {
                        if(model != null){
                                progressMonitor.beginTask("Looking for entities", model.size());
-                               for(UuidAndTitleCache<T> element : model){
-                                       contentProvider.add(element, itemsFilter);
+                               filterExcludedObjects();
+                               Iterator<UuidAndTitleCache<T>> iterator = model.iterator();
+                               UuidAndTitleCache<T> element;
+                               while(iterator.hasNext()){
+                                   element = iterator.next();
+                                   if (!element.equals(cdmBaseToBeFiltered)){
+                                       contentProvider.add(element, itemsFilter);
+                                   }
                                        if (progressMonitor.isCanceled()) {
                                                throw new OperationCanceledException();
                                        }
@@ -363,7 +365,14 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                        public int compare(UuidAndTitleCache entity1,
                                        UuidAndTitleCache entity2) {
                                Collator collator = Collator.getInstance();
-                               return collator.compare(entity1.getTitleCache(), entity2.getTitleCache());
+                               if (entity1.getUuid().equals(entity2.getUuid())){
+                                   return 0;
+                               }
+                               int result = collator.compare(entity1.getTitleCache(), entity2.getTitleCache());
+                               if (result == 0){
+                                   result = entity1.getUuid().compareTo(entity2.getUuid());
+                               }
+                               return result;
                        }
                };
        }
@@ -477,26 +486,29 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                        @Override
                        public void widgetSelected(SelectionEvent e) {
 
-                               AbstractNewEntityWizard wizard = getNewEntityWizard(e.text);
-
-                               wizard.init(null, null);
-                               if(wizard.getEntity() != null) {
-                                       WizardDialog dialog = new WizardDialog(getShell(), wizard);
-                                       int status = dialog.open();
-
-                                       if (status == IStatus.OK) {
-
-                                               T entity = (T) wizard.getEntity();
-                                               model.add(new UuidAndTitleCache<T>(entity.getUuid(),
-                                                       entity.getId(),
-                                                       getTitle(entity)));
-                                               refresh();
-                                               setPattern(entity);
-                                               getConversationHolder().bind();
-                                       }
-                                       //FIXME : Need to make sure this is a stable fix (ticket 3822)
-                                       getConversationHolder().commit();
-                               }
+                           AbstractNewEntityWizard wizard = getNewEntityWizard(e.text);
+                           if(wizard!=null){
+                               wizard.init(null, null);
+                               if(wizard.getEntity() != null) {
+                                   WizardDialog dialog = new WizardDialog(getShell(), wizard);
+                                   int status = dialog.open();
+
+                                   if (status == IStatus.OK) {
+
+                                       T entity = (T) wizard.getEntity();
+                                      // model.add(new UuidAndTitleCache<T>(entity.getUuid(), entity.getId(), getTitle(entity)));
+                                       refresh();
+                                       setPattern(entity);
+                                       if (getConversationHolder() != null){
+                                           getConversationHolder().bind();
+                                       }
+                                   }
+                                   //FIXME : Need to make sure this is a stable fix (ticket 3822)
+                                   if (getConversationHolder() != null){
+                                       getConversationHolder().commit();
+                                   }
+                               }
+                           }
                        }
                };
        }