Merge branch 'hotfix/3.12.4' into develop
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / AbstractFilteredCdmResourceSelectionDialog.java
index d9727902a7c5f96df5b847f9231a47a4d9ae4fbe..a22f14454b57b8fe06cad6624edb8ee1e1ce9153 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
@@ -32,6 +32,7 @@ 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.graphics.Cursor;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Link;
@@ -45,10 +46,12 @@ import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.ICdmBase;
 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
-import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
-import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
+import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
 
 /**
@@ -66,14 +69,15 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
        protected List<UuidAndTitleCache<T>> model;
        private final Set<T> transientCdmObjects = new HashSet<T>();
        private final String settings;
-       
+
        protected T cdmBaseToBeFiltered;
-       
+
+
        /**
         * <p>Constructor for AbstractFilteredCdmResourceSelectionDialog.</p>
         *
         * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
-        * @param conversation 
+        * @param conversation
         * @param title a {@link java.lang.String} object.
         * @param multi a boolean.
         * @param settings a {@link java.lang.String} object.
@@ -85,29 +89,30 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                setTitle(title);
                setMessage("Use * for wildcard, or ? to see all entries");
                this.settings = settings;
-               
+
                this.conversation = conversation;
-               
+               this.cdmBaseToBeFiltered = cdmObject;
+               Cursor cursor = shell.getCursor();
+               shell.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT));
                init();
-               
                initModel();
-               
+               shell.setCursor(cursor);
                String objectTitle = getTitle(cdmObject);
                if (objectTitle != null) {
                        setInitialPattern(objectTitle);
                }
-               
+
                setListLabelProvider(createListLabelProvider());
                setDetailsLabelProvider(createDetailsLabelProvider());
-               
+
                setSelectionHistory(new ResourceSelectionHistory());
        }
-       
+
        /**
         * By default, we are returning the standard list label provider
-        * 
-        * Override in subclasses if you want different behavior 
-        * 
+        *
+        * Override in subclasses if you want different behavior
+        *
         * @return
         */
        protected ILabelProvider createDetailsLabelProvider() {
@@ -115,7 +120,7 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
        }
 
        /**
-        * 
+        *
         * @return
         */
        protected ILabelProvider createListLabelProvider() {
@@ -127,9 +132,9 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
         * Will run before initModel()
         */
        protected void init() {
-               
+
        }
-       
+
        /**
         * <p>getSelectionFromDialog</p>
         *
@@ -140,18 +145,18 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
        protected static <TYPE extends CdmBase> TYPE getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog<TYPE> dialog) {
                //dialog.setInitialPattern("");
                int result = dialog.open();
-               
+
                if (result == Window.CANCEL) {
                        return null;
                }
-               
+
                UUID uuid = dialog.getSelectedUuidAndTitleCache().getUuid();
                if(uuid == null){
                        return null;
-               }       
+               }
                return dialog.getCdmObjectByUuid(uuid);
        }
-       
+
        /**
         * Check if object was created during the life of this dialog. If not,
         * retrieve it from the CdmStore.
@@ -167,7 +172,7 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                }
                return getPersistentObject(cdmUuid);
        }
-       
+
        /**
         * <p>getPersistentObject</p>
         *
@@ -204,16 +209,16 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                if(cdmObject == null){
                        return "";
                }
-               
+
                if (cdmObject instanceof IIdentifiableEntity) {
-                       return ((IIdentifiableEntity) cdmObject).getTitleCache();                       
+                       return ((IIdentifiableEntity) cdmObject).getTitleCache();
                }
-               
+
                throw new IllegalArgumentException("Generic method only" +
                                " supports cdmObject of type IIdentifiableEntity." +
                                " Please implement specific method in subclass.");
        }
-       
+
 
        /** {@inheritDoc} */
        @Override
@@ -222,7 +227,7 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                filterExcludedObjects();
                super.refresh();
        }
-       
+
        /**
         * <p>initModel</p>
         */
@@ -243,7 +248,7 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                        public boolean equalsFilter(ItemsFilter filter) {
                                return false;
                        }
-                       
+
                        @Override
                        public boolean isConsistentItem(Object item) {
                                return false;
@@ -259,10 +264,10 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                                }
                                return text != null ? matches(text) : false;
                        }
-                       
+
                };
        }
-       
+
 
        /**
         * Set the filter input to the Agent's title cache
@@ -270,33 +275,33 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
         * @param cdmObject a T object.
         */
        protected void setPattern(T cdmObject) {
-               // FilteredSelection does some very tricky caching to make sure it 
-               // runs with high performance. 
+               // FilteredSelection does some very tricky caching to make sure it
+               // runs with high performance.
                // This works for most use cases, but we want to change the model while the dialog is open
                // 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 
+               // 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) {
-                       AbstractUtility.error(getClass(), e);
+                       MessagingUtils.error(getClass(), e);
                } catch (NoSuchFieldException e) {
-                       AbstractUtility.error(getClass(), e);
+                       MessagingUtils.error(getClass(), e);
                } catch (IllegalArgumentException e) {
-                       AbstractUtility.error(getClass(), e);
+                       MessagingUtils.error(getClass(), e);
                } catch (IllegalAccessException e) {
-                       AbstractUtility.error(getClass(), e);
+                       MessagingUtils.error(getClass(), e);
                }
-               
-               // this also is not the nicest way to do it. 
+
+               // 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
                // once it was opened. Am I doing it wrong?
                String pattern = getTitle(cdmObject);
                ((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)
        */
@@ -306,7 +311,7 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
                throws CoreException {
                try {
-                       if(model != null){                              
+                       if(model != null){
                                progressMonitor.beginTask("Looking for entities", model.size());
                                for(UuidAndTitleCache<T> element : model){
                                        contentProvider.add(element, itemsFilter);
@@ -316,7 +321,7 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                                        progressMonitor.worked(1);
                                }
                        }else{
-                               AbstractUtility.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
+                               MessagingUtils.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
                        }
                }
                finally {
@@ -371,7 +376,7 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
        protected IStatus validateItem(Object item) {
                return Status.OK_STATUS;
        }
-       
+
        /**
         * <p>getSelectedUuidAndTitleCache</p>
         *
@@ -381,7 +386,7 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                Object[] result = getResult();
                return result[0] == null ? null : (UuidAndTitleCache) result[0];
        }
-       
+
        /**
         * <p>Getter for the field <code>settings</code>.</p>
         *
@@ -393,9 +398,9 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                }
                return settings;
        }
-       
+
        /**
-        * 
+        *
         * @author n.hoffmann
         * @created Oct 19, 2009
         * @version 1.0
@@ -417,29 +422,34 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                        element.putString("resource", item.toString()); //$NON-NLS-1$
                }
        }
-       
+
        /**
         * <p>getNewWizardLinkText</p>
         *
         * @return a {@link java.lang.String} object.
         */
        protected abstract String getNewWizardLinkText();
-       
+
        /**
         * <p>getNewEntityWizard</p>
-        * @param parameter 
+        * @param parameter
         * @return a {@link eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard} object.
         */
        protected abstract AbstractNewEntityWizard getNewEntityWizard(String parameter);
-       
+
        public class FilteredCdmResourceLabelProvider extends LabelProvider {
                @Override
                public String getText(Object element) {
                        if (element == null) {
                                return null;
                        }
-                       return ((UuidAndTitleCache) element).getTitleCache();
-               }                       
+                       UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
+                       String titleCache = uuidAndTitleCache.getTitleCache();
+                       if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
+                           titleCache += " ["+uuidAndTitleCache.getId()+"]";
+                       }
+            return titleCache;
+               }
        };
 
        /* (non-Javadoc)
@@ -457,35 +467,41 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                }
                return null;
        }
-       
+
        protected SelectionListener getNewWizardLinkSelectionListener(){
                return new SelectionAdapter() {
-                       
+
                        /* (non-Javadoc)
                         * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
                         */
                        @Override
                        public void widgetSelected(SelectionEvent e) {
 
-                               AbstractNewEntityWizard wizard = getNewEntityWizard(e.text);
+                           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();
 
-                               wizard.init(null, null);
-                               if(wizard.getEntity() != null) {
-                                       WizardDialog dialog = new WizardDialog(getShell(), wizard);
-                                       int status = dialog.open();
+                                   if (status == IStatus.OK) {
 
-                                       if (status == IStatus.OK) {
-
-                                               T entity = (T) wizard.getEntity();
-                                               refresh();
-                                               setPattern(entity);
-                                               getConversationHolder().bind();
-                                       }
-                               }
+                                       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();
+                               }
+                           }
                        }
                };
        }
-       
+
        /**
         * <p>getConversationHolder</p>
         *
@@ -495,26 +511,26 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
        public ConversationHolder getConversationHolder() {
                return conversation;
        }
-       
+
        /** {@inheritDoc} */
        @Override
        public void update(CdmDataChangeMap changeEvents) {}
 
        /**
-        * Don't want to add for example a taxon or synonym to itself 
-        * so filter the list to remove the taxon in question 
+        * Don't want to add for example a taxon or synonym to itself
+        * so filter the list to remove the taxon in question
         * (<code>cdmBaseToBeFiltered</code>)
         * so it is not available in the filtered list.
         */
        private void filterExcludedObjects() {
                if (model != null && cdmBaseToBeFiltered != null) {
-                       
+
                        UuidAndTitleCache uuidAndTitleCacheToRemove = null;
-                                               
+
                        for (UuidAndTitleCache uuidAndTitleCache : model){
                                if ((cdmBaseToBeFiltered.getUuid()).equals(uuidAndTitleCache.getUuid())) {
                                        uuidAndTitleCacheToRemove = uuidAndTitleCache;
-                               }                               
+                               }
                        }
                        model.remove(uuidAndTitleCacheToRemove);
                }