]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/monitor/MonitorComposite.java
(refs #7571) Fixed incorrect handling of effects in edo
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / monitor / MonitorComposite.java
1 package org.simantics.modeling.ui.diagram.monitor;
2
3 import java.util.Arrays;
4
5 import org.eclipse.core.runtime.Platform;
6 import org.eclipse.jface.layout.GridDataFactory;
7 import org.eclipse.jface.layout.GridLayoutFactory;
8 import org.eclipse.jface.resource.ImageDescriptor;
9 import org.eclipse.jface.resource.JFaceResources;
10 import org.eclipse.jface.resource.LocalResourceManager;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.events.SelectionListener;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.swt.graphics.RGB;
16 import org.eclipse.swt.widgets.ColorDialog;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.ui.IWorkbenchSite;
21 import org.osgi.framework.Bundle;
22 import org.simantics.Simantics;
23 import org.simantics.browsing.ui.swt.widgets.Button;
24 import org.simantics.browsing.ui.swt.widgets.Combo;
25 import org.simantics.browsing.ui.swt.widgets.Label;
26 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
27 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;
28 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
29 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
30 import org.simantics.common.color.Color;
31 import org.simantics.databoard.Bindings;
32 import org.simantics.databoard.util.ObjectUtils;
33 import org.simantics.db.ReadGraph;
34 import org.simantics.db.Resource;
35 import org.simantics.db.WriteGraph;
36 import org.simantics.db.common.CommentMetadata;
37 import org.simantics.db.common.request.PossibleIndexRoot;
38 import org.simantics.db.common.request.PossibleObjectWithType;
39 import org.simantics.db.common.request.WriteRequest;
40 import org.simantics.db.exception.DatabaseException;
41 import org.simantics.db.management.ISessionContext;
42 import org.simantics.diagram.stubs.DiagramResource;
43 import org.simantics.diagram.stubs.G2DResource;
44 import org.simantics.layer0.Layer0;
45 import org.simantics.operation.Layer0X;
46 import org.simantics.selectionview.ConfigurationComposite;
47 import org.simantics.ui.colors.Colors;
48 import org.simantics.ui.utils.ResourceAdaptionUtils;
49 import org.simantics.utils.datastructures.Triple;
50 import org.simantics.utils.ui.BundleUtils;
51 import org.simantics.utils.ui.ErrorLogger;
52 import org.simantics.utils.ui.SWTUtils;
53 import org.simantics.utils.ui.gfx.ColorImageDescriptor;
54 import org.simantics.utils.ui.workbench.WorkbenchUtils;
55
56 public class MonitorComposite extends ConfigurationComposite {
57
58     private static final String DATA_CURRENT_COLOR = "COLOR";
59
60     public void create(final Composite body, IWorkbenchSite site, final ISessionContext context, final WidgetSupport support) {
61         final Display display = body.getDisplay();
62
63         body.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
64
65         Composite buttonComposite = new Composite(body, SWT.NONE);
66         buttonComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
67         GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(buttonComposite);
68         GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(buttonComposite);
69
70         LocalResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources(), buttonComposite);
71         support.setParameter(WidgetSupport.RESOURCE_MANAGER, resourceManager);
72
73         Label templateHeader = new Label(buttonComposite, support, 0);
74         templateHeader.setText("Template");
75         //templateHeader.setFont(smallFont);
76         templateHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
77         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(templateHeader.getWidget());
78
79         Combo templateCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
80         templateCombo.setItemFactory(new AvailableTemplateFactory());
81         templateCombo.setSelectionFactory(new CurrentTemplateFactory());
82         templateCombo.addModifyListener(new TemplateModifier());
83         GridDataFactory.fillDefaults().grab(true, false).applyTo(templateCombo.getWidget());
84
85         Button setDefaultButton = new Button(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
86         setDefaultButton.setText("Set As Default");
87         setDefaultButton.setTooltipText("Set As Default for Newly Created Monitors");
88         setDefaultButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
89             @Override
90             public void apply(WriteGraph graph, Resource monitor) throws DatabaseException {
91                 graph.markUndoPoint();
92                 Layer0X L0X = Layer0X.getInstance(graph);
93                 DiagramResource DIA = DiagramResource.getInstance(graph);
94                 Resource root = graph.syncRequest(new PossibleIndexRoot(monitor));
95                 Resource currentTemplate = graph.getPossibleObject(monitor, L0X.ObtainsProperty);
96                 if (root != null && !graph.isImmutable(root) && currentTemplate != null) {
97                     graph.deny(root, DIA.HasDefaultMonitorTemplate);
98                     graph.claim(root, DIA.HasDefaultMonitorTemplate, currentTemplate);
99                 }
100             }
101         });
102         GridDataFactory.fillDefaults().grab(false, false).applyTo(setDefaultButton.getWidget());
103
104         Button resetButton = new Button(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
105         resetButton.setText("Reset Local Changes");
106         resetButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
107             @Override
108             public void apply(WriteGraph graph, Resource monitor) throws DatabaseException {
109                 graph.markUndoPoint();
110                 Layer0X L0X = Layer0X.getInstance(graph);
111                 DiagramResource DIA = DiagramResource.getInstance(graph);
112                 if(graph.hasStatement(monitor, DIA.HasFont)) graph.deny(monitor, DIA.HasFont);
113                 if(graph.hasStatement(monitor, DIA.HasFormatter)) graph.deny(monitor, DIA.HasFormatter);
114                 if(graph.hasStatement(monitor, DIA.HasColor)) graph.deny(monitor, DIA.HasColor);
115                 if(graph.hasStatement(monitor, L0X.HasExpression)) graph.deny(monitor, L0X.HasExpression);
116             }
117         });
118
119         GridDataFactory.fillDefaults().grab(false, false).applyTo(resetButton.getWidget());
120
121         Label fontHeader = new Label(buttonComposite, support, 0);
122         fontHeader.setText("Font Family");
123         //fontHeader.setFont(smallFont);
124         fontHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
125         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(fontHeader.getWidget());
126
127         Combo fontCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
128         fontCombo.setItemFactory2(new AvailableFontFactory());
129         fontCombo.setSelectionFactory(new CurrentFontFactory());
130         fontCombo.addModifyListener(new FontModifier());
131         GridDataFactory.fillDefaults().grab(true, false).span(3,1).applyTo(fontCombo.getWidget());
132
133         Label sizeHeader = new Label(buttonComposite, support, 0);
134         sizeHeader.setText("Font Size");
135         //sizeHeader.setFont(smallFont);
136         sizeHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
137         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(sizeHeader.getWidget());
138
139         Combo sizeCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
140         sizeCombo.setItemFactory2(new AvailableFontSizeFactory());
141         sizeCombo.setSelectionFactory(new CurrentFontSizeFactory());
142         sizeCombo.addModifyListener(new FontSizeModifier());
143         GridDataFactory.fillDefaults().grab(false, false).span(3,1).minSize(50, 0).applyTo(sizeCombo.getWidget());
144
145         Label formatterHeader = new Label(buttonComposite, support, 0);
146         formatterHeader.setText("Formatting");
147         //formatterHeader.setFont(smallFont);
148         formatterHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
149         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(formatterHeader.getWidget());
150
151         Combo formatCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
152         formatCombo.setItemFactory(new AvailableFormatFactory());
153         formatCombo.setSelectionFactory(new CurrentFormatFactory());
154         formatCombo.addModifyListener(new FormatModifier());
155         GridDataFactory.fillDefaults().grab(true, false).span(3,1).applyTo(formatCombo.getWidget());
156
157         Composite rowComposite = new Composite(buttonComposite, SWT.NONE);
158         rowComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
159         GridDataFactory.fillDefaults().span(3, 1).grab(true, false).applyTo(rowComposite);
160         GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(rowComposite);
161
162         Label alignHeader = new Label(rowComposite, support, 0);
163         alignHeader.setText("Alignment");
164         //alignHeader.setFont(smallFont);
165         alignHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
166         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(alignHeader.getWidget());
167
168         Bundle iconBundle = Platform.getBundle("com.famfamfam.silk");
169
170         Composite alignComposite = new Composite(rowComposite, SWT.NONE);
171         alignComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
172         GridDataFactory.fillDefaults().span(1, 1).grab(false, false).applyTo(alignComposite);
173         GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(alignComposite);
174
175         Button leadButton = new Button(alignComposite, support, SWT.TOGGLE);
176         leadButton.setTooltipText("Left Alignment");
177         leadButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromBundle(iconBundle, "icons/text_align_left.png")));
178         leadButton.setSelectionFactory(new AlignmentSelectedFactory(G2DResource.URIs.Alignment_Leading));
179         leadButton.addSelectionListener(new AlignmentSelectionListener(context, G2DResource.URIs.Alignment_Leading));
180
181         Button centerButton = new Button(alignComposite, support, SWT.TOGGLE);
182         centerButton.setTooltipText("Center Alignment");
183         centerButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromBundle(iconBundle, "icons/text_align_center.png")));
184         centerButton.setSelectionFactory(new AlignmentSelectedFactory(G2DResource.URIs.Alignment_Center));
185         centerButton.addSelectionListener(new AlignmentSelectionListener(context, G2DResource.URIs.Alignment_Center));
186
187         Button trailButton = new Button(alignComposite, support, SWT.TOGGLE);
188         trailButton.setTooltipText("Right Alignment");
189         trailButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromBundle(iconBundle, "icons/text_align_right.png")));
190         trailButton.setSelectionFactory(new AlignmentSelectedFactory(G2DResource.URIs.Alignment_Trailing));
191         trailButton.addSelectionListener(new AlignmentSelectionListener(context, G2DResource.URIs.Alignment_Trailing));
192
193         Label colorHeader = new Label(rowComposite, support, 0);
194         colorHeader.setText("Color");
195         colorHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
196         GridDataFactory.fillDefaults().indent(20, 0).grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(colorHeader.getWidget());
197
198         Button colorButton = new Button(rowComposite, support, SWT.PUSH);
199         colorButton.setImageFactory( new CurrentColorImageFactory(colorButton.getWidget(), DATA_CURRENT_COLOR) );
200         GridDataFactory.fillDefaults().indent(0, 0).grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(colorButton.getWidget());
201         StyleEditWidget colorWidget = new StyleEditWidget(colorButton.getWidget());
202         colorButton.addSelectionListener(colorWidget);
203         support.register(colorWidget);
204     }
205
206     static class StyleEditWidget implements Widget, SelectionListener {
207
208         Control control;
209         Object input;
210
211         public StyleEditWidget(Control control) {
212             this.control = control;
213         }
214
215         @Override
216         public void widgetSelected(SelectionEvent e) {
217             final Resource[] resources = ResourceAdaptionUtils.toResources(input);
218             if (resources.length != 0) {
219                 ColorDialog dialog = new ColorDialog(WorkbenchUtils.getActiveWorkbenchWindowShell(), SWT.NONE);
220                 dialog.setText("Set Monitor Color");
221                 Color oldColor = (Color) control.getData(DATA_CURRENT_COLOR);
222                 if (oldColor != null) {
223                     RGB oldRgb = Colors.rgb(oldColor);
224                     dialog.setRGB(oldRgb);
225                 }
226                 RGB rgb = dialog.open();
227                 if (rgb != null) {
228                     setColor(resources, rgb);
229                 }
230             }
231         }
232
233         @Override
234         public void widgetDefaultSelected(SelectionEvent e) {
235             widgetSelected(e);
236         }
237
238         @Override
239         public void setInput(ISessionContext context, Object input) {
240             this.input = input;
241         }
242
243     }
244
245     static class AlignmentSelectedFactory extends ReadFactoryImpl<Resource, Boolean> {
246         private String alignmentURI;
247
248         public AlignmentSelectedFactory(String alignmentURI) {
249             this.alignmentURI = alignmentURI;
250         }
251
252         @Override
253         public Object getIdentity(Object inputContents) {
254             return new Triple<Object, Object, Class<?>>(inputContents, alignmentURI, getClass());
255         }
256
257         @Override
258         public Boolean perform(ReadGraph graph, Resource monitor) throws DatabaseException {
259             G2DResource G2D = G2DResource.getInstance(graph);
260             Resource expectedAlignment = graph.getResource(alignmentURI);
261             Resource alignment = graph.getPossibleObject(monitor, G2D.HasHorizontalAlignment);
262             return ObjectUtils.objectEquals(expectedAlignment, alignment);
263         }
264     }
265
266     static class AlignmentSelectionListener extends SelectionListenerImpl<Resource> {
267         private String alignmentURI;
268
269         public AlignmentSelectionListener(ISessionContext context, String alignmentURI) {
270             super(context);
271             this.alignmentURI = alignmentURI;
272         }
273
274         @Override
275         public void apply(WriteGraph graph, Resource monitor) throws DatabaseException {
276             graph.markUndoPoint();
277             G2DResource G2D = G2DResource.getInstance(graph);
278             Resource alignment = graph.getResource(alignmentURI);
279             graph.deny(monitor, G2D.HasHorizontalAlignment);
280             graph.claim(monitor, G2D.HasHorizontalAlignment, null, alignment);
281         }
282     }
283
284     private static void setColor(final Resource[] resources, final RGB rgb) {
285         Color c = Colors.irgb(rgb);
286         final double[] color = new double[] { c.getR(), c.getG(), c.getB() };
287         try {
288             Simantics.sync(new WriteRequest() {
289                 @Override
290                 public void perform(WriteGraph graph) throws DatabaseException {
291                     graph.markUndoPoint();
292                     Layer0 L0 = Layer0.getInstance(graph);
293                     DiagramResource DIA = DiagramResource.getInstance(graph);
294                     for (Resource r : resources) {
295                         Resource realizedColor = graph.syncRequest(new PossibleObjectWithType(r, DIA.HasColor, DIA.RealizedColor));
296                         if (realizedColor == null) {
297                             realizedColor = graph.newResource();
298                             graph.claim(realizedColor, L0.InstanceOf, null, DIA.RealizedColor);
299                             graph.claim(r, DIA.HasColor, realizedColor);
300                         }
301                         graph.claimLiteral(realizedColor, DIA.RealizedColor_HasRGB, L0.DoubleArray, color, Bindings.DOUBLE_ARRAY);
302                     }
303                     CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
304                     graph.addMetadata(cm.add("Set color to " + rgb + " for resources " + Arrays.toString(resources)));
305                 }
306             });
307         } catch (DatabaseException e) {
308             ErrorLogger.defaultLogError(e);
309         }
310     }
311
312     private static class CurrentColorImageFactory extends ReadFactoryImpl<Resource, ImageDescriptor> {
313
314         Control control;
315         String colorKey;
316
317         public CurrentColorImageFactory(Control control, String colorKey) {
318             this.control = control;
319             this.colorKey = colorKey;
320         }
321
322         @Override
323         public ImageDescriptor perform(ReadGraph graph, Resource monitor) throws DatabaseException {
324             DiagramResource DIA = DiagramResource.getInstance(graph);
325             Color c = graph.getPossibleRelatedAdapter(monitor, DIA.HasColor, Color.class);
326             if (c == null)
327                  c = new org.simantics.datatypes.literal.RGB.Integer(0, 0, 0);
328             final Color color = c;
329             if (control != null) {
330                 SWTUtils.asyncExec(control, new Runnable() {
331                     @Override
332                     public void run() {
333                         if (!control.isDisposed())
334                             control.setData(colorKey, color);
335                     }
336                 });
337             }
338             RGB rgb = Colors.rgb(color);
339             return new ColorImageDescriptor(rgb.red, rgb.green, rgb.blue, 16, 16, false);
340         }
341
342     }
343
344 }