2 * Copyright (C) 2015 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
.menu
;
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
;
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
;
32 public abstract class CdmPreferencePage
extends PreferencePage
implements IE4PreferencePage
{
34 private boolean isDefaultButtonActivated
= true;
36 protected boolean isAdminPreference
= false;
38 private boolean isApply
;
40 protected final String SHOW
= Messages
.GeneralPreference_yes
;
41 protected final String DO_NOT_SHOW
= Messages
.GeneralPreference_no
;
43 public CdmPreferencePage() {
47 public CdmPreferencePage(String title
) {
51 public CdmPreferencePage(String title
, ImageDescriptor image
) {
58 setPreferenceStore(PreferencesUtil
.getPreferenceStore());
62 public void createControl(Composite parent
){
63 super.createControl(parent
);
64 if (getApplyButton() != null){
65 this.getApplyButton().setEnabled(false);
67 if (getDefaultsButton() != null){
68 this.getDefaultsButton().setEnabled(isDefaultButtonActivated
);
73 public boolean performOk() {
75 return super.performOk();
80 public boolean performCancel() {
82 return super.performCancel();
86 public void performApply() {
92 protected Composite
createComposite(Composite composite
) {
93 Composite activateComp
= new Composite(composite
, SWT
.NULL
);
94 GridLayout layout
= new GridLayout();
95 layout
.numColumns
= 2;
97 activateComp
.setLayout(layout
);
98 GridData gridData
= new GridData(GridData
.FILL_HORIZONTAL
);
100 activateComp
.setLayoutData(gridData
);
105 protected GridData
createTextGridData() {
106 GridData sepGrid
= new GridData(GridData
.FILL_HORIZONTAL
);
107 sepGrid
.horizontalSpan
= 2;
111 protected void getValues(){
114 protected Button
createAllowOverrideButton(Composite activateComp
) {
115 final Button allowOverrideCheckButton
= new Button(activateComp
, SWT
.CHECK
);
116 String overrideString
;
117 if (isAdminPreference
){
118 overrideString
= Messages
.GeneralPreference_allowOverride
;
120 overrideString
= Messages
.GeneralPreference_override
;
122 allowOverrideCheckButton
.setText(overrideString
);
123 GridData grid
= new GridData();
124 grid
.horizontalAlignment
= SWT
.END
;
125 allowOverrideCheckButton
.setLayoutData(grid
);
126 return allowOverrideCheckButton
;
130 public boolean isApply() {
134 public void setApply(boolean isApply
) {
135 this.isApply
= isApply
;
136 if (this.getApplyButton() != null){
137 this.getApplyButton().setEnabled(this.isApply
);
141 public boolean isDefaultButtonActivated() {
142 return isDefaultButtonActivated
;
145 public void setDefaultButtonActivated(boolean isDefaultButtonActivated
) {
146 this.isDefaultButtonActivated
= isDefaultButtonActivated
;
149 public Combo
createBooleanCombo(Composite parent
, String textTrue
, String textFalse
, IPreferencePredicate predicate
, String labelText
, boolean isAdminPage
) {
150 final CLabel rankDescription
= new CLabel(parent
, SWT
.NULL
);
151 rankDescription
.setText(labelText
);
152 GridData textGrid
= createTextGridData();
153 textGrid
.verticalSpan
= 2;
154 rankDescription
.setLayoutData(textGrid
);
156 Combo booleanCombo
= new Combo(parent
, SWT
.BORDER
| SWT
.READ_ONLY
);
158 CdmPreference dbPref
= PreferencesUtil
.getPreferenceFromDB(predicate
);
159 boolean defaultValue
= true;
160 if (dbPref
!= null && !isAdminPage
){
161 defaultValue
= Boolean
.valueOf(dbPref
.getValue());
164 defaultValue
= (Boolean
)predicate
.getDefaultValue();
165 }catch(NullPointerException e
){
166 defaultValue
= false;
170 String defaultString
;
172 defaultString
= " ("+textTrue
+")";
174 defaultString
= " ("+textFalse
+")";
176 booleanCombo
.add(Messages
.Preference_Use_Default
+ defaultString
);
177 booleanCombo
.add(textTrue
);
178 booleanCombo
.add(textFalse
);
179 if (!isAdminPage
&& dbPref
!= null){
180 booleanCombo
.setEnabled(dbPref
.isAllowOverride());
185 public Combo
createCombo(Composite parent
, IKeyLabel
[] enumValues
, IPreferencePredicate predicate
, String labelText
, boolean isAdminPage
) {
186 final CLabel description
= new CLabel(parent
, SWT
.NULL
);
187 description
.setText(labelText
);
188 GridData textGrid
= createTextGridData();
189 textGrid
.verticalSpan
= 2;
190 description
.setLayoutData(textGrid
);
192 Combo booleanCombo
= new Combo(parent
, SWT
.BORDER
| SWT
.READ_ONLY
);
194 CdmPreference dbPref
= PreferencesUtil
.getPreferenceFromDB(predicate
);
195 String defaultValue
= " - ";
196 if (dbPref
!= null && !isAdminPage
&& dbPref
.getValue() != null){
197 defaultValue
= dbPref
.getValue();
198 if (enumValues
!= null){
199 for (IKeyLabel value
: enumValues
){
200 if (value
.getKey().equals(defaultValue
)){
201 defaultValue
= value
.getLabel();
207 Object defaultObject
= predicate
.getDefaultValue();
208 if (defaultObject
!= null){
209 if (defaultObject
instanceof IKeyLabel
){
210 defaultValue
= ((IKeyLabel
)defaultObject
).getLabel();
212 defaultValue
= defaultObject
.toString();
215 }catch(NullPointerException e
){
220 booleanCombo
.add(Messages
.Preference_Use_Default
+ " ("+defaultValue
+")");
221 if (enumValues
!= null){
222 for (IKeyLabel value
: enumValues
){
223 booleanCombo
.add(value
.getLabel());
224 booleanCombo
.setData(value
.getLabel(), value
);
231 public Boolean
getBooleanPrefValue(CdmPreference pref
) {
232 return pref
!= null?
(pref
.getValue()!= null?Boolean
.valueOf(pref
.getValue()): null): null;
235 public String
getStringPrefValue(CdmPreference pref
) {
236 return pref
!= null?
(pref
.getValue()!= null? pref
.getValue(): null): null;
239 public boolean getPrefAllowOverride(CdmPreference pref
) {
240 return pref
!= null? pref
.isAllowOverride(): true;