Merge branch 'release/4.4.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / NamedAreaSelectionDialog.java
index cf39589ad5b5d0257308742ac2ce10faa265d947..024bdd6db4fb41b606cb530cfd6b51656eab2436 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.
 */
@@ -28,16 +28,16 @@ import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.service.IVocabularyService;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.TermType;
 import eu.etaxonomy.cdm.model.common.TermVocabulary;
-import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
-import eu.etaxonomy.cdm.model.common.VocabularyEnum;
 import eu.etaxonomy.cdm.model.location.NamedArea;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
- * <p>FilteredNamedAreaSelectionDialog class.</p>
- *
  * @author n.hoffmann
  * @created May 11, 2010
  * @version 1.0
@@ -46,7 +46,7 @@ public class NamedAreaSelectionDialog extends
                AbstractFilteredCdmResourceSelectionDialog<NamedArea> {
 
        private class IncludeNamedAreaVocabulary extends Action {
-               private TermVocabulary<NamedArea> vocabulary;
+               private final TermVocabulary<NamedArea> vocabulary;
 
                /**
                 * Creates a new instance of the class.
@@ -55,21 +55,24 @@ public class NamedAreaSelectionDialog extends
                        super(vocabulary.getTitleCache(), IAction.AS_CHECK_BOX);
                        this.vocabulary = vocabulary;
                }
-               
-               public void run(){
+
+               @Override
+        public void run(){
                        if(isChecked()){
                                selectedVocabularies.add(vocabulary);
                        }else{
                                selectedVocabularies.remove(vocabulary);
                        }
-                       
+                       PreferencesUtil.getPreferenceStore().setValue(getPrefKey(vocabulary), !isChecked());
                        initModel();
                }
        }
-       
-       private Collection<TermVocabulary<NamedArea>> selectedVocabularies;
-       
-       
+
+       protected Collection<TermVocabulary> selectedVocabularies;
+    protected ArrayList<TermVocabulary> preselectedVocabularies;
+    private Object preferenceID;
+
+
        /**
         * Creates a filtered selection dialog to select a named area.
         *
@@ -78,40 +81,58 @@ public class NamedAreaSelectionDialog extends
         * @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) {
+       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);
+                               "Choose an area", false, namedArea, preferenceId, preselectedVocabularyUuids);
                return getSelectionFromDialog(dialog);
        }
-       
-       /**
-        * <p>Constructor for FilteredNamedAreaSelectionDialog.</p>
-        *
-        * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
-        * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
-        * @param title a {@link java.lang.String} object.
-        * @param multi a boolean.
-        * @param namedArea a {@link eu.etaxonomy.cdm.model.location.NamedArea} object.
-        */
-       protected NamedAreaSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, NamedArea namedArea) {
-               super(shell, conversation, title, multi, NamedAreaSelectionDialog.class.getCanonicalName(), namedArea);         
+
+       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);
+               this.preferenceID = preferenceId;
+               preselectedVocabularies = new ArrayList<TermVocabulary>();
+               for(int i=0;i<preselectedVocabularyUuids.length;i++){
+                       TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
+                       preselectedVocabularies.add(preselectedVocabulary);
+               }
+               Collection<TermVocabulary> tempSelectedVocabularies = new HashSet<>();
+               for(TermVocabulary vocabulary:selectedVocabularies){
+                       if(preselectedVocabularies.contains(vocabulary)
+                                       || !PreferencesUtil.getPreferenceStore().getBoolean(getPrefKey(vocabulary))){
+                               tempSelectedVocabularies.add(vocabulary);
+                       }
+               }
+               selectedVocabularies = tempSelectedVocabularies;
+               initModel();//re-init to consider pre-selected vocabularies
        }
-       
+
        /** {@inheritDoc} */
        @Override
        protected void fillViewMenu(IMenuManager menuManager) {
-                               
+
                super.fillViewMenu(menuManager);
-                               
-               for(TermVocabulary<NamedArea> vocabulary : getVocabularies()){
+
+               for(TermVocabulary<NamedArea> vocabulary : getAvailableVocabularies()){
                        IncludeNamedAreaVocabulary action = new IncludeNamedAreaVocabulary(vocabulary);
                        menuManager.add(action);
-                       action.setChecked(true);
+                       if(preselectedVocabularies.contains(vocabulary)) {
+                           action.setChecked(true);
+                       }
+                       else{
+                               action.setChecked(!PreferencesUtil.getPreferenceStore().getBoolean(getPrefKey(vocabulary)));
+                       }
                }
        }
        
+       private String getPrefKey(TermVocabulary vocabulary){
+               return "hide_"+NamedAreaSelectionDialog.class.getCanonicalName()+vocabulary.getUuid()+preferenceID;
+       }
+
        /** {@inheritDoc} */
        @Override
        protected NamedArea getPersistentObject(UUID uuid) {
@@ -119,43 +140,40 @@ public class NamedAreaSelectionDialog extends
                        for(Object object : vocabulary.getTerms()){
                                CdmBase cdmBaseObject = (CdmBase) object;
                                if(uuid.equals(cdmBaseObject.getUuid())){
-                                       return (NamedArea) cdmBaseObject;
+                                       return CdmBase.deproxy(cdmBaseObject, NamedArea.class);
                                }
                        }
                }
                return null;
        }
-       
+
        /** {@inheritDoc} */
        @Override
        protected void init() {
-               selectedVocabularies = getVocabularies();
+               selectedVocabularies = getAvailableVocabularies();
        }
-       
-       private List<TermVocabulary<NamedArea>> getVocabularies(){
-               List<TermVocabulary<NamedArea>> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermClass(NamedArea.class, null, null, null, null);
-               vocabularies.add(CdmStore.getService(IVocabularyService.class).getVocabulary(VocabularyEnum.TdwgArea));
-               vocabularies.add(CdmStore.getService(IVocabularyService.class).getVocabulary(VocabularyEnum.WaterbodyOrCountry));
-               vocabularies.add(CdmStore.getService(IVocabularyService.class).getVocabulary(VocabularyEnum.Continent));
+
+       private List<TermVocabulary> getAvailableVocabularies(){
+               List<TermVocabulary> vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
                return vocabularies;
        }
 
        /** {@inheritDoc} */
        @Override
        protected void initModel() {
-               
+
                Set<NamedArea> terms = new HashSet<NamedArea>();
                for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
                        terms.addAll(vocabulary.getTermsOrderedByLabels(CdmStore.getDefaultLanguage()));
                }
-               
+
                if(model == null){
                        model = new ArrayList<UuidAndTitleCache<NamedArea>>();
                }
                model.clear();
                for(Object areaObject : terms){
                        NamedArea area = (NamedArea) HibernateProxyHelper.deproxy(areaObject);
-                       UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), getTitle(area)); 
+                       UuidAndTitleCache<NamedArea> element = new UuidAndTitleCache<NamedArea>(NamedArea.class, area.getUuid(), area.getId(), getTitle(area));
                        model.add(element);
                }
        }
@@ -165,7 +183,7 @@ public class NamedAreaSelectionDialog extends
        protected Control createExtendedContentArea(Composite parent) {
                return null;
        }
-       
+
        /** {@inheritDoc} */
        @Override
        protected String getTitle(NamedArea namedArea) {
@@ -173,12 +191,11 @@ public class NamedAreaSelectionDialog extends
                        String result = NamedArea.labelWithLevel(namedArea, CdmStore.getDefaultLanguage());
                        return result;
                } catch (Exception e) {
-                       //TODO still need to learn how errors are handled in the Tax Editor
-                       System.out.println("Error occurred when trying retrieve title for Named Area: " + namedArea.getUuid());
+                       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) {