1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.preferences;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
18 import org.eclipse.core.runtime.preferences.InstanceScope;
19 import org.eclipse.jface.preference.FieldEditor;
20 import org.eclipse.jface.preference.FieldEditorPreferencePage;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.widgets.Combo;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.ui.IWorkbench;
30 import org.eclipse.ui.IWorkbenchPreferencePage;
31 import org.eclipse.ui.preferences.ScopedPreferenceStore;
32 import org.simantics.Simantics;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.Resource;
35 import org.simantics.db.common.request.ObjectsWithType;
36 import org.simantics.db.exception.DatabaseException;
37 import org.simantics.db.request.Read;
38 import org.simantics.diagram.flag.DiagramFlagPreferences;
39 import org.simantics.diagram.stubs.DiagramResource;
40 import org.simantics.layer0.Layer0;
41 import org.simantics.modeling.ui.Activator;
42 import org.simantics.utils.datastructures.map.Tuple;
43 import org.simantics.utils.ui.ExceptionUtils;
46 * @author Tuukka Lehtonen
48 public class DiagramFlagPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
50 private SchemeSelector scheme;
51 private List<LabelingScheme> schemes;
53 public DiagramFlagPreferencePage() {
58 public void init(IWorkbench workbench) {
62 protected IPreferenceStore doGetPreferenceStore() {
63 return new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
66 static class LabelingScheme extends Tuple {
67 public LabelingScheme(String label, String description, String uri, Resource scheme) {
68 super(label, description, uri, scheme);
70 public String getLabel() {
71 return (String) getField(0);
73 public String getDescription() {
74 return (String) getField(1);
76 public String getURI() {
77 return (String) getField(2);
79 public Resource getResource() {
80 return (Resource) getField(3);
84 static class SchemeSelector extends FieldEditor {
87 * The <code>Combo</code> widget.
92 * The value (not the name) of the currently selected item in the Combo widget.
94 private LabelingScheme fValue;
96 private LabelingScheme fOriginalValue;
98 private LabelingScheme fDefaultValue;
100 private List<LabelingScheme> fValues;
103 * Create the combo box field editor.
105 * @param label the name of the preference this field editor works on
106 * @param labelText the label text of the field editor
107 * @param entryNamesAndValues the names (labels) and underlying values to populate the combo widget. These should be
108 * arranged as: { {name1, value1}, {name2, value2}, ...}
109 * @param parent the parent composite
111 public SchemeSelector(String labelText, List<LabelingScheme> values, LabelingScheme originalValue, LabelingScheme defaultValue, Composite parent) {
112 init("labelingScheme", labelText);
114 fOriginalValue = originalValue;
115 fDefaultValue = defaultValue;
116 createControl(parent);
119 protected void adjustForNumColumns(int numColumns) {
120 if (numColumns > 1) {
121 Control control = getLabelControl();
122 int left = numColumns;
123 if (control != null) {
124 ((GridData)control.getLayoutData()).horizontalSpan = 1;
127 ((GridData)fCombo.getLayoutData()).horizontalSpan = left;
129 Control control = getLabelControl();
130 if (control != null) {
131 ((GridData)control.getLayoutData()).horizontalSpan = 1;
133 ((GridData)fCombo.getLayoutData()).horizontalSpan = 1;
137 protected void doFillIntoGrid(Composite parent, int numColumns) {
139 if (numColumns > 1) {
140 comboC = numColumns - 1;
142 Control control = getLabelControl(parent);
143 GridData gd = new GridData();
144 gd.horizontalSpan = 1;
145 control.setLayoutData(gd);
146 control = getComboBoxControl(parent);
148 gd.horizontalSpan = comboC;
149 gd.horizontalAlignment = GridData.FILL;
150 control.setLayoutData(gd);
151 control.setFont(parent.getFont());
154 public int getNumberOfControls() {
158 private Combo getComboBoxControl(Composite parent) {
159 if (fCombo == null) {
160 fCombo = new Combo(parent, SWT.READ_ONLY);
161 fCombo.setFont(parent.getFont());
162 for (int i = 0; i < fValues.size(); i++) {
163 fCombo.add(fValues.get(i).getLabel(), i);
166 fCombo.addSelectionListener(new SelectionAdapter() {
167 public void widgetSelected(SelectionEvent evt) {
168 LabelingScheme oldValue = fValue;
169 String name = fCombo.getText();
170 fValue = getValueForName(name);
171 setPresentsDefaultValue(false);
172 fireValueChanged(VALUE, oldValue, fValue);
179 private LabelingScheme getValueForName(String name) {
180 for (int i = 0; i < fValues.size(); i++) {
181 LabelingScheme scheme = fValues.get(i);
182 if (scheme.getLabel().equals(name)) {
186 return fValues.get(0);
189 private void updateComboForValue(LabelingScheme value) {
191 for (int i = 0; i < fValues.size(); i++) {
192 if (value.equals(fValues.get(i))) {
193 fCombo.setText(value.getLabel());
197 if (fValues.size() > 0) {
198 fValue = fValues.get(0);
199 fCombo.setText(fValue.getLabel());
203 public void setEnabled(boolean enabled, Composite parent) {
204 super.setEnabled(enabled, parent);
205 getComboBoxControl(parent).setEnabled(enabled);
209 protected void doLoad() {
210 updateComboForValue(fOriginalValue);
213 protected void doLoadDefault() {
214 updateComboForValue(fDefaultValue);
218 protected void doStore() {
219 if (fValue == null) {
220 fOriginalValue = fDefaultValue;
221 DiagramFlagPreferences.setProjectFlagLabelingScheme(fDefaultValue.getResource());
224 fOriginalValue = fValue;
225 DiagramFlagPreferences.setProjectFlagLabelingScheme(fValue.getResource());
230 static class FindSchemes implements Read<List<LabelingScheme>> {
232 public List<LabelingScheme> perform(ReadGraph graph) throws DatabaseException {
233 DiagramResource DIA = DiagramResource.getInstance(graph);
234 Layer0 L0 = Layer0.getInstance(graph);
235 List<LabelingScheme> result = new ArrayList<LabelingScheme>();
236 for (Resource scheme : graph.syncRequest(new ObjectsWithType(DIA.FlagLabelingScheme, L0.ConsistsOf, DIA.FlagLabelingScheme))) {
237 String label = graph.adapt(scheme, String.class);
238 String description = graph.getPossibleRelatedValue(scheme, L0.HasDescription);
239 String uri = graph.getURI(scheme);
240 result.add(new LabelingScheme(label, description, uri, scheme));
247 protected void createFieldEditors() {
248 schemes = Collections.emptyList();
249 Resource schemeValue = null;
251 schemes = Simantics.getSession().syncRequest(new FindSchemes());
252 schemeValue = DiagramFlagPreferences.getActiveFlagLabelingSchemeResource(Simantics.getSession());
253 } catch (DatabaseException e) {
254 ExceptionUtils.logAndShowError(e);
257 LabelingScheme previousValue = null;
258 LabelingScheme defaultValue = null;
260 for (int i = 0; i < schemes.size(); ++i) {
261 LabelingScheme s = schemes.get(i);
262 if (DiagramResource.URIs.FlagLabelingScheme_Alphabetical.equals(s.getURI()))
265 if (defaultValue == null && !schemes.isEmpty())
266 defaultValue = schemes.get(0);
268 if (schemeValue != null) {
269 for (int i = 0; i < schemes.size(); ++i) {
270 LabelingScheme s = schemes.get(i);
271 if (schemeValue.equals(s.getResource()))
275 if (previousValue == null)
276 previousValue = defaultValue;
278 scheme = new SchemeSelector("Labeling Scheme", schemes, previousValue, defaultValue, getFieldEditorParent());