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