fix #8943: fix handling of term preferences in combo boxes and code cleaning
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / SearchDialogPreferences.java
index 6fc86491176b561b76cdcc3c9c19eef607ff4e1b..3bfe7f492dc73e88c3b27f2816e64d09fe7d7003 100755 (executable)
@@ -20,6 +20,7 @@ import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 
 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
+import eu.etaxonomy.cdm.model.metadata.NamedAreaSearchField;
 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
 import eu.etaxonomy.taxeditor.l10n.Messages;
 import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
@@ -36,6 +37,7 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
     protected CdmPreference searchForIdentifierAndTitleCachePref;
     protected CdmPreference sortTaxaByRankAndNamePref;
     protected CdmPreference filterCommonNameReferencesPref;
+    protected CdmPreference searchNamedAreasByAbbrevPref;
 
 
     protected Boolean showIdInSelectionDialog;
@@ -43,12 +45,14 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
     protected Boolean searchForIdentifierAndTitleCache;
     protected Boolean sortTaxaByRankAndName;
     protected Boolean filterCommonNameReferences;
+    protected NamedAreaSearchField searchNamedAreasByAbbrev;
 
     private boolean overrideShowIdInSelectionDialog = true;
     private boolean overrideSearchForIdentifierAsDefault = true;
     private boolean overrideSearchForIdentifierAndTitleCache = true;
     private boolean overrideSortTaxaByRankAndName = true;
     private boolean overrideFilterCommonNameReferences = true;
+    private boolean overrideSearchNamedAreasByAbbrev = true;
 
     protected Combo showIdInSelectionDialogButton;
     protected Button allowOverrideIsShowIdInSelectionDialogButton;
@@ -60,6 +64,8 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
     protected Button allowOverrideIsSortTaxaByRankAndNameButton;
     protected Combo filterCommonNameReferenceButton;
     protected Button allowOverrideFilterCommonNameReferencesButton;
+    protected Combo searchNamedAreasByAbbrevButton;
+    protected Button allowOverridesearchNamedAreasByAbbrevButton;
 
 
 
@@ -71,12 +77,15 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
         searchForIdentifierAndTitleCachePref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.SearchForIdentifierAndTitleCache);
         sortTaxaByRankAndNamePref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.SortTaxaByRankAndName);
         filterCommonNameReferencesPref =  PreferencesUtil.getPreferenceFromDB(PreferencePredicate.CommonNameReferencesWithMarker);
+        searchNamedAreasByAbbrevPref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.NamedAreaSearchForAbbrev);
+
+        overrideShowIdInSelectionDialog = PreferencesUtil.getOverrideForPreference(PreferencePredicate.ShowIdInSelectionDialog.getKey());
+        overrideSearchForIdentifierAsDefault = PreferencesUtil.getOverrideForPreference(PreferencePredicate.SearchForIdentifierAsDefault.getKey());
+        overrideSearchForIdentifierAndTitleCache = PreferencesUtil.getOverrideForPreference(PreferencePredicate.SearchForIdentifierAndTitleCache.getKey());
+        overrideSortTaxaByRankAndName = PreferencesUtil.getOverrideForPreference(PreferencePredicate.SortTaxaByRankAndName.getKey());
+        overrideFilterCommonNameReferences = PreferencesUtil.getOverrideForPreference(PreferencePredicate.CommonNameReferencesWithMarker.getKey());
+        overrideSearchNamedAreasByAbbrev = PreferencesUtil.getOverrideForPreference(PreferencePredicate.NamedAreaSearchForAbbrev.getKey());
 
-        overrideShowIdInSelectionDialog = PreferencesUtil.getBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.ShowIdInSelectionDialog.getKey()));
-        overrideSearchForIdentifierAsDefault = PreferencesUtil.getBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.SearchForIdentifierAsDefault.getKey()));
-        overrideSearchForIdentifierAndTitleCache = PreferencesUtil.getBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.SearchForIdentifierAndTitleCache.getKey()));
-        overrideSortTaxaByRankAndName = PreferencesUtil.getBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.SortTaxaByRankAndName.getKey()));
-        overrideFilterCommonNameReferences = PreferencesUtil.getBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.CommonNameReferencesWithMarker.getKey()));
         if(overrideShowIdInSelectionDialog){
             showIdInSelectionDialog = PreferencesUtil.getBooleanValue(PreferencePredicate.ShowIdInSelectionDialog.getKey(), true);
         }
@@ -92,6 +101,11 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
         if(overrideFilterCommonNameReferences){
             filterCommonNameReferences = PreferencesUtil.getBooleanValue(PreferencePredicate.CommonNameReferencesWithMarker.getKey(), true);
         }
+        if(overrideSearchNamedAreasByAbbrev){
+            String stringValue = PreferencesUtil.getStringValue(PreferencePredicate.NamedAreaSearchForAbbrev.getKey(), true);
+            searchNamedAreasByAbbrev = stringValue != null? NamedAreaSearchField.byKey(stringValue):null;
+        }
+
 
 
     }
@@ -105,16 +119,9 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
             String text = showIdInSelectionDialogButton.getText();
             if(text.startsWith(Messages.Preference_Use_Default)){
                 showIdInSelectionDialog = null;
-                if (isAdminPreference){
-                    showIdInSelectionDialogPref.setAllowOverride(true);
-                    allowOverrideIsShowIdInSelectionDialogButton.setSelection(true);
-                    allowOverrideIsShowIdInSelectionDialogButton.setEnabled(false);
-                }
                 return;
             }
-            if (isAdminPreference){
-                allowOverrideIsShowIdInSelectionDialogButton.setEnabled(true);
-            }
+
             if (text.equals(Messages.GeneralPreference_yes)){
                 showIdInSelectionDialog = true;
             }else{
@@ -126,16 +133,9 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
             String text = searchForIdentifierAsDefaultButton.getText();
             if(text.startsWith(Messages.Preference_Use_Default)){
                 searchForIdentifierAsDefault = null;
-                if (isAdminPreference){
-                    searchForIdentifierAsDefaultPref.setAllowOverride(true);
-                    allowOverrideSearchForIdentifierAsDefaultButton.setSelection(true);
-                    allowOverrideSearchForIdentifierAsDefaultButton.setEnabled(false);
-                }
                 return;
             }
-            if (isAdminPreference){
-                allowOverrideSearchForIdentifierAsDefaultButton.setEnabled(true);
-            }
+
             if (text.equals(Messages.GeneralPreference_yes)){
                 searchForIdentifierAsDefault = true;
             }else{
@@ -147,16 +147,9 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
             String text = searchForIdentifierAndTitleCacheButton.getText();
             if(text.startsWith(Messages.Preference_Use_Default)){
                 searchForIdentifierAndTitleCache = null;
-                if (isAdminPreference){
-                    searchForIdentifierAndTitleCachePref.setAllowOverride(true);
-                    allowOverridesSearchForIdentifierAndTitleCacheButton.setSelection(true);
-                    allowOverridesSearchForIdentifierAndTitleCacheButton.setEnabled(false);
-                }
                 return;
             }
-            if (isAdminPreference){
-                allowOverridesSearchForIdentifierAndTitleCacheButton.setEnabled(true);
-            }
+
             if (text.equals(Messages.GeneralPreference_yes)){
                 searchForIdentifierAndTitleCache = true;
             }else{
@@ -168,16 +161,9 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
             String text = sortTaxaByRankAndNameButton.getText();
             if(text.startsWith(Messages.Preference_Use_Default)){
                 sortTaxaByRankAndName = null;
-                if (isAdminPreference){
-                    sortTaxaByRankAndNamePref.setAllowOverride(true);
-                    allowOverrideIsSortTaxaByRankAndNameButton.setSelection(true);
-                    allowOverrideIsSortTaxaByRankAndNameButton.setEnabled(false);
-                }
                 return;
             }
-            if (isAdminPreference){
-                allowOverrideIsSortTaxaByRankAndNameButton.setEnabled(true);
-            }
+
             if (text.equals(Messages.GeneralPreference_yes)){
                 sortTaxaByRankAndName = true;
             }else{
@@ -189,16 +175,9 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
             String text = filterCommonNameReferenceButton.getText();
             if(text.startsWith(Messages.Preference_Use_Default)){
                 filterCommonNameReferences = null;
-                if (isAdminPreference){
-                    filterCommonNameReferencesPref.setAllowOverride(true);
-                    allowOverrideFilterCommonNameReferencesButton.setSelection(true);
-                    allowOverrideFilterCommonNameReferencesButton.setEnabled(false);
-                }
                 return;
             }
-            if (isAdminPreference){
-                allowOverrideFilterCommonNameReferencesButton.setEnabled(true);
-            }
+
             if (text.equals(Messages.GeneralPreference_yes)){
                 filterCommonNameReferences = true;
             }else{
@@ -206,6 +185,17 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
             }
         }
 
+        if (e.getSource().equals(this.searchNamedAreasByAbbrevButton)) {
+            String text = searchNamedAreasByAbbrevButton.getText();
+            if(text.startsWith(Messages.Preference_Use_Default)){
+                searchNamedAreasByAbbrev = null;
+                return;
+            }
+            searchNamedAreasByAbbrev = (NamedAreaSearchField) searchNamedAreasByAbbrevButton.getData(text);
+
+        }
+
+
 
     }
 
@@ -236,7 +226,6 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
         if (isAdminPreference){
             allowOverrideIsShowIdInSelectionDialogButton = createAllowOverrideButton(titleComp);
             allowOverrideIsShowIdInSelectionDialogButton.setSelection(showIdInSelectionDialogPref != null? showIdInSelectionDialogPref.isAllowOverride(): true);
-            allowOverrideIsShowIdInSelectionDialogButton.setEnabled(showIdInSelectionDialog != null);
             allowOverrideIsShowIdInSelectionDialogButton.addSelectionListener(new SelectionAdapter(){
                 @Override
                 public void widgetSelected(SelectionEvent e) {
@@ -263,7 +252,6 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
         if (isAdminPreference){
             allowOverrideSearchForIdentifierAsDefaultButton = createAllowOverrideButton(composite);
             allowOverrideSearchForIdentifierAsDefaultButton.setSelection(searchForIdentifierAsDefaultPref != null?searchForIdentifierAsDefaultPref.isAllowOverride(): true);
-            allowOverrideSearchForIdentifierAsDefaultButton.setEnabled(searchForIdentifierAsDefault != null);
             allowOverrideSearchForIdentifierAsDefaultButton.addSelectionListener(new SelectionAdapter(){
                 @Override
                 public void widgetSelected(SelectionEvent e) {
@@ -289,7 +277,6 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
         if (isAdminPreference){
             allowOverridesSearchForIdentifierAndTitleCacheButton = createAllowOverrideButton(composite);
             allowOverridesSearchForIdentifierAndTitleCacheButton.setSelection(searchForIdentifierAndTitleCachePref != null?searchForIdentifierAndTitleCachePref.isAllowOverride():true);
-            allowOverridesSearchForIdentifierAndTitleCacheButton.setEnabled(searchForIdentifierAndTitleCache != null);
             allowOverridesSearchForIdentifierAndTitleCacheButton.addSelectionListener(new SelectionAdapter(){
                 @Override
                 public void widgetSelected(SelectionEvent e) {
@@ -314,7 +301,6 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
         if (isAdminPreference){
             allowOverrideIsSortTaxaByRankAndNameButton = createAllowOverrideButton(composite);
             allowOverrideIsSortTaxaByRankAndNameButton.setSelection(sortTaxaByRankAndNamePref != null? sortTaxaByRankAndNamePref.isAllowOverride(): true);
-            allowOverrideIsSortTaxaByRankAndNameButton.setEnabled(sortTaxaByRankAndName != null);
             allowOverrideIsSortTaxaByRankAndNameButton.addSelectionListener(new SelectionAdapter(){
                 @Override
                 public void widgetSelected(SelectionEvent e) {
@@ -338,7 +324,6 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
         if (isAdminPreference){
             allowOverrideFilterCommonNameReferencesButton = createAllowOverrideButton(composite);
             allowOverrideFilterCommonNameReferencesButton.setSelection(filterCommonNameReferencesPref != null? filterCommonNameReferencesPref.isAllowOverride():true);
-            allowOverrideFilterCommonNameReferencesButton.setEnabled(filterCommonNameReferences != null);
             allowOverrideFilterCommonNameReferencesButton.addSelectionListener(new SelectionAdapter(){
                 @Override
                 public void widgetSelected(SelectionEvent e) {
@@ -351,6 +336,29 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
             });
         }
 
+        searchNamedAreasByAbbrevButton = createCombo(composite, NamedAreaSearchField.values(), PreferencePredicate.NamedAreaSearchForAbbrev, Messages.DatabasePreferncesPage_NamedAreaSearchField, isAdminPreference);
+        searchNamedAreasByAbbrevButton.addSelectionListener(this);
+
+
+        if (!isAdminPreference && searchNamedAreasByAbbrevPref != null){
+            searchNamedAreasByAbbrevButton.setEnabled(searchNamedAreasByAbbrevPref.isAllowOverride());
+        }
+
+        if (isAdminPreference){
+            allowOverridesearchNamedAreasByAbbrevButton = createAllowOverrideButton(composite);
+            allowOverridesearchNamedAreasByAbbrevButton.setSelection(searchNamedAreasByAbbrevPref != null? searchNamedAreasByAbbrevPref.isAllowOverride():true);
+            allowOverridesearchNamedAreasByAbbrevButton.addSelectionListener(new SelectionAdapter(){
+                @Override
+                public void widgetSelected(SelectionEvent e) {
+                    setApply(true);
+                    if (searchNamedAreasByAbbrevPref == null && !allowOverridesearchNamedAreasByAbbrevButton.getSelection()){
+                        searchNamedAreasByAbbrevPref = CdmPreference.NewTaxEditorInstance(PreferencePredicate.NamedAreaSearchForAbbrev, null);
+                    }
+                    searchNamedAreasByAbbrevPref.setAllowOverride(allowOverridesearchNamedAreasByAbbrevButton.getSelection());
+                }
+            });
+        }
+
         setSelections();
 
         return composite;
@@ -362,37 +370,38 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
         showIdInSelectionDialog = null;
         if (isAdminPreference){
             allowOverrideIsShowIdInSelectionDialogButton.setSelection(true);
-            allowOverrideIsShowIdInSelectionDialogButton.setEnabled(false);
             showIdInSelectionDialogPref.setAllowOverride(true);
         }
         searchForIdentifierAsDefaultButton.select(0);
         searchForIdentifierAsDefault = null;
         if (isAdminPreference){
             allowOverrideSearchForIdentifierAsDefaultButton.setSelection(true);
-            allowOverrideSearchForIdentifierAsDefaultButton.setEnabled(false);
             searchForIdentifierAsDefaultPref.setAllowOverride(true);
         }
         searchForIdentifierAndTitleCacheButton.select(0);
         searchForIdentifierAndTitleCache = null;
         if (isAdminPreference){
             allowOverridesSearchForIdentifierAndTitleCacheButton.setSelection(true);
-            allowOverridesSearchForIdentifierAndTitleCacheButton.setEnabled(false);
             searchForIdentifierAndTitleCachePref.setAllowOverride(true);
         }
         sortTaxaByRankAndNameButton.select(0);
         sortTaxaByRankAndName = null;
         if (isAdminPreference){
             allowOverrideIsSortTaxaByRankAndNameButton.setSelection(true);
-            allowOverrideIsSortTaxaByRankAndNameButton.setEnabled(false);
             sortTaxaByRankAndNamePref.setAllowOverride(true);
         }
         filterCommonNameReferenceButton.select(0);
         filterCommonNameReferences = null;
         if (isAdminPreference){
             allowOverrideFilterCommonNameReferencesButton.setSelection(true);
-            allowOverrideFilterCommonNameReferencesButton.setEnabled(false);
             filterCommonNameReferencesPref.setAllowOverride(true);
         }
+        searchNamedAreasByAbbrevButton.select(0);
+        searchNamedAreasByAbbrev = null;
+        if (isAdminPreference){
+            allowOverridesearchNamedAreasByAbbrevButton.setSelection(true);
+            searchNamedAreasByAbbrevPref.setAllowOverride(true);
+        }
         setApply(true);
     }
 
@@ -449,6 +458,16 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
                 override);
         override = false;
 
+        if(searchNamedAreasByAbbrev != null){
+            override = true;
+            PreferencesUtil.setStringValue(PreferencePredicate.NamedAreaSearchForAbbrev.getKey(), searchNamedAreasByAbbrev.getKey());
+        }
+        PreferencesUtil.setBooleanValue(
+                PreferencesUtil.prefOverrideKey(PreferencePredicate.NamedAreaSearchForAbbrev.getKey()),
+                override);
+
+
+
         return true;
     }
 
@@ -538,6 +557,21 @@ public class SearchDialogPreferences extends CdmPreferencePage implements Select
             }
             index++;
         }
+        index = 0;
+        for (String itemLabel: searchNamedAreasByAbbrevButton.getItems()) {
+            if ((searchNamedAreasByAbbrev == null || (!overrideSearchNamedAreasByAbbrev && !isAdminPreference)) && itemLabel.startsWith(Messages.Preference_Use_Default)) {
+                searchNamedAreasByAbbrevButton.select(index);
+                break;
+            }
+
+
+            if (searchNamedAreasByAbbrev != null && itemLabel.equals(searchNamedAreasByAbbrev.getLabel())){
+                searchNamedAreasByAbbrevButton.select(index);
+                break;
+            }
+
+            index++;
+        }
     }