Merge branch 'release/5.19.0'
[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.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Map.Entry;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.CLabel;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Combo;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.swt.widgets.Control;
24
25 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
26 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
27 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
28 import eu.etaxonomy.taxeditor.databaseAdmin.preferencePage.IE4AdminPreferencePage;
29 import eu.etaxonomy.taxeditor.l10n.Messages;
30 import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
31 import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
32 import eu.etaxonomy.taxeditor.store.CdmStore;
33
34 /**
35 * <p>
36 * NomenclaturalCodePreferences class.
37 * </p>
38 *
39 * @author k.luther
40 * @created 14.09.2018
41 */
42 public class NomenclaturalCodePreferences extends CdmPreferencePage implements IE4AdminPreferencePage{
43 protected CdmPreference pref;
44 protected NomenclaturalCode actualCode = null;
45 protected Map<NomenclaturalCode, String> labelAndValues;
46 protected Map<NomenclaturalCode, Integer> keyAndIndex;
47 protected Combo nomenclaturalCode;
48 protected Button allowOverrideButton;
49 protected boolean allowOverride;
50 private boolean override;
51
52
53 @Override
54 protected Control createContents(Composite parent) {
55 // PreferencesUtil.setPreferredNomenclaturalCode(null, false);
56
57 getValues();
58 Composite composite = createComposite(parent);
59 if (pref != null && !pref.isAllowOverride() && !isAdminPreference){
60 final CLabel description = new CLabel(composite, SWT.NULL);
61 description.setText(Messages.NomenclaturalCodePreferences_localChangesNotAllowed);
62 description.setLayoutData(createTextGridData());
63 return composite;
64 }
65 final CLabel description = new CLabel(composite, SWT.NULL);
66 description.setText(Messages.NomenclaturalCodePreferences_description);
67 description.setLayoutData(createTextGridData());
68
69 nomenclaturalCode= new Combo(composite, SWT.READ_ONLY);
70 nomenclaturalCode.setText(Messages.NomenclaturalCodePreferences_available_codes);
71
72
73
74 nomenclaturalCode.setFont(parent.getFont());
75 getLabelAndValues();
76 keyAndIndex = new HashMap<>();
77 Integer index = 0;
78 if (isAdminPreference){
79 nomenclaturalCode.add(Messages.Preference_Use_Default + " (" +PreferencePredicate.NomenclaturalCode.getDefaultValue().toString()+")", index);
80 }else{
81 if (pref != null){
82 if (pref.getValue() == null){
83 nomenclaturalCode.add(Messages.Preference_Use_Default + " (" +PreferencePredicate.NomenclaturalCode.getDefaultValue().toString()+")", index);
84 }else{
85 nomenclaturalCode.add(Messages.Preference_Use_Default + " (" +pref.getValue()+")", index);
86 }
87 }
88 }
89 index++;
90 for (NomenclaturalCode key: labelAndValues.keySet()) {
91 nomenclaturalCode.add(labelAndValues.get(key));
92 keyAndIndex.put(key, index);
93 index++;
94 }
95 index = keyAndIndex.get(actualCode);
96 if(index!=null){
97 nomenclaturalCode.select(index);
98 }else{
99 nomenclaturalCode.select(0);
100
101 }
102 if (isAdminPreference){
103 allowOverrideButton = createAllowOverrideButton(composite);
104 allowOverrideButton.setText(Messages.GeneralPreference_allowOverride);
105 allowOverrideButton.setSelection(allowOverride);
106 if (actualCode == null){
107 // allowOverrideButton.setEnabled(false);
108 }
109 allowOverrideButton.addSelectionListener(new SelectionAdapter(){
110 @Override
111 public void widgetSelected(SelectionEvent e) {
112 allowOverride = allowOverrideButton.getSelection();
113 setApply(true);
114 }
115 });
116 }
117
118 nomenclaturalCode.addSelectionListener(new SelectionAdapter() {
119 @Override
120 public void widgetSelected(SelectionEvent evt) {
121 setApply(true);
122 String name = nomenclaturalCode.getText();
123 actualCode = null;
124 for (Entry<NomenclaturalCode, String> label:labelAndValues.entrySet()){
125 if (label.getValue().equals(name)){
126 actualCode = label.getKey();
127 }
128 }
129
130
131
132 }
133 });
134
135 return composite;
136
137 }
138
139 /**
140 * @return
141 */
142 private void getLabelAndValues() {
143 List<NomenclaturalCode> supportedCodes = NomenclaturalCodeHelper
144 .getSupportedCodes();
145 labelAndValues = new HashMap<>();
146
147 for (int i = 0; i < supportedCodes.size(); i++) {
148 labelAndValues.put(supportedCodes.get(i), NomenclaturalCodeHelper
149 .getDescription(supportedCodes.get(i))) ;
150
151 }
152
153 }
154
155 /**
156 * {@inheritDoc}
157 *
158 * Initialize the preference page
159 */
160 @Override
161 public void init() {
162 super.init();
163 if(!CdmStore.isActive()){
164 return;
165 }
166
167
168
169 }
170
171 @Override
172 public boolean performOk() {
173 if (!isApply()){
174 return true;
175 }
176
177 if (actualCode != null && pref.isAllowOverride()){
178 PreferencesUtil.setStringValue(PreferencePredicate.NomenclaturalCode.getKey(), actualCode.getKey());
179 PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), true);
180 }else{
181 PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), false);
182 }
183 return true;
184 }
185
186 @Override
187 protected void performDefaults() {
188 actualCode = null;
189 allowOverride = true;
190
191 nomenclaturalCode.select(0);
192
193 if (isAdminPreference){
194 allowOverrideButton.setSelection(allowOverride);
195 }
196 setApply(true);
197 super.performDefaults();
198 }
199
200 @Override
201 protected void getValues(){
202 isAdminPreference = false;
203 pref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.NomenclaturalCode);
204 override = PreferencesUtil.getBooleanValue(
205 PreferencesUtil.prefOverrideKey(PreferencePredicate.NomenclaturalCode.getKey()), true) != null? PreferencesUtil.getBooleanValue(
206 PreferencesUtil.prefOverrideKey(PreferencePredicate.NomenclaturalCode.getKey()), true):false;
207 String nomString = null;
208 if (pref != null) {
209 if (pref.isAllowOverride() ) {
210 if (override){
211 nomString = PreferencesUtil.getStringValue(PreferencePredicate.NomenclaturalCode.getKey(), true);
212 }
213
214 } else {
215 nomString = pref.getValue() != null? String.valueOf(pref.getValue()): null;
216 }
217
218 } else {
219 if(!override){
220 actualCode = null;
221 }else{
222 nomString = PreferencesUtil.getStringValue(PreferencePredicate.NomenclaturalCode.getKey(), true);
223
224 }
225 pref = CdmPreference.NewTaxEditorInstance(PreferencePredicate.NomenclaturalCode, PreferencePredicate.NomenclaturalCode.getDefaultValue() != null ? PreferencePredicate.NomenclaturalCode.getDefaultValue().toString(): null);
226 }
227 if (nomString != null){
228 actualCode = NomenclaturalCode.fromString(nomString);
229 }
230
231
232
233
234 }
235
236 }