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.databaseAdmin.preferencePage;
|
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.events.SelectionAdapter;
|
18
|
import org.eclipse.swt.events.SelectionEvent;
|
19
|
import org.eclipse.swt.layout.GridLayout;
|
20
|
import org.eclipse.swt.widgets.Combo;
|
21
|
import org.eclipse.swt.widgets.Composite;
|
22
|
import org.eclipse.swt.widgets.Control;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
25
|
import eu.etaxonomy.cdm.api.service.IPreferenceService;
|
26
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
|
27
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
|
28
|
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
|
29
|
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
|
30
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
31
|
import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
|
32
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
33
|
import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
|
34
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
35
|
|
36
|
/**
|
37
|
* <p>
|
38
|
* NomenclaturalCodePreferences class.
|
39
|
* </p>
|
40
|
*
|
41
|
* @author k.luther
|
42
|
* @created 14.09.2018
|
43
|
*/
|
44
|
public class NomenclaturalCodePreferences extends CdmPreferencePage implements IE4AdminPreferencePage{
|
45
|
|
46
|
String actualCode;
|
47
|
Map<String, String> labelAndValues;
|
48
|
Map<String, Integer> keyAndIndex;
|
49
|
Combo nomenclaturalCode;
|
50
|
|
51
|
|
52
|
@Override
|
53
|
protected Control createContents(Composite parent) {
|
54
|
// PreferencesUtil.setPreferredNomenclaturalCode(null, false);
|
55
|
|
56
|
Composite composite = new Composite(parent, SWT.NULL);
|
57
|
composite.setLayout(new GridLayout());
|
58
|
|
59
|
nomenclaturalCode= new Combo(composite, SWT.READ_ONLY);
|
60
|
nomenclaturalCode.setText("Available Codes");
|
61
|
|
62
|
|
63
|
|
64
|
nomenclaturalCode.setFont(parent.getFont());
|
65
|
getLabelAndValues();
|
66
|
keyAndIndex = new HashMap<>();
|
67
|
Integer index = 0;
|
68
|
for (String key: labelAndValues.keySet()) {
|
69
|
nomenclaturalCode.add(labelAndValues.get(key));
|
70
|
keyAndIndex.put(key, index);
|
71
|
index++;
|
72
|
}
|
73
|
index = keyAndIndex.get(actualCode);
|
74
|
nomenclaturalCode.select(index);
|
75
|
|
76
|
nomenclaturalCode.addSelectionListener(new SelectionAdapter() {
|
77
|
@Override
|
78
|
public void widgetSelected(SelectionEvent evt) {
|
79
|
String name = nomenclaturalCode.getText();
|
80
|
for (Entry<String, String> label:labelAndValues.entrySet()){
|
81
|
if (label.getValue().equals(name)){
|
82
|
actualCode = label.getKey();
|
83
|
}
|
84
|
}
|
85
|
// actualCode = labelAndValues.get(name);;
|
86
|
|
87
|
|
88
|
}
|
89
|
});
|
90
|
|
91
|
return composite;
|
92
|
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* @return
|
97
|
*/
|
98
|
private Map<String, String> getLabelAndValues() {
|
99
|
List<NomenclaturalCode> supportedCodes = NomenclaturalCodeHelper
|
100
|
.getSupportedCodes();
|
101
|
labelAndValues = new HashMap<>();
|
102
|
for (int i = 0; i < supportedCodes.size(); i++) {
|
103
|
labelAndValues.put(PreferencesUtil.getPreferenceKey(supportedCodes.get(i)), NomenclaturalCodeHelper
|
104
|
.getDescription(supportedCodes.get(i))) ;
|
105
|
|
106
|
}
|
107
|
return labelAndValues;
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* {@inheritDoc}
|
112
|
*
|
113
|
* Initialize the preference page
|
114
|
*/
|
115
|
@Override
|
116
|
public void init() {
|
117
|
super.init();
|
118
|
if(!CdmStore.isActive()){
|
119
|
return;
|
120
|
}
|
121
|
ICdmRepository controller = CdmStore.getCurrentApplicationConfiguration();
|
122
|
if (controller != null){
|
123
|
IPreferenceService service = controller.getPreferenceService();
|
124
|
PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
|
125
|
CdmPreference pref = service.find(key);
|
126
|
if (pref != null){
|
127
|
actualCode = pref.getValue();
|
128
|
|
129
|
}
|
130
|
}
|
131
|
|
132
|
|
133
|
}
|
134
|
|
135
|
@Override
|
136
|
public boolean performOk() {
|
137
|
|
138
|
boolean result = super.performOk();
|
139
|
if (labelAndValues != null){
|
140
|
String key = labelAndValues.get(actualCode);
|
141
|
NomenclaturalCode preferredCode = NomenclaturalCode.getByKey(actualCode);
|
142
|
PreferencesUtil.setPreferredNomenclaturalCode(actualCode, false);
|
143
|
}
|
144
|
return result;
|
145
|
}
|
146
|
|
147
|
}
|