Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / menu / CdmPreferencePage.java
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 public abstract class CdmPreferencePage extends PreferencePage implements IE4PreferencePage {
33
34 private boolean isDefaultButtonActivated = true;
35
36 protected boolean isAdminPreference = false;
37
38 private boolean isApply;
39
40 protected final String SHOW = Messages.GeneralPreference_yes;
41 protected final String DO_NOT_SHOW = Messages.GeneralPreference_no;
42
43 public CdmPreferencePage() {
44 this(null, null);
45 }
46
47 public CdmPreferencePage(String title) {
48 this(title, null);
49 }
50
51 public CdmPreferencePage(String title, ImageDescriptor image) {
52 super(title, image);
53
54 init();
55 }
56
57 public void init() {
58 setPreferenceStore(PreferencesUtil.getPreferenceStore());
59 }
60
61 @Override
62 public void createControl(Composite parent){
63 super.createControl(parent);
64 if (getApplyButton() != null){
65 this.getApplyButton().setEnabled(false);
66 }
67 if (getDefaultsButton() != null){
68 this.getDefaultsButton().setEnabled(isDefaultButtonActivated);
69 }
70 }
71
72 @Override
73 public boolean performOk() {
74 if (isValid()){
75 return super.performOk();
76 }return false;
77 }
78
79 @Override
80 public boolean performCancel() {
81
82 return super.performCancel();
83 }
84
85 @Override
86 public void performApply() {
87 setApply(true);
88 super.performApply();
89 setApply(false);
90 }
91
92 protected Composite createComposite(Composite composite) {
93 Composite activateComp = new Composite(composite, SWT.NULL);
94 GridLayout layout = new GridLayout();
95 layout.numColumns = 2;
96
97 activateComp.setLayout(layout);
98 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
99
100 activateComp.setLayoutData(gridData);
101
102 return activateComp;
103 }
104
105 protected GridData createTextGridData() {
106 GridData sepGrid = new GridData(GridData.FILL_HORIZONTAL);
107 sepGrid.horizontalSpan = 2;
108 return sepGrid;
109 }
110
111 protected void getValues(){
112 }
113
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;
119 }else{
120 overrideString = Messages.GeneralPreference_override;
121 }
122 allowOverrideCheckButton.setText(overrideString);
123 GridData grid = new GridData();
124 grid.horizontalAlignment = SWT.END;
125 allowOverrideCheckButton.setLayoutData(grid);
126 return allowOverrideCheckButton;
127 }
128
129 @Override
130 public boolean isApply() {
131 return isApply;
132 }
133
134 public void setApply(boolean isApply) {
135 this.isApply = isApply;
136 if (this.getApplyButton() != null){
137 this.getApplyButton().setEnabled(this.isApply);
138 }
139 }
140
141 public boolean isDefaultButtonActivated() {
142 return isDefaultButtonActivated;
143 }
144
145 public void setDefaultButtonActivated(boolean isDefaultButtonActivated) {
146 this.isDefaultButtonActivated = isDefaultButtonActivated;
147 }
148
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);
155
156 Combo booleanCombo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
157
158 CdmPreference dbPref = PreferencesUtil.getPreferenceFromDB(predicate);
159 boolean defaultValue = true;
160 if (dbPref != null && !isAdminPage){
161 defaultValue = Boolean.valueOf(dbPref.getValue());
162 }else{
163 try{
164 defaultValue = (Boolean)predicate.getDefaultValue();
165 }catch(NullPointerException e){
166 defaultValue = false;
167 }
168
169 }
170 String defaultString;
171 if (defaultValue){
172 defaultString = " ("+textTrue+")";
173 }else{
174 defaultString = " ("+textFalse+")";
175 }
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());
181 }
182 return booleanCombo;
183 }
184
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);
191
192 Combo booleanCombo = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
193
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();
202 }
203 }
204 }
205 }else {
206 try{
207 Object defaultObject = predicate.getDefaultValue();
208 if (defaultObject != null){
209 if (defaultObject instanceof IKeyLabel){
210 defaultValue = ((IKeyLabel)defaultObject).getLabel();
211 }else{
212 defaultValue = defaultObject.toString();
213 }
214 }
215 }catch(NullPointerException e){
216 defaultValue = "";
217 }
218 }
219
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);
225 }
226 }
227
228 return booleanCombo;
229 }
230
231 public Boolean getBooleanPrefValue(CdmPreference pref) {
232 return pref != null? (pref.getValue()!= null?Boolean.valueOf(pref.getValue()): null): null;
233 }
234
235 public String getStringPrefValue(CdmPreference pref) {
236 return pref != null? (pref.getValue()!= null? pref.getValue(): null): null;
237 }
238
239 public boolean getPrefAllowOverride(CdmPreference pref) {
240 return pref != null? pref.isAllowOverride(): true;
241 }
242 }