ref #8385: fix nomenclatural code preference page
authorKatja Luther <k.luther@bgbm.org>
Mon, 19 Aug 2019 11:05:44 +0000 (13:05 +0200)
committerKatja Luther <k.luther@bgbm.org>
Mon, 19 Aug 2019 11:06:28 +0000 (13:06 +0200)
eu.etaxonomy.taxeditor.store/plugin.xml
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/databaseAdmin/preferencePage/NomenclaturalCodeAdminPreferencePage.java [new file with mode: 0755]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/NomenclaturalCodeHelper.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/NomenclaturalCodePreferences.java [moved from eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/databaseAdmin/preferencePage/NomenclaturalCodePreferences.java with 58% similarity]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/PreferencesUtil.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/preference/menu/NomenclaturalCodePreferences.java

index 8f4faee4c32d209526c1517cdadf8f308da7383f..c869b4e5975633eeaeff4fd45f97218e9309fd96 100755 (executable)
    </page>
    <page
          category="eu.etaxonomy.taxeditor.preferences.namePreferences"
-         class="eu.etaxonomy.taxeditor.preference.menu.NomenclaturalCodePreferences"
+         class="eu.etaxonomy.taxeditor.preference.NomenclaturalCodePreferences"
          id="eu.etaxonomy.taxeditor.preferences.nomenclatural"
          name="%page.name.4">
    </page>
    </page>
    <page
          category="eu.etaxonomy.taxeditor.preferences.namePreferences"
-         class="eu.etaxonomy.taxeditor.databaseAdmin.preferencePage.NomenclaturalCodePreferences"
+         class="eu.etaxonomy.taxeditor.databaseAdmin.preferencePage.NomenclaturalCodeAdminPreferencePage"
          id="eu.etaxonomy.taxeditor.store.page2"
          name="%page.name.43">
    </page>
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/databaseAdmin/preferencePage/NomenclaturalCodeAdminPreferencePage.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/databaseAdmin/preferencePage/NomenclaturalCodeAdminPreferencePage.java
new file mode 100755 (executable)
index 0000000..4c71ec9
--- /dev/null
@@ -0,0 +1,66 @@
+/**
+* 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 eu.etaxonomy.cdm.api.application.ICdmRepository;
+import eu.etaxonomy.cdm.api.service.IPreferenceService;
+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.name.NomenclaturalCode;
+import eu.etaxonomy.taxeditor.preference.NomenclaturalCodePreferences;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.store.CdmStore;
+
+/**
+ * @author k.luther
+ * @since 19.08.2019
+ */
+public class NomenclaturalCodeAdminPreferencePage extends NomenclaturalCodePreferences {
+
+
+    @Override
+    public boolean performOk() {
+       if (!isApply()){
+           return true;
+       }
+       CdmPreference pref = null;
+       if (actualCode != null){
+           pref = CdmPreference.NewDatabaseInstance(PreferencePredicate.NomenclaturalCode, actualCode.toString());
+           pref.setAllowOverride(allowOverride);
+       }
+
+       PreferencesUtil.setPreferredNomenclaturalCode(pref);
+
+       return true;
+   }
+
+    @Override
+    protected void getValues(){
+        isAdminPreference = true;
+        ICdmRepository controller = CdmStore.getCurrentApplicationConfiguration();
+        if (controller != null){
+            IPreferenceService service = controller.getPreferenceService();
+            PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
+            pref = service.find(key);
+            if (pref != null){
+                allowOverride = pref.isAllowOverride();
+                actualCode = NomenclaturalCode.fromString(pref.getValue());
+
+            }else{
+                actualCode = null;
+                allowOverride = true;
+            }
+
+        }
+
+
+    }
+}
index ea4c9303650b13c15bce17a559e2d18039fdf7a5..1cf9d3f80e9d6ebe23d8faa91629cfac5f52d10b 100644 (file)
@@ -42,9 +42,10 @@ public class NomenclaturalCodeHelper {
         */
        public static List<NomenclaturalCode> getSupportedCodes() {
                List<NomenclaturalCode> supportedCodes = new ArrayList<NomenclaturalCode>();
+               NomenclaturalCode nomenclaturalCode =PreferencesUtil.getPreferredNomenclaturalCode();
                for (NomenclaturalCode code : getAllCodes()) {
                        if (isSupported(code)) {
-                           if (PreferencesUtil.getPreferredNomenclaturalCode().equals(code)){
+                           if (nomenclaturalCode != null && nomenclaturalCode.equals(code)){
                                supportedCodes.add(0, code);
                            }else{
                                supportedCodes.add(code);
-/**
- * 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.databaseAdmin.preferencePage;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.CLabel;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Combo;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-
-import eu.etaxonomy.cdm.api.application.ICdmRepository;
-import eu.etaxonomy.cdm.api.service.IPreferenceService;
-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.name.NomenclaturalCode;
-import eu.etaxonomy.taxeditor.l10n.Messages;
-import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
-import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
-import eu.etaxonomy.taxeditor.store.CdmStore;
-
-/**
- * <p>
- * NomenclaturalCodePreferences class.
- * </p>
- *
- * @author k.luther
- * @created 14.09.2018
- */
-public class NomenclaturalCodePreferences extends CdmPreferencePage implements IE4AdminPreferencePage{
-
-    NomenclaturalCode actualCode = null;
-    Map<NomenclaturalCode, String>  labelAndValues;
-    Map<NomenclaturalCode, Integer> keyAndIndex;
-    Combo nomenclaturalCode;
-    Button allowOverrideButton;
-    boolean allowOverride;
-
-
-       @Override
-       protected Control createContents(Composite parent) {
-//         PreferencesUtil.setPreferredNomenclaturalCode(null, false);
-           ICdmRepository controller = CdmStore.getCurrentApplicationConfiguration();
-        if (controller != null){
-            IPreferenceService service = controller.getPreferenceService();
-            PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
-            CdmPreference pref = service.find(key);
-            if (pref != null){
-                actualCode = NomenclaturalCode.fromString(pref.getValue());
-                allowOverride = pref.isAllowOverride();
-            }else{
-                actualCode = NomenclaturalCode.fromString(PreferencePredicate.NomenclaturalCode.getDefaultValue().toString());
-                allowOverride = true;
-            }
-
-        }
-
-           Composite composite = createComposite(parent);
-
-        final CLabel description = new CLabel(composite, SWT.NULL);
-        description.setText(Messages.NomenclaturalCodePreferences_description);
-        description.setLayoutData(createTextGridData());
-        nomenclaturalCode= new Combo(composite, SWT.READ_ONLY);
-        nomenclaturalCode.setText(Messages.NomenclaturalCodePreferences_available_codes);
-
-
-
-        nomenclaturalCode.setFont(parent.getFont());
-        getLabelAndValues();
-        keyAndIndex = new HashMap<>();
-        Integer index = 0;
-        for (NomenclaturalCode key: labelAndValues.keySet()) {
-            nomenclaturalCode.add(labelAndValues.get(key));
-            keyAndIndex.put(key, index);
-            index++;
-        }
-        index = keyAndIndex.get(actualCode);
-        if(index!=null){
-            nomenclaturalCode.select(index);
-        }
-
-        nomenclaturalCode.addSelectionListener(new SelectionAdapter() {
-            @Override
-            public void widgetSelected(SelectionEvent evt) {
-                setApply(true);
-                String name = nomenclaturalCode.getText();
-                for (Entry<NomenclaturalCode, String> label:labelAndValues.entrySet()){
-                    if (label.getValue().equals(name)){
-                        actualCode = label.getKey();
-                    }
-                }
-            }
-        });
-        Button allowOverrideButton = createAllowOverrideButton(composite);
-        allowOverrideButton.setText(Messages.GeneralPreference_allowOverride);
-        allowOverrideButton.setSelection(allowOverride);
-        allowOverrideButton.addSelectionListener(new SelectionAdapter(){
-            @Override
-            public void widgetSelected(SelectionEvent e) {
-                allowOverride = allowOverrideButton.getSelection();
-                setApply(true);
-            }
-        });
-
-        return composite;
-
-       }
-
-       /**
-        * @return
-        */
-       private Map<NomenclaturalCode, String> getLabelAndValues() {
-               List<NomenclaturalCode> supportedCodes = NomenclaturalCodeHelper
-                               .getSupportedCodes();
-               labelAndValues = new HashMap<>();
-
-               for (int i = 0; i < supportedCodes.size(); i++) {
-                       labelAndValues.put(supportedCodes.get(i), NomenclaturalCodeHelper
-                    .getDescription(supportedCodes.get(i))) ;
-
-               }
-               return labelAndValues;
-       }
-
-       /**
-        * {@inheritDoc}
-        *
-        * Initialize the preference page
-        */
-    @Override
-    public void init() {
-        super.init();
-        if(!CdmStore.isActive()){
-            return;
-        }
-
-
-
-       }
-
-       @Override
-        public boolean performOk() {
-           if (!isApply()){
-            return true;
-        }
-           CdmPreference pref = CdmPreference.NewDatabaseInstance(PreferencePredicate.NomenclaturalCode, actualCode.toString());
-           pref.setAllowOverride(allowOverride);
-           PreferencesUtil.setPreferredNomenclaturalCode(pref);
-
-        return true;
-    }
-
-       @Override
-       protected void performDefaults() {
-           actualCode = null;
-           allowOverride = true;
-           Integer index = keyAndIndex.get(actualCode);
-        if(index!=null){
-            nomenclaturalCode.select(index);
-        }
-        allowOverrideButton.setSelection(allowOverride);
-           super.performDefaults();
-       }
-
-}
+/**\r
+ * Copyright (C) 2007 EDIT\r
+ * European Distributed Institute of Taxonomy\r
+ * http://www.e-taxonomy.eu\r
+ *\r
+ * The contents of this file are subject to the Mozilla Public License Version 1.1\r
+ * See LICENSE.TXT at the top of this package for the full license terms.\r
+ */\r
+package eu.etaxonomy.taxeditor.preference;\r
+\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Map.Entry;\r
+\r
+import org.apache.commons.lang.StringUtils;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.custom.CLabel;\r
+import org.eclipse.swt.events.SelectionAdapter;\r
+import org.eclipse.swt.events.SelectionEvent;\r
+import org.eclipse.swt.widgets.Button;\r
+import org.eclipse.swt.widgets.Combo;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.eclipse.swt.widgets.Control;\r
+\r
+import eu.etaxonomy.cdm.api.application.ICdmRepository;\r
+import eu.etaxonomy.cdm.api.service.IPreferenceService;\r
+import eu.etaxonomy.cdm.model.metadata.CdmPreference;\r
+import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;\r
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;\r
+import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;\r
+import eu.etaxonomy.cdm.model.name.NomenclaturalCode;\r
+import eu.etaxonomy.taxeditor.databaseAdmin.preferencePage.IE4AdminPreferencePage;\r
+import eu.etaxonomy.taxeditor.l10n.Messages;\r
+import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;\r
+import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;\r
+import eu.etaxonomy.taxeditor.store.CdmStore;\r
+\r
+/**\r
+ * <p>\r
+ * NomenclaturalCodePreferences class.\r
+ * </p>\r
+ *\r
+ * @author k.luther\r
+ * @created 14.09.2018\r
+ */\r
+public class NomenclaturalCodePreferences extends CdmPreferencePage implements IE4AdminPreferencePage{\r
+    protected CdmPreference pref;\r
+    protected NomenclaturalCode actualCode = null;\r
+    protected Map<NomenclaturalCode, String>  labelAndValues;\r
+    protected Map<NomenclaturalCode, Integer> keyAndIndex;\r
+    protected Combo nomenclaturalCode;\r
+    protected Button allowOverrideButton;\r
+    protected boolean allowOverride;\r
+    private boolean override;\r
+\r
+\r
+       @Override\r
+       protected Control createContents(Composite parent) {\r
+//         PreferencesUtil.setPreferredNomenclaturalCode(null, false);\r
+\r
+           getValues();\r
+           Composite composite = createComposite(parent);\r
+           if (!pref.isAllowOverride() && !isAdminPreference){\r
+               final CLabel description = new CLabel(composite, SWT.NULL);\r
+               description.setText(Messages.NomenclaturalCodePreferences_localChangesNotAllowed);\r
+               description.setLayoutData(createTextGridData());\r
+               return composite;\r
+           }\r
+        final CLabel description = new CLabel(composite, SWT.NULL);\r
+        description.setText(Messages.NomenclaturalCodePreferences_description);\r
+        description.setLayoutData(createTextGridData());\r
+\r
+        nomenclaturalCode= new Combo(composite, SWT.READ_ONLY);\r
+        nomenclaturalCode.setText(Messages.NomenclaturalCodePreferences_available_codes);\r
+\r
+\r
+\r
+        nomenclaturalCode.setFont(parent.getFont());\r
+        getLabelAndValues();\r
+        keyAndIndex = new HashMap<>();\r
+        Integer index = 0;\r
+        nomenclaturalCode.add(Messages.Preference_Use_Default + " (" +PreferencePredicate.NomenclaturalCode.getDefaultValue().toString()+")", index);\r
+        index++;\r
+        for (NomenclaturalCode key: labelAndValues.keySet()) {\r
+            nomenclaturalCode.add(labelAndValues.get(key));\r
+            keyAndIndex.put(key, index);\r
+            index++;\r
+        }\r
+        index = keyAndIndex.get(actualCode);\r
+        if(index!=null){\r
+            nomenclaturalCode.select(index);\r
+        }else{\r
+            nomenclaturalCode.select(0);\r
+        }\r
+\r
+        nomenclaturalCode.addSelectionListener(new SelectionAdapter() {\r
+            @Override\r
+            public void widgetSelected(SelectionEvent evt) {\r
+                setApply(true);\r
+                String name = nomenclaturalCode.getText();\r
+                actualCode = null;\r
+                for (Entry<NomenclaturalCode, String> label:labelAndValues.entrySet()){\r
+                    if (label.getValue().equals(name)){\r
+                        actualCode = label.getKey();\r
+                    }\r
+                }\r
+\r
+            }\r
+        });\r
+        if (isAdminPreference){\r
+            Button allowOverrideButton = createAllowOverrideButton(composite);\r
+            allowOverrideButton.setText(Messages.GeneralPreference_allowOverride);\r
+            allowOverrideButton.setSelection(allowOverride);\r
+            allowOverrideButton.addSelectionListener(new SelectionAdapter(){\r
+                @Override\r
+                public void widgetSelected(SelectionEvent e) {\r
+                    allowOverride = allowOverrideButton.getSelection();\r
+                    setApply(true);\r
+                }\r
+            });\r
+        }\r
+        return composite;\r
+\r
+       }\r
+\r
+       /**\r
+        * @return\r
+        */\r
+       private Map<NomenclaturalCode, String> getLabelAndValues() {\r
+               List<NomenclaturalCode> supportedCodes = NomenclaturalCodeHelper\r
+                               .getSupportedCodes();\r
+               labelAndValues = new HashMap<>();\r
+\r
+               for (int i = 0; i < supportedCodes.size(); i++) {\r
+                       labelAndValues.put(supportedCodes.get(i), NomenclaturalCodeHelper\r
+                    .getDescription(supportedCodes.get(i))) ;\r
+\r
+               }\r
+               return labelAndValues;\r
+       }\r
+\r
+       /**\r
+        * {@inheritDoc}\r
+        *\r
+        * Initialize the preference page\r
+        */\r
+    @Override\r
+    public void init() {\r
+        super.init();\r
+        if(!CdmStore.isActive()){\r
+            return;\r
+        }\r
+\r
+\r
+\r
+       }\r
+\r
+       @Override\r
+        public boolean performOk() {\r
+           if (!isApply()){\r
+            return true;\r
+        }\r
+\r
+           if (actualCode != null && pref.isAllowOverride()){\r
+               PreferencesUtil.setStringValue(PreferencePredicate.NomenclaturalCode.getKey(), actualCode.getKey());\r
+               PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), true);\r
+           }else{\r
+               PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), false);\r
+           }\r
+        return true;\r
+    }\r
+\r
+       @Override\r
+       protected void performDefaults() {\r
+           actualCode = null;\r
+           allowOverride = true;\r
+\r
+           nomenclaturalCode.select(0);\r
+\r
+        if (isAdminPreference){\r
+            allowOverrideButton.setSelection(allowOverride);\r
+        }\r
+        setApply(true);\r
+           super.performDefaults();\r
+       }\r
+\r
+       @Override\r
+    protected void getValues(){\r
+           isAdminPreference = false;\r
+           ICdmRepository controller = CdmStore.getCurrentApplicationConfiguration();\r
+        if (controller != null){\r
+            IPreferenceService service = controller.getPreferenceService();\r
+            PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);\r
+            pref = service.find(key);\r
+            if (pref != null){\r
+\r
+                allowOverride = pref.isAllowOverride();\r
+            }else{\r
+\r
+                allowOverride = true;\r
+            }\r
+\r
+        }\r
+        String stringValue = PreferencesUtil.getStringValue(PreferencePredicate.NomenclaturalCode.getKey(), true);\r
+        Boolean overridePref = PreferencesUtil.getBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), true);\r
+        override = overridePref != null? overridePref:false;\r
+        if (!StringUtils.isBlank(stringValue) && override){\r
+            actualCode = NomenclaturalCode.fromString(stringValue);\r
+        }else{\r
+            actualCode = null;\r
+        }\r
+\r
+\r
+       }\r
+\r
+}\r
index 3e1ba39435289822d4844d71123880730f4b273f..b8d394a74da463606d0cea7d3b70bbd8f624fe90 100644 (file)
@@ -457,14 +457,18 @@ public class PreferencesUtil implements IPreferenceKeys {
         if (controller == null){
             return;
         }
-        PrefKey key = null;
-        try{
-            key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
-        }catch (Exception e){
-            System.out.println(e.getStackTrace());
-        }
 
-        controller.getPreferenceService().set(preference);
+        if (preference == null){
+            PrefKey key = null;
+              try{
+                  key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
+              }catch (Exception e){
+                  System.out.println(e.getStackTrace());
+              }
+            controller.getPreferenceService().remove(key);
+        }else{
+            controller.getPreferenceService().set(preference);
+        }
 
     }
 
index e0cf2903a1174b53ea8e62ca64217866ee746879..5748ec8c86168cd3e30ca92b68b755dbe6643d3f 100644 (file)
@@ -60,14 +60,16 @@ public class NomenclaturalCodePreferences extends FieldEditorPreferencePageE4 {
                                .getSupportedCodes();
                String[][] labelAndValues = new String[supportedCodes.size()][2];
                String label;
-               for (int i = 0; i < supportedCodes.size(); i++) {
+               labelAndValues[0][0] = Messages.Preference_Use_Default + "(" + pref.getValue() + ")";
+        labelAndValues[0][1] = null;
+               for (int i = 1; i < supportedCodes.size(); i++) {
                    label = NomenclaturalCodeHelper
                     .getDescription(supportedCodes.get(i));
                    String value = supportedCodes.get(i).toString();
                    String prefValue = pref.getValue();
-                   if (pref.getValue().equals(supportedCodes.get(i).toString())) {
-                        label +=Messages.Preference_Use_Default;
-            }
+//                 if (pref.getValue().equals(supportedCodes.get(i).toString())) {
+//                      label +=Messages.Preference_Use_Default;
+//            }
                    labelAndValues[i][0] = label;
                        labelAndValues[i][1] = PreferencesUtil
                                        .getPreferenceKey(supportedCodes.get(i));
@@ -112,6 +114,7 @@ public class NomenclaturalCodePreferences extends FieldEditorPreferencePageE4 {
            }
                boolean result = super.performOk();
                String value = pref.getValue();
+
                String test = PreferencesUtil.getStringValue(PreferencePredicate.NomenclaturalCode.getKey(), true);
                if (!pref.getValue().equals(PreferencesUtil.getStringValue(PreferencePredicate.NomenclaturalCode.getKey(), true))) {
             PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), true);