1 package org.simantics.workbench.internal.dialogs;
3 import java.io.IOException;
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;
25 * Generic workbench main preference page.
27 public class SimanticsPreferencePage extends PreferencePage implements
28 IWorkbenchPreferencePage {
30 private Button showDatabaseControlsButton;
35 * @see org.eclipse.jface.preference.PreferencePage
37 protected Control createContents(Composite parent) {
39 Composite composite = createComposite(parent);
41 createButtons(composite);
43 createSpace(composite);
45 applyDialogFont(composite);
51 * Create the buttons at the top of the preference page.
54 protected void createButtons(Composite composite) {
55 createDatabaseControlPref(composite);
59 * Create the widget for the heap status preference.
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));
71 * Creates the composite which will contain all the preference controls for
75 * the parent composite
76 * @return the composite for this page
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));
89 * Utility method that creates a radio button instance and sets the default
93 * the parent for the new button
95 * the label for the new button
96 * @return the newly-created button
98 protected static Button createRadioButton(Composite parent, String label) {
99 Button button = new Button(parent, SWT.RADIO | SWT.LEFT);
100 button.setText(label);
105 * Utility method that creates a combo box
108 * the parent for the new label
109 * @return the new widget
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);
120 * Creates a tab of one horizontal spans.
123 * the parent in which the tab should be created
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);
137 * Returns preference store that belongs to the our plugin.
139 * @return the preference store for this plugin
141 protected IPreferenceStore doGetPreferenceStore() {
142 return Activator.getDefault().getPreferenceStore();
146 * @see IWorkbenchPreferencePage
148 public void init(IWorkbench aWorkbench) {
152 * The default button has been pressed.
154 protected void performDefaults() {
155 IPreferenceStore store = getPreferenceStore();
156 boolean showDatabaseControls = store.getDefaultBoolean(SimanticsPreferences.SHOW_DATABASE_CONTROLS);
157 showDatabaseControlsButton.setSelection(showDatabaseControls);
158 super.performDefaults();
162 * The user has pressed Ok. Store/apply this page's values appropriately.
164 public boolean performOk() {
165 IPreferenceStore store = getPreferenceStore();
166 store.setValue(SimanticsPreferences.SHOW_DATABASE_CONTROLS, showDatabaseControlsButton.getSelection());
167 if (store instanceof IPersistentPreferenceStore) {
169 ((IPersistentPreferenceStore) store).save();
170 } catch (IOException e) {
171 ErrorLogger.defaultLogError(e);