fix IllegalArgumentException in publish flag preference
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / PublishFlagLocalPreference.java
1 /**
2 * Copyright (C) 2019 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.eclipse.swt.SWT;
12 import org.eclipse.swt.custom.CLabel;
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
21 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
22 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
23 import eu.etaxonomy.taxeditor.l10n.Messages;
24 import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
25
26 /**
27 * @author k.luther
28 * @since 08.01.2019
29 *
30 */
31 public class PublishFlagLocalPreference extends CdmPreferencePage {
32
33 PublishEnum publishBehaviour;
34
35 Combo publishFlagBehaviour;
36 CdmPreference pref = null;
37
38 protected boolean allowOverride;
39
40 @Override
41 public void init() {
42 super.init();
43
44
45 }
46 /**
47 * {@inheritDoc}
48 */
49 @Override
50 protected Control createContents(Composite parent) {
51 pref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
52 if (pref == null){
53 pref = CdmPreference.NewTaxEditorInstance(EditorPreferencePredicate.DefaultBehaviourForPublishFlag, EditorPreferencePredicate.DefaultBehaviourForPublishFlag.getDefaultValue().toString());
54 }
55 allowOverride = pref.isAllowOverride();
56 Composite composite = createComposite(parent);
57 final CLabel description = new CLabel(composite, SWT.NULL);
58 if (!allowOverride){
59 description.setText(Messages.PublishFlagPreference_description_not_allowed);
60 description.setLayoutData(createTextGridData());
61 return composite;
62 }
63 PublishEnum publishFlag;
64 try{
65 publishFlag = PublishEnum.valueOf(PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey(), true));
66 }catch (IllegalArgumentException e){
67 publishFlag = PublishEnum.InheritFromParent;
68 }
69
70 description.setText(Messages.PublishFlagPreference_description);
71 description.setLayoutData(createTextGridData());
72 publishFlagBehaviour = new Combo(composite, SWT.READ_ONLY);
73
74 publishFlagBehaviour.setFont(parent.getFont());
75 for(PublishEnum display: PublishEnum.values()){
76 publishFlagBehaviour.add(display.getLabel());
77 }
78
79 publishFlagBehaviour.addSelectionListener(new SelectionListener() {
80
81 @Override
82 public void widgetSelected(SelectionEvent e) {
83 setApply(true);
84 }
85
86 @Override
87 public void widgetDefaultSelected(SelectionEvent e) {
88 // TODO Auto-generated method stub
89
90 }
91 });
92 Button allowOverrideButton = createAllowOverrideButton(composite);
93 allowOverrideButton.setText(Messages.GeneralPreference_override);
94 allowOverrideButton.setSelection(allowOverride);
95 allowOverrideButton.addSelectionListener(new SelectionAdapter(){
96 @Override
97 public void widgetSelected(SelectionEvent e) {
98 allowOverride = allowOverrideButton.getSelection();
99 setApply(true);
100 }
101 });
102
103 int index = 0;
104
105
106 for (String itemLabel : publishFlagBehaviour.getItems()){
107 if (itemLabel.equalsIgnoreCase(publishFlag.getLabel())){
108 publishFlagBehaviour.select(index);
109 break;
110 }
111 index++;
112 }
113
114 return composite;
115
116 }
117
118
119 @Override
120 public boolean performOk() {
121 String text = publishFlagBehaviour.getText();
122 for (PublishEnum display: PublishEnum.values()){
123 if (display.getLabel().equals(text)){
124 text = display.getKey();
125 break;
126 }
127 }
128 PreferencesUtil.setStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey(), text);
129 PreferencesUtil.setBooleanValue(PreferencesUtil.prefOverrideKey(EditorPreferencePredicate.DefaultBehaviourForPublishFlag.getKey()), allowOverride);
130 return true;
131 }
132
133
134 }