ref #9630: make computed descriptions handling configurable
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / EnableComputedDescriptions.java
1 /**
2 * Copyright (C) 2021 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 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;
21
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;
27
28 /**
29 * @author k.luther
30 * @since May 26, 2021
31 */
32 public class EnableComputedDescriptions extends CdmPreferencePage implements SelectionListener{
33
34 protected CdmPreference computedDescriptionsHandling = null;
35 protected EnabledComputedDescription selectedHandling = null;
36 protected boolean allowOverride;
37 protected boolean isOverride = true;
38
39 protected Combo comboSelectHandling;
40 Button allowOverrideHandlingButton;
41
42
43 @Override
44 protected Control createContents(Composite parent) {
45 getValues();
46
47 Composite composite = createComposite(parent);
48 Label description = new Label(composite, SWT.NULL);
49 description.setText(Messages.Computed_factualData_handling_description);
50
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(){
57 @Override
58 public void widgetSelected(SelectionEvent e) {
59 allowOverride = !allowOverride;
60 setApply(true);
61 }
62 });
63 }
64 int index = 0;
65
66 for (String itemLabel : comboSelectHandling.getItems()) {
67 if (itemLabel.startsWith(Messages.Preference_Use_Default) && ((!isOverride && !isAdminPreference) || selectedHandling == null)){
68 comboSelectHandling.select(index);
69 break;
70 }
71 if (itemLabel.equals(selectedHandling.getLabel())) {
72 comboSelectHandling.select(index);
73 break;
74 }
75
76 index++;
77 }
78
79 if (!isAdminPreference){
80 comboSelectHandling.setEnabled(allowOverride);
81 }
82
83 return composite;
84 }
85
86
87 @Override
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());
93 }
94 allowOverride = computedDescriptionsHandling.isAllowOverride();
95
96
97
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);
103 }
104
105 }else{
106 if (computedDescriptionsHandling.getValue() != null){
107 try{
108 selectedHandling = EnabledComputedDescription.byKey(computedDescriptionsHandling.getValue());
109 }catch (IllegalArgumentException e){
110 selectedHandling = (EnabledComputedDescription) PreferencePredicate.EnableComputedDescription.getDefaultValue();
111 }
112 }
113 }
114
115
116
117
118 }
119
120 @Override
121 protected void performDefaults() {
122 selectedHandling = null;
123 allowOverride = true;
124
125 comboSelectHandling.select(0);
126 if (isAdminPreference){
127 allowOverrideHandlingButton.setSelection(allowOverride);
128 }
129 setApply(true);
130 super.performDefaults();
131 }
132
133
134 @Override
135 public void widgetSelected(SelectionEvent e) {
136 setApply(true);
137 if (e.getSource().equals(this.comboSelectHandling)){
138 int selIndex = comboSelectHandling.getSelectionIndex();
139 if (selIndex == 0){
140 isOverride = false;
141 }else{
142 isOverride = true;
143 }
144 selectedHandling = (EnabledComputedDescription) comboSelectHandling.getData(comboSelectHandling.getText());
145
146 }
147
148 }
149
150
151 @Override
152 public void widgetDefaultSelected(SelectionEvent e) {
153 // TODO Auto-generated method stub
154
155 }
156
157 @Override
158 public boolean performOk() {
159 if (!isApply()){
160 return true;
161 }
162
163 if(isOverride){
164 PreferencesUtil.setStringValue(PreferencePredicate.EnableComputedDescription.getKey(), this.selectedHandling.getKey());
165 PreferencesUtil.setBooleanValue(PreferencesUtil.prefOverrideKey(PreferencePredicate.EnableComputedDescription.getKey()), isOverride);
166 }else{
167 PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.EnableComputedDescription.getKey()), isOverride);
168 }
169 return true;
170 }
171
172
173
174 }