minor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / NamedAreaSelectionDialog.java
index db440361160a8f46ebd511e98c725040c951eb09..5bd973b915c0428456e577d737dda478d4b525b4 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2007 EDIT
 * European Distributed Institute of Taxonomy
 package eu.etaxonomy.taxeditor.ui.dialog.selection;
 
 import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Set;
 import java.util.UUID;
 
 import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.action.IMenuManager;
+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.Control;
 import org.eclipse.swt.widgets.Shell;
 
-import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
+import eu.etaxonomy.cdm.api.service.ITermService;
 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.TermVocabulary;
-import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
-import eu.etaxonomy.cdm.model.location.Country;
 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;
 
 /**
- * <p>FilteredNamedAreaSelectionDialog class.</p>
- *
  * @author n.hoffmann
  * @created May 11, 2010
  * @version 1.0
@@ -46,7 +45,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.
@@ -63,12 +62,14 @@ public class NamedAreaSelectionDialog extends
                        }else{
                                selectedVocabularies.remove(vocabulary);
                        }
-
-                       initModel();
+                       PreferencesUtil.setBooleanValue(getPrefKey(vocabulary), !isChecked());
+                       search();
                }
        }
 
-       private Collection<TermVocabulary<NamedArea>> selectedVocabularies;
+       protected List<TermVocabulary> selectedVocabularies;
+    protected List<TermVocabulary> vocabularies;
+
 
 
        /**
@@ -79,91 +80,125 @@ 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) {
-               NamedAreaSelectionDialog dialog = new NamedAreaSelectionDialog(shell, conversation,
-                               "Choose an area", false, 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, 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);
 
-       /** {@inheritDoc} */
-       @Override
-       protected void fillViewMenu(IMenuManager menuManager) {
+               selectedVocabularies = new ArrayList<TermVocabulary>();
+               this.preferenceID = preferenceId;
 
-               super.fillViewMenu(menuManager);
+               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();
+               }
+
+       }
 
-               for(TermVocabulary<NamedArea> vocabulary : getVocabularies()){
-                       IncludeNamedAreaVocabulary action = new IncludeNamedAreaVocabulary(vocabulary);
-                       menuManager.add(action);
-                       action.setChecked(true);
+    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) {
-               for(TermVocabulary<NamedArea> vocabulary : selectedVocabularies){
-                       for(Object object : vocabulary.getTerms()){
-                               CdmBase cdmBaseObject = (CdmBase) object;
-                               if(uuid.equals(cdmBaseObject.getUuid())){
-                                       return (NamedArea) cdmBaseObject;
-                               }
-                       }
-               }
+
+           DefinedTermBase area =  CdmStore.getService(ITermService.class).find(uuid);
+           if (area instanceof NamedArea){
+               return (NamedArea) area;
+           }
+
                return null;
        }
 
        /** {@inheritDoc} */
        @Override
        protected void init() {
-               selectedVocabularies = getVocabularies();
+               vocabularies = 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).find(Country.uuidCountryVocabulary));
+       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 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));
-                       model.add(element);
-               }
-       }
+//     /** {@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 Control createExtendedContentArea(Composite parent) {
+//             return null;
+//     }
 
        /** {@inheritDoc} */
        @Override
@@ -172,8 +207,7 @@ 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();
                }
        }
@@ -186,7 +220,61 @@ public class NamedAreaSelectionDialog extends
 
        /** {@inheritDoc} */
        @Override
-       protected String getNewWizardLinkText() {
+       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());
+        }
+    }
+
 }