212cb992dbcc65f3b5ac0768aba663ee019c1dcb
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / NomenclaturalCodePreferences.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.preference;
10
11 import java.util.List;
12
13 import org.eclipse.jface.preference.BooleanFieldEditor;
14 import org.eclipse.jface.preference.ComboFieldEditor;
15 import org.eclipse.jface.preference.FieldEditorPreferencePage;
16 import org.eclipse.ui.IWorkbench;
17 import org.eclipse.ui.IWorkbenchPreferencePage;
18
19 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
20 import eu.etaxonomy.cdm.api.service.IPreferenceService;
21 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
22 import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
23 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
24 import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
25 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
26 import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
27 import eu.etaxonomy.taxeditor.store.CdmStore;
28 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
29
30 /**
31 * <p>
32 * NomenclaturalCodePreferences class.
33 * </p>
34 *
35 * @author p.ciardelli
36 * @created 16.09.2008
37 * @version 1.0
38 */
39 public class NomenclaturalCodePreferences extends FieldEditorPreferencePage
40 implements IWorkbenchPreferencePage {
41
42 /*
43 * (non-Javadoc)
44 *
45 * @see
46 * org.eclipse.jface.preference.FieldEditorPreferencePage#createFieldEditors
47 * ()
48 */
49 @Override
50 protected void createFieldEditors() {
51 PreferencesUtil.setPreferredNomenclaturalCode(null, false);
52 if (getPreferenceStore().getBoolean(IPreferenceKeys.ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
53 addField(new ComboFieldEditor(
54 IPreferenceKeys.PREFERRED_NOMENCLATURAL_CODE_KEY,
55 "Available Codes", getLabelAndValues(),
56 getFieldEditorParent()));
57
58 addField(new BooleanFieldEditor(
59 IPreferenceKeys.OVERRIDE_NOMENCLATURAL_CODE_KEY,
60 "Use local nomenclatural code",
61 getFieldEditorParent()));
62 } else {
63 setDescription("The CDM settings don't allow to set the nomenclatural code locally. If you need to make local settings, please ask an administrator.");
64 }
65
66 }
67
68 /**
69 * @return
70 */
71 private String[][] getLabelAndValues() {
72 List<NomenclaturalCode> supportedCodes = NomenclaturalCodeHelper
73 .getSupportedCodes();
74 String[][] labelAndValues = new String[supportedCodes.size()][2];
75 for (int i = 0; i < supportedCodes.size(); i++) {
76 labelAndValues[i][0] = NomenclaturalCodeHelper
77 .getDescription(supportedCodes.get(i));
78 labelAndValues[i][1] = PreferencesUtil
79 .getPreferenceKey(supportedCodes.get(i));
80 }
81 return labelAndValues;
82 }
83
84 /**
85 * {@inheritDoc}
86 *
87 * Initialize the preference page
88 */
89 @Override
90 public void init(IWorkbench workbench) {
91
92 setPreferenceStore(TaxeditorStorePlugin.getDefault()
93 .getPreferenceStore());
94
95 ICdmApplicationConfiguration controller = CdmStore.getCurrentApplicationConfiguration();
96 if (controller != null){
97 IPreferenceService service = controller.getPreferenceService();
98 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
99 CdmPreference pref = service.find(key);
100 getPreferenceStore().setValue(IPreferenceKeys.ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY, pref.isAllowOverride());
101 }
102
103 if (getPreferenceStore().getBoolean(IPreferenceKeys.ALLOW_OVERRIDE_NOMENCLATURAL_CODE_KEY)) {
104 setDescription("Choose which nomenclatural code you would like to use in your local application for scientific names unless otherwise specified.");
105 }else{
106 setDescription("The CDM settings don't allow to set the nomenclatural code locally. If you need to make local settings, please ask an administrator.");
107
108 }
109 }
110
111 @Override
112 public boolean performOk() {
113
114 boolean result = super.performOk();
115 // if (result){
116 // String value = getPreferenceStore().getString(IPreferenceKeys.PREFERRED_NOMENCLATURAL_CODE_KEY);
117 // CdmPreference pref = CdmPreference.NewDatabaseInstance( PreferencePredicate.NomenclaturalCode, value);
118 // ICdmApplicationConfiguration controller = CdmStore.getCurrentApplicationConfiguration();
119 // if (controller == null){
120 // return false;
121 // }
122 // IPreferenceService service = controller.getPreferenceService();
123 // service.set(pref);
124 // }
125 return result;
126 }
127
128
129
130 }