2 * Copyright (C) 2020 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
.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
;
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
;
37 public class CdmLightExportPreference
extends CdmPreferencePage
implements SelectionListener
{
39 protected Combo createCondensedDistributionString
;
40 protected Combo comboDistributionStringRecipe
;
42 protected Button allowOverrideCreateCondensedDistributionString
;
43 protected Button allowOverrideDistributionStringRecipe
;
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;
55 protected Control
createContents(Composite parent
) {
57 Composite composite
= createComposite(parent
);
58 // GridLayout gridLayout = new GridLayout();
59 // composite.setLayout(gridLayout);
61 Label description
= new Label(composite
, SWT
.NULL
);
62 description
.setText(Messages
.CdmLightPreference_description
);
64 createCondensedDistributionString
= createBooleanCombo(composite
, Messages
.GeneralPreference_yes
, Messages
.GeneralPreference_no
, PreferencePredicate
.CdmLightExportCondensedDistributionString
, Messages
.CdmLightPreference_distributionString
, isAdminPreference
);
66 createCondensedDistributionString
67 .setToolTipText(Messages
.CdmLightPreference_distributionString_tooltip
);
69 for (String itemLabel
: createCondensedDistributionString
.getItems()) {
70 if (isExportCondensedDistribution
== null && itemLabel
.startsWith(Messages
.Preference_Use_Default
)) {
71 createCondensedDistributionString
.select(index
);
74 if (isExportCondensedDistribution
!= null && itemLabel
.equals(Messages
.GeneralPreference_yes
) && isExportCondensedDistribution
){
75 createCondensedDistributionString
.select(index
);
78 if (isExportCondensedDistribution
!= null && itemLabel
.equals(Messages
.GeneralPreference_no
) && !isExportCondensedDistribution
){
79 createCondensedDistributionString
.select(index
);
84 createCondensedDistributionString
.addSelectionListener(this);
86 if (isAdminPreference
){
87 allowOverrideCreateCondensedDistributionString
= createAllowOverrideButton(composite
);
88 allowOverrideCreateCondensedDistributionString
.setSelection(this.preferenceIsExportCondensedDistribution
!= null?
this.preferenceIsExportCondensedDistribution
.isAllowOverride(): isAdminPreference
);
89 allowOverrideCreateCondensedDistributionString
.addSelectionListener(new SelectionAdapter() {
91 public void widgetSelected(SelectionEvent e
) {
93 // if (preferenceIsExportCondensedDistribution == null){
94 // preferenceIsExportCondensedDistribution = CdmPreference.NewTaxEditorInstance(PreferencePredicate.CdmLightExportCondensedDistributionString, PreferencePredicate.CdmLightExportCondensedDistributionString.getDefaultValue() != null ? PreferencePredicate.CdmLightExportCondensedDistributionString.getDefaultValue().toString(): null);
96 // preferenceIsExportCondensedDistribution.setAllowOverride(!preferenceIsExportCondensedDistribution.isAllowOverride());
97 isAllowOverrideIsExportCondensedDistribution
= !isAllowOverrideIsExportCondensedDistribution
;
102 comboDistributionStringRecipe
= createCombo(composite
, CondensedDistributionRecipe
.values(), PreferencePredicate
.CondensedDistributionRecipe
, "Condensed Distribution String recipe", isAdminPreference
);
104 // GridData gridData = new GridData(GridData.BEGINNING, GridData.CENTER, true, false);
105 // gridData.horizontalIndent = 9;
106 // comboDistributionStringRecipe.setLayoutData(gridData);
107 GridLayoutFactory
.fillDefaults();
109 comboDistributionStringRecipe
.addSelectionListener(this);
110 comboDistributionStringRecipe
.setEnabled((isExportCondensedDistribution
!= null && isExportCondensedDistribution
)|| (isExportCondensedDistribution
== null) && Boolean
.valueOf(preferenceIsExportCondensedDistribution
.getValue()));
113 for (String itemLabel
: comboDistributionStringRecipe
.getItems()) {
114 if (itemLabel
.startsWith(Messages
.Preference_Use_Default
) && recipe
== null){
115 comboDistributionStringRecipe
.select(index
);
118 if (itemLabel
.equals(recipe
.getLabel())) {
119 comboDistributionStringRecipe
.select(index
);
125 if (isAdminPreference
){
126 allowOverrideDistributionStringRecipe
= createAllowOverrideButton(composite
);
127 allowOverrideDistributionStringRecipe
.setSelection(this.preferenceCondensedDistribtionRecipe
!= null?
this.preferenceCondensedDistribtionRecipe
.isAllowOverride(): true);
128 allowOverrideDistributionStringRecipe
.addSelectionListener(new SelectionAdapter() {
130 public void widgetSelected(SelectionEvent e
) {
132 // if (preferenceCondensedDistribtionRecipe == null){
133 // preferenceCondensedDistribtionRecipe = CdmPreference.NewTaxEditorInstance(PreferencePredicate.CondensedDistributionRecipe, PreferencePredicate.CondensedDistributionRecipe.getDefaultValue() != null ? PreferencePredicate.CondensedDistributionRecipe.getDefaultValue().toString(): null);
135 // preferenceCondensedDistribtionRecipe.setAllowOverride(!preferenceCondensedDistribtionRecipe.isAllowOverride());
136 isAllowOverrideCondensedDistributionRecipe
= !isAllowOverrideCondensedDistributionRecipe
;
149 public void widgetSelected(SelectionEvent e
) {
151 if (e
.getSource().equals(this.comboDistributionStringRecipe
)){
152 int selIndex
= comboDistributionStringRecipe
.getSelectionIndex();
154 overrideCondensedDistributionRecipe
= false;
156 overrideCondensedDistributionRecipe
= true;
158 recipe
= (CondensedDistributionRecipe
) comboDistributionStringRecipe
.getData(comboDistributionStringRecipe
.getText());
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;
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;
175 comboDistributionStringRecipe
.setEnabled((isExportCondensedDistribution
!= null && isExportCondensedDistribution
)|| (isExportCondensedDistribution
== null) && Boolean
.valueOf(preferenceIsExportCondensedDistribution
.getValue()));
184 public boolean performOk() {
188 if(overrideExportCondensedDistribution
){
189 PreferencesUtil
.setBooleanValue(PreferencePredicate
.CdmLightExportCondensedDistributionString
.getKey(), isExportCondensedDistribution
);
190 PreferencesUtil
.setBooleanValue(PreferencesUtil
.prefOverrideKey(PreferencePredicate
.CdmLightExportCondensedDistributionString
.getKey()), overrideExportCondensedDistribution
);
192 PreferencesUtil
.setBooleanValue( PreferencesUtil
.prefOverrideKey(PreferencePredicate
.CdmLightExportCondensedDistributionString
.getKey()), overrideExportCondensedDistribution
);
194 if(overrideCondensedDistributionRecipe
){
195 PreferencesUtil
.setStringValue(PreferencePredicate
.CondensedDistributionRecipe
.getKey(), recipe
.getKey());
196 PreferencesUtil
.setBooleanValue(PreferencesUtil
.prefOverrideKey(PreferencePredicate
.CondensedDistributionRecipe
.getKey()), overrideCondensedDistributionRecipe
);
198 PreferencesUtil
.setBooleanValue( PreferencesUtil
.prefOverrideKey(PreferencePredicate
.CondensedDistributionRecipe
.getKey()), overrideCondensedDistributionRecipe
);
210 public void widgetDefaultSelected(SelectionEvent e
) {
211 // TODO Auto-generated method stub
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);
226 public void getValues() {
227 isAdminPreference
= false;
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
);
234 if (preferenceIsExportCondensedDistribution
!= null ){
235 isAllowOverrideIsExportCondensedDistribution
= preferenceIsExportCondensedDistribution
.isAllowOverride();
237 preferenceIsExportCondensedDistribution
= CdmPreference
.NewTaxEditorInstance(PreferencePredicate
.CdmLightExportCondensedDistributionString
, PreferencePredicate
.CdmLightExportCondensedDistributionString
.getDefaultValue().toString());
238 isAllowOverrideIsExportCondensedDistribution
= true;
241 overrideExportCondensedDistribution
= PreferencesUtil
.getBooleanValue(
242 PreferencesUtil
.prefOverrideKey(PreferencePredicate
.CdmLightExportCondensedDistributionString
.getKey()), true) != null? PreferencesUtil
.getBooleanValue(
243 PreferencesUtil
.prefOverrideKey(PreferencePredicate
.CdmLightExportCondensedDistributionString
.getKey()), true):false;
245 if (isAllowOverrideIsExportCondensedDistribution
){
246 if( overrideExportCondensedDistribution
){
247 isExportCondensedDistribution
= PreferencesUtil
.getBooleanValue(PreferencePredicate
.CdmLightExportCondensedDistributionString
.getKey(), true);
249 isExportCondensedDistribution
= null;
253 isExportCondensedDistribution
= preferenceIsExportCondensedDistribution
.getValue()!= null?Boolean
.valueOf(preferenceIsExportCondensedDistribution
.getValue()): null;
255 // recipe for creation of condensed string
257 key
= CdmPreference
.NewKey(PreferenceSubject
.NewDatabaseInstance(), PreferencePredicate
.CondensedDistributionRecipe
);
258 preferenceCondensedDistribtionRecipe
= controller
.getPreferenceService().find(key
);
260 if (preferenceCondensedDistribtionRecipe
!= null ){
261 isAllowOverrideCondensedDistributionRecipe
= preferenceCondensedDistribtionRecipe
.isAllowOverride();
263 preferenceCondensedDistribtionRecipe
= CdmPreference
.NewDatabaseInstance(PreferencePredicate
.CondensedDistributionRecipe
,
264 PreferencePredicate
.CondensedDistributionRecipe
.getDefaultValue() != null? PreferencePredicate
.CondensedDistributionRecipe
.getDefaultValue().toString(): null);
265 isAllowOverrideCondensedDistributionRecipe
= true;
268 overrideCondensedDistributionRecipe
= PreferencesUtil
.getBooleanValue(
269 PreferencesUtil
.prefOverrideKey(PreferencePredicate
.CondensedDistributionRecipe
.getKey()), true) != null? PreferencesUtil
.getBooleanValue(
270 PreferencesUtil
.prefOverrideKey(PreferencePredicate
.CondensedDistributionRecipe
.getKey()), true):false;
272 if (isAllowOverrideCondensedDistributionRecipe
&& overrideCondensedDistributionRecipe
){
273 String configString
= PreferencesUtil
.getStringValue(PreferencePredicate
.CondensedDistributionRecipe
.getKey(), true);
274 if (StringUtils
.isNotBlank(configString
)){
275 recipe
= CondensedDistributionRecipe
.valueOf(configString
);
278 if (preferenceCondensedDistribtionRecipe
.getValue() != null){
279 recipe
= CondensedDistributionRecipe
.valueOf(preferenceCondensedDistribtionRecipe
.getValue());