Fixes #2257
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialogs / filteredSelection / AbstractFilteredCdmResourceSelectionDialog.java
index f42eb82f60ca8bc4e96565196d77e34fca5d954a..ac6059f555b778e04fe7358dff84ef4a5fc917b7 100644 (file)
@@ -10,6 +10,8 @@
 
 package eu.etaxonomy.taxeditor.ui.dialogs.filteredSelection;
 
+import java.lang.reflect.Field;
+import java.security.acl.LastOwnerException;
 import java.text.Collator;
 import java.util.Comparator;
 import java.util.HashSet;
@@ -28,6 +30,7 @@ import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.jface.window.Window;
 import org.eclipse.jface.wizard.WizardDialog;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
@@ -97,17 +100,17 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                setListLabelProvider(labelProvider);
                setDetailsLabelProvider(labelProvider);
                
-//             setSelectionHistory(new ResourceSelectionHistory());
+               setSelectionHistory(new ResourceSelectionHistory());
        }
        
        /**
-        * Override in suibclasses.
+        * Override in subclasses.
         * Will run before initModel()
         */
        protected void init() {
                
        }
-
+       
        /**
         * <p>getSelectionFromDialog</p>
         *
@@ -116,7 +119,10 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
         * @return a TYPE object.
         */
        protected static <TYPE extends CdmBase> TYPE getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog<TYPE> dialog) {
-               if (dialog.open() == Window.CANCEL) {
+               //dialog.setInitialPattern("");
+               int result = dialog.open();
+               
+               if (result == Window.CANCEL) {
                        return null;
                }
                
@@ -181,6 +187,7 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
                }
                return null;
        }
+       
 
        /** {@inheritDoc} */
        @Override
@@ -202,6 +209,14 @@ public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmB
        protected ItemsFilter createFilter() {
                return new ItemsFilter() {
 
+                       /**
+                        * Always returns false to enforce refiltering even if the pattern is equal
+                        */
+                       @Override
+                       public boolean equalsFilter(ItemsFilter filter) {
+                               return false;
+                       }
+                       
                        @Override
                        public boolean isConsistentItem(Object item) {
                                return false;
@@ -228,6 +243,29 @@ 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. 
+               // 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 
+               // way to solve the problem.
+               try {
+                       Field lastCompletedFilter = this.getClass().getSuperclass().getSuperclass().getDeclaredField("lastCompletedFilter");
+                       lastCompletedFilter.setAccessible(true);
+                       lastCompletedFilter.set(this, null);
+               } catch (SecurityException e) {
+                       StoreUtil.error(getClass(), e);
+               } catch (NoSuchFieldException e) {
+                       StoreUtil.error(getClass(), e);
+               } catch (IllegalArgumentException e) {
+                       StoreUtil.error(getClass(), e);
+               } catch (IllegalAccessException e) {
+                       StoreUtil.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
+               // once it was opened. Am I doing it wrong?
                ((Text) getPatternControl()).setText(getTitle(cdmObject));
        }