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