-package org.simantics.modeling.ui.diagram.monitor;\r
-\r
-import java.util.Arrays;\r
-\r
-import org.eclipse.core.runtime.Platform;\r
-import org.eclipse.jface.layout.GridDataFactory;\r
-import org.eclipse.jface.layout.GridLayoutFactory;\r
-import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.jface.resource.JFaceResources;\r
-import org.eclipse.jface.resource.LocalResourceManager;\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.events.SelectionEvent;\r
-import org.eclipse.swt.events.SelectionListener;\r
-import org.eclipse.swt.graphics.Image;\r
-import org.eclipse.swt.graphics.RGB;\r
-import org.eclipse.swt.widgets.ColorDialog;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.ui.IWorkbenchSite;\r
-import org.osgi.framework.Bundle;\r
-import org.simantics.Simantics;\r
-import org.simantics.browsing.ui.swt.widgets.Button;\r
-import org.simantics.browsing.ui.swt.widgets.Combo;\r
-import org.simantics.browsing.ui.swt.widgets.Label;\r
-import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
-import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;\r
-import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
-import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
-import org.simantics.common.color.Color;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.util.ObjectUtils;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.CommentMetadata;\r
-import org.simantics.db.common.request.PossibleIndexRoot;\r
-import org.simantics.db.common.request.PossibleObjectWithType;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.diagram.stubs.G2DResource;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.operation.Layer0X;\r
-import org.simantics.selectionview.ConfigurationComposite;\r
-import org.simantics.ui.colors.Colors;\r
-import org.simantics.ui.utils.ResourceAdaptionUtils;\r
-import org.simantics.utils.datastructures.Triple;\r
-import org.simantics.utils.ui.BundleUtils;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.SWTUtils;\r
-import org.simantics.utils.ui.gfx.ColorImageDescriptor;\r
-import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
-\r
-public class MonitorComposite extends ConfigurationComposite {\r
-\r
- private static final String DATA_CURRENT_COLOR = "COLOR";\r
-\r
- public void create(final Composite body, IWorkbenchSite site, final ISessionContext context, final WidgetSupport support) {\r
- final Display display = body.getDisplay();\r
-\r
- body.setBackground(display.getSystemColor(SWT.COLOR_WHITE));\r
-\r
- Composite buttonComposite = new Composite(body, SWT.NONE);\r
- buttonComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));\r
- GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(buttonComposite);\r
- GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(buttonComposite);\r
-\r
- LocalResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources(), buttonComposite);\r
- support.setParameter(WidgetSupport.RESOURCE_MANAGER, resourceManager);\r
-\r
- Label templateHeader = new Label(buttonComposite, support, 0);\r
- templateHeader.setText("Template");\r
- //templateHeader.setFont(smallFont);\r
- templateHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));\r
- GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(templateHeader.getWidget());\r
-\r
- Combo templateCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);\r
- templateCombo.setItemFactory(new AvailableTemplateFactory());\r
- templateCombo.setSelectionFactory(new CurrentTemplateFactory());\r
- templateCombo.addModifyListener(new TemplateModifier());\r
- GridDataFactory.fillDefaults().grab(true, false).applyTo(templateCombo.getWidget());\r
-\r
- Button setDefaultButton = new Button(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);\r
- setDefaultButton.setText("Set As Default");\r
- setDefaultButton.setTooltipText("Set As Default for Newly Created Monitors");\r
- setDefaultButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
- @Override\r
- public void apply(WriteGraph graph, Resource monitor) throws DatabaseException {\r
- graph.markUndoPoint();\r
- Layer0X L0X = Layer0X.getInstance(graph);\r
- DiagramResource DIA = DiagramResource.getInstance(graph);\r
- Resource root = graph.syncRequest(new PossibleIndexRoot(monitor));\r
- Resource currentTemplate = graph.getPossibleObject(monitor, L0X.ObtainsProperty);\r
- if (root != null && !graph.isImmutable(root) && currentTemplate != null) {\r
- graph.deny(root, DIA.HasDefaultMonitorTemplate);\r
- graph.claim(root, DIA.HasDefaultMonitorTemplate, currentTemplate);\r
- }\r
- }\r
- });\r
- GridDataFactory.fillDefaults().grab(false, false).applyTo(setDefaultButton.getWidget());\r
-\r
- Button resetButton = new Button(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);\r
- resetButton.setText("Reset Local Changes");\r
- resetButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
- @Override\r
- public void apply(WriteGraph graph, Resource monitor) throws DatabaseException {\r
- graph.markUndoPoint();\r
- Layer0X L0X = Layer0X.getInstance(graph);\r
- DiagramResource DIA = DiagramResource.getInstance(graph);\r
- if(graph.hasStatement(monitor, DIA.HasFont)) graph.deny(monitor, DIA.HasFont);\r
- if(graph.hasStatement(monitor, DIA.HasFormatter)) graph.deny(monitor, DIA.HasFormatter);\r
- if(graph.hasStatement(monitor, DIA.HasColor)) graph.deny(monitor, DIA.HasColor);\r
- if(graph.hasStatement(monitor, L0X.HasExpression)) graph.deny(monitor, L0X.HasExpression);\r
- }\r
- });\r
-\r
- GridDataFactory.fillDefaults().grab(false, false).applyTo(resetButton.getWidget());\r
-\r
- Label fontHeader = new Label(buttonComposite, support, 0);\r
- fontHeader.setText("Font Family");\r
- //fontHeader.setFont(smallFont);\r
- fontHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));\r
- GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(fontHeader.getWidget());\r
-\r
- Combo fontCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);\r
- fontCombo.setItemFactory2(new AvailableFontFactory());\r
- fontCombo.setSelectionFactory(new CurrentFontFactory());\r
- fontCombo.addModifyListener(new FontModifier());\r
- GridDataFactory.fillDefaults().grab(true, false).span(3,1).applyTo(fontCombo.getWidget());\r
-\r
- Label sizeHeader = new Label(buttonComposite, support, 0);\r
- sizeHeader.setText("Font Size");\r
- //sizeHeader.setFont(smallFont);\r
- sizeHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));\r
- GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(sizeHeader.getWidget());\r
-\r
- Combo sizeCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);\r
- sizeCombo.setItemFactory2(new AvailableFontSizeFactory());\r
- sizeCombo.setSelectionFactory(new CurrentFontSizeFactory());\r
- sizeCombo.addModifyListener(new FontSizeModifier());\r
- GridDataFactory.fillDefaults().grab(false, false).span(3,1).minSize(50, 0).applyTo(sizeCombo.getWidget());\r
-\r
- Label formatterHeader = new Label(buttonComposite, support, 0);\r
- formatterHeader.setText("Formatting");\r
- //formatterHeader.setFont(smallFont);\r
- formatterHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));\r
- GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(formatterHeader.getWidget());\r
-\r
- Combo formatCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);\r
- formatCombo.setItemFactory(new AvailableFormatFactory());\r
- formatCombo.setSelectionFactory(new CurrentFormatFactory());\r
- formatCombo.addModifyListener(new FormatModifier());\r
- GridDataFactory.fillDefaults().grab(true, false).span(3,1).applyTo(formatCombo.getWidget());\r
-\r
- Composite rowComposite = new Composite(buttonComposite, SWT.NONE);\r
- rowComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));\r
- GridDataFactory.fillDefaults().span(3, 1).grab(true, false).applyTo(rowComposite);\r
- GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(rowComposite);\r
-\r
- Label alignHeader = new Label(rowComposite, support, 0);\r
- alignHeader.setText("Alignment");\r
- //alignHeader.setFont(smallFont);\r
- alignHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));\r
- GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(alignHeader.getWidget());\r
-\r
- Bundle iconBundle = Platform.getBundle("com.famfamfam.silk");\r
-\r
- Composite alignComposite = new Composite(rowComposite, SWT.NONE);\r
- alignComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));\r
- GridDataFactory.fillDefaults().span(1, 1).grab(false, false).applyTo(alignComposite);\r
- GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(alignComposite);\r
-\r
- Button leadButton = new Button(alignComposite, support, SWT.TOGGLE);\r
- leadButton.setTooltipText("Left Alignment");\r
- leadButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromBundle(iconBundle, "icons/text_align_left.png")));\r
- leadButton.setSelectionFactory(new AlignmentSelectedFactory(G2DResource.URIs.Alignment_Leading));\r
- leadButton.addSelectionListener(new AlignmentSelectionListener(context, G2DResource.URIs.Alignment_Leading));\r
-\r
- Button centerButton = new Button(alignComposite, support, SWT.TOGGLE);\r
- centerButton.setTooltipText("Center Alignment");\r
- centerButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromBundle(iconBundle, "icons/text_align_center.png")));\r
- centerButton.setSelectionFactory(new AlignmentSelectedFactory(G2DResource.URIs.Alignment_Center));\r
- centerButton.addSelectionListener(new AlignmentSelectionListener(context, G2DResource.URIs.Alignment_Center));\r
-\r
- Button trailButton = new Button(alignComposite, support, SWT.TOGGLE);\r
- trailButton.setTooltipText("Right Alignment");\r
- trailButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromBundle(iconBundle, "icons/text_align_right.png")));\r
- trailButton.setSelectionFactory(new AlignmentSelectedFactory(G2DResource.URIs.Alignment_Trailing));\r
- trailButton.addSelectionListener(new AlignmentSelectionListener(context, G2DResource.URIs.Alignment_Trailing));\r
-\r
- Label colorHeader = new Label(rowComposite, support, 0);\r
- colorHeader.setText("Color");\r
- colorHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));\r
- GridDataFactory.fillDefaults().indent(20, 0).grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(colorHeader.getWidget());\r
-\r
- Button colorButton = new Button(rowComposite, support, SWT.PUSH);\r
- colorButton.setImageFactory( new CurrentColorImageFactory(colorButton.getWidget(), DATA_CURRENT_COLOR) );\r
- GridDataFactory.fillDefaults().indent(0, 0).grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(colorButton.getWidget());\r
- StyleEditWidget colorWidget = new StyleEditWidget(colorButton.getWidget());\r
- colorButton.addSelectionListener(colorWidget);\r
- support.register(colorWidget);\r
- }\r
-\r
- static class StyleEditWidget implements Widget, SelectionListener {\r
-\r
- Control control;\r
- Object input;\r
-\r
- public StyleEditWidget(Control control) {\r
- this.control = control;\r
- }\r
-\r
- @Override\r
- public void widgetSelected(SelectionEvent e) {\r
- final Resource[] resources = ResourceAdaptionUtils.toResources(input);\r
- if (resources.length != 0) {\r
- ColorDialog dialog = new ColorDialog(WorkbenchUtils.getActiveWorkbenchWindowShell(), SWT.NONE);\r
- dialog.setText("Set Monitor Color");\r
- Color oldColor = (Color) control.getData(DATA_CURRENT_COLOR);\r
- if (oldColor != null) {\r
- RGB oldRgb = Colors.rgb(oldColor);\r
- dialog.setRGB(oldRgb);\r
- }\r
- RGB rgb = dialog.open();\r
- if (rgb != null) {\r
- setColor(resources, rgb);\r
- }\r
- }\r
- }\r
-\r
- @Override\r
- public void widgetDefaultSelected(SelectionEvent e) {\r
- widgetSelected(e);\r
- }\r
-\r
- @Override\r
- public void setInput(ISessionContext context, Object input) {\r
- this.input = input;\r
- }\r
-\r
- }\r
-\r
- static class AlignmentSelectedFactory extends ReadFactoryImpl<Resource, Boolean> {\r
- private String alignmentURI;\r
-\r
- public AlignmentSelectedFactory(String alignmentURI) {\r
- this.alignmentURI = alignmentURI;\r
- }\r
-\r
- @Override\r
- public Object getIdentity(Object inputContents) {\r
- return new Triple<Object, Object, Class<?>>(inputContents, alignmentURI, getClass());\r
- }\r
-\r
- @Override\r
- public Boolean perform(ReadGraph graph, Resource monitor) throws DatabaseException {\r
- G2DResource G2D = G2DResource.getInstance(graph);\r
- Resource expectedAlignment = graph.getResource(alignmentURI);\r
- Resource alignment = graph.getPossibleObject(monitor, G2D.HasHorizontalAlignment);\r
- return ObjectUtils.objectEquals(expectedAlignment, alignment);\r
- }\r
- }\r
-\r
- static class AlignmentSelectionListener extends SelectionListenerImpl<Resource> {\r
- private String alignmentURI;\r
-\r
- public AlignmentSelectionListener(ISessionContext context, String alignmentURI) {\r
- super(context);\r
- this.alignmentURI = alignmentURI;\r
- }\r
-\r
- @Override\r
- public void apply(WriteGraph graph, Resource monitor) throws DatabaseException {\r
- graph.markUndoPoint();\r
- G2DResource G2D = G2DResource.getInstance(graph);\r
- Resource alignment = graph.getResource(alignmentURI);\r
- graph.deny(monitor, G2D.HasHorizontalAlignment);\r
- graph.claim(monitor, G2D.HasHorizontalAlignment, null, alignment);\r
- }\r
- }\r
-\r
- private static void setColor(final Resource[] resources, final RGB rgb) {\r
- Color c = Colors.irgb(rgb);\r
- final double[] color = new double[] { c.getR(), c.getG(), c.getB() };\r
- try {\r
- Simantics.sync(new WriteRequest() {\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- graph.markUndoPoint();\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- DiagramResource DIA = DiagramResource.getInstance(graph);\r
- for (Resource r : resources) {\r
- Resource realizedColor = graph.syncRequest(new PossibleObjectWithType(r, DIA.HasColor, DIA.RealizedColor));\r
- if (realizedColor == null) {\r
- realizedColor = graph.newResource();\r
- graph.claim(realizedColor, L0.InstanceOf, null, DIA.RealizedColor);\r
- graph.claim(r, DIA.HasColor, realizedColor);\r
- }\r
- graph.claimLiteral(realizedColor, DIA.RealizedColor_HasRGB, L0.DoubleArray, color, Bindings.DOUBLE_ARRAY);\r
- }\r
- CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
- graph.addMetadata(cm.add("Set color to " + rgb + " for resources " + Arrays.toString(resources)));\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- ErrorLogger.defaultLogError(e);\r
- }\r
- }\r
-\r
- private static class CurrentColorImageFactory extends ReadFactoryImpl<Resource, ImageDescriptor> {\r
-\r
- Control control;\r
- String colorKey;\r
-\r
- public CurrentColorImageFactory(Control control, String colorKey) {\r
- this.control = control;\r
- this.colorKey = colorKey;\r
- }\r
-\r
- @Override\r
- public ImageDescriptor perform(ReadGraph graph, Resource monitor) throws DatabaseException {\r
- DiagramResource DIA = DiagramResource.getInstance(graph);\r
- Color c = graph.getPossibleRelatedAdapter(monitor, DIA.HasColor, Color.class);\r
- if (c == null)\r
- c = new org.simantics.datatypes.literal.RGB.Integer(0, 0, 0);\r
- final Color color = c;\r
- if (control != null) {\r
- SWTUtils.asyncExec(control, new Runnable() {\r
- @Override\r
- public void run() {\r
- if (!control.isDisposed())\r
- control.setData(colorKey, color);\r
- }\r
- });\r
- }\r
- RGB rgb = Colors.rgb(color);\r
- return new ColorImageDescriptor(rgb.red, rgb.green, rgb.blue, 16, 16, false);\r
- }\r
-\r
- }\r
-\r
-}\r
+package org.simantics.modeling.ui.diagram.monitor;
+
+import java.util.Arrays;
+
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.resource.JFaceResources;
+import org.eclipse.jface.resource.LocalResourceManager;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.RGB;
+import org.eclipse.swt.widgets.ColorDialog;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.IWorkbenchSite;
+import org.osgi.framework.Bundle;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.swt.widgets.Button;
+import org.simantics.browsing.ui.swt.widgets.Combo;
+import org.simantics.browsing.ui.swt.widgets.Label;
+import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
+import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;
+import org.simantics.browsing.ui.swt.widgets.impl.Widget;
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
+import org.simantics.common.color.Color;
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.util.ObjectUtils;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.request.PossibleIndexRoot;
+import org.simantics.db.common.request.PossibleObjectWithType;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.diagram.stubs.G2DResource;
+import org.simantics.layer0.Layer0;
+import org.simantics.operation.Layer0X;
+import org.simantics.selectionview.ConfigurationComposite;
+import org.simantics.ui.colors.Colors;
+import org.simantics.ui.utils.ResourceAdaptionUtils;
+import org.simantics.utils.datastructures.Triple;
+import org.simantics.utils.ui.BundleUtils;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.SWTUtils;
+import org.simantics.utils.ui.gfx.ColorImageDescriptor;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
+
+public class MonitorComposite extends ConfigurationComposite {
+
+ private static final String DATA_CURRENT_COLOR = "COLOR";
+
+ public void create(final Composite body, IWorkbenchSite site, final ISessionContext context, final WidgetSupport support) {
+ final Display display = body.getDisplay();
+
+ body.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
+
+ Composite buttonComposite = new Composite(body, SWT.NONE);
+ buttonComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
+ GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(buttonComposite);
+ GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(buttonComposite);
+
+ LocalResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources(), buttonComposite);
+ support.setParameter(WidgetSupport.RESOURCE_MANAGER, resourceManager);
+
+ Label templateHeader = new Label(buttonComposite, support, 0);
+ templateHeader.setText("Template");
+ //templateHeader.setFont(smallFont);
+ templateHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
+ GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(templateHeader.getWidget());
+
+ Combo templateCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
+ templateCombo.setItemFactory(new AvailableTemplateFactory());
+ templateCombo.setSelectionFactory(new CurrentTemplateFactory());
+ templateCombo.addModifyListener(new TemplateModifier());
+ GridDataFactory.fillDefaults().grab(true, false).applyTo(templateCombo.getWidget());
+
+ Button setDefaultButton = new Button(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
+ setDefaultButton.setText("Set As Default");
+ setDefaultButton.setTooltipText("Set As Default for Newly Created Monitors");
+ setDefaultButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
+ @Override
+ public void apply(WriteGraph graph, Resource monitor) throws DatabaseException {
+ graph.markUndoPoint();
+ Layer0X L0X = Layer0X.getInstance(graph);
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ Resource root = graph.syncRequest(new PossibleIndexRoot(monitor));
+ Resource currentTemplate = graph.getPossibleObject(monitor, L0X.ObtainsProperty);
+ if (root != null && !graph.isImmutable(root) && currentTemplate != null) {
+ graph.deny(root, DIA.HasDefaultMonitorTemplate);
+ graph.claim(root, DIA.HasDefaultMonitorTemplate, currentTemplate);
+ }
+ }
+ });
+ GridDataFactory.fillDefaults().grab(false, false).applyTo(setDefaultButton.getWidget());
+
+ Button resetButton = new Button(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
+ resetButton.setText("Reset Local Changes");
+ resetButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
+ @Override
+ public void apply(WriteGraph graph, Resource monitor) throws DatabaseException {
+ graph.markUndoPoint();
+ Layer0X L0X = Layer0X.getInstance(graph);
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ if(graph.hasStatement(monitor, DIA.HasFont)) graph.deny(monitor, DIA.HasFont);
+ if(graph.hasStatement(monitor, DIA.HasFormatter)) graph.deny(monitor, DIA.HasFormatter);
+ if(graph.hasStatement(monitor, DIA.HasColor)) graph.deny(monitor, DIA.HasColor);
+ if(graph.hasStatement(monitor, L0X.HasExpression)) graph.deny(monitor, L0X.HasExpression);
+ }
+ });
+
+ GridDataFactory.fillDefaults().grab(false, false).applyTo(resetButton.getWidget());
+
+ Label fontHeader = new Label(buttonComposite, support, 0);
+ fontHeader.setText("Font Family");
+ //fontHeader.setFont(smallFont);
+ fontHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
+ GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(fontHeader.getWidget());
+
+ Combo fontCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
+ fontCombo.setItemFactory2(new AvailableFontFactory());
+ fontCombo.setSelectionFactory(new CurrentFontFactory());
+ fontCombo.addModifyListener(new FontModifier());
+ GridDataFactory.fillDefaults().grab(true, false).span(3,1).applyTo(fontCombo.getWidget());
+
+ Label sizeHeader = new Label(buttonComposite, support, 0);
+ sizeHeader.setText("Font Size");
+ //sizeHeader.setFont(smallFont);
+ sizeHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
+ GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(sizeHeader.getWidget());
+
+ Combo sizeCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
+ sizeCombo.setItemFactory2(new AvailableFontSizeFactory());
+ sizeCombo.setSelectionFactory(new CurrentFontSizeFactory());
+ sizeCombo.addModifyListener(new FontSizeModifier());
+ GridDataFactory.fillDefaults().grab(false, false).span(3,1).minSize(50, 0).applyTo(sizeCombo.getWidget());
+
+ Label formatterHeader = new Label(buttonComposite, support, 0);
+ formatterHeader.setText("Formatting");
+ //formatterHeader.setFont(smallFont);
+ formatterHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
+ GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(formatterHeader.getWidget());
+
+ Combo formatCombo = new Combo(buttonComposite, support, SWT.NONE | SWT.READ_ONLY);
+ formatCombo.setItemFactory(new AvailableFormatFactory());
+ formatCombo.setSelectionFactory(new CurrentFormatFactory());
+ formatCombo.addModifyListener(new FormatModifier());
+ GridDataFactory.fillDefaults().grab(true, false).span(3,1).applyTo(formatCombo.getWidget());
+
+ Composite rowComposite = new Composite(buttonComposite, SWT.NONE);
+ rowComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
+ GridDataFactory.fillDefaults().span(3, 1).grab(true, false).applyTo(rowComposite);
+ GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(rowComposite);
+
+ Label alignHeader = new Label(rowComposite, support, 0);
+ alignHeader.setText("Alignment");
+ //alignHeader.setFont(smallFont);
+ alignHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
+ GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(alignHeader.getWidget());
+
+ Bundle iconBundle = Platform.getBundle("com.famfamfam.silk");
+
+ Composite alignComposite = new Composite(rowComposite, SWT.NONE);
+ alignComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
+ GridDataFactory.fillDefaults().span(1, 1).grab(false, false).applyTo(alignComposite);
+ GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(alignComposite);
+
+ Button leadButton = new Button(alignComposite, support, SWT.TOGGLE);
+ leadButton.setTooltipText("Left Alignment");
+ leadButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromBundle(iconBundle, "icons/text_align_left.png")));
+ leadButton.setSelectionFactory(new AlignmentSelectedFactory(G2DResource.URIs.Alignment_Leading));
+ leadButton.addSelectionListener(new AlignmentSelectionListener(context, G2DResource.URIs.Alignment_Leading));
+
+ Button centerButton = new Button(alignComposite, support, SWT.TOGGLE);
+ centerButton.setTooltipText("Center Alignment");
+ centerButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromBundle(iconBundle, "icons/text_align_center.png")));
+ centerButton.setSelectionFactory(new AlignmentSelectedFactory(G2DResource.URIs.Alignment_Center));
+ centerButton.addSelectionListener(new AlignmentSelectionListener(context, G2DResource.URIs.Alignment_Center));
+
+ Button trailButton = new Button(alignComposite, support, SWT.TOGGLE);
+ trailButton.setTooltipText("Right Alignment");
+ trailButton.setImage((Image) resourceManager.get(BundleUtils.getImageDescriptorFromBundle(iconBundle, "icons/text_align_right.png")));
+ trailButton.setSelectionFactory(new AlignmentSelectedFactory(G2DResource.URIs.Alignment_Trailing));
+ trailButton.addSelectionListener(new AlignmentSelectionListener(context, G2DResource.URIs.Alignment_Trailing));
+
+ Label colorHeader = new Label(rowComposite, support, 0);
+ colorHeader.setText("Color");
+ colorHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
+ GridDataFactory.fillDefaults().indent(20, 0).grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(colorHeader.getWidget());
+
+ Button colorButton = new Button(rowComposite, support, SWT.PUSH);
+ colorButton.setImageFactory( new CurrentColorImageFactory(colorButton.getWidget(), DATA_CURRENT_COLOR) );
+ GridDataFactory.fillDefaults().indent(0, 0).grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(colorButton.getWidget());
+ StyleEditWidget colorWidget = new StyleEditWidget(colorButton.getWidget());
+ colorButton.addSelectionListener(colorWidget);
+ support.register(colorWidget);
+ }
+
+ static class StyleEditWidget implements Widget, SelectionListener {
+
+ Control control;
+ Object input;
+
+ public StyleEditWidget(Control control) {
+ this.control = control;
+ }
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ final Resource[] resources = ResourceAdaptionUtils.toResources(input);
+ if (resources.length != 0) {
+ ColorDialog dialog = new ColorDialog(WorkbenchUtils.getActiveWorkbenchWindowShell(), SWT.NONE);
+ dialog.setText("Set Monitor Color");
+ Color oldColor = (Color) control.getData(DATA_CURRENT_COLOR);
+ if (oldColor != null) {
+ RGB oldRgb = Colors.rgb(oldColor);
+ dialog.setRGB(oldRgb);
+ }
+ RGB rgb = dialog.open();
+ if (rgb != null) {
+ setColor(resources, rgb);
+ }
+ }
+ }
+
+ @Override
+ public void widgetDefaultSelected(SelectionEvent e) {
+ widgetSelected(e);
+ }
+
+ @Override
+ public void setInput(ISessionContext context, Object input) {
+ this.input = input;
+ }
+
+ }
+
+ static class AlignmentSelectedFactory extends ReadFactoryImpl<Resource, Boolean> {
+ private String alignmentURI;
+
+ public AlignmentSelectedFactory(String alignmentURI) {
+ this.alignmentURI = alignmentURI;
+ }
+
+ @Override
+ public Object getIdentity(Object inputContents) {
+ return new Triple<Object, Object, Class<?>>(inputContents, alignmentURI, getClass());
+ }
+
+ @Override
+ public Boolean perform(ReadGraph graph, Resource monitor) throws DatabaseException {
+ G2DResource G2D = G2DResource.getInstance(graph);
+ Resource expectedAlignment = graph.getResource(alignmentURI);
+ Resource alignment = graph.getPossibleObject(monitor, G2D.HasHorizontalAlignment);
+ return ObjectUtils.objectEquals(expectedAlignment, alignment);
+ }
+ }
+
+ static class AlignmentSelectionListener extends SelectionListenerImpl<Resource> {
+ private String alignmentURI;
+
+ public AlignmentSelectionListener(ISessionContext context, String alignmentURI) {
+ super(context);
+ this.alignmentURI = alignmentURI;
+ }
+
+ @Override
+ public void apply(WriteGraph graph, Resource monitor) throws DatabaseException {
+ graph.markUndoPoint();
+ G2DResource G2D = G2DResource.getInstance(graph);
+ Resource alignment = graph.getResource(alignmentURI);
+ graph.deny(monitor, G2D.HasHorizontalAlignment);
+ graph.claim(monitor, G2D.HasHorizontalAlignment, null, alignment);
+ }
+ }
+
+ private static void setColor(final Resource[] resources, final RGB rgb) {
+ Color c = Colors.irgb(rgb);
+ final double[] color = new double[] { c.getR(), c.getG(), c.getB() };
+ try {
+ Simantics.sync(new WriteRequest() {
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ graph.markUndoPoint();
+ Layer0 L0 = Layer0.getInstance(graph);
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ for (Resource r : resources) {
+ Resource realizedColor = graph.syncRequest(new PossibleObjectWithType(r, DIA.HasColor, DIA.RealizedColor));
+ if (realizedColor == null) {
+ realizedColor = graph.newResource();
+ graph.claim(realizedColor, L0.InstanceOf, null, DIA.RealizedColor);
+ graph.claim(r, DIA.HasColor, realizedColor);
+ }
+ graph.claimLiteral(realizedColor, DIA.RealizedColor_HasRGB, L0.DoubleArray, color, Bindings.DOUBLE_ARRAY);
+ }
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
+ graph.addMetadata(cm.add("Set color to " + rgb + " for resources " + Arrays.toString(resources)));
+ }
+ });
+ } catch (DatabaseException e) {
+ ErrorLogger.defaultLogError(e);
+ }
+ }
+
+ private static class CurrentColorImageFactory extends ReadFactoryImpl<Resource, ImageDescriptor> {
+
+ Control control;
+ String colorKey;
+
+ public CurrentColorImageFactory(Control control, String colorKey) {
+ this.control = control;
+ this.colorKey = colorKey;
+ }
+
+ @Override
+ public ImageDescriptor perform(ReadGraph graph, Resource monitor) throws DatabaseException {
+ DiagramResource DIA = DiagramResource.getInstance(graph);
+ Color c = graph.getPossibleRelatedAdapter(monitor, DIA.HasColor, Color.class);
+ if (c == null)
+ c = new org.simantics.datatypes.literal.RGB.Integer(0, 0, 0);
+ final Color color = c;
+ if (control != null) {
+ SWTUtils.asyncExec(control, new Runnable() {
+ @Override
+ public void run() {
+ if (!control.isDisposed())
+ control.setData(colorKey, color);
+ }
+ });
+ }
+ RGB rgb = Colors.rgb(color);
+ return new ColorImageDescriptor(rgb.red, rgb.green, rgb.blue, 16, 16, false);
+ }
+
+ }
+
+}