1 package org.simantics.diagram.function;
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Point2D;
5 import java.util.Arrays;
6 import java.util.Collection;
8 import java.util.function.Consumer;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.graphics.FontData;
12 import org.eclipse.swt.widgets.Control;
13 import org.eclipse.swt.widgets.FontDialog;
14 import org.simantics.Simantics;
15 import org.simantics.browsing.ui.NodeContext;
16 import org.simantics.browsing.ui.content.Labeler.DialogModifier;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.request.PossibleIndexRoot;
22 import org.simantics.db.common.request.UniqueRead;
23 import org.simantics.db.common.request.WriteRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.exception.MissingVariableException;
26 import org.simantics.db.layer0.request.PossibleConfiguration;
27 import org.simantics.db.layer0.variable.Variable;
28 import org.simantics.db.layer0.variable.Variables;
29 import org.simantics.diagram.content.ElementContext;
30 import org.simantics.diagram.stubs.DiagramResource;
31 import org.simantics.diagram.stubs.G2DResource;
32 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
33 import org.simantics.issues.common.IssueUtils;
34 import org.simantics.layer0.Layer0;
35 import org.simantics.modeling.ModelingResources;
36 import org.simantics.modeling.template2d.ontology.Template2dResource;
37 import org.simantics.scenegraph.loader.SceneGraphContext;
38 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
39 import org.simantics.scl.reflection.annotations.SCLValue;
40 import org.simantics.ui.fonts.Fonts;
41 import org.simantics.utils.ui.AdaptionUtils;
42 import org.simantics.utils.ui.ErrorLogger;
47 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
48 public static Variable defaultRuntimeVariable(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
50 SceneGraphContext vc = ScenegraphLoaderUtils.getContext(graph, context);
51 if(vc == null) return null;
53 DiagramResource DIA = DiagramResource.getInstance(graph);
54 String uri = graph.getRelatedValue(vc.getRuntime(), DIA.RuntimeDiagram_HasVariable, Bindings.STRING);
55 return Variables.getVariable(graph, uri);
59 @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
60 public static String referenceText(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
62 //DiagramResource DIA = DiagramResource.getInstance(graph);
64 //Resource runtime = ScenegraphLoaderUtils.getRuntime(graph, context);
65 String path = context.getParent(graph).getPropertyValue(graph, "reference", Bindings.STRING);
67 // diagram uri and diagram variable
68 // String diagramURI = graph.getRelatedValue(runtime, DIA.RuntimeDiagram_HasVariable, Bindings.STRING);
69 // Variable diagramVariable = org.simantics.db.layer0.variable.Variables.getVariable(graph, diagramURI);
71 // Variable property = diagramVariable.browse(graph, path);
74 Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
75 PredefinedVariables vars = PredefinedVariables.getInstance();
76 Variable property = vars.getVariable(graph, path, converter, selection);
78 return property.getValue(graph).toString();
79 } catch (MissingVariableException e){
80 return "Invalid path.";
85 @SCLValue(type = "ReadGraph -> Resource -> ElementContext -> a")
86 public static Object modelledTransform(ReadGraph graph, Resource converter, ElementContext context) throws DatabaseException {
87 return graph.getRelatedValue(context.element, G2DResource.getInstance(graph).HasTransform, Bindings.DOUBLE_ARRAY);
90 @SCLValue(type = "ReadGraph -> Resource -> ElementContext -> a")
91 public static Object flagTransform(ReadGraph graph, Resource converter, ElementContext context) throws DatabaseException {
92 Layer0 L0 = Layer0.getInstance(graph);
93 DiagramResource DIA = DiagramResource.getInstance(graph);
94 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
96 Resource flag = context.element;
97 Resource runtimeDiagram = context.runtime;
99 Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
101 return flagTransformImpl(graph, converter, context);
103 Resource template = getTemplateFromDiagram(graph, diagram);
104 if (template == null)
105 return flagTransformImpl(graph, converter, context);
107 double gridSize = DiagramGraphUtil.getGridSize(graph, diagram, 0.0);
109 String tableName = graph.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableBinding, Bindings.STRING);
110 Integer rowIndex = graph.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableRowIndex, Bindings.INTEGER);
111 // double[] mat = graph.getRelatedValue(flag, DIA.HasTransform, Bindings.DOUBLE_ARRAY);
114 Collection<Resource> slotTables = graph.getObjects(template, L0.ConsistsOf);
115 for (Resource slotTable:slotTables){
116 if (!graph.isInstanceOf(slotTable, TEMPLATE2D.FlagTable))
118 String name = graph.getPossibleRelatedValue(slotTable, L0.HasName, Bindings.STRING);
119 if (!name.equals(tableName))
122 double[] transform = graph.getPossibleRelatedValue2(slotTable, DIA.Scenegraph_Composite_transform, Bindings.getBindingUnchecked(double[].class));
123 Resource align = graph.getPossibleObject(slotTable, TEMPLATE2D.FlagTable_HasAlignment);
124 boolean isRightAlignment = false;
125 if (align.equals(TEMPLATE2D.FlagTable_Alignment_Right))
126 isRightAlignment = true;
127 Float width = graph.getPossibleRelatedValue(slotTable, TEMPLATE2D.FlagTable_HasWidth, Bindings.FLOAT);
128 Float rowHeight = graph.getPossibleRelatedValue(slotTable, TEMPLATE2D.FlagTable_HasRowHeigth, Bindings.FLOAT);
129 //Integer rowCount = g.getPossibleRelatedValue(slotTable, TEMPLATE2D.FlagTable_HasRowCount, Bindings.INTEGER);
131 // Rectangle2D rowBounds = new Rectangle2D.Double(0, rowHeight*rowIndex, width, rowHeight);
132 // rowBounds = GeometryUtils.transformShape(rowBounds, new AffineTransform(transform)).getBounds2D();
133 // e.setHint(FlagClass.KEY_FLAG_BOUNDS, rowBounds);
135 double[] flagTransform = graph.getRelatedValue(flag, DIA.HasTransform, Bindings.DOUBLE_ARRAY);
136 return calcTransform(transform, flagTransform, rowHeight, width, isRightAlignment, rowIndex, gridSize);
139 return flagTransformImpl(graph, converter, context);
142 private static Object flagTransformImpl(ReadGraph graph, Resource converter, ElementContext context) throws DatabaseException {
143 double[] mat = graph.getRelatedValue(context.element, G2DResource.getInstance(graph).HasTransform, Bindings.DOUBLE_ARRAY);
147 private static double[] calcTransform(double[] tableTransform, double[] flagTransform, Float rowHeight, Float tableWidth, boolean isRightAlignment, Integer flagRowIndex, double gridSize) {
148 double scale = tableTransform[3];
149 double scaledRowHeigth = scale*rowHeight;
151 // move to coordinate system of parent of iotable
152 double rowMiddleY = tableTransform[5]+(flagRowIndex+0.5F)*scaledRowHeigth;
153 if (gridSize != 0.0) {
154 double modulo = rowMiddleY%gridSize;
155 int count =(int) (rowMiddleY/gridSize);
156 double gridOnRow = (modulo > (gridSize/2))?gridSize*(1+count):gridSize*count;
157 double diff = (gridOnRow > rowMiddleY)? gridOnRow - rowMiddleY:rowMiddleY-gridOnRow;
158 if (diff < (scaledRowHeigth/2))
159 rowMiddleY = gridOnRow;
161 // move back to iotable coordinate system
162 rowMiddleY = (rowMiddleY-tableTransform[5])/scale;
164 double xx = isRightAlignment ? tableWidth : 0.0;
166 AffineTransform trans = new AffineTransform(new double[]{tableTransform[0],tableTransform[1],tableTransform[2],tableTransform[3],0.0,0.0});
167 Point2D point = new Point2D.Double(xx, rowMiddleY);
168 trans.transform(point, point);
170 double[] copy = Arrays.copyOf(flagTransform, flagTransform.length);
171 copy[4] = tableTransform[4] + point.getX();
172 copy[5] = tableTransform[5] + point.getY();
176 public static Resource getTemplate(ReadGraph graph, Resource runtimeDiagram) throws DatabaseException {
177 DiagramResource DIA = DiagramResource.getInstance(graph);
178 Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
181 return getTemplateFromDiagram(graph, diagram);
184 public static Resource getTemplateFromDiagram(ReadGraph graph, Resource diagram) throws DatabaseException {
185 Template2dResource TEMPLATE2D = Template2dResource.getInstance(graph);
186 Resource template = graph.getPossibleObject(diagram, TEMPLATE2D.HasDrawingTemplate);
190 @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
191 public static String diagramElementIssuePath(ReadGraph graph, Resource converter, Variable property) throws DatabaseException {
192 Layer0 L0 = Layer0.getInstance(graph);
193 ModelingResources MOD = ModelingResources.getInstance(graph);
194 DiagramResource DIA = DiagramResource.getInstance(graph);
196 List<Resource> contexts = IssueUtils.getContextsForProperty(graph, property);
197 if(contexts.isEmpty()) return "";
199 Resource context = contexts.get(0);
201 Resource mapped = graph.getPossibleObject(context, MOD.ComponentToElement);
202 if(mapped != null) context = mapped;
203 mapped = graph.getPossibleObject(context, MOD.ConnectionToDiagramConnection);
204 if(mapped != null) context = mapped;
206 if(!graph.isInstanceOf(context, DIA.Element)) return "";
208 Resource configuration = graph.sync(new PossibleConfiguration(context));
209 if(configuration == null) return "";
211 Resource diagram = graph.getPossibleObject(context, L0.PartOf);
212 if(diagram == null) return "";
214 Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
215 if(composite == null) return "";
217 Resource issueRoot = Variables.getPossibleIndexRoot(graph, property);
218 if(issueRoot == null) return "";
219 Resource contextRoot = graph.sync(new PossibleIndexRoot(composite));
220 if(contextRoot == null) return "";
221 if(issueRoot.equals(contextRoot)) {
222 String uri = graph.getURI(composite);
223 String modelURI = graph.getURI(contextRoot);
224 return IssueUtils.pathString(uri, modelURI.length()+1);
226 String uri = graph.getURI(composite);
227 String modelURI = graph.getURI(graph.getRootLibrary());
228 return IssueUtils.pathString(uri, modelURI.length()+1);
232 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
233 public static Object fontModifier(ReadGraph graph, Resource r, final Variable variable) throws DatabaseException {
234 return new DialogModifier() {
237 public String getValue() {
242 public String isValid(String label) {
247 public void modify(final String label) {
248 Simantics.getSession().async(new WriteRequest() {
251 public void perform(WriteGraph graph) throws DatabaseException {
252 Variable displayValue = variable.getParent(graph);
253 displayValue.setValue(graph, label, Bindings.STRING);
259 public String query(Object parentControl, Object controlItem, int columnIndex, NodeContext context, Consumer<String> applyCallback) {
261 Control ctrl = (Control) parentControl;
262 FontData[] initialValue = null;
265 String font = Simantics.getSession().syncRequest(new UniqueRead<String>() {
267 public String perform(ReadGraph graph) throws DatabaseException {
268 Variable v = AdaptionUtils.adaptToSingle(context, Variable.class);
269 if(v == null) return null;
270 String displayValue = v.getPossiblePropertyValue(graph, "HasDisplayValue", Bindings.STRING);
275 String[] fields = font.split(",");
276 if (fields.length == 3) {
279 size = Integer.parseInt(fields[1]);
280 } catch (NumberFormatException e) {
281 ErrorLogger.defaultLogError(e);
283 int style = SWT.NORMAL;
285 style = Fonts.swtStyle(fields[2]);
286 } catch (RuntimeException e) {
287 ErrorLogger.defaultLogError(e);
289 initialValue = new FontData[] { new FontData(fields[0], size, style) };
292 } catch (DatabaseException e) {
293 ErrorLogger.defaultLogError(e);
296 FontDialog dialog = new FontDialog(ctrl.getShell());
297 if (initialValue != null)
298 dialog.setFontList(initialValue);
299 FontData font = dialog.open();
301 applyCallback.accept(font.getName() + "," + font.getHeight() + "," + Fonts.fromSwtStyle(font.getStyle()));