1
|
/**
|
2
|
* Copyright (C) 2015 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.menu;
|
10
|
|
11
|
import org.eclipse.jface.preference.PreferencePage;
|
12
|
import org.eclipse.jface.resource.ImageDescriptor;
|
13
|
import org.eclipse.swt.SWT;
|
14
|
import org.eclipse.swt.custom.CLabel;
|
15
|
import org.eclipse.swt.layout.GridData;
|
16
|
import org.eclipse.swt.layout.GridLayout;
|
17
|
import org.eclipse.swt.widgets.Button;
|
18
|
import org.eclipse.swt.widgets.Combo;
|
19
|
import org.eclipse.swt.widgets.Composite;
|
20
|
|
21
|
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
|
22
|
import eu.etaxonomy.cdm.model.metadata.IKeyLabel;
|
23
|
import eu.etaxonomy.cdm.model.metadata.IPreferencePredicate;
|
24
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
25
|
import eu.etaxonomy.taxeditor.preference.IE4PreferencePage;
|
26
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
27
|
|
28
|
/**
|
29
|
* @author cmathew
|
30
|
* @date 30 Jul 2015
|
31
|
*
|
32
|
*/
|
33
|
public abstract class CdmPreferencePage extends PreferencePage implements IE4PreferencePage {
|
34
|
|
35
|
|
36
|
private boolean isDefaultButtonActivated = true;
|
37
|
|
38
|
protected boolean isAdminPreference = false;
|
39
|
|
40
|
private boolean isApply;
|
41
|
|
42
|
protected final String SHOW = Messages.GeneralPreference_yes;
|
43
|
protected final String DO_NOT_SHOW = Messages.GeneralPreference_no;
|
44
|
|
45
|
public CdmPreferencePage() {
|
46
|
this(null, null);
|
47
|
}
|
48
|
|
49
|
public CdmPreferencePage(String title) {
|
50
|
this(title, null);
|
51
|
}
|
52
|
|
53
|
public CdmPreferencePage(String title, ImageDescriptor image) {
|
54
|
super(title, image);
|
55
|
|
56
|
init();
|
57
|
}
|
58
|
|
59
|
|
60
|
|
61
|
public void init() {
|
62
|
setPreferenceStore(PreferencesUtil.getPreferenceStore());
|
63
|
}
|
64
|
|
65
|
@Override
|
66
|
public void createControl(Composite parent){
|
67
|
super.createControl(parent);
|
68
|
if (getApplyButton() != null){
|
69
|
this.getApplyButton().setEnabled(false);
|
70
|
}
|
71
|
if (getDefaultsButton() != null){
|
72
|
this.getDefaultsButton().setEnabled(isDefaultButtonActivated);
|
73
|
}
|
74
|
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
public boolean performOk() {
|
79
|
if (isValid()){
|
80
|
return super.performOk();
|
81
|
}return false;
|
82
|
}
|
83
|
|
84
|
@Override
|
85
|
public boolean performCancel() {
|
86
|
|
87
|
return super.performCancel();
|
88
|
}
|
89
|
|
90
|
@Override
|
91
|
public void performApply() {
|
92
|
setApply(true);
|
93
|
super.performApply();
|
94
|
setApply(false);
|
95
|
}
|
96
|
|
97
|
protected Composite createComposite(Composite composite) {
|
98
|
Composite activateComp = new Composite(composite, SWT.NULL);
|
99
|
GridLayout layout = new GridLayout();
|
100
|
layout.numColumns = 2;
|
101
|
|
102
|
activateComp.setLayout(layout);
|
103
|
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
|
104
|
|
105
|
activateComp.setLayoutData(gridData);
|
106
|
|
107
|
return activateComp;
|
108
|
}
|
109
|
|
110
|
|
111
|
protected GridData createTextGridData() {
|
112
|
GridData sepGrid = new GridData(GridData.FILL_HORIZONTAL);
|
113
|
sepGrid.horizontalSpan = 2;
|
114
|
return sepGrid;
|
115
|
}
|
116
|
|
117
|
protected void getValues(){
|
118
|
|
119
|
}
|
120
|
|
121
|
|
122
|
protected Button createAllowOverrideButton(Composite activateComp) {
|
123
|
final Button allowOverrideCheckButton = new Button(activateComp, SWT.CHECK);
|
124
|
String overrideString;
|
125
|
if (isAdminPreference){
|
126
|
overrideString = Messages.GeneralPreference_allowOverride;
|
127
|
}else{
|
128
|
overrideString = Messages.GeneralPreference_override;
|
129
|
}
|
130
|
allowOverrideCheckButton.setText(overrideString);
|
131
|
GridData grid = new GridData();
|
132
|
grid.horizontalAlignment = SWT.END;
|
133
|
allowOverrideCheckButton.setLayoutData(grid);
|
134
|
return allowOverrideCheckButton;
|
135
|
}
|
136
|
|
137
|
@Override
|
138
|
public boolean isApply() {
|
139
|
return isApply;
|
140
|
}
|
141
|
|
142
|
public void setApply(boolean isApply) {
|
143
|
this.isApply = isApply;
|
144
|
if (this.getApplyButton() != null){
|
145
|
this.getApplyButton().setEnabled(this.isApply);
|
146
|
}
|
147
|
}
|
148
|
|
149
|
public boolean isDefaultButtonActivated() {
|
150
|
return isDefaultButtonActivated;
|
151
|
}
|
152
|
|
153
|
public void setDefaultButtonActivated(boolean isDefaultButtonActivated) {
|
154
|
this.isDefaultButtonActivated = isDefaultButtonActivated;
|
155
|
}
|
156
|
|
157
|
public Combo createBooleanCombo(Composite parent, String textTrue, String textFalse, IPreferencePredicate predicate, String labelText, boolean isAdminPage) {
|
158
|
final CLabel rankDescription = new CLabel(parent, SWT.NULL);
|
159
|
rankDescription.setText(labelText);
|
160
|
GridData textGrid = createTextGridData();
|
161
|
textGrid.verticalSpan = 2;
|
162
|
rankDescription.setLayoutData(textGrid);
|
163
|
|
164
|
Combo booleanCombo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
|
165
|
|
166
|
CdmPreference dbPref = PreferencesUtil.getPreferenceFromDB(predicate);
|
167
|
boolean defaultValue = true;
|
168
|
if (dbPref != null && !isAdminPage){
|
169
|
defaultValue = Boolean.valueOf(dbPref.getValue());
|
170
|
}else{
|
171
|
try{
|
172
|
defaultValue = (Boolean)predicate.getDefaultValue();
|
173
|
}catch(NullPointerException e){
|
174
|
defaultValue = false;
|
175
|
}
|
176
|
|
177
|
}
|
178
|
String defaultString;
|
179
|
if (defaultValue){
|
180
|
defaultString = " ("+textTrue+")";
|
181
|
}else{
|
182
|
defaultString = " ("+textFalse+")";
|
183
|
}
|
184
|
booleanCombo.add(Messages.Preference_Use_Default + defaultString);
|
185
|
booleanCombo.add(textTrue);
|
186
|
booleanCombo.add(textFalse);
|
187
|
|
188
|
return booleanCombo;
|
189
|
|
190
|
}
|
191
|
|
192
|
|
193
|
public Combo createCombo(Composite parent, IKeyLabel[] enumValues, IPreferencePredicate predicate, String labelText, boolean isAdminPage) {
|
194
|
final CLabel description = new CLabel(parent, SWT.NULL);
|
195
|
description.setText(labelText);
|
196
|
GridData textGrid = createTextGridData();
|
197
|
textGrid.verticalSpan = 2;
|
198
|
description.setLayoutData(textGrid);
|
199
|
|
200
|
Combo booleanCombo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
|
201
|
|
202
|
|
203
|
CdmPreference dbPref = PreferencesUtil.getPreferenceFromDB(predicate);
|
204
|
String defaultValue = "";
|
205
|
if (dbPref != null && !isAdminPage && dbPref.getValue() != null){
|
206
|
defaultValue = dbPref.getValue();
|
207
|
if (enumValues != null){
|
208
|
for (IKeyLabel value: enumValues){
|
209
|
if (value.getKey().equals(defaultValue)){
|
210
|
defaultValue = value.getLabel();
|
211
|
}
|
212
|
}
|
213
|
}
|
214
|
}else {
|
215
|
try{
|
216
|
Object defaultObject = predicate.getDefaultValue();
|
217
|
if (defaultObject != null){
|
218
|
if (defaultObject instanceof IKeyLabel){
|
219
|
defaultValue = ((IKeyLabel)defaultObject).getLabel();
|
220
|
}else{
|
221
|
defaultValue = defaultObject.toString();
|
222
|
}
|
223
|
}
|
224
|
}catch(NullPointerException e){
|
225
|
defaultValue = "";
|
226
|
}
|
227
|
|
228
|
}
|
229
|
|
230
|
booleanCombo.add(Messages.Preference_Use_Default + " ("+defaultValue+")");;
|
231
|
for (IKeyLabel value: enumValues){
|
232
|
booleanCombo.add(value.getLabel());
|
233
|
booleanCombo.setData(value.getLabel(), value);
|
234
|
}
|
235
|
|
236
|
return booleanCombo;
|
237
|
|
238
|
}
|
239
|
|
240
|
public Boolean getBooleanPrefValue(CdmPreference pref) {
|
241
|
return pref != null? (pref.getValue()!= null?Boolean.valueOf(pref.getValue()): null): null;
|
242
|
}
|
243
|
|
244
|
public String getStringPrefValue(CdmPreference pref) {
|
245
|
return pref != null? (pref.getValue()!= null? pref.getValue(): null): null;
|
246
|
}
|
247
|
|
248
|
public boolean getPrefAllowOverride(CdmPreference pref) {
|
249
|
return pref != null? pref.isAllowOverride(): true;
|
250
|
}
|
251
|
|
252
|
|
253
|
|
254
|
|
255
|
|
256
|
}
|