Project

General

Profile

Download (7.46 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.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
        nomenclaturalCode.add(Messages.Preference_Use_Default + " (" +PreferencePredicate.NomenclaturalCode.getDefaultValue().toString()+")", index);
84
        index++;
85
        for (NomenclaturalCode key: labelAndValues.keySet()) {
86
            nomenclaturalCode.add(labelAndValues.get(key));
87
            keyAndIndex.put(key, index);
88
            index++;
89
        }
90
        index = keyAndIndex.get(actualCode);
91
        if(index!=null){
92
            nomenclaturalCode.select(index);
93
        }else{
94
            nomenclaturalCode.select(0);
95
        }
96

    
97
        nomenclaturalCode.addSelectionListener(new SelectionAdapter() {
98
            @Override
99
            public void widgetSelected(SelectionEvent evt) {
100
                setApply(true);
101
                String name = nomenclaturalCode.getText();
102
                actualCode = null;
103
                for (Entry<NomenclaturalCode, String> label:labelAndValues.entrySet()){
104
                    if (label.getValue().equals(name)){
105
                        actualCode = label.getKey();
106
                    }
107
                }
108

    
109
            }
110
        });
111
        if (isAdminPreference){
112
            Button allowOverrideButton = createAllowOverrideButton(composite);
113
            allowOverrideButton.setText(Messages.GeneralPreference_allowOverride);
114
            allowOverrideButton.setSelection(allowOverride);
115
            allowOverrideButton.addSelectionListener(new SelectionAdapter(){
116
                @Override
117
                public void widgetSelected(SelectionEvent e) {
118
                    allowOverride = allowOverrideButton.getSelection();
119
                    setApply(true);
120
                }
121
            });
122
        }
123
        return composite;
124

    
125
	}
126

    
127
	/**
128
	 * @return
129
	 */
130
	private Map<NomenclaturalCode, String> getLabelAndValues() {
131
		List<NomenclaturalCode> supportedCodes = NomenclaturalCodeHelper
132
				.getSupportedCodes();
133
		labelAndValues = new HashMap<>();
134

    
135
		for (int i = 0; i < supportedCodes.size(); i++) {
136
			labelAndValues.put(supportedCodes.get(i), NomenclaturalCodeHelper
137
                    .getDescription(supportedCodes.get(i))) ;
138

    
139
		}
140
		return labelAndValues;
141
	}
142

    
143
	/**
144
	 * {@inheritDoc}
145
	 *
146
	 * Initialize the preference page
147
	 */
148
    @Override
149
    public void init() {
150
        super.init();
151
        if(!CdmStore.isActive()){
152
            return;
153
        }
154

    
155

    
156

    
157
	}
158

    
159
	@Override
160
	 public boolean performOk() {
161
	    if (!isApply()){
162
            return true;
163
        }
164

    
165
	    if (actualCode != null && pref.isAllowOverride()){
166
	        PreferencesUtil.setStringValue(PreferencePredicate.NomenclaturalCode.getKey(), actualCode.getKey());
167
	        PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), true);
168
	    }else{
169
	        PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), false);
170
	    }
171
        return true;
172
    }
173

    
174
	@Override
175
	protected void performDefaults() {
176
	    actualCode = null;
177
	    allowOverride = true;
178

    
179
	    nomenclaturalCode.select(0);
180

    
181
        if (isAdminPreference){
182
            allowOverrideButton.setSelection(allowOverride);
183
        }
184
        setApply(true);
185
	    super.performDefaults();
186
	}
187

    
188
	@Override
189
    protected void getValues(){
190
	    isAdminPreference = false;
191
	    ICdmRepository controller = CdmStore.getCurrentApplicationConfiguration();
192
        if (controller != null){
193
            IPreferenceService service = controller.getPreferenceService();
194
            PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.NomenclaturalCode);
195
            pref = service.find(key);
196
            if (pref != null){
197

    
198
                allowOverride = pref.isAllowOverride();
199
            }else{
200

    
201
                allowOverride = true;
202
            }
203

    
204
        }
205
        String stringValue = PreferencesUtil.getStringValue(PreferencePredicate.NomenclaturalCode.getKey(), true);
206
        Boolean overridePref = PreferencesUtil.getBooleanValue(PreferencesUtil.createOverridePreferenceString(PreferencePredicate.NomenclaturalCode.getKey()), true);
207
        override = overridePref != null? overridePref:false;
208
        if (!StringUtils.isBlank(stringValue) && override){
209
            actualCode = NomenclaturalCode.fromString(stringValue);
210
        }else{
211
            actualCode = null;
212
        }
213

    
214

    
215
	}
216

    
217
}
(32-32/52)