]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.workbench/src/org/simantics/workbench/internal/dialogs/SimanticsPreferencePage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.workbench / src / org / simantics / workbench / internal / dialogs / SimanticsPreferencePage.java
1 package org.simantics.workbench.internal.dialogs;
2
3 import java.io.IOException;
4
5 import org.eclipse.jface.dialogs.IDialogConstants;
6 import org.eclipse.jface.preference.IPersistentPreferenceStore;
7 import org.eclipse.jface.preference.IPreferenceStore;
8 import org.eclipse.jface.preference.PreferencePage;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Button;
13 import org.eclipse.swt.widgets.Combo;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Label;
17 import org.eclipse.ui.IWorkbench;
18 import org.eclipse.ui.IWorkbenchPreferencePage;
19 import org.simantics.utils.ui.ErrorLogger;
20 import org.simantics.workbench.internal.Activator;
21 import org.simantics.workbench.internal.Messages;
22 import org.simantics.workbench.internal.preferences.SimanticsPreferences;
23
24 /**
25  * Generic workbench main preference page.
26  */
27 public class SimanticsPreferencePage extends PreferencePage implements
28 IWorkbenchPreferencePage {
29
30     private Button showDatabaseControlsButton;
31
32     /*
33      * (non-Javadoc)
34      * 
35      * @see org.eclipse.jface.preference.PreferencePage
36      */
37     protected Control createContents(Composite parent) {
38
39         Composite composite = createComposite(parent);
40
41         createButtons(composite);
42
43         createSpace(composite);
44
45         applyDialogFont(composite);
46
47         return composite;
48     }
49
50     /**
51      * Create the buttons at the top of the preference page.
52      * @param composite
53      */
54     protected void createButtons(Composite composite) {
55         createDatabaseControlPref(composite);
56     }
57
58     /**
59      * Create the widget for the heap status preference.
60      * 
61      * @param composite
62      */
63     protected void createDatabaseControlPref(Composite composite) {
64         showDatabaseControlsButton = new Button(composite, SWT.CHECK);
65         showDatabaseControlsButton.setText(Messages.SimanticsPreferencePage_ShowDatabaseControlsButton);
66         showDatabaseControlsButton.setToolTipText(Messages.SimanticsPreferencePage_ShowDatabaseControlsTooltip);
67         showDatabaseControlsButton.setSelection(getPreferenceStore().getBoolean(SimanticsPreferences.SHOW_DATABASE_CONTROLS));
68     }
69
70     /**
71      * Creates the composite which will contain all the preference controls for
72      * this page.
73      * 
74      * @param parent
75      *            the parent composite
76      * @return the composite for this page
77      */
78     protected Composite createComposite(Composite parent) {
79         Composite composite = new Composite(parent, SWT.NONE);
80         GridLayout layout = new GridLayout();
81         layout.marginWidth = 0;
82         layout.marginHeight = 0;
83         composite.setLayout(layout);
84         composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
85         return composite;
86     }
87
88     /**
89      * Utility method that creates a radio button instance and sets the default
90      * layout data.
91      * 
92      * @param parent
93      *            the parent for the new button
94      * @param label
95      *            the label for the new button
96      * @return the newly-created button
97      */
98     protected static Button createRadioButton(Composite parent, String label) {
99         Button button = new Button(parent, SWT.RADIO | SWT.LEFT);
100         button.setText(label);
101         return button;
102     }
103
104     /**
105      * Utility method that creates a combo box
106      * 
107      * @param parent
108      *            the parent for the new label
109      * @return the new widget
110      */
111     protected static Combo createCombo(Composite parent) {
112         Combo combo = new Combo(parent, SWT.READ_ONLY);
113         GridData data = new GridData(GridData.FILL_HORIZONTAL);
114         data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
115         combo.setLayoutData(data);
116         return combo;
117     }
118
119     /**
120      * Creates a tab of one horizontal spans.
121      * 
122      * @param parent
123      *            the parent in which the tab should be created
124      */
125     protected static void createSpace(Composite parent) {
126         Label vfiller = new Label(parent, SWT.LEFT);
127         GridData gridData = new GridData();
128         gridData = new GridData();
129         gridData.horizontalAlignment = GridData.BEGINNING;
130         gridData.grabExcessHorizontalSpace = false;
131         gridData.verticalAlignment = GridData.CENTER;
132         gridData.grabExcessVerticalSpace = false;
133         vfiller.setLayoutData(gridData);
134     }
135
136     /**
137      * Returns preference store that belongs to the our plugin.
138      * 
139      * @return the preference store for this plugin
140      */
141     protected IPreferenceStore doGetPreferenceStore() {
142         return Activator.getDefault().getPreferenceStore();
143     }
144
145     /**
146      * @see IWorkbenchPreferencePage
147      */
148     public void init(IWorkbench aWorkbench) {
149     }
150
151     /**
152      * The default button has been pressed.
153      */
154     protected void performDefaults() {
155         IPreferenceStore store = getPreferenceStore();
156         boolean showDatabaseControls = store.getDefaultBoolean(SimanticsPreferences.SHOW_DATABASE_CONTROLS);
157         showDatabaseControlsButton.setSelection(showDatabaseControls);
158         super.performDefaults();
159     }
160
161     /**
162      * The user has pressed Ok. Store/apply this page's values appropriately.
163      */
164     public boolean performOk() {
165         IPreferenceStore store = getPreferenceStore();
166         store.setValue(SimanticsPreferences.SHOW_DATABASE_CONTROLS, showDatabaseControlsButton.getSelection());
167         if (store instanceof IPersistentPreferenceStore) {
168             try {
169                 ((IPersistentPreferenceStore) store).save();
170             } catch (IOException e) {
171                 ErrorLogger.defaultLogError(e);
172             }
173         }
174         return true;
175     }
176
177 }