2 * Copyright (C) 2021 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.preference
;
11 import org
.apache
.commons
.lang
.StringUtils
;
12 import org
.eclipse
.swt
.SWT
;
13 import org
.eclipse
.swt
.events
.SelectionAdapter
;
14 import org
.eclipse
.swt
.events
.SelectionEvent
;
15 import org
.eclipse
.swt
.events
.SelectionListener
;
16 import org
.eclipse
.swt
.widgets
.Button
;
17 import org
.eclipse
.swt
.widgets
.Combo
;
18 import org
.eclipse
.swt
.widgets
.Composite
;
19 import org
.eclipse
.swt
.widgets
.Control
;
20 import org
.eclipse
.swt
.widgets
.Label
;
22 import eu
.etaxonomy
.cdm
.model
.metadata
.CdmPreference
;
23 import eu
.etaxonomy
.cdm
.model
.metadata
.EnabledComputedDescription
;
24 import eu
.etaxonomy
.cdm
.model
.metadata
.PreferencePredicate
;
25 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
26 import eu
.etaxonomy
.taxeditor
.preference
.menu
.CdmPreferencePage
;
32 public class EnableComputedDescriptions
extends CdmPreferencePage
implements SelectionListener
{
34 protected CdmPreference computedDescriptionsHandling
= null;
35 protected EnabledComputedDescription selectedHandling
= null;
36 protected boolean allowOverride
;
37 protected boolean isOverride
= true;
39 protected Combo comboSelectHandling
;
40 Button allowOverrideHandlingButton
;
44 protected Control
createContents(Composite parent
) {
47 Composite composite
= createComposite(parent
);
48 Label description
= new Label(composite
, SWT
.NULL
);
49 description
.setText(Messages
.Computed_factualData_handling_description
);
51 comboSelectHandling
= createCombo(composite
, EnabledComputedDescription
.values(), PreferencePredicate
.EnableComputedDescription
, Messages
.FactualData_EnableComputedFactualData
, isAdminPreference
);
52 comboSelectHandling
.addSelectionListener(this);
53 if (this.isAdminPreference
){
54 allowOverrideHandlingButton
= createAllowOverrideButton(composite
);
55 allowOverrideHandlingButton
.setSelection(allowOverride
);
56 allowOverrideHandlingButton
.addSelectionListener(new SelectionAdapter(){
58 public void widgetSelected(SelectionEvent e
) {
59 allowOverride
= !allowOverride
;
66 for (String itemLabel
: comboSelectHandling
.getItems()) {
67 if (itemLabel
.startsWith(Messages
.Preference_Use_Default
) && ((!isOverride
&& !isAdminPreference
) || selectedHandling
== null)){
68 comboSelectHandling
.select(index
);
71 if (itemLabel
.equals(selectedHandling
.getLabel())) {
72 comboSelectHandling
.select(index
);
79 if (!isAdminPreference
){
80 comboSelectHandling
.setEnabled(allowOverride
);
88 public void getValues(){
89 // get modifier values
90 computedDescriptionsHandling
= PreferencesUtil
.getPreferenceFromDB(PreferencePredicate
.EnableComputedDescription
);
91 if (computedDescriptionsHandling
== null){
92 computedDescriptionsHandling
= CdmPreference
.NewTaxEditorInstance(PreferencePredicate
.EnableComputedDescription
, ((EnabledComputedDescription
)PreferencePredicate
.EnableComputedDescription
.getDefaultValue()).getKey());
94 allowOverride
= computedDescriptionsHandling
.isAllowOverride();
98 isOverride
= PreferencesUtil
.getBooleanValue(PreferencesUtil
.prefOverrideKey(PreferencePredicate
.EnableComputedDescription
.getKey()), true);
99 if (allowOverride
&& isOverride
){
100 String localHandlingPref
= PreferencesUtil
.getStringValue(PreferencePredicate
.EnableComputedDescription
.getKey(), true);
101 if (StringUtils
.isNotBlank(localHandlingPref
)){
102 selectedHandling
= EnabledComputedDescription
.byKey(localHandlingPref
);
106 if (computedDescriptionsHandling
.getValue() != null){
108 selectedHandling
= EnabledComputedDescription
.byKey(computedDescriptionsHandling
.getValue());
109 }catch (IllegalArgumentException e
){
110 selectedHandling
= (EnabledComputedDescription
) PreferencePredicate
.EnableComputedDescription
.getDefaultValue();
121 protected void performDefaults() {
122 selectedHandling
= null;
123 allowOverride
= true;
125 comboSelectHandling
.select(0);
126 if (isAdminPreference
){
127 allowOverrideHandlingButton
.setSelection(allowOverride
);
130 super.performDefaults();
135 public void widgetSelected(SelectionEvent e
) {
137 if (e
.getSource().equals(this.comboSelectHandling
)){
138 int selIndex
= comboSelectHandling
.getSelectionIndex();
144 selectedHandling
= (EnabledComputedDescription
) comboSelectHandling
.getData(comboSelectHandling
.getText());
152 public void widgetDefaultSelected(SelectionEvent e
) {
153 // TODO Auto-generated method stub
158 public boolean performOk() {
164 PreferencesUtil
.setStringValue(PreferencePredicate
.EnableComputedDescription
.getKey(), this.selectedHandling
.getKey());
165 PreferencesUtil
.setBooleanValue(PreferencesUtil
.prefOverrideKey(PreferencePredicate
.EnableComputedDescription
.getKey()), isOverride
);
167 PreferencesUtil
.setBooleanValue( PreferencesUtil
.prefOverrideKey(PreferencePredicate
.EnableComputedDescription
.getKey()), isOverride
);