Merge branch 'release/5.8.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / NamedAreaSelectionDialog.java
index 5bd973b915c0428456e577d737dda478d4b525b4..b1808a5b27e34088819949a230cb4daf4ab01858 100644 (file)
-/**
-* Copyright (C) 2007 EDIT
-* 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.
-*/
-
-package eu.etaxonomy.taxeditor.ui.dialog.selection;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.UUID;
-
-import org.eclipse.jface.action.Action;
-import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.dialogs.Dialog;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.events.SelectionListener;
-import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Shell;
-
-import eu.etaxonomy.cdm.api.service.ITermService;
-import eu.etaxonomy.cdm.api.service.IVocabularyService;
-import eu.etaxonomy.cdm.model.location.NamedArea;
-import eu.etaxonomy.cdm.model.term.DefinedTermBase;
-import eu.etaxonomy.cdm.model.term.TermType;
-import eu.etaxonomy.cdm.model.term.TermVocabulary;
-import eu.etaxonomy.taxeditor.model.ImageResources;
-import eu.etaxonomy.taxeditor.model.MessagingUtils;
-import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
-import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
-import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.store.CdmStore;
-
-/**
- * @author n.hoffmann
- * @created May 11, 2010
- * @version 1.0
- */
-public class NamedAreaSelectionDialog extends
-               AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
-
-       private class IncludeNamedAreaVocabulary extends Action {
-               private final TermVocabulary<NamedArea> vocabulary;
-
-               /**
-                * Creates a new instance of the class.
-                */
-               public IncludeNamedAreaVocabulary(TermVocabulary<NamedArea> vocabulary) {
-                       super(vocabulary.getTitleCache(), IAction.AS_CHECK_BOX);
-                       this.vocabulary = vocabulary;
-               }
-
-               @Override
-        public void run(){
-                       if(isChecked()){
-                               selectedVocabularies.add(vocabulary);
-                       }else{
-                               selectedVocabularies.remove(vocabulary);
-                       }
-                       PreferencesUtil.setBooleanValue(getPrefKey(vocabulary), !isChecked());
-                       search();
-               }
-       }
-
-       protected List<TermVocabulary> selectedVocabularies;
-    protected List<TermVocabulary> vocabularies;
-
-
-
-       /**
-        * Creates a filtered selection dialog to select a named area.
-        *
-        * @param shell
-        *                              The shell for displaying this widget
-        * @param namedArea
-        *                              A namedArea that should be selected when the dialog opens
-        * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
-        * @param preferenceId a class which is used for generating the preference key so that every
-        * dialogs can be grouped to have their own preferences depending on this id
-        * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies
-        * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
-        */
-       public static NamedArea select(Shell shell, //ConversationHolder conversation,
-               NamedArea namedArea, String preferenceId, UUID... preselectedVocabularyUuids) {
-               NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, //conversation,
-                               "Choose an area", false, namedArea, preferenceId, preselectedVocabularyUuids);
-               return getSelectionFromDialog(dialog);
-       }
-
-       protected NamedAreaSelectionDialog(Shell shell, //ConversationHolder conversation,
-               String title, boolean multi, NamedArea namedArea, Object preferenceId, UUID... preselectedVocabularyUuids) {
-               super(shell, //conversation,
-                       title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);
-
-               selectedVocabularies = new ArrayList<TermVocabulary>();
-               this.preferenceID = preferenceId;
-
-               if (preselectedVocabularyUuids != null && preselectedVocabularyUuids.length > 0){
-               for(int i=0;i<preselectedVocabularyUuids.length;i++){
-                       TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
-                       selectedVocabularies.add(preselectedVocabulary);
-               }
-               }else{
-                   selectedVocabularies = createSelectedVocabularies();
-               }
-
-       }
-
-    protected List<TermVocabulary> createSelectedVocabularies() {
-        List<TermVocabulary> tempSelectedVocabularies = new ArrayList<TermVocabulary>();
-        for(TermVocabulary vocabulary:vocabularies){
-                       if((selectedVocabularies.contains(vocabulary) && !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary)))
-                                       || !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary))){
-                               tempSelectedVocabularies.add(vocabulary);
-                       }
-               }
-        return tempSelectedVocabularies;
-    }
-
-//    private static UUID[] createVocabularyUuidList() {
-//        String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
-//        if (StringUtils.isBlank(preselectedVocString)){
-//            return null;
-//        }
-//        String[] preselectedVocArray = preselectedVocString.split(";");
-//        UUID[] uuidList = new UUID[preselectedVocArray.length];
-//        int i = 0;
-//        for (String uuidString: preselectedVocArray){
-//            uuidList[i]= UUID.fromString(uuidString);
-//            i++;
-//        }
-//        return uuidList;
-//    }
-
-
-       private String getPrefKey(TermVocabulary vocabulary){
-               return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       protected NamedArea getPersistentObject(UUID uuid) {
-
-           DefinedTermBase area =  CdmStore.getService(ITermService.class).find(uuid);
-           if (area instanceof NamedArea){
-               return (NamedArea) area;
-           }
-
-               return null;
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       protected void init() {
-               vocabularies = getAvailableVocabularies();
-       }
-
-       private List<TermVocabulary> getAvailableVocabularies(){
-           vocabularies = new ArrayList();
-           CdmPreferenceCache cache = CdmPreferenceCache.instance();
-//        CdmPreference pref = cache.get(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
-//         if (pref != null && !pref.isAllowOverride()){
-//            UUID[] preselectedVocabularyUuids = createVocabularyUuidList();
-//
-//            for(int i=0;i<preselectedVocabularyUuids.length;i++){
-//                TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
-//                vocabularies.add(preselectedVocabulary);
-//            }
-//        }else{
-            vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
-//        }
-               //List<TermVocabulary> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
-               return vocabularies;
-       }
-
-//     /** {@inheritDoc} */
-//     @Override
-//     protected void search() {
-//         Control control =getSearchField();
-//        String pattern = null;
-//        if (control != null){
-//            pattern = ((Text)control).getText();
-//        }
-//
-//        if (pattern == null || pattern.equals("?")){
-//            model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, null, PreferencesUtil.getGlobalLanguage());
-//        }else{
-//            model = CdmStore.getService(ITermService.class).getUuidAndTitleCache(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
-//        }
-//     }
-
-//     /** {@inheritDoc} */
-//     @Override
-//     protected Control createExtendedContentArea(Composite parent) {
-//             return null;
-//     }
-
-       /** {@inheritDoc} */
-       @Override
-       protected String getTitle(NamedArea namedArea) {
-               try {
-                       String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
-                       return result;
-               } catch (Exception e) {
-                       MessagingUtils.error(NamedAreaSelectionDialog.class, "Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid(), e);
-                       return namedArea.getTitleCache();
-               }
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
-               return null;
-       }
-
-       /** {@inheritDoc} */
-       @Override
-       protected String[] getNewWizardText() {
-               return null;
-       }
-
-       @Override
-    void createFilterButton(Composite searchAndFilter)
-           {
-            filterButton = new Button(searchAndFilter, SWT.NONE);
-//            filterButton.setText("Filter");
-            filterButton.setImage(ImageResources.getImage(ImageResources.FUNNEL_ICON));
-//            SelectionListener filterSelectionListener = new FilterSelectionListener(preferenceID, this);
-            filterButton.addSelectionListener(new SelectionListener(){
-                @Override
-                public void widgetSelected(SelectionEvent e) {
-
-                        Object source = e.getSource();
-                        String text = null;
-                        if (source instanceof Button){
-                            Shell shell = ((Button)source).getShell();
-                            Dialog dialog = new FilterDialog(getShell(), preferenceID, selectedVocabularies, vocabularies);
-                            if(dialog!=null){
-                                dialog.open();
-                            }
-                            createSelectedVocabularies();
-                            search();
-                        }
-
-
-
-
-                }
-
-                @Override
-                public void widgetDefaultSelected(SelectionEvent e) {
-                    // TODO Auto-generated method stub
-
-                }
-
-
-            });
-
-        }
-
-
-
-    /* (non-Javadoc)
-     * @see eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog#callService(java.lang.String)
-     */
-    @Override
-    void callService(String pattern) {
-        if (selectedVocabularies == null || selectedVocabularies.size() == 0){
-            model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(vocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
-        }else{
-            model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());
-        }
-    }
-
-}
+/**\r
+* Copyright (C) 2007 EDIT\r
+* European Distributed Institute of Taxonomy\r
+* http://www.e-taxonomy.eu\r
+*\r
+* The contents of this file are subject to the Mozilla Public License Version 1.1\r
+* See LICENSE.TXT at the top of this package for the full license terms.\r
+*/\r
+\r
+package eu.etaxonomy.taxeditor.ui.dialog.selection;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.UUID;\r
+\r
+import org.eclipse.jface.action.Action;\r
+import org.eclipse.jface.action.IAction;\r
+import org.eclipse.jface.dialogs.Dialog;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.events.SelectionListener;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Shell;\r
+\r
+import eu.etaxonomy.cdm.api.service.ITermService;\r
+import eu.etaxonomy.cdm.api.service.IVocabularyService;\r
+import eu.etaxonomy.cdm.model.location.NamedArea;\r
+import eu.etaxonomy.cdm.model.term.DefinedTermBase;\r
+import eu.etaxonomy.cdm.model.term.TermType;\r
+import eu.etaxonomy.cdm.model.term.TermVocabulary;\r
+import eu.etaxonomy.taxeditor.model.ImageResources;\r
+import eu.etaxonomy.taxeditor.model.MessagingUtils;\r
+import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;\r
+import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;\r
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;\r
+import eu.etaxonomy.taxeditor.store.CdmStore;\r
+\r
+/**\r
+ * @author n.hoffmann\r
+ * @created May 11, 2010\r
+ * @version 1.0\r
+ */\r
+public class NamedAreaSelectionDialog extends\r
+               AbstractFilteredCdmResourceSelectionDialog<NamedArea> {\r
+\r
+       private class IncludeNamedAreaVocabulary extends Action {\r
+               private final TermVocabulary<NamedArea> vocabulary;\r
+\r
+               /**\r
+                * Creates a new instance of the class.\r
+                */\r
+               public IncludeNamedAreaVocabulary(TermVocabulary<NamedArea> vocabulary) {\r
+                       super(vocabulary.getTitleCache(), IAction.AS_CHECK_BOX);\r
+                       this.vocabulary = vocabulary;\r
+               }\r
+\r
+               @Override\r
+        public void run(){\r
+                       if(isChecked()){\r
+                               selectedVocabularies.add(vocabulary);\r
+                       }else{\r
+                               selectedVocabularies.remove(vocabulary);\r
+                       }\r
+                       PreferencesUtil.setBooleanValue(getPrefKey(vocabulary), !isChecked());\r
+                       search();\r
+               }\r
+       }\r
+       protected List<NamedArea> selectedAreas;\r
+       protected List<TermVocabulary> selectedVocabularies;\r
+    protected List<TermVocabulary> vocabularies;\r
+\r
+\r
+\r
+       /**\r
+        * Creates a filtered selection dialog to select a named area.\r
+        *\r
+        * @param shell\r
+        *                              The shell for displaying this widget\r
+        * @param namedArea\r
+        *                              A namedArea that should be selected when the dialog opens\r
+        * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.\r
+        * @param preferenceId a class which is used for generating the preference key so that every\r
+        * dialogs can be grouped to have their own preferences depending on this id\r
+        * @param preselectedVocabularyUuids the {@link UUID}s of the pre-selected vocabularies\r
+        * @return a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.\r
+        */\r
+       public static NamedArea select(Shell shell, //ConversationHolder conversation,\r
+               NamedArea namedArea, String preferenceId, UUID... preselectedVocabularyUuids) {\r
+               NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, //conversation,\r
+                               "Choose an area", false, namedArea, preferenceId, preselectedVocabularyUuids);\r
+               return getSelectionFromDialog(dialog);\r
+       }\r
+\r
+       protected NamedAreaSelectionDialog(Shell shell, //ConversationHolder conversation,\r
+               String title, boolean multi, NamedArea namedArea, Object preferenceId, UUID... preselectedVocabularyUuids) {\r
+               super(shell, //conversation,\r
+                       title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);\r
+\r
+               selectedVocabularies = new ArrayList<TermVocabulary>();\r
+               this.preferenceID = preferenceId;\r
+\r
+               if (preselectedVocabularyUuids != null && preselectedVocabularyUuids.length > 0){\r
+               for(int i=0;i<preselectedVocabularyUuids.length;i++){\r
+                       TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);\r
+                       selectedVocabularies.add(preselectedVocabulary);\r
+               }\r
+               }else{\r
+                   selectedVocabularies = createSelectedVocabularies();\r
+               }\r
+\r
+       }\r
+\r
+    protected List<TermVocabulary> createSelectedVocabularies() {\r
+        List<TermVocabulary> tempSelectedVocabularies = new ArrayList<TermVocabulary>();\r
+        for(TermVocabulary vocabulary:vocabularies){\r
+                       if((selectedVocabularies.contains(vocabulary) && !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary)))\r
+                                       || !PreferencesUtil.getBooleanValue(getPrefKey(vocabulary))){\r
+                               tempSelectedVocabularies.add(vocabulary);\r
+                       }\r
+               }\r
+        return tempSelectedVocabularies;\r
+    }\r
+\r
+//    private static UUID[] createVocabularyUuidList() {\r
+//        String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());\r
+//        if (StringUtils.isBlank(preselectedVocString)){\r
+//            return null;\r
+//        }\r
+//        String[] preselectedVocArray = preselectedVocString.split(";");\r
+//        UUID[] uuidList = new UUID[preselectedVocArray.length];\r
+//        int i = 0;\r
+//        for (String uuidString: preselectedVocArray){\r
+//            uuidList[i]= UUID.fromString(uuidString);\r
+//            i++;\r
+//        }\r
+//        return uuidList;\r
+//    }\r
+\r
+\r
+       private String getPrefKey(TermVocabulary vocabulary){\r
+               return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;\r
+       }\r
+\r
+       /** {@inheritDoc} */\r
+       @Override\r
+       protected NamedArea getPersistentObject(UUID uuid) {\r
+\r
+           DefinedTermBase area =  CdmStore.getService(ITermService.class).find(uuid);\r
+           if (area instanceof NamedArea){\r
+               return (NamedArea) area;\r
+           }\r
+\r
+               return null;\r
+       }\r
+\r
+       /** {@inheritDoc} */\r
+       @Override\r
+       protected void init() {\r
+               vocabularies = getAvailableVocabularies();\r
+       }\r
+\r
+       private List<TermVocabulary> getAvailableVocabularies(){\r
+           vocabularies = new ArrayList();\r
+           CdmPreferenceCache cache = CdmPreferenceCache.instance();\r
+           vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);\r
+           return vocabularies;\r
+       }\r
+\r
+\r
+\r
+       /** {@inheritDoc} */\r
+       @Override\r
+       protected String getTitle(NamedArea namedArea) {\r
+               try {\r
+                       String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());\r
+                       return result;\r
+               } catch (Exception e) {\r
+                       MessagingUtils.error(NamedAreaSelectionDialog.class, "Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid(), e);\r
+                       return namedArea.getTitleCache();\r
+               }\r
+       }\r
+\r
+       /** {@inheritDoc} */\r
+       @Override\r
+       protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {\r
+               return null;\r
+       }\r
+\r
+       /** {@inheritDoc} */\r
+       @Override\r
+       protected String[] getNewWizardText() {\r
+               return null;\r
+       }\r
+\r
+       @Override\r
+    void createFilterButton(Composite searchAndFilter)\r
+           {\r
+            filterButton = new Button(searchAndFilter, SWT.NONE);\r
+//            filterButton.setText("Filter");\r
+            filterButton.setImage(ImageResources.getImage(ImageResources.FUNNEL_ICON));\r
+//            SelectionListener filterSelectionListener = new FilterSelectionListener(preferenceID, this);\r
+            filterButton.addSelectionListener(new SelectionListener(){\r
+                @Override\r
+                public void widgetSelected(SelectionEvent e) {\r
+\r
+                        Object source = e.getSource();\r
+                        String text = null;\r
+                        if (source instanceof Button){\r
+                            Shell shell = ((Button)source).getShell();\r
+                            Dialog dialog = new FilterDialog(getShell(), preferenceID, selectedVocabularies, vocabularies);\r
+                            if(dialog!=null){\r
+                                dialog.open();\r
+                            }\r
+                            createSelectedVocabularies();\r
+                            search();\r
+                        }\r
+\r
+\r
+\r
+\r
+                }\r
+\r
+                @Override\r
+                public void widgetDefaultSelected(SelectionEvent e) {\r
+                    // TODO Auto-generated method stub\r
+\r
+                }\r
+\r
+\r
+            });\r
+\r
+        }\r
+\r
+\r
+\r
+    /* (non-Javadoc)\r
+     * @see eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog#callService(java.lang.String)\r
+     */\r
+    @Override\r
+    void callService(String pattern) {\r
+        if (selectedVocabularies == null || selectedVocabularies.size() == 0){\r
+            model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(vocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());\r
+        }else{\r
+            model = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(selectedVocabularies, limitOfInitialElements, pattern, PreferencesUtil.getGlobalLanguage());\r
+        }\r
+    }\r
+\r
+}\r