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.cdm.model.metadata.PublishEnum;
|
24
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
25
|
import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
|
26
|
|
27
|
/**
|
28
|
* @author k.luther
|
29
|
* @since 08.01.2019
|
30
|
*
|
31
|
*/
|
32
|
public class PublishFlagLocalPreference extends CdmPreferencePage {
|
33
|
|
34
|
protected PublishEnum publishBehaviour;
|
35
|
|
36
|
protected Combo publishFlagBehaviour;
|
37
|
protected CdmPreference pref = null;
|
38
|
Button allowOverrideButton;
|
39
|
|
40
|
protected boolean allowOverride;
|
41
|
private boolean override = true;
|
42
|
|
43
|
|
44
|
@Override
|
45
|
public void init() {
|
46
|
super.init();
|
47
|
|
48
|
|
49
|
}
|
50
|
/**
|
51
|
* {@inheritDoc}
|
52
|
*/
|
53
|
@Override
|
54
|
protected Control createContents(Composite parent) {
|
55
|
getValues();
|
56
|
pref = PreferencesUtil.getPreferenceFromDB(PreferencePredicate.DefaultBehaviourForPublishFlag);
|
57
|
if (pref == null){
|
58
|
pref = CdmPreference.NewTaxEditorInstance(PreferencePredicate.DefaultBehaviourForPublishFlag, PreferencePredicate.DefaultBehaviourForPublishFlag.getDefaultValue().toString());
|
59
|
}
|
60
|
allowOverride = pref.isAllowOverride();
|
61
|
Composite composite = createComposite(parent);
|
62
|
|
63
|
if (!allowOverride && !isAdminPreference){
|
64
|
final CLabel description = new CLabel(composite, SWT.NULL);
|
65
|
description.setText(Messages.PublishFlagPreference_description_not_allowed);
|
66
|
description.setLayoutData(createTextGridData());
|
67
|
return composite;
|
68
|
}
|
69
|
|
70
|
publishFlagBehaviour = createCombo(composite, PublishEnum.values(), PreferencePredicate.DefaultBehaviourForPublishFlag, Messages.PublishFlagPreference_description, isAdminPreference);
|
71
|
publishFlagBehaviour.addSelectionListener(new SelectionListener() {
|
72
|
|
73
|
@Override
|
74
|
public void widgetSelected(SelectionEvent e) {
|
75
|
setApply(true);
|
76
|
if (!publishFlagBehaviour.getText().startsWith(Messages.Preference_Use_Default)){
|
77
|
publishBehaviour = (PublishEnum)publishFlagBehaviour.getData(publishFlagBehaviour.getText());
|
78
|
}else{
|
79
|
publishBehaviour = null;
|
80
|
}
|
81
|
|
82
|
}
|
83
|
|
84
|
@Override
|
85
|
public void widgetDefaultSelected(SelectionEvent e) {
|
86
|
// TODO Auto-generated method stub
|
87
|
|
88
|
}
|
89
|
});
|
90
|
if (this.isAdminPreference){
|
91
|
allowOverrideButton = createAllowOverrideButton(composite);
|
92
|
allowOverrideButton.setSelection(allowOverride);
|
93
|
allowOverrideButton.addSelectionListener(new SelectionAdapter(){
|
94
|
@Override
|
95
|
public void widgetSelected(SelectionEvent e) {
|
96
|
allowOverride = !allowOverride;
|
97
|
setApply(true);
|
98
|
}
|
99
|
});
|
100
|
}
|
101
|
int index = 0;
|
102
|
|
103
|
if(publishBehaviour==null){
|
104
|
publishFlagBehaviour.select(0);
|
105
|
}
|
106
|
else{
|
107
|
for (String itemLabel : publishFlagBehaviour.getItems()){
|
108
|
if (itemLabel.startsWith(publishBehaviour.getLabel())){
|
109
|
publishFlagBehaviour.select(index);
|
110
|
break;
|
111
|
}
|
112
|
index++;
|
113
|
}
|
114
|
}
|
115
|
|
116
|
return composite;
|
117
|
|
118
|
}
|
119
|
|
120
|
|
121
|
@Override
|
122
|
public boolean performOk() {
|
123
|
if (isApply()){
|
124
|
if (publishBehaviour != null){
|
125
|
String text = publishFlagBehaviour.getText();
|
126
|
for (PublishEnum display: PublishEnum.values()){
|
127
|
if (text.startsWith(display.getLabel())){
|
128
|
text = display.getKey();
|
129
|
break;
|
130
|
}
|
131
|
}
|
132
|
PreferencesUtil.setStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey(), text);
|
133
|
if (pref == null || !pref.getValue().equals(text)){
|
134
|
PreferencesUtil.setBooleanValue(PreferencesUtil.prefOverrideKey(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey()), true);
|
135
|
}
|
136
|
}else{
|
137
|
PreferencesUtil.setBooleanValue(PreferencesUtil.prefOverrideKey(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey()), false);
|
138
|
}
|
139
|
}
|
140
|
return true;
|
141
|
}
|
142
|
|
143
|
@Override
|
144
|
public void getValues(){
|
145
|
publishBehaviour = null;
|
146
|
try{
|
147
|
String publishString = PreferencesUtil.getStringValue(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey(), true);
|
148
|
|
149
|
if (publishString != null){
|
150
|
publishBehaviour = PublishEnum.valueOf(publishString);
|
151
|
}
|
152
|
}catch (IllegalArgumentException e){
|
153
|
publishBehaviour = PublishEnum.InheritFromParent;
|
154
|
}
|
155
|
|
156
|
override = PreferencesUtil.getBooleanValue(PreferencesUtil.prefOverrideKey(PreferencePredicate.DefaultBehaviourForPublishFlag.getKey()));
|
157
|
if (!override){
|
158
|
publishBehaviour = null;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
@Override
|
163
|
protected void performDefaults() {
|
164
|
publishBehaviour = null;
|
165
|
allowOverride = true;
|
166
|
int index = 0;
|
167
|
|
168
|
for (String itemLabel : publishFlagBehaviour.getItems()){
|
169
|
if (itemLabel.startsWith(Messages.Preference_Use_Default)){
|
170
|
publishFlagBehaviour.select(index);
|
171
|
break;
|
172
|
}
|
173
|
index++;
|
174
|
}
|
175
|
if (isAdminPreference){
|
176
|
allowOverrideButton.setSelection(allowOverride);
|
177
|
}
|
178
|
setApply(true);
|
179
|
super.performDefaults();
|
180
|
}
|
181
|
|
182
|
|
183
|
|
184
|
}
|