]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/preferences/DiagramFlagPreferencePage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / preferences / DiagramFlagPreferencePage.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.preferences;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collections;\r
16 import java.util.List;\r
17 \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
44 \r
45 /**\r
46  * @author Tuukka Lehtonen\r
47  */\r
48 public class DiagramFlagPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {\r
49 \r
50     private SchemeSelector       scheme;\r
51     private List<LabelingScheme> schemes;\r
52 \r
53     public DiagramFlagPreferencePage() {\r
54         super(FLAT);\r
55     }\r
56 \r
57     @Override\r
58     public void init(IWorkbench workbench) {\r
59     }\r
60 \r
61     @Override\r
62     protected IPreferenceStore doGetPreferenceStore() {\r
63         return new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
64     }\r
65 \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
69         }\r
70         public String getLabel() {\r
71             return (String) getField(0);\r
72         }\r
73         public String getDescription() {\r
74             return (String) getField(1);\r
75         }\r
76         public String getURI() {\r
77             return (String) getField(2);\r
78         }\r
79         public Resource getResource() {\r
80             return (Resource) getField(3);\r
81         }\r
82     }\r
83 \r
84     static class SchemeSelector extends FieldEditor {\r
85 \r
86         /**\r
87          * The <code>Combo</code> widget.\r
88          */\r
89         private Combo fCombo;\r
90 \r
91         /**\r
92          * The value (not the name) of the currently selected item in the Combo widget.\r
93          */\r
94         private LabelingScheme fValue;\r
95 \r
96         private LabelingScheme fOriginalValue;\r
97 \r
98         private LabelingScheme fDefaultValue;\r
99 \r
100         private List<LabelingScheme> fValues;\r
101 \r
102         /**\r
103          * Create the combo box field editor.\r
104          * \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
110          */\r
111         public SchemeSelector(String labelText, List<LabelingScheme> values, LabelingScheme originalValue, LabelingScheme defaultValue, Composite parent) {\r
112             init("labelingScheme", labelText);\r
113             fValues = values;\r
114             fOriginalValue = originalValue;\r
115             fDefaultValue = defaultValue;\r
116             createControl(parent);\r
117         }\r
118 \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
125                     left = left - 1;\r
126                 }\r
127                 ((GridData)fCombo.getLayoutData()).horizontalSpan = left;\r
128             } else {\r
129                 Control control = getLabelControl();\r
130                 if (control != null) {\r
131                     ((GridData)control.getLayoutData()).horizontalSpan = 1;\r
132                 }\r
133                 ((GridData)fCombo.getLayoutData()).horizontalSpan = 1;\r
134             }\r
135         }\r
136 \r
137         protected void doFillIntoGrid(Composite parent, int numColumns) {\r
138             int comboC = 1;\r
139             if (numColumns > 1) {\r
140                 comboC = numColumns - 1;\r
141             }\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
152         }\r
153 \r
154         public int getNumberOfControls() {\r
155             return 2;\r
156         }\r
157 \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
164                 }\r
165 \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
173                     }\r
174                 });\r
175             }\r
176             return fCombo;\r
177         }\r
178 \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
183                     return scheme;\r
184                 }\r
185             }\r
186             return fValues.get(0);\r
187         }\r
188 \r
189         private void updateComboForValue(LabelingScheme value) {\r
190             fValue = 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
194                     return;\r
195                 }\r
196             }\r
197             if (fValues.size() > 0) {\r
198                 fValue = fValues.get(0);\r
199                 fCombo.setText(fValue.getLabel());\r
200             }\r
201         }\r
202 \r
203         public void setEnabled(boolean enabled, Composite parent) {\r
204             super.setEnabled(enabled, parent);\r
205             getComboBoxControl(parent).setEnabled(enabled);\r
206         }\r
207 \r
208         @Override\r
209         protected void doLoad() {\r
210             updateComboForValue(fOriginalValue);\r
211         }\r
212 \r
213         protected void doLoadDefault() {\r
214             updateComboForValue(fDefaultValue);\r
215         }\r
216 \r
217         @Override\r
218         protected void doStore() {\r
219             if (fValue == null) {\r
220                 fOriginalValue = fDefaultValue;\r
221                 DiagramFlagPreferences.setProjectFlagLabelingScheme(fDefaultValue.getResource());\r
222                 return;\r
223             }\r
224             fOriginalValue = fValue;\r
225             DiagramFlagPreferences.setProjectFlagLabelingScheme(fValue.getResource());\r
226         }\r
227 \r
228     }\r
229 \r
230     static class FindSchemes implements Read<List<LabelingScheme>> {\r
231         @Override\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
241             }\r
242             return result;\r
243         }\r
244     }\r
245 \r
246     @Override\r
247     protected void createFieldEditors() {\r
248         schemes = Collections.emptyList();\r
249         Resource schemeValue = null;\r
250         try {\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
255         }\r
256 \r
257         LabelingScheme previousValue = null;\r
258         LabelingScheme defaultValue = null;\r
259 \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
263                 defaultValue = s;\r
264         }\r
265         if (defaultValue == null && !schemes.isEmpty())\r
266             defaultValue = schemes.get(0);\r
267 \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
272                     previousValue = s;\r
273             }\r
274         }\r
275         if (previousValue == null)\r
276             previousValue = defaultValue;\r
277 \r
278         scheme = new SchemeSelector("Labeling Scheme", schemes, previousValue, defaultValue, getFieldEditorParent());\r
279         addField(scheme);\r
280     }\r
281 \r
282 }\r