4c8b0d1f210326a8055099d57d6b7cf1f38abbd6
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / AdminNomenclaturalCode.java
1 // $Id$
2 /**
3 * Copyright (C) 2016 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.preference;
11
12 import java.util.List;
13
14 import org.eclipse.jface.preference.BooleanFieldEditor;
15 import org.eclipse.jface.preference.ComboFieldEditor;
16 import org.eclipse.jface.preference.FieldEditorPreferencePage;
17 import org.eclipse.ui.IWorkbench;
18 import org.eclipse.ui.IWorkbenchPreferencePage;
19
20 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
21 import eu.etaxonomy.cdm.api.service.IPreferenceService;
22 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
23 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
24 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
25 import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
26 import eu.etaxonomy.taxeditor.store.CdmStore;
27 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
28
29 /**
30 * @author k.luther
31 * @date 01.11.2016
32 *
33 */
34 public class AdminNomenclaturalCode extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
35
36 /* (non-Javadoc)
37 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
38 */
39 @Override
40 public void init(IWorkbench workbench) {
41 setPreferenceStore(TaxeditorStorePlugin.getDefault()
42 .getPreferenceStore());
43 setDescription("Choose which nomenclatural code you would like to use for scientific names unless otherwise specified. \nATTENTION, this preference is stored in DB!");
44 PreferencesUtil.setPreferredNomenclaturalCode(null, false);
45
46
47 }
48
49 /* (non-Javadoc)
50 * @see org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors()
51 */
52 @Override
53 protected void createFieldEditors() {
54 addField(new ComboFieldEditor(
55 IPreferenceKeys.CDM_NOMENCLATURAL_CODE_KEY,
56 "Available Codes", getLabelAndValues(),
57 getFieldEditorParent()));
58 addField(new BooleanFieldEditor(
59 IPreferenceKeys.ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY,
60 "allow to use local preference",
61 getFieldEditorParent()));
62
63
64 }
65
66 /**
67 * @return
68 */
69 private String[][] getLabelAndValues() {
70 List<NomenclaturalCode> supportedCodes = NomenclaturalCodeHelper
71 .getSupportedCodes();
72 String[][] labelAndValues = new String[supportedCodes.size()][2];
73 for (int i = 0; i < supportedCodes.size(); i++) {
74 labelAndValues[i][0] = NomenclaturalCodeHelper
75 .getDescription(supportedCodes.get(i));
76 labelAndValues[i][1] = PreferencesUtil
77 .getPreferenceKey(supportedCodes.get(i));
78 }
79 return labelAndValues;
80 }
81 @Override
82 public boolean performOk() {
83
84 boolean result = super.performOk();
85 if (result){
86 String value = getPreferenceStore().getString(IPreferenceKeys.CDM_NOMENCLATURAL_CODE_KEY);
87 String allowOverride = getPreferenceStore().getString(IPreferenceKeys.ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY);
88 CdmPreference pref = CdmPreference.NewDatabaseInstance( PreferencePredicate.NomenclaturalCode, value);
89 pref.setAllowOverride(Boolean.getBoolean(allowOverride));
90 ICdmApplicationConfiguration controller = CdmStore.getCurrentApplicationConfiguration();
91 if (controller == null){
92 return false;
93 }
94 IPreferenceService service = controller.getPreferenceService();
95 service.set(pref);
96 }
97 return result;
98 }
99
100
101 }