1 /*******************************************************************************
\r
2 * Copyright (c) 2013 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
11 *******************************************************************************/
\r
13 package org.simantics.sysdyn.ui.preferences;
\r
15 import java.util.HashMap;
\r
17 import org.eclipse.jface.layout.GridDataFactory;
\r
18 import org.eclipse.jface.layout.GridLayoutFactory;
\r
19 import org.eclipse.jface.preference.BooleanFieldEditor;
\r
20 import org.eclipse.jface.preference.ColorFieldEditor;
\r
21 import org.eclipse.jface.preference.FieldEditor;
\r
22 import org.eclipse.jface.preference.FieldEditorPreferencePage;
\r
23 import org.eclipse.jface.preference.FontFieldEditor;
\r
24 import org.eclipse.swt.SWT;
\r
25 import org.eclipse.swt.layout.GridLayout;
\r
26 import org.eclipse.swt.widgets.Composite;
\r
27 import org.eclipse.swt.widgets.Group;
\r
28 import org.eclipse.swt.widgets.Label;
\r
29 import org.eclipse.ui.IWorkbench;
\r
30 import org.eclipse.ui.IWorkbenchPreferencePage;
\r
31 import org.simantics.sysdyn.ui.Activator;
\r
35 * @author Teemu Lempinen
\r
38 public class SysdynDiagramPreferencePage extends FieldEditorPreferencePage implements
\r
39 IWorkbenchPreferencePage {
\r
41 private HashMap<FieldEditor, Composite> fieldParents;
\r
42 private Group colorEditors, fontEditors;
\r
44 public SysdynDiagramPreferencePage() {
\r
46 setDescription("System dynamics diagram preferences");
\r
47 fieldParents = new HashMap<FieldEditor, Composite>();
\r
51 public void init(IWorkbench workbench) {
\r
52 setPreferenceStore(Activator.getDefault().getPreferenceStore());
\r
56 protected void createFieldEditors() {
\r
57 Composite parent = new Composite(getFieldEditorParent(), SWT.NONE);
\r
58 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(parent);
\r
59 GridDataFactory.fillDefaults().grab(true, true).applyTo(parent);
\r
62 Group defaults = new Group(parent, SWT.NONE);
\r
63 defaults.setText("Default settings");
\r
64 GridDataFactory.fillDefaults().grab(true, false).applyTo(defaults);
\r
65 addField(new ColorFieldEditor(SysdynDiagramPreferences.DEFAULT_COLOR, "Color", defaults));
\r
66 addField(new CustomFontFieldEditor(SysdynDiagramPreferences.DEFAULT_FONT, "Font", defaults));
\r
67 GridLayout layout = (GridLayout) defaults.getLayout();
\r
68 layout.marginHeight = 3;
\r
69 layout.marginWidth = 3;
\r
70 defaults.setLayout(layout);
\r
73 colorEditors = new Group(parent, SWT.NONE);
\r
74 colorEditors.setText("Colors");
\r
75 GridDataFactory.fillDefaults().applyTo(colorEditors);
\r
76 GridDataFactory.fillDefaults().applyTo(colorEditors);
\r
77 Label label = new Label(colorEditors, SWT.NONE);
\r
78 GridDataFactory.fillDefaults().span(3, 1).applyTo(label);
\r
80 label = new Label(colorEditors, SWT.NONE);
\r
81 label.setText("Use default");
\r
82 GridDataFactory.fillDefaults().applyTo(label);
\r
84 addColorFieldEditor(SysdynDiagramPreferences.ARROW_COLOR, "&Dependency", SysdynDiagramPreferences.ARROW_USE_DEFAULT_COLOR);
\r
85 addColorFieldEditor(SysdynDiagramPreferences.FLOW_COLOR, "&Flow", SysdynDiagramPreferences.FLOW_USE_DEFAULT_COLOR);
\r
86 addColorFieldEditor(SysdynDiagramPreferences.AUXILIARY_COLOR, "&Auxiliary", SysdynDiagramPreferences.AUXILIARY_USE_DEFAULT_COLOR);
\r
87 addColorFieldEditor(SysdynDiagramPreferences.STOCK_COLOR, "&Stock", SysdynDiagramPreferences.STOCK_USE_DEFAULT_COLOR);
\r
88 addColorFieldEditor(SysdynDiagramPreferences.VALVE_COLOR, "&Valve", SysdynDiagramPreferences.VALVE_USE_DEFAULT_COLOR);
\r
89 addColorFieldEditor(SysdynDiagramPreferences.INPUT_COLOR, "&Input", SysdynDiagramPreferences.VALVE_USE_DEFAULT_COLOR);
\r
90 addColorFieldEditor(SysdynDiagramPreferences.CLOUD_COLOR, "&Cloud", SysdynDiagramPreferences.CLOUD_USE_DEFAULT_COLOR);
\r
91 addColorFieldEditor(SysdynDiagramPreferences.MODULE_COLOR, "&Module", SysdynDiagramPreferences.MODULE_USE_DEFAULT_COLOR);
\r
94 fontEditors = new Group(parent, SWT.NONE);
\r
95 fontEditors.setText("Fonts");
\r
96 GridDataFactory.fillDefaults().applyTo(fontEditors);
\r
97 GridLayoutFactory.fillDefaults().applyTo(fontEditors);
\r
99 label = new Label(fontEditors, SWT.NONE);
\r
100 GridDataFactory.fillDefaults().span(3, 1).applyTo(label);
\r
102 label = new Label(fontEditors, SWT.NONE);
\r
103 label.setText("Use default");
\r
104 GridDataFactory.fillDefaults().applyTo(label);
\r
106 addFontFieldEditor(SysdynDiagramPreferences.ARROW_FONT, "Dependency", SysdynDiagramPreferences.ARROW_USE_DEFAULT_FONT);
\r
107 addFontFieldEditor(SysdynDiagramPreferences.AUXILIARY_FONT, "Auxiliary", SysdynDiagramPreferences.AUXILIARY_USE_DEFAULT_FONT);
\r
108 addFontFieldEditor(SysdynDiagramPreferences.STOCK_FONT, "Stock", SysdynDiagramPreferences.STOCK_USE_DEFAULT_FONT);
\r
109 addFontFieldEditor(SysdynDiagramPreferences.VALVE_FONT, "Valve", SysdynDiagramPreferences.VALVE_USE_DEFAULT_FONT);
\r
110 addFontFieldEditor(SysdynDiagramPreferences.INPUT_FONT, "Input", SysdynDiagramPreferences.INPUT_USE_DEFAULT_FONT);
\r
111 addFontFieldEditor(SysdynDiagramPreferences.CLOUD_FONT, "Cloud", SysdynDiagramPreferences.CLOUD_USE_DEFAULT_FONT);
\r
112 addFontFieldEditor(SysdynDiagramPreferences.MODULE_FONT, "Module", SysdynDiagramPreferences.MODULE_USE_DEFAULT_FONT);
\r
114 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(4).applyTo(colorEditors);
\r
115 GridLayoutFactory.fillDefaults().margins(3, 3).numColumns(4).applyTo(fontEditors);
\r
119 private void addColorFieldEditor(String property, String label, String defaultBoolean) {
\r
120 ColorFieldEditor colorField = new ColorFieldEditor(property, label, colorEditors);
\r
121 colorField.setEnabled(!getPreferenceStore().getBoolean(defaultBoolean), colorEditors);
\r
122 Composite c = new Composite(colorEditors, SWT.NONE);
\r
123 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(c);
\r
124 GridLayoutFactory.fillDefaults().applyTo(c);
\r
125 BooleanFieldEditor useDefault = new DefaultBooleanFiedEditor(colorField, defaultBoolean, c);
\r
126 fieldParents.put(colorField, colorEditors);
\r
127 addField(useDefault);
\r
128 addField(colorField);
\r
131 private void addFontFieldEditor(String property, String label, String defaultBoolean) {
\r
132 CustomFontFieldEditor fontField = new CustomFontFieldEditor(property, label, fontEditors);
\r
133 fontField.setEnabled(!getPreferenceStore().getBoolean(defaultBoolean), fontEditors);
\r
134 Composite c = new Composite(fontEditors, SWT.NONE);
\r
135 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).applyTo(c);
\r
136 GridLayoutFactory.fillDefaults().applyTo(c);
\r
137 BooleanFieldEditor useDefault = new DefaultBooleanFiedEditor(fontField, defaultBoolean, c);
\r
141 fieldParents.put(fontField, fontEditors);
\r
142 addField(useDefault);
\r
143 addField(fontField);
\r
146 private class DefaultBooleanFiedEditor extends BooleanFieldEditor {
\r
147 private FieldEditor fieldEditor;
\r
149 public DefaultBooleanFiedEditor(FieldEditor fieldEditor, String name, Composite parent) {
\r
150 super(name, "", parent);
\r
151 this.fieldEditor = fieldEditor;
\r
155 protected void valueChanged(boolean oldValue, boolean newValue) {
\r
156 super.valueChanged(oldValue, newValue);
\r
157 fieldEditor.setEnabled(!newValue, fieldParents.get(fieldEditor));
\r
161 private class CustomFontFieldEditor extends FontFieldEditor {
\r
163 public CustomFontFieldEditor(String name, String labelText, Composite parent) {
\r
164 super(name, labelText, parent);
\r
166 GridDataFactory.fillDefaults().applyTo(getChangeControl(parent));
\r
167 setChangeButtonText("...");
\r
168 getChangeControl(parent).setToolTipText("Change");
\r