Project

General

Profile

Download (8.14 KB) Statistics
| Branch: | Tag: | Revision:
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.apache.commons.lang.StringUtils;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.custom.CLabel;
19
import org.eclipse.swt.events.SelectionAdapter;
20
import org.eclipse.swt.events.SelectionEvent;
21
import org.eclipse.swt.widgets.Button;
22
import org.eclipse.swt.widgets.Combo;
23
import org.eclipse.swt.widgets.Composite;
24
import org.eclipse.swt.widgets.Control;
25

    
26
import eu.etaxonomy.cdm.api.application.ICdmRepository;
27
import eu.etaxonomy.cdm.api.service.IPreferenceService;
28
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
29
import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
30
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
31
import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
32
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
33
import eu.etaxonomy.taxeditor.databaseAdmin.preferencePage.IE4AdminPreferencePage;
34
import eu.etaxonomy.taxeditor.l10n.Messages;
35
import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper;
36
import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
37
import eu.etaxonomy.taxeditor.store.CdmStore;
38

    
39
/**
40
 * <p>
41
 * NomenclaturalCodePreferences class.
42
 * </p>
43
 *
44
 * @author k.luther
45
 * @created 14.09.2018
46
 */
47
public class NomenclaturalCodePreferences extends CdmPreferencePage implements IE4AdminPreferencePage{
48
    protected CdmPreference pref;
49
    protected NomenclaturalCode actualCode = null;
50
    protected Map<NomenclaturalCode, String>  labelAndValues;
51
    protected Map<NomenclaturalCode, Integer> keyAndIndex;
52
    protected Combo nomenclaturalCode;
53
    protected Button allowOverrideButton;
54
    protected boolean allowOverride;
55
    private boolean override;
56

    
57

    
58
	@Override
59
	protected Control createContents(Composite parent) {
60
//	    PreferencesUtil.setPreferredNomenclaturalCode(null, false);
61

    
62
	    getValues();
63
	    Composite composite = createComposite(parent);
64
	    if (pref != null && !pref.isAllowOverride() && !isAdminPreference){
65
	        final CLabel description = new CLabel(composite, SWT.NULL);
66
	        description.setText(Messages.NomenclaturalCodePreferences_localChangesNotAllowed);
67
	        description.setLayoutData(createTextGridData());
68
	        return composite;
69
	    }
70
        final CLabel description = new CLabel(composite, SWT.NULL);
71
        description.setText(Messages.NomenclaturalCodePreferences_description);
72
        description.setLayoutData(createTextGridData());
73

    
74
        nomenclaturalCode= new Combo(composite, SWT.READ_ONLY);
75
        nomenclaturalCode.setText(Messages.NomenclaturalCodePreferences_available_codes);
76

    
77

    
78

    
79
        nomenclaturalCode.setFont(parent.getFont());
80
        getLabelAndValues();
81
        keyAndIndex = new HashMap<>();
82
        Integer index = 0;
83
        if (isAdminPreference){
84
            nomenclaturalCode.add(Messages.Preference_Use_Default + " (" +PreferencePredicate.NomenclaturalCode.getDefaultValue().toString()+")", index);
85
        }else{
86
            if (pref != null){
87
                nomenclaturalCode.add(Messages.Preference_Use_Default + " (" +pref.getValue()+")", index);
88
            }
89
        }
90
        index++;
91
        for (NomenclaturalCode key: labelAndValues.keySet()) {
92
            nomenclaturalCode.add(labelAndValues.get(key));
93
            keyAndIndex.put(key, index);
94
            index++;
95
        }
96
        index = keyAndIndex.get(actualCode);
97
        if(index!=null){
98
            nomenclaturalCode.select(index);
99
        }else{
100
            nomenclaturalCode.select(0);
101

    
102
        }
103
        if (isAdminPreference){
104
            allowOverrideButton = createAllowOverrideButton(composite);
105
            allowOverrideButton.setText(Messages.GeneralPreference_allowOverride);
106
            allowOverrideButton.setSelection(allowOverride);
107
            if (actualCode == null){
108
                allowOverrideButton.setEnabled(false);
109
            }
110
            allowOverrideButton.addSelectionListener(new SelectionAdapter(){
111
                @Override
112
                public void widgetSelected(SelectionEvent e) {
113
                    allowOverride = allowOverrideButton.getSelection();
114
                    setApply(true);
115
                }
116
            });
117
        }
118

    
119
        nomenclaturalCode.addSelectionListener(new SelectionAdapter() {
120
            @Override
121
            public void widgetSelected(SelectionEvent evt) {
122
                setApply(true);
123
                String name = nomenclaturalCode.getText();
124
                actualCode = null;
125
                for (Entry<NomenclaturalCode, String> label:labelAndValues.entrySet()){
126
                    if (label.getValue().equals(name)){
127
                        actualCode = label.getKey();
128
                    }
129
                }
130
                if (isAdminPreference){
131
                    allowOverrideButton.setEnabled(actualCode != null);
132
                    //if default value is selected, allow override is true
133
                    allowOverrideButton.setSelection(allowOverride  || (actualCode == null));
134
                }
135

    
136

    
137
            }
138
        });
139

    
140
        return composite;
141

    
142
	}
143

    
144
	/**
145
	 * @return
146
	 */
147
	private Map<NomenclaturalCode, String> getLabelAndValues() {
148
		List<NomenclaturalCode> supportedCodes = NomenclaturalCodeHelper
149
				.getSupportedCodes();
150
		labelAndValues = new HashMap<>();
151

    
152
		for (int i = 0; i < supportedCodes.size(); i++) {
153
			labelAndValues.put(supportedCodes.get(i), NomenclaturalCodeHelper
154
                    .getDescription(supportedCodes.get(i))) ;
155

    
156
		}
157
		return labelAndValues;
158
	}
159

    
160
	/**
161
	 * {@inheritDoc}
162
	 *
163
	 * Initialize the preference page
164
	 */
165
    @Override
166
    public void init() {
167
        super.init();
168
        if(!CdmStore.isActive()){
169
            return;
170
        }
171

    
172

    
173

    
174
	}
175

    
176
	@Override
177
	 public boolean performOk() {
178
	    if (!isApply()){
179
            return true;
180
        }
181

    
182
	    if (actualCode != null && pref.isAllowOverride()){
183
	        PreferencesUtil.setStringValue(PreferencePredicate.NomenclaturalCode.getKey(), actualCode.getKey());
184
	        PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), true);
185
	    }else{
186
	        PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), false);
187
	    }
188
        return true;
189
    }
190

    
191
	@Override
192
	protected void performDefaults() {
193
	    actualCode = null;
194
	    allowOverride = true;
195

    
196
	    nomenclaturalCode.select(0);
197

    
198
        if (isAdminPreference){
199
            allowOverrideButton.setSelection(allowOverride);
200
            allowOverrideButton.setEnabled(false);
201
        }
202
        setApply(true);
203
	    super.performDefaults();
204
	}
205

    
206
	@Override
207
    protected void getValues(){
208
	    isAdminPreference = false;
209
	    ICdmRepository controller = CdmStore.getCurrentApplicationConfiguration();
210
        if (controller != null){
211
            IPreferenceService service = controller.getPreferenceService();
212
            PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
213
            pref = service.find(key);
214
            if (pref != null){
215

    
216
                allowOverride = pref.isAllowOverride();
217
            }else{
218

    
219
                allowOverride = true;
220
            }
221

    
222
        }
223
        String stringValue = PreferencesUtil.getStringValue(PreferencePredicate.NomenclaturalCode.getKey(), true);
224
        Boolean overridePref = PreferencesUtil.getBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), true);
225
        override = overridePref != null? overridePref:false;
226
        if (!StringUtils.isBlank(stringValue) && override){
227
            actualCode = NomenclaturalCode.fromString(stringValue);
228
        }else{
229
            actualCode = null;
230
        }
231

    
232

    
233
	}
234

    
235
}
(32-32/52)