]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/function/All.java
Font property for DIA.TextElement
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / function / All.java
1 package org.simantics.diagram.function;
2
3 import java.awt.geom.AffineTransform;
4 import java.awt.geom.Point2D;
5 import java.util.Arrays;
6 import java.util.Collection;
7 import java.util.List;
8 import java.util.function.Consumer;
9
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;
43
44
45 public class All {
46
47     @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
48     public static Variable defaultRuntimeVariable(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
49
50         SceneGraphContext vc = ScenegraphLoaderUtils.getContext(graph, context);
51         if(vc == null) return null;
52
53         DiagramResource DIA = DiagramResource.getInstance(graph);
54         String uri = graph.getRelatedValue(vc.getRuntime(), DIA.RuntimeDiagram_HasVariable, Bindings.STRING);
55         return Variables.getVariable(graph, uri);
56
57     }
58
59     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
60     public static String referenceText(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
61
62         //DiagramResource DIA = DiagramResource.getInstance(graph);
63
64         //Resource runtime = ScenegraphLoaderUtils.getRuntime(graph, context);
65         String path = context.getParent(graph).getPropertyValue(graph, "reference", Bindings.STRING);
66
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);
70
71 //        Variable property = diagramVariable.browse(graph, path);
72
73         try {
74             Variable selection = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);
75                 PredefinedVariables vars = PredefinedVariables.getInstance();
76                 Variable property = vars.getVariable(graph, path, converter, selection);
77                 if (property  != null)
78                         return property.getValue(graph).toString();
79         } catch (MissingVariableException e){
80                 return "Invalid path.";
81         }
82         return null;
83     }
84
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);
88     }
89     
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);
95
96                 Resource flag = context.element;
97                 Resource runtimeDiagram = context.runtime;
98
99                 Resource diagram = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
100                 if (diagram == null)
101                         return flagTransformImpl(graph, converter, context);
102
103                 Resource template = getTemplateFromDiagram(graph, diagram);
104                 if (template == null)
105                         return flagTransformImpl(graph, converter, context);
106                 
107                 double gridSize = DiagramGraphUtil.getGridSize(graph, diagram, 0.0);
108
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);
112         
113         
114         Collection<Resource> slotTables = graph.getObjects(template, L0.ConsistsOf);
115         for (Resource slotTable:slotTables){
116                 if (!graph.isInstanceOf(slotTable, TEMPLATE2D.FlagTable))
117                         continue;
118                 String name = graph.getPossibleRelatedValue(slotTable, L0.HasName, Bindings.STRING);
119                 if (!name.equals(tableName))
120                         continue;
121                 
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);
130                 
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);
134             
135             double[] flagTransform = graph.getRelatedValue(flag, DIA.HasTransform, Bindings.DOUBLE_ARRAY);
136             return calcTransform(transform, flagTransform, rowHeight, width, isRightAlignment, rowIndex, gridSize);
137         }
138
139         return flagTransformImpl(graph, converter, context);
140     }
141
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);
144         return mat;
145     }
146
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;
150
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; 
160                 }
161                 // move back to iotable coordinate system
162                 rowMiddleY = (rowMiddleY-tableTransform[5])/scale;
163
164                 double xx = isRightAlignment ? tableWidth : 0.0; 
165
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);
169
170                 double[] copy = Arrays.copyOf(flagTransform, flagTransform.length);
171                 copy[4] = tableTransform[4] + point.getX();
172                 copy[5] = tableTransform[5] + point.getY();
173                 return copy;
174         }
175
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);
179         if (diagram == null)
180             return null;
181         return getTemplateFromDiagram(graph, diagram);
182     }
183
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);
187         return template;
188     }
189
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);
195
196         List<Resource> contexts = IssueUtils.getContextsForProperty(graph, property);
197         if(contexts.isEmpty()) return "";
198
199         Resource context = contexts.get(0);
200
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;
205
206         if(!graph.isInstanceOf(context, DIA.Element)) return "";
207
208         Resource configuration = graph.sync(new PossibleConfiguration(context));
209         if(configuration == null) return "";
210
211         Resource diagram = graph.getPossibleObject(context, L0.PartOf);
212         if(diagram == null) return "";
213
214         Resource composite = graph.getPossibleObject(diagram, MOD.DiagramToComposite);
215         if(composite == null) return "";
216
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);
225         } else {
226             String uri = graph.getURI(composite);
227             String modelURI = graph.getURI(graph.getRootLibrary());
228             return IssueUtils.pathString(uri, modelURI.length()+1);
229         }
230     }
231
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() {
235
236                 @Override
237                 public String getValue() {
238                         return null;
239                 }
240
241                 @Override
242                 public String isValid(String label) {
243                         return null;
244                 }
245
246                 @Override
247                 public void modify(final String label) {
248                         Simantics.getSession().async(new WriteRequest() {
249
250                                 @Override
251                                 public void perform(WriteGraph graph) throws DatabaseException {
252                                         Variable displayValue = variable.getParent(graph);
253                                         displayValue.setValue(graph, label, Bindings.STRING);
254                                 }
255
256                         });
257                 }
258
259                 public String query(Object parentControl, Object controlItem, int columnIndex, NodeContext context, Consumer<String> applyCallback) {
260                         
261                         Control ctrl = (Control) parentControl;
262                         FontData[] initialValue = null;
263                         
264                         try {
265                                 String font = Simantics.getSession().syncRequest(new UniqueRead<String>() {
266                                         @Override
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);
271                                                 return displayValue;
272                                         }
273                                 });
274                                 if (font != null) {
275                                         String[] fields = font.split(",");
276                                         if (fields.length == 3) {
277                                                 int size = 14;
278                                                 try {
279                                                         size = Integer.parseInt(fields[1]);
280                                                 } catch (NumberFormatException e) {
281                                                         ErrorLogger.defaultLogError(e);
282                                                 }
283                                                 int style = SWT.NORMAL;
284                                                 try {
285                                                         style = Fonts.swtStyle(fields[2]);
286                                                 } catch (RuntimeException e) {
287                                                         ErrorLogger.defaultLogError(e);
288                                                 }
289                                                 initialValue = new FontData[] { new FontData(fields[0], size, style) };
290                                         }
291                                 }
292                         } catch (DatabaseException e) {
293                                 ErrorLogger.defaultLogError(e);
294                         }
295
296                         FontDialog dialog = new FontDialog(ctrl.getShell());
297                         if (initialValue != null)
298                                 dialog.setFontList(initialValue);
299                         FontData font = dialog.open();
300                         if (font != null)
301                                 applyCallback.accept(font.getName() + "," + font.getHeight() + "," + Fonts.fromSwtStyle(font.getStyle()));
302                         return null;
303                 }
304
305         };
306         
307     }
308     
309 }