commit 91d58d1537d59fc0efecc55feaadae3d6fd56fe0
Author: Katja Luther <k.luther@bgbm.org>
Date:   Tue Jun 11 15:37:34 2019 +0200

    ref #8293: use distribution editor preference for area filter in details view

diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/databaseAdmin/preferencePage/NomenclaturalStatusTypeAdminPreference.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/databaseAdmin/preferencePage/NomenclaturalStatusTypeAdminPreference.java
new file mode 100755
index 000000000..292293db7
--- /dev/null
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/databaseAdmin/preferencePage/NomenclaturalStatusTypeAdminPreference.java
@@ -0,0 +1,74 @@
+/**
+* Copyright (C) 2019 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.databaseAdmin.preferencePage;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.UUID;
+
+import org.apache.commons.lang.StringUtils;
+
+import eu.etaxonomy.cdm.api.service.ITermService;
+import eu.etaxonomy.cdm.model.metadata.CdmPreference;
+import eu.etaxonomy.cdm.model.term.DefinedTermBase;
+import eu.etaxonomy.cdm.persistence.dto.TermDto;
+import eu.etaxonomy.taxeditor.preference.LocalOrDefaultEnum;
+import eu.etaxonomy.taxeditor.preference.NomenclaturalStatusTypePreferences;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+
+/**
+ * @author k.luther
+ * @since 05.06.2019
+ *
+ */
+public class NomenclaturalStatusTypeAdminPreference extends NomenclaturalStatusTypePreferences {
+
+
+    public NomenclaturalStatusTypeAdminPreference(){
+        super();
+        setLocalPref(false);
+     }
+
+
+
+    @Override
+    public boolean performOk() {
+        if (!isApply()){
+            return true;
+        }
+        Object[] checkedElements = treeComposite.getViewer().getCheckedElements();
+        List<UUID> listUIIDChecked = new ArrayList<>();
+        List<DefinedTermBase> preferredTerms = new ArrayList<>();
+        for (Object o : checkedElements) {
+            if(o instanceof TermDto){
+                TermDto termDto = (TermDto) o;
+                listUIIDChecked.add(termDto.getUuid());
+                preferredTerms.add(CdmStore.getService(ITermService.class).load(termDto.getUuid()));
+            }
+        }
+        String saveCheckedElements = StringUtils.join(listUIIDChecked, ";"); //$NON-NLS-1$
+        if (StringUtils.isBlank(saveCheckedElements)){
+            saveCheckedElements = null;
+        }
+        CdmPreference savePref = CdmPreference.NewTaxEditorInstance(predicate, saveCheckedElements);
+        String text = this.useLocalOrAdmin.getText();
+        if (text.equals(LocalOrDefaultEnum.Database.getLabel()) ){
+            savePref.setAllowOverride(false);
+        }else if (text.equals(LocalOrDefaultEnum.AllowOverride.getLabel())){
+            savePref.setAllowOverride(true);
+        }
+
+        PreferencesUtil.setPreferenceToDB(savePref);
+        PreferencesUtil.updateDBPreferences();
+        PreferencesUtil.firePreferencesChanged(this.getClass());
+        return true;
+
+    }
+}
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/CommonNameNamedAreaSelectionDialog.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/CommonNameNamedAreaSelectionDialog.java
index e6d87905a..5a0fb678b 100755
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/CommonNameNamedAreaSelectionDialog.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/CommonNameNamedAreaSelectionDialog.java
@@ -16,11 +16,13 @@ import org.apache.commons.lang.StringUtils;
 import org.eclipse.swt.widgets.Shell;
 
 import eu.etaxonomy.cdm.api.service.IVocabularyService;
-import eu.etaxonomy.cdm.model.term.TermType;
-import eu.etaxonomy.cdm.model.term.TermVocabulary;
 import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
+import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
+import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
+import eu.etaxonomy.cdm.model.term.TermType;
+import eu.etaxonomy.cdm.model.term.TermVocabulary;
 import eu.etaxonomy.taxeditor.preference.CdmPreferenceCache;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.store.CdmStore;
@@ -48,7 +50,7 @@ public class CommonNameNamedAreaSelectionDialog extends NamedAreaSelectionDialog
     }
 
     private static UUID[] createVocabularyUuidList() {
-        String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.CommonNameAreaVocabularies.getKey());
+        String preselectedVocString = PreferencesUtil.getStringValue(PreferencePredicate.CommonNameAreaVocabularies.getKey(), false);
         if (StringUtils.isBlank(preselectedVocString)){
             return null;
         }
@@ -72,17 +74,23 @@ public class CommonNameNamedAreaSelectionDialog extends NamedAreaSelectionDialog
 
         UUID[] preselectedVocabularyUuids = createVocabularyUuidList();
         CdmPreferenceCache cache = CdmPreferenceCache.instance();
-        CdmPreference pref = cache.get(PreferencePredicate.CommonNameAreaVocabularies.getKey());
+        PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.CommonNameAreaVocabularies);
+        CdmPreference pref = cache.findBestMatching(key);
+        if (preselectedVocabularyUuids != null){
+            for(int i=0;i<preselectedVocabularyUuids.length;i++){
+                TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
+                vocabularies.add(preselectedVocabulary);
+            }
+        }
         if ((pref != null && !pref.isAllowOverride()) && preselectedVocabularyUuids!=null) {
-                for(int i=0;i<preselectedVocabularyUuids.length;i++){
-                    TermVocabulary preselectedVocabulary = CdmStore.getService(IVocabularyService.class).find(preselectedVocabularyUuids[i]);
-                    vocabularies.add(preselectedVocabulary);
-                }
+             return vocabularies;
         }else{
-            vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
+            selectedVocabularies = vocabularies;
+
+            return CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
         }
 
-        return vocabularies;
+
     }
 
     /**
@@ -104,4 +112,6 @@ public class CommonNameNamedAreaSelectionDialog extends NamedAreaSelectionDialog
                 "Choose an area", false, namedArea, preferenceId);
         return getSelectionFromDialog(dialog);
     }
+
+
 }
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/NamedAreaSelectionDialog.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/NamedAreaSelectionDialog.java
index 5bd973b91..b1808a5b2 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/NamedAreaSelectionDialog.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/NamedAreaSelectionDialog.java
@@ -1,280 +1,249 @@
-/**
-* 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());
-        }
-    }
-
-}
+/**
+* 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<NamedArea> selectedAreas;
+	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();
+	    vocabularies = CdmStore.getService(IVocabularyService.class).listByTermType(TermType.NamedArea, true, null, null, null, null);
+	    return vocabularies;
+	}
+
+
+
+	/** {@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());
+        }
+    }
+
+}
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/SelectionDialogFactory.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/SelectionDialogFactory.java
index c8d9eba8c..111cfab25 100644
--- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/SelectionDialogFactory.java
+++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/selection/SelectionDialogFactory.java
@@ -1,230 +1,240 @@
-/**
-* Copyright (C) 2009 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 org.eclipse.swt.widgets.Shell;
-
-import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
-import eu.etaxonomy.cdm.model.agent.AgentBase;
-import eu.etaxonomy.cdm.model.agent.Institution;
-import eu.etaxonomy.cdm.model.agent.Person;
-import eu.etaxonomy.cdm.model.agent.Team;
-import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
-import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
-import eu.etaxonomy.cdm.model.common.Group;
-import eu.etaxonomy.cdm.model.common.ICdmBase;
-import eu.etaxonomy.cdm.model.common.User;
-import eu.etaxonomy.cdm.model.description.Feature;
-import eu.etaxonomy.cdm.model.description.PolytomousKey;
-import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
-import eu.etaxonomy.cdm.model.location.Country;
-import eu.etaxonomy.cdm.model.location.NamedArea;
-import eu.etaxonomy.cdm.model.media.Media;
-import eu.etaxonomy.cdm.model.media.Rights;
-import eu.etaxonomy.cdm.model.molecular.Amplification;
-import eu.etaxonomy.cdm.model.molecular.Primer;
-import eu.etaxonomy.cdm.model.name.TaxonName;
-import eu.etaxonomy.cdm.model.occurrence.Collection;
-import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
-import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
-import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
-import eu.etaxonomy.cdm.model.reference.Reference;
-import eu.etaxonomy.cdm.model.taxon.Classification;
-import eu.etaxonomy.cdm.model.taxon.Synonym;
-import eu.etaxonomy.cdm.model.taxon.Taxon;
-import eu.etaxonomy.cdm.model.taxon.TaxonBase;
-import eu.etaxonomy.cdm.model.taxon.TaxonNode;
-import eu.etaxonomy.cdm.model.term.FeatureTree;
-import eu.etaxonomy.taxeditor.model.MessagingUtils;
-import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
-import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
-import eu.etaxonomy.taxeditor.ui.section.agent.TeamMemberElement;
-import eu.etaxonomy.taxeditor.ui.section.description.CommonNameSourceElement;
-import eu.etaxonomy.taxeditor.ui.section.description.detail.CommonNameDetailElement;
-import eu.etaxonomy.taxeditor.ui.section.group.MemberDetailElement;
-import eu.etaxonomy.taxeditor.ui.section.group.MemberDetailSection;
-import eu.etaxonomy.taxeditor.ui.section.name.AuthorshipDetailElement;
-import eu.etaxonomy.taxeditor.ui.section.reference.NomenclaturalReferenceDetailElement;
-import eu.etaxonomy.taxeditor.ui.section.reference.ReferenceDetailElement;
-import eu.etaxonomy.taxeditor.ui.section.user.GroupsByUserDetailElement;
-import eu.etaxonomy.taxeditor.ui.section.user.GroupsByUserDetailSection;
-
-/**
- * @author n.hoffmann
- * @date Jan 25, 2012
- *
- */
-public class SelectionDialogFactory {
-
-    public static <T extends ICdmBase> T getSelectionFromDialog(Class<T> clazz, Shell shell, //ConversationHolder conversation,
-            T currentSelection){
-        return getSelectionFromDialog(clazz, shell, //conversation,
-                currentSelection, null);
-    }
-
-	public static <T extends ICdmBase> T getSelectionFromDialog(Class<T> clazz, Shell shell, //ConversationHolder conversation,
-	        T currentSelection, ICdmFormElement parentElement){
-
-		if(clazz.equals(Taxon.class)){
-			return (T) TaxonSelectionDialog.selectTaxon(shell, //conversation,
-			        (Taxon) currentSelection);
-		}
-		if(clazz.equals(Synonym.class)){
-			return (T) TaxonBaseSelectionDialog.selectSynonym(shell );//,conversation);
-		}
-		if(clazz.equals(TaxonBase.class)){
-			return (T) TaxonBaseSelectionDialog.selectTaxonBase(shell);//, conversation);
-		}
-		if(clazz.equals(Classification.class)){
-			return (T) ClassificationSelectionDialog.select(shell, //conversation,
-			        (Classification) currentSelection);
-		}
-		if(clazz.equals(TaxonNode.class)){
-			return (T) TaxonNodeSelectionDialog.select(shell, //conversation,
-			        null, null, (TaxonNode) currentSelection, ((TaxonNode) currentSelection).getClassification().getUuid());
-		}
-		if(clazz.equals(Reference.class) && parentElement instanceof NomenclaturalReferenceDetailElement){
-			return (T) NomenclaturalReferenceSelectionDialog.select(shell, //conversation,
-			        (Reference) currentSelection, ((NomenclaturalReferenceDetailElement)parentElement).getEntity().getCombinationAuthorship());
-		}
-		if(clazz.equals(Reference.class) && parentElement instanceof CommonNameSourceElement && PreferencesUtil.getFilterCommonNameReferences()){
-            return (T) CommonNameReferenceSelectionDialog.select(shell, //conversation,
-                    (Reference) currentSelection);
-        }
-		if(clazz.equals(Reference.class)){
-		    if (parentElement instanceof ReferenceDetailElement){
-		        return (T) ReferenceSelectionDialog.select(shell, //conversation,
-		                ((ReferenceDetailElement) parentElement).getEntity(), true);
-		    }
-			return (T) ReferenceSelectionDialog.select(shell, //conversation,
-			        (Reference) currentSelection);
-		}
-
-		if(clazz.equals(TaxonName.class)){
-			return (T) NameSelectionDialog.select(shell, //conversation,
-			        (TaxonName) currentSelection);
-		}
-		if(clazz.equals(Team.class)){
-			return (T) TeamSelectionDialog.select(shell, //conversation,
-			        (Team) currentSelection);
-		}
-		if(clazz.equals(TeamOrPersonBase.class) && parentElement instanceof AuthorshipDetailElement){
-            return (T) NomenclaturalAuthorSelectionDialog.select(shell, //conversation,
-                    (TeamOrPersonBase) currentSelection, false);
-        }else if(clazz.equals(TeamOrPersonBase.class)){
-		    //TODO: add TeamOrPersonBaseSelectionDialog (see ticket #4545)
-		    return (T) TeamOrPersonBaseSelectionDialog.select(shell, //conversation,
-		            (TeamOrPersonBase) currentSelection, false);
-		}
-		if(clazz.equals(Person.class) && parentElement instanceof TeamMemberElement){
-		    return (T) NomenclaturalPersonAuthorSelectionDialog.select(shell, //conversation,
-		            (TeamOrPersonBase) currentSelection, true);
-        }
-		if(clazz.equals(AgentBase.class)){
-			return (T) AgentSelectionDialog.select(shell, //conversation,
-			        (AgentBase) currentSelection, false);
-		}
-		if(clazz.equals(Feature.class)){
-			return (T) FeatureSelectionDialog.select(shell, //conversation,
-			        (Feature) currentSelection);
-		}
-		if(clazz.equals(FeatureTree.class)){
-			return (T) FeatureTreeSelectionDialog.select(shell, //conversation,
-			        (FeatureTree) currentSelection);
-		}
-		if(clazz.equals(PolytomousKey.class)){
-			return (T) PolytomousKeySelectionDialog.select(shell, //conversation,
-			        (PolytomousKey) currentSelection);
-		}
-		if(clazz.equals(PolytomousKeyNode.class)){
-			MessagingUtils.warningDialog("Not implemented yet", SelectionDialogFactory.class,
-					"This functionality is not implemented yet.");
-			return null;
-		}
-		if(clazz.equals(DerivedUnit.class)){
-		    return (T) DerivedUnitSelectionDialog.select(shell, //conversation,
-		            (DerivedUnit) currentSelection);
-		}
-		if(clazz.equals(FieldUnit.class)){
-		    return (T) FieldUnitSelectionDialog.select(shell, //conversation,
-		            (FieldUnit) currentSelection);
-		}
-		if(clazz.equals(SpecimenOrObservationBase.class)){
-		    return (T) SpecimenOrObservationBaseSelectionDialog.select(shell, //conversation,
-		            (SpecimenOrObservationBase) currentSelection);
-		}
-		if(clazz.equals(NamedArea.class)){
-		    if(parentElement instanceof IEntityElement && ((IEntityElement) parentElement).getEntity() instanceof DerivedUnitFacade){
-		        return (T) NamedAreaSelectionDialog.select(shell, //conversation,
-		                (NamedArea) currentSelection, DerivedUnit.class.getCanonicalName(), Country.uuidCountryVocabulary);
-		    }
-		    else if(parentElement instanceof CommonNameDetailElement ){
-                return (T) CommonNameNamedAreaSelectionDialog.select(shell, //conversation,
-                        (NamedArea) currentSelection, parentElement.getClass().getCanonicalName());
-            }
-		    else{
-		        return (T) NamedAreaSelectionDialog.select(shell, //conversation,
-		                (NamedArea) currentSelection, parentElement.getClass().getCanonicalName());
-		    }
-		}
-		if(clazz.equals(Collection.class)){
-			return (T) CollectionSelectionDialog.select(shell, //conversation,
-			        (Collection) currentSelection);
-		}
-		if(clazz.equals(User.class) && parentElement instanceof MemberDetailElement){
-		    MemberDetailSection section = (MemberDetailSection)((MemberDetailElement)parentElement).getParentElement();
-			return (T) UserSelectionDialog.select(shell, //conversation,
-			        (User) currentSelection, section.getEntity());
-		}
-		if(clazz.equals(GrantedAuthorityImpl.class)){
-			return (T) GrantedAuthoritySelectionDialog.select(shell, //conversation,
-			        (GrantedAuthorityImpl) currentSelection);
-		}
-		if (clazz.equals(Person.class)){
-			return (T) PersonSelectionDialog.select(shell, //conversation,
-			        (Person) currentSelection);
-		}
-		if(clazz.equals(Group.class) &&  parentElement instanceof GroupsByUserDetailElement){
-		    GroupsByUserDetailSection section =(GroupsByUserDetailSection)((GroupsByUserDetailElement)parentElement).getParentElement();
-			return (T) GroupSelectionDialog.select(shell, //conversation,
-			        (Group) currentSelection, section.getEntity());
-		}
-		if(clazz.equals(Institution.class)){
-			return (T) InstitutionSelectionDialog.select(shell, //conversation,
-			        (Institution) currentSelection);
-		}
-		if(clazz.equals(Primer.class)){
-		    return (T) PrimerSelectionDialog.select(shell, //conversation,
-		            (Primer) currentSelection);
-		}
-		if(clazz.equals(Amplification.class)){
-		    return (T) AmplificationSelectionDialog.select(shell, //conversation,
-		            (Amplification) currentSelection);
-		}
-		if(clazz.equals(Media.class)){
-		    return (T) MediaSelectionDialog.select(shell, //conversation,
-		            (Media) currentSelection);
-		}
-		if(clazz.equals(Rights.class)){
-            return (T) RightsSelectionDialog.select(shell, //conversation,
-                    (Rights) currentSelection);
-        }
-
-		return null;
-	}
-
-	public static <T extends ICdmBase> T getSelectionFromExtDialog(Class<T> clazz, Shell shell, //ConversationHolder conversation,
-	        ICdmFormElement parentElement){
-	    return (T) ExtReferenceSelectionDialog.select(shell, //conversation,
-	            null);
-	}
-
-
-}
+/**
+* Copyright (C) 2009 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.List;
+import java.util.UUID;
+
+import org.eclipse.swt.widgets.Shell;
+
+import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
+import eu.etaxonomy.cdm.model.agent.AgentBase;
+import eu.etaxonomy.cdm.model.agent.Institution;
+import eu.etaxonomy.cdm.model.agent.Person;
+import eu.etaxonomy.cdm.model.agent.Team;
+import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
+import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
+import eu.etaxonomy.cdm.model.common.Group;
+import eu.etaxonomy.cdm.model.common.ICdmBase;
+import eu.etaxonomy.cdm.model.common.User;
+import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.description.PolytomousKey;
+import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
+import eu.etaxonomy.cdm.model.location.Country;
+import eu.etaxonomy.cdm.model.location.NamedArea;
+import eu.etaxonomy.cdm.model.media.Media;
+import eu.etaxonomy.cdm.model.media.Rights;
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
+import eu.etaxonomy.cdm.model.molecular.Amplification;
+import eu.etaxonomy.cdm.model.molecular.Primer;
+import eu.etaxonomy.cdm.model.name.TaxonName;
+import eu.etaxonomy.cdm.model.occurrence.Collection;
+import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
+import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
+import eu.etaxonomy.cdm.model.reference.Reference;
+import eu.etaxonomy.cdm.model.taxon.Classification;
+import eu.etaxonomy.cdm.model.taxon.Synonym;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.model.term.FeatureTree;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.element.IEntityElement;
+import eu.etaxonomy.taxeditor.ui.section.agent.TeamMemberElement;
+import eu.etaxonomy.taxeditor.ui.section.description.CommonNameSourceElement;
+import eu.etaxonomy.taxeditor.ui.section.description.detail.CommonNameDetailElement;
+import eu.etaxonomy.taxeditor.ui.section.group.MemberDetailElement;
+import eu.etaxonomy.taxeditor.ui.section.group.MemberDetailSection;
+import eu.etaxonomy.taxeditor.ui.section.name.AuthorshipDetailElement;
+import eu.etaxonomy.taxeditor.ui.section.reference.NomenclaturalReferenceDetailElement;
+import eu.etaxonomy.taxeditor.ui.section.reference.ReferenceDetailElement;
+import eu.etaxonomy.taxeditor.ui.section.user.GroupsByUserDetailElement;
+import eu.etaxonomy.taxeditor.ui.section.user.GroupsByUserDetailSection;
+
+/**
+ * @author n.hoffmann
+ * @date Jan 25, 2012
+ *
+ */
+public class SelectionDialogFactory {
+
+    public static <T extends ICdmBase> T getSelectionFromDialog(Class<T> clazz, Shell shell, //ConversationHolder conversation,
+            T currentSelection){
+        return getSelectionFromDialog(clazz, shell, //conversation,
+                currentSelection, null);
+    }
+
+	public static <T extends ICdmBase> T getSelectionFromDialog(Class<T> clazz, Shell shell, //ConversationHolder conversation,
+	        T currentSelection, ICdmFormElement parentElement){
+
+		if(clazz.equals(Taxon.class)){
+			return (T) TaxonSelectionDialog.selectTaxon(shell, //conversation,
+			        (Taxon) currentSelection);
+		}
+		if(clazz.equals(Synonym.class)){
+			return (T) TaxonBaseSelectionDialog.selectSynonym(shell );//,conversation);
+		}
+		if(clazz.equals(TaxonBase.class)){
+			return (T) TaxonBaseSelectionDialog.selectTaxonBase(shell);//, conversation);
+		}
+		if(clazz.equals(Classification.class)){
+			return (T) ClassificationSelectionDialog.select(shell, //conversation,
+			        (Classification) currentSelection);
+		}
+		if(clazz.equals(TaxonNode.class)){
+			return (T) TaxonNodeSelectionDialog.select(shell, //conversation,
+			        null, null, (TaxonNode) currentSelection, ((TaxonNode) currentSelection).getClassification().getUuid());
+		}
+		if(clazz.equals(Reference.class) && parentElement instanceof NomenclaturalReferenceDetailElement){
+			return (T) NomenclaturalReferenceSelectionDialog.select(shell, //conversation,
+			        (Reference) currentSelection, ((NomenclaturalReferenceDetailElement)parentElement).getEntity().getCombinationAuthorship());
+		}
+		if(clazz.equals(Reference.class) && parentElement instanceof CommonNameSourceElement && PreferencesUtil.getFilterCommonNameReferences()){
+            return (T) CommonNameReferenceSelectionDialog.select(shell, //conversation,
+                    (Reference) currentSelection);
+        }
+		if(clazz.equals(Reference.class)){
+		    if (parentElement instanceof ReferenceDetailElement){
+		        return (T) ReferenceSelectionDialog.select(shell, //conversation,
+		                ((ReferenceDetailElement) parentElement).getEntity(), true);
+		    }
+			return (T) ReferenceSelectionDialog.select(shell, //conversation,
+			        (Reference) currentSelection);
+		}
+
+		if(clazz.equals(TaxonName.class)){
+			return (T) NameSelectionDialog.select(shell, //conversation,
+			        (TaxonName) currentSelection);
+		}
+		if(clazz.equals(Team.class)){
+			return (T) TeamSelectionDialog.select(shell, //conversation,
+			        (Team) currentSelection);
+		}
+		if(clazz.equals(TeamOrPersonBase.class) && parentElement instanceof AuthorshipDetailElement){
+            return (T) NomenclaturalAuthorSelectionDialog.select(shell, //conversation,
+                    (TeamOrPersonBase) currentSelection, false);
+        }else if(clazz.equals(TeamOrPersonBase.class)){
+		    //TODO: add TeamOrPersonBaseSelectionDialog (see ticket #4545)
+		    return (T) TeamOrPersonBaseSelectionDialog.select(shell, //conversation,
+		            (TeamOrPersonBase) currentSelection, false);
+		}
+		if(clazz.equals(Person.class) && parentElement instanceof TeamMemberElement){
+		    return (T) NomenclaturalPersonAuthorSelectionDialog.select(shell, //conversation,
+		            (TeamOrPersonBase) currentSelection, true);
+        }
+		if(clazz.equals(AgentBase.class)){
+			return (T) AgentSelectionDialog.select(shell, //conversation,
+			        (AgentBase) currentSelection, false);
+		}
+		if(clazz.equals(Feature.class)){
+			return (T) FeatureSelectionDialog.select(shell, //conversation,
+			        (Feature) currentSelection);
+		}
+		if(clazz.equals(FeatureTree.class)){
+			return (T) FeatureTreeSelectionDialog.select(shell, //conversation,
+			        (FeatureTree) currentSelection);
+		}
+		if(clazz.equals(PolytomousKey.class)){
+			return (T) PolytomousKeySelectionDialog.select(shell, //conversation,
+			        (PolytomousKey) currentSelection);
+		}
+		if(clazz.equals(PolytomousKeyNode.class)){
+			MessagingUtils.warningDialog("Not implemented yet", SelectionDialogFactory.class,
+					"This functionality is not implemented yet.");
+			return null;
+		}
+		if(clazz.equals(DerivedUnit.class)){
+		    return (T) DerivedUnitSelectionDialog.select(shell, //conversation,
+		            (DerivedUnit) currentSelection);
+		}
+		if(clazz.equals(FieldUnit.class)){
+		    return (T) FieldUnitSelectionDialog.select(shell, //conversation,
+		            (FieldUnit) currentSelection);
+		}
+		if(clazz.equals(SpecimenOrObservationBase.class)){
+		    return (T) SpecimenOrObservationBaseSelectionDialog.select(shell, //conversation,
+		            (SpecimenOrObservationBase) currentSelection);
+		}
+		if(clazz.equals(NamedArea.class)){
+		    if(parentElement instanceof IEntityElement && ((IEntityElement) parentElement).getEntity() instanceof DerivedUnitFacade){
+		        return (T) NamedAreaSelectionDialog.select(shell, //conversation,
+		                (NamedArea) currentSelection, DerivedUnit.class.getCanonicalName(), Country.uuidCountryVocabulary);
+		    }
+		    else if(parentElement instanceof CommonNameDetailElement ){
+                return (T) CommonNameNamedAreaSelectionDialog.select(shell, //conversation,
+                        (NamedArea) currentSelection, parentElement.getClass().getCanonicalName());
+            }
+		    else{
+		        //if (parentElement == null){
+		            List<UUID> uuidList = PreferencesUtil.createUUIDListFromStringPref(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
+		            UUID[] uuidArray = new UUID[uuidList.size()];
+		            uuidList.toArray(uuidArray);
+		            return (T)NamedAreaSelectionDialog.select(shell, (NamedArea) currentSelection, null, uuidArray);
+		        //}
+		        //return (T) NamedAreaSelectionDialog.select(shell, //conversation,
+		        //        (NamedArea) currentSelection, parentElement.getClass().getCanonicalName());
+		    }
+		}
+		if(clazz.equals(Collection.class)){
+			return (T) CollectionSelectionDialog.select(shell, //conversation,
+			        (Collection) currentSelection);
+		}
+		if(clazz.equals(User.class) && parentElement instanceof MemberDetailElement){
+		    MemberDetailSection section = (MemberDetailSection)((MemberDetailElement)parentElement).getParentElement();
+			return (T) UserSelectionDialog.select(shell, //conversation,
+			        (User) currentSelection, section.getEntity());
+		}
+		if(clazz.equals(GrantedAuthorityImpl.class)){
+			return (T) GrantedAuthoritySelectionDialog.select(shell, //conversation,
+			        (GrantedAuthorityImpl) currentSelection);
+		}
+		if (clazz.equals(Person.class)){
+			return (T) PersonSelectionDialog.select(shell, //conversation,
+			        (Person) currentSelection);
+		}
+		if(clazz.equals(Group.class) &&  parentElement instanceof GroupsByUserDetailElement){
+		    GroupsByUserDetailSection section =(GroupsByUserDetailSection)((GroupsByUserDetailElement)parentElement).getParentElement();
+			return (T) GroupSelectionDialog.select(shell, //conversation,
+			        (Group) currentSelection, section.getEntity());
+		}
+		if(clazz.equals(Institution.class)){
+			return (T) InstitutionSelectionDialog.select(shell, //conversation,
+			        (Institution) currentSelection);
+		}
+		if(clazz.equals(Primer.class)){
+		    return (T) PrimerSelectionDialog.select(shell, //conversation,
+		            (Primer) currentSelection);
+		}
+		if(clazz.equals(Amplification.class)){
+		    return (T) AmplificationSelectionDialog.select(shell, //conversation,
+		            (Amplification) currentSelection);
+		}
+		if(clazz.equals(Media.class)){
+		    return (T) MediaSelectionDialog.select(shell, //conversation,
+		            (Media) currentSelection);
+		}
+		if(clazz.equals(Rights.class)){
+            return (T) RightsSelectionDialog.select(shell, //conversation,
+                    (Rights) currentSelection);
+        }
+
+		return null;
+	}
+
+	public static <T extends ICdmBase> T getSelectionFromExtDialog(Class<T> clazz, Shell shell, //ConversationHolder conversation,
+	        ICdmFormElement parentElement){
+	    return (T) ExtReferenceSelectionDialog.select(shell, //conversation,
+	            null);
+	}
+
+
+}
