ref #9204 upgrade and remove further libraries in taxeditor (including commons-lang...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / CdmFilteredItemsSelectionDialog.java
index 45a5810430cc3b7bac7259f6646bb8691ed3a191..3d98ed0fc057b47e12b130f8207361fab5451169 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2016 EDIT
 * European Distributed Institute of Taxonomy
@@ -127,7 +126,18 @@ import org.eclipse.ui.statushandlers.StatusManager;
  * @author k.luther
  * @date 10.06.2016
  *
+ * This is a class copied from FilteredItemsSelectionDialog and adapted to the Cdm use case. The original
+ * dialog gets all items when opening the dialog and then apply the filter on all items.
+ * In our case we need a possibility to filter the items already when getting them from the DB, so we adapt the
+ * original class
+ * the abstract method initMOdel was added and the method applyFilter was modified
+ * original description:
+ * Shows a list of items to the user with a text entry field for a string
+ * pattern used to filter the list of items.
+ *
+ *
  */
+
 public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDialog {
 
 
@@ -407,7 +417,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
             this.refreshCacheJob.cancel();
             this.refreshProgressMessageJob.cancel();
             if (showViewHandler != null) {
-                IHandlerService service = (IHandlerService) PlatformUI
+                IHandlerService service = PlatformUI
                         .getWorkbench().getService(IHandlerService.class);
                 service.deactivateHandler(showViewHandler);
                 showViewHandler.getHandler().dispose();
@@ -513,7 +523,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
                 public void keyTraversed(TraverseEvent e) {
                     if (e.detail == SWT.TRAVERSE_MNEMONIC && e.doit) {
                         e.detail = SWT.TRAVERSE_NONE;
-                        list.getTable().setFocus();
+                        getList().getTable().setFocus();
                     }
                 }
             });
@@ -558,7 +568,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
 
             fillViewMenu(menuManager);
 
-            IHandlerService service = (IHandlerService) PlatformUI.getWorkbench()
+            IHandlerService service = PlatformUI.getWorkbench()
                     .getService(IHandlerService.class);
             IHandler handler = new AbstractHandler() {
                 @Override
@@ -601,7 +611,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
          * @since 3.5
          */
         protected void fillContextMenu(IMenuManager menuManager) {
-            List selectedElements= ((StructuredSelection)list.getSelection()).toList();
+            List selectedElements= ((StructuredSelection)getList().getSelection()).toList();
 
             Object item= null;
 
@@ -634,7 +644,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
                 }
             });
 
-            final Table table = list.getTable();
+            final Table table = getList().getTable();
             Menu menu= contextMenuManager.createContextMenu(table);
             table.setMenu(menu);
         }
@@ -682,9 +692,9 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
 
             final Label listLabel = createLabels(content);
 
-            list = new TableViewer(content, (multi ? SWT.MULTI : SWT.SINGLE)
-                    | SWT.BORDER | SWT.V_SCROLL | SWT.VIRTUAL);
-            list.getTable().getAccessible().addAccessibleListener(
+            setList(new TableViewer(content, (multi ? SWT.MULTI : SWT.SINGLE)
+                    | SWT.BORDER | SWT.V_SCROLL | SWT.VIRTUAL));
+            getList().getTable().getAccessible().addAccessibleListener(
                     new AccessibleAdapter() {
                         @Override
                         public void getName(AccessibleEvent e) {
@@ -694,14 +704,14 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
                             }
                         }
                     });
-            list.setContentProvider(contentProvider);
-            list.setLabelProvider(getItemsListLabelProvider());
-            list.setInput(new Object[0]);
-            list.setItemCount(contentProvider.getNumberOfElements());
+            getList().setContentProvider(contentProvider);
+            getList().setLabelProvider(getItemsListLabelProvider());
+            getList().setInput(new Object[0]);
+            getList().setItemCount(contentProvider.getNumberOfElements());
             gd = new GridData(GridData.FILL_BOTH);
-            applyDialogFont(list.getTable());
-            gd.heightHint= list.getTable().getItemHeight() * 15;
-            list.getTable().setLayoutData(gd);
+            applyDialogFont(getList().getTable());
+            gd.heightHint= getList().getTable().getItemHeight() * 15;
+            getList().getTable().setLayoutData(gd);
 
             createPopupMenu();
 
@@ -716,14 +726,14 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
                 @Override
                 public void keyPressed(KeyEvent e) {
                     if (e.keyCode == SWT.ARROW_DOWN) {
-                        if (list.getTable().getItemCount() > 0) {
-                            list.getTable().setFocus();
+                        if (getList().getTable().getItemCount() > 0) {
+                            getList().getTable().setFocus();
                         }
                     }
                 }
             });
 
-            list.addSelectionChangedListener(new ISelectionChangedListener() {
+            getList().addSelectionChangedListener(new ISelectionChangedListener() {
                 @Override
                 public void selectionChanged(SelectionChangedEvent event) {
                     StructuredSelection selection = (StructuredSelection) event
@@ -732,20 +742,20 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
                 }
             });
 
-            list.addDoubleClickListener(new IDoubleClickListener() {
+            getList().addDoubleClickListener(new IDoubleClickListener() {
                 @Override
                 public void doubleClick(DoubleClickEvent event) {
                     handleDoubleClick();
                 }
             });
 
-            list.getTable().addKeyListener(new KeyAdapter() {
+            getList().getTable().addKeyListener(new KeyAdapter() {
                 @Override
                 public void keyPressed(KeyEvent e) {
 
                     if (e.keyCode == SWT.DEL) {
 
-                        List selectedElements = ((StructuredSelection) list
+                        List selectedElements = ((StructuredSelection) getList()
                                 .getSelection()).toList();
 
                         Object item = null;
@@ -768,20 +778,20 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
 
                     if (e.keyCode == SWT.ARROW_UP && (e.stateMask & SWT.SHIFT) != 0
                             && (e.stateMask & SWT.CTRL) != 0) {
-                        StructuredSelection selection = (StructuredSelection) list
+                        StructuredSelection selection = (StructuredSelection) getList()
                                 .getSelection();
 
                         if (selection.size() == 1) {
                             Object element = selection.getFirstElement();
-                            if (element.equals(list.getElementAt(0))) {
+                            if (element.equals(getList().getElementAt(0))) {
                                 pattern.setFocus();
                             }
-                            if (list.getElementAt(list.getTable()
+                            if (getList().getElementAt(getList().getTable()
                                     .getSelectionIndex() - 1) instanceof ItemsListSeparator) {
-                                list.getTable().setSelection(
-                                        list.getTable().getSelectionIndex() - 1);
+                                getList().getTable().setSelection(
+                                        getList().getTable().getSelectionIndex() - 1);
                             }
-                            list.getTable().notifyListeners(SWT.Selection,
+                            getList().getTable().notifyListeners(SWT.Selection,
                                     new Event());
 
                         }
@@ -791,12 +801,12 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
                             && (e.stateMask & SWT.SHIFT) != 0
                             && (e.stateMask & SWT.CTRL) != 0) {
 
-                        if (list
-                                .getElementAt(list.getTable().getSelectionIndex() + 1) instanceof ItemsListSeparator) {
-                            list.getTable().setSelection(
-                                    list.getTable().getSelectionIndex() + 1);
+                        if (getList()
+                                .getElementAt(getList().getTable().getSelectionIndex() + 1) instanceof ItemsListSeparator) {
+                            getList().getTable().setSelection(
+                                    getList().getTable().getSelectionIndex() + 1);
                         }
-                        list.getTable().notifyListeners(SWT.Selection, new Event());
+                        getList().getTable().notifyListeners(SWT.Selection, new Event());
                     }
 
                 }
@@ -880,9 +890,9 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
             IStatus status = new Status(IStatus.OK, PlatformUI.PLUGIN_ID,
                     IStatus.OK, EMPTY_STRING, null);
 
-            Object[] lastSelection = currentSelection;
+            Object[] lastSelection = getCurrentSelection();
 
-            currentSelection = selection.toArray();
+            setCurrentSelection(selection.toArray());
 
             if (selection.size() == 0) {
                 status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID,
@@ -890,10 +900,10 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
 
                 if (lastSelection != null
                         && getListSelectionLabelDecorator() != null) {
-                    list.update(lastSelection, null);
+                    getList().update(lastSelection, null);
                 }
 
-                currentSelection = null;
+                setCurrentSelection(null);
 
             } else {
                 status = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID,
@@ -927,11 +937,11 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
 
                 if (lastSelection != null
                         && getListSelectionLabelDecorator() != null) {
-                    list.update(lastSelection, null);
+                    getList().update(lastSelection, null);
                 }
 
                 if (getListSelectionLabelDecorator() != null) {
-                    list.update(currentSelection, null);
+                    getList().update(getCurrentSelection(), null);
                 }
             }
 
@@ -967,28 +977,28 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
          * Refreshes the dialog - has to be called in UI thread.
          */
         public void refresh() {
-            if (list != null && !list.getTable().isDisposed()) {
+            if (getList() != null && !getList().getTable().isDisposed()) {
 
-                List lastRefreshSelection = ((StructuredSelection) list
+                List lastRefreshSelection = ((StructuredSelection) getList()
                         .getSelection()).toList();
-                list.getTable().deselectAll();
+                getList().getTable().deselectAll();
 
-                list.setItemCount(contentProvider.getNumberOfElements());
-                list.refresh();
+                getList().setItemCount(contentProvider.getNumberOfElements());
+                getList().refresh();
 
-                if (list.getTable().getItemCount() > 0) {
+                if (getList().getTable().getItemCount() > 0) {
                     // preserve previous selection
                     if (refreshWithLastSelection && lastRefreshSelection != null
                             && lastRefreshSelection.size() > 0) {
-                        list.setSelection(new StructuredSelection(
+                        getList().setSelection(new StructuredSelection(
                                 lastRefreshSelection));
                     } else {
                         refreshWithLastSelection = true;
-                        list.getTable().setSelection(0);
-                        list.getTable().notifyListeners(SWT.Selection, new Event());
+                        getList().getTable().setSelection(0);
+                        getList().getTable().notifyListeners(SWT.Selection, new Event());
                     }
                 } else {
-                    list.setSelection(StructuredSelection.EMPTY);
+                    getList().setSelection(StructuredSelection.EMPTY);
                 }
 
             }
@@ -1022,7 +1032,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
          *            available
          */
         public void reloadCache(boolean checkDuplicates, IProgressMonitor monitor) {
-            if (list != null && !list.getTable().isDisposed()
+            if (getList() != null && !getList().getTable().isDisposed()
                     && contentProvider != null) {
                 contentProvider.reloadCache(checkDuplicates, monitor);
             }
@@ -1054,7 +1064,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
         @Override
         protected void computeResult() {
 
-            List selectedElements = ((StructuredSelection) list.getSelection())
+            List selectedElements = ((StructuredSelection) getList().getSelection())
                     .toList();
 
             List objectsToReturn = new ArrayList();
@@ -1140,7 +1150,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
          */
         protected StructuredSelection getSelectedItems() {
 
-            StructuredSelection selection = (StructuredSelection) list
+            StructuredSelection selection = (StructuredSelection) getList()
                     .getSelection();
 
             List selectedItems = selection.toList();
@@ -1185,11 +1195,13 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
         protected abstract ItemsFilter createFilter();
 
         /**
+         *
          * Applies the filter created by <code>createFilter()</code> method to the
          * items list. When new filter is different than previous one it will cause
          * refiltering.
          */
         protected void applyFilter() {
+            // to get an already filtered selection of the database we added the initModel() method here.
             initModel();
             ItemsFilter newFilter = createFilter();
 
@@ -1581,13 +1593,13 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
              */
             @Override
             public void run() {
-                List selectedElements = ((StructuredSelection) list.getSelection())
+                List selectedElements = ((StructuredSelection) getList().getSelection())
                         .toList();
                 removeSelectedItems(selectedElements);
             }
         }
 
-        private static boolean showColoredLabels() {
+        protected static boolean showColoredLabels() {
             return PlatformUI.getPreferenceStore().getBoolean(IWorkbenchPreferenceConstants.USE_COLORED_LABELS);
         }
 
@@ -1682,9 +1694,9 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
             }
 
             private boolean isSelected(Object element) {
-                if (element != null && currentSelection != null) {
-                    for (int i = 0; i < currentSelection.length; i++) {
-                        if (element.equals(currentSelection[i])) {
+                if (element != null && getCurrentSelection() != null) {
+                    for (int i = 0; i < getCurrentSelection().length; i++) {
+                        if (element.equals(getCurrentSelection()[i])) {
                             return true;
                         }
                     }
@@ -1749,17 +1761,17 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
             }
 
             private String getSeparatorLabel(String separatorLabel) {
-                Rectangle rect = list.getTable().getBounds();
+                Rectangle rect = getList().getTable().getBounds();
 
-                int borderWidth = list.getTable().computeTrim(0, 0, 0, 0).width;
+                int borderWidth = getList().getTable().computeTrim(0, 0, 0, 0).width;
 
                 int imageWidth = WorkbenchImages.getImage(
                         IWorkbenchGraphicConstants.IMG_OBJ_SEPARATOR).getBounds().width;
 
                 int width = rect.width - borderWidth - imageWidth;
 
-                GC gc = new GC(list.getTable());
-                gc.setFont(list.getTable().getFont());
+                GC gc = new GC(getList().getTable());
+                gc.setFont(getList().getTable().getFont());
 
                 int fSeparatorWidth = gc.getAdvanceWidth('-');
                 int fMessageLength = gc.textExtent(separatorLabel).x;
@@ -1884,7 +1896,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
          * Used in ItemsListContentProvider, separates history and non-history
          * items.
          */
-        private class ItemsListSeparator {
+        protected class ItemsListSeparator {
 
             private final String name;
 
@@ -2974,7 +2986,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
             @Override
             public void updateElement(int index) {
 
-                CdmFilteredItemsSelectionDialog.this.list.replace((lastFilteredItems
+                CdmFilteredItemsSelectionDialog.this.getList().replace((lastFilteredItems
                         .size() > index) ? lastFilteredItems.get(index) : null,
                         index);
 
@@ -3010,7 +3022,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
 
                 // the TableViewer's root (the input) is treated as parent
 
-                lastFilteredItems = Arrays.asList(getFilteredItems(list.getInput(),
+                lastFilteredItems = Arrays.asList(getFilteredItems(getList().getInput(),
                         monitor != null ? new SubProgressMonitor(monitor, 100)
                                 : null));
 
@@ -3104,7 +3116,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
                 if (filters != null && filteredElements != null) {
                     for (Iterator iter = filters.iterator(); iter.hasNext();) {
                         ViewerFilter f = (ViewerFilter) iter.next();
-                        filteredElements = f.filter(list, parent, filteredElements);
+                        filteredElements = f.filter(getList(), parent, filteredElements);
                         monitor.worked(ticks);
                     }
                 }
@@ -3149,7 +3161,7 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
             /**
              * Adds a filter to this content provider. For an example usage of such
              * filters look at the project <code>org.eclipse.ui.ide</code>, class
-             * <code>org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog.CustomWorkingSetFilter</code>.
+             * <code>org.eclipse.ui.dialogs.FilteredResourcesSelectionDialog.CustomDescriptiveDataSetFilter</code>.
              *
              *
              * @param filter
@@ -3404,10 +3416,28 @@ public abstract class CdmFilteredItemsSelectionDialog extends SelectionStatusDia
         }
 
         /**
+         * CDM IMPLEMENTATION
+         * we have to call the initModel() method in the applyFilter method, so we had to add this abstract method (moved from AbstractFilteredCdmResourceSelectionDialog)
          * <p>initModel</p>
          */
         abstract protected void initModel();
 
+               public Object[] getCurrentSelection() {
+                       return currentSelection;
+               }
+
+               public void setCurrentSelection(Object[] currentSelection) {
+                       this.currentSelection = currentSelection;
+               }
+
+               public TableViewer getList() {
+                       return list;
+               }
+
+               public void setList(TableViewer list) {
+                       this.list = list;
+               }
+
     }