8b83dac132353eebde7c9b55a6ed51fcd646b37a
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / CdmLightExportPreference.java
1 /**
2 * Copyright (C) 2020 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.jface.layout.GridLayoutFactory;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.SelectionAdapter;
15 import org.eclipse.swt.events.SelectionEvent;
16 import org.eclipse.swt.events.SelectionListener;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Combo;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22
23 import eu.etaxonomy.cdm.api.application.ICdmRepository;
24 import eu.etaxonomy.cdm.ext.geo.CondensedDistributionRecipe;
25 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
26 import eu.etaxonomy.cdm.model.metadata.CdmPreference.PrefKey;
27 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
28 import eu.etaxonomy.cdm.model.metadata.PreferenceSubject;
29 import eu.etaxonomy.taxeditor.l10n.Messages;
30 import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
31 import eu.etaxonomy.taxeditor.store.CdmStore;
32
33 /**
34 * @author k.luther
35 * @since Nov 12, 2020
36 */
37 public class CdmLightExportPreference extends CdmPreferencePage implements SelectionListener {
38
39 protected Combo createCondensedDistributionString;
40 protected Combo comboDistributionStringRecipe;
41
42 protected Button allowOverrideCreateCondensedDistributionString;
43 protected Button allowOverrideDistributionStringRecipe;
44
45 protected CdmPreference preferenceIsExportCondensedDistribution = null;
46 protected boolean isAllowOverrideIsExportCondensedDistribution = false;
47 protected boolean overrideExportCondensedDistribution = false;
48 protected CdmPreference preferenceCondensedDistribtionRecipe = null;
49 protected boolean isAllowOverrideCondensedDistributionRecipe = false;
50 protected boolean overrideCondensedDistributionRecipe = false;
51 protected Boolean isExportCondensedDistribution = null;
52 protected CondensedDistributionRecipe recipe = null;
53
54 @Override
55 protected Control createContents(Composite parent) {
56 getValues();
57 Composite composite = createComposite(parent);
58 // GridLayout gridLayout = new GridLayout();
59 // composite.setLayout(gridLayout);
60
61 Label description = new Label(composite, SWT.NULL);
62 description.setText(Messages.CdmLightPreference_description);
63
64 createCondensedDistributionString = createBooleanCombo(composite, Messages.GeneralPreference_yes, Messages.GeneralPreference_no, PreferencePredicate.CdmLightExportCondensedDistributionString, Messages.CdmLightPreference_distributionString, isAdminPreference);
65
66 createCondensedDistributionString
67 .setToolTipText(Messages.CdmLightPreference_distributionString_tooltip);
68 int index = 0;
69 for (String itemLabel : createCondensedDistributionString.getItems()) {
70 if (isExportCondensedDistribution == null && itemLabel.startsWith(Messages.Preference_Use_Default)) {
71 createCondensedDistributionString.select(index);
72 break;
73 }
74 if (isExportCondensedDistribution != null && itemLabel.equals(Messages.GeneralPreference_yes) && isExportCondensedDistribution){
75 createCondensedDistributionString.select(index);
76 break;
77 }
78 if (isExportCondensedDistribution != null && itemLabel.equals(Messages.GeneralPreference_no) && !isExportCondensedDistribution){
79 createCondensedDistributionString.select(index);
80 break;
81 }
82 index++;
83 }
84 createCondensedDistributionString.addSelectionListener(this);
85
86 if (isAdminPreference){
87 allowOverrideCreateCondensedDistributionString = createAllowOverrideButton(composite);
88 allowOverrideCreateCondensedDistributionString.setSelection(this.preferenceIsExportCondensedDistribution != null? this.preferenceIsExportCondensedDistribution.isAllowOverride(): isAdminPreference);
89 allowOverrideCreateCondensedDistributionString.addSelectionListener(new SelectionAdapter() {
90 @Override
91 public void widgetSelected(SelectionEvent e) {
92 setApply(true);
93 // if (preferenceIsExportCondensedDistribution == null){
94 // preferenceIsExportCondensedDistribution = CdmPreference.NewTaxEditorInstance(PreferencePredicate.CdmLightExportCondensedDistributionString, PreferencePredicate.CdmLightExportCondensedDistributionString.getDefaultValue() != null ? PreferencePredicate.CdmLightExportCondensedDistributionString.getDefaultValue().toString(): null);
95 // }
96 // preferenceIsExportCondensedDistribution.setAllowOverride(!preferenceIsExportCondensedDistribution.isAllowOverride());
97 isAllowOverrideIsExportCondensedDistribution = !isAllowOverrideIsExportCondensedDistribution;
98 }
99 });
100 }
101
102 comboDistributionStringRecipe = createCombo(composite, CondensedDistributionRecipe.values(), PreferencePredicate.CondensedDistributionRecipe, "Condensed Distribution String recipe", isAdminPreference);
103
104 // GridData gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
105 // gridData.horizontalIndent = 9;
106 // comboDistributionStringRecipe.setLayoutData(gridData);
107 GridLayoutFactory.fillDefaults();
108
109 comboDistributionStringRecipe.addSelectionListener(this);
110 comboDistributionStringRecipe.setEnabled((isExportCondensedDistribution != null && isExportCondensedDistribution )|| (isExportCondensedDistribution == null) && Boolean.valueOf(preferenceIsExportCondensedDistribution.getValue()));
111 index = 0;
112
113 for (String itemLabel : comboDistributionStringRecipe.getItems()) {
114 if (itemLabel.startsWith(Messages.Preference_Use_Default) && recipe == null){
115 comboDistributionStringRecipe.select(index);
116 break;
117 }
118 if (itemLabel.equals(recipe.getLabel())) {
119 comboDistributionStringRecipe.select(index);
120 break;
121 }
122
123 index++;
124 }
125 if (isAdminPreference){
126 allowOverrideDistributionStringRecipe = createAllowOverrideButton(composite);
127 allowOverrideDistributionStringRecipe.setSelection(this.preferenceCondensedDistribtionRecipe != null? this.preferenceCondensedDistribtionRecipe.isAllowOverride(): true);
128 allowOverrideDistributionStringRecipe.addSelectionListener(new SelectionAdapter() {
129 @Override
130 public void widgetSelected(SelectionEvent e) {
131 setApply(true);
132 // if (preferenceCondensedDistribtionRecipe == null){
133 // preferenceCondensedDistribtionRecipe = CdmPreference.NewTaxEditorInstance(PreferencePredicate.CondensedDistributionRecipe, PreferencePredicate.CondensedDistributionRecipe.getDefaultValue() != null ? PreferencePredicate.CondensedDistributionRecipe.getDefaultValue().toString(): null);
134 // }
135 // preferenceCondensedDistribtionRecipe.setAllowOverride(!preferenceCondensedDistribtionRecipe.isAllowOverride());
136 isAllowOverrideCondensedDistributionRecipe = !isAllowOverrideCondensedDistributionRecipe;
137 }
138 });
139 }
140
141 return composite;
142 }
143
144
145
146
147
148 @Override
149 public void widgetSelected(SelectionEvent e) {
150 setApply(true);
151 if (e.getSource().equals(this.comboDistributionStringRecipe)){
152 int selIndex = comboDistributionStringRecipe.getSelectionIndex();
153 if (selIndex == 0){
154 overrideCondensedDistributionRecipe = false;
155 }else{
156 overrideCondensedDistributionRecipe = true;
157 }
158 recipe = (CondensedDistributionRecipe) comboDistributionStringRecipe.getData(comboDistributionStringRecipe.getText());
159
160 }
161 if (e.getSource().equals(this.createCondensedDistributionString)){
162 String text = createCondensedDistributionString.getText();
163 if(text.startsWith(Messages.Preference_Use_Default)){
164 isExportCondensedDistribution = null;
165 overrideExportCondensedDistribution = false;
166 }
167
168 if (text.equals(Messages.GeneralPreference_yes)){
169 isExportCondensedDistribution = true;
170 overrideExportCondensedDistribution = true;
171 }else if (text.equals(Messages.GeneralPreference_no)){
172 isExportCondensedDistribution = false;
173 overrideExportCondensedDistribution = true;
174 }
175 comboDistributionStringRecipe.setEnabled((isExportCondensedDistribution != null && isExportCondensedDistribution )|| (isExportCondensedDistribution == null) && Boolean.valueOf(preferenceIsExportCondensedDistribution.getValue()));
176 }
177
178
179
180 }
181
182
183 @Override
184 public boolean performOk() {
185 if (!isApply()){
186 return true;
187 }
188 if(overrideExportCondensedDistribution){
189 PreferencesUtil.setBooleanValue(PreferencePredicate.CdmLightExportCondensedDistributionString.getKey(), isExportCondensedDistribution);
190 PreferencesUtil.setBooleanValue(PreferencesUtil.prefOverrideKey(PreferencePredicate.CdmLightExportCondensedDistributionString.getKey()), overrideExportCondensedDistribution);
191 }else{
192 PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.CdmLightExportCondensedDistributionString.getKey()), overrideExportCondensedDistribution);
193 }
194 if(overrideCondensedDistributionRecipe){
195 PreferencesUtil.setStringValue(PreferencePredicate.CondensedDistributionRecipe.getKey(), recipe.getKey());
196 PreferencesUtil.setBooleanValue(PreferencesUtil.prefOverrideKey(PreferencePredicate.CondensedDistributionRecipe.getKey()), overrideCondensedDistributionRecipe);
197 }else{
198 PreferencesUtil.setBooleanValue( PreferencesUtil.prefOverrideKey(PreferencePredicate.CondensedDistributionRecipe.getKey()), overrideCondensedDistributionRecipe);
199 }
200
201
202
203 return true;
204 }
205
206 /**
207 * {@inheritDoc}
208 */
209 @Override
210 public void widgetDefaultSelected(SelectionEvent e) {
211 // TODO Auto-generated method stub
212
213 }
214
215 @Override
216 protected void performDefaults() {
217 isExportCondensedDistribution = Boolean.valueOf(PreferencePredicate.CdmLightExportCondensedDistributionString.getDefaultValue().toString());
218 createCondensedDistributionString.select(0);
219 recipe = PreferencePredicate.CondensedDistributionRecipe.getDefaultValue() != null? CondensedDistributionRecipe.valueOf(PreferencePredicate.CondensedDistributionRecipe.getDefaultValue().toString()): null;
220 comboDistributionStringRecipe.select(0);
221 setApply(true);
222 }
223
224
225 @Override
226 public void getValues() {
227 isAdminPreference = false;
228
229 ICdmRepository controller = CdmStore.getCurrentApplicationConfiguration();
230 // general export of condensed string
231 PrefKey key = CdmPreference.NewKey(PreferenceSubject.NewTaxEditorInstance(), PreferencePredicate.CdmLightExportCondensedDistributionString);
232 preferenceIsExportCondensedDistribution = controller.getPreferenceService().find(key);
233
234 if (preferenceIsExportCondensedDistribution != null ){
235 isAllowOverrideIsExportCondensedDistribution = preferenceIsExportCondensedDistribution.isAllowOverride();
236 }else{
237 preferenceIsExportCondensedDistribution = CdmPreference.NewTaxEditorInstance(PreferencePredicate.CdmLightExportCondensedDistributionString, PreferencePredicate.CdmLightExportCondensedDistributionString.getDefaultValue().toString());
238 isAllowOverrideIsExportCondensedDistribution = true;
239 }
240
241 overrideExportCondensedDistribution = PreferencesUtil.getBooleanValue(
242 PreferencesUtil.prefOverrideKey(PreferencePredicate.CdmLightExportCondensedDistributionString.getKey()), true) != null? PreferencesUtil.getBooleanValue(
243 PreferencesUtil.prefOverrideKey(PreferencePredicate.CdmLightExportCondensedDistributionString.getKey()), true):false;
244
245 if (isAllowOverrideIsExportCondensedDistribution){
246 if( overrideExportCondensedDistribution){
247 isExportCondensedDistribution = PreferencesUtil.getBooleanValue(PreferencePredicate.CdmLightExportCondensedDistributionString.getKey(), true);
248 }else{
249 isExportCondensedDistribution = null;
250 }
251
252 }else{
253 isExportCondensedDistribution = preferenceIsExportCondensedDistribution.getValue()!= null?Boolean.valueOf(preferenceIsExportCondensedDistribution.getValue()): null;
254 }
255 // recipe for creation of condensed string
256
257 key = CdmPreference.NewKey(PreferenceSubject.NewDatabaseInstance(), PreferencePredicate.CondensedDistributionRecipe);
258 preferenceCondensedDistribtionRecipe = controller.getPreferenceService().find(key);
259
260 if (preferenceCondensedDistribtionRecipe != null ){
261 isAllowOverrideCondensedDistributionRecipe = preferenceCondensedDistribtionRecipe.isAllowOverride();
262 }else{
263 preferenceCondensedDistribtionRecipe = CdmPreference.NewDatabaseInstance(PreferencePredicate.CondensedDistributionRecipe,
264 PreferencePredicate.CondensedDistributionRecipe.getDefaultValue() != null? PreferencePredicate.CondensedDistributionRecipe.getDefaultValue().toString(): null);
265 isAllowOverrideCondensedDistributionRecipe = true;
266 }
267
268 overrideCondensedDistributionRecipe = PreferencesUtil.getBooleanValue(
269 PreferencesUtil.prefOverrideKey(PreferencePredicate.CondensedDistributionRecipe.getKey()), true) != null? PreferencesUtil.getBooleanValue(
270 PreferencesUtil.prefOverrideKey(PreferencePredicate.CondensedDistributionRecipe.getKey()), true):false;
271
272 if (isAllowOverrideCondensedDistributionRecipe && overrideCondensedDistributionRecipe){
273 String configString = PreferencesUtil.getStringValue(PreferencePredicate.CondensedDistributionRecipe.getKey(), true);
274 if (StringUtils.isNotBlank(configString)){
275 recipe = CondensedDistributionRecipe.valueOf(configString);
276 }
277 }else{
278 if (preferenceCondensedDistribtionRecipe.getValue() != null){
279 recipe = CondensedDistributionRecipe.valueOf(preferenceCondensedDistribtionRecipe.getValue());
280 }
281 }
282 }
283 }