]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/profile/TextGridStyle.java
(refs #7519) Configurable branch point radius
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / TextGridStyle.java
1 package org.simantics.diagram.profile;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.awt.geom.AffineTransform;
6 import java.awt.geom.Point2D;
7 import java.awt.geom.Rectangle2D;
8
9 import org.simantics.databoard.Bindings;
10 import org.simantics.datatypes.literal.Vec2d;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.variable.RVI;
15 import org.simantics.db.layer0.variable.Variable;
16 import org.simantics.diagram.elements.ITextListener;
17 import org.simantics.diagram.elements.TextGridNode;
18 import org.simantics.diagram.elements.TextNode;
19 import org.simantics.diagram.synchronization.graph.DiagramGraphUtil;
20 import org.simantics.g2d.utils.Alignment;
21 import org.simantics.modeling.ModelingResources;
22 import org.simantics.scenegraph.INode;
23 import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
24 import org.simantics.scenegraph.profile.EvaluationContext;
25 import org.simantics.scenegraph.profile.common.ProfileVariables;
26 import org.simantics.scenegraph.utils.GeometryUtils;
27 import org.simantics.scenegraph.utils.NodeUtil;
28 import org.simantics.scl.runtime.function.Function1;
29 import org.simantics.ui.colors.Colors;
30
31 /**
32  * @author Antti Villberg
33  * @author Tuukka Lehtonen
34  */
35 public abstract class TextGridStyle extends StyleBase<MonitorTextGridResult> {
36
37         private final Font  FONT             = Font.decode("Arial 12");
38         private final Color BACKGROUND_COLOR = new Color(255, 255, 255, 192);
39         private static final Rectangle2D EMPTY_BOUNDS = new Rectangle2D.Double(0, 0, 0, 0);
40
41         // NOTE: this is a hack
42         String id;
43
44         protected double xOffset;
45         protected double yOffset;
46
47         public TextGridStyle() {
48                 this(0.0, 2.1);
49         }
50
51         public TextGridStyle(double xOffset, double yOffset) {
52                 this.xOffset = xOffset;
53                 this.yOffset = yOffset;
54         }
55
56         public Resource getPropertyRelation(ReadGraph g, Resource module) {
57         throw new Error("Fix this");
58         }
59
60         /**
61          * @return the name of the scene graph node to create to represent the text
62          *         element created by this style
63          */
64         public String getNodeName() {
65                 return getClass().getSimpleName();
66         }
67
68         /**
69          * Override to customize.
70          * 
71          * @param graph
72          * @param element
73          * @return
74          * @throws DatabaseException
75          */
76         protected Resource getConfigurationComponent(ReadGraph graph, Resource element) throws DatabaseException {
77                 ModelingResources mr = ModelingResources.getInstance(graph);
78                 Resource config = graph.getPossibleObject(element, mr.ElementToComponent);
79                 return config;
80         }
81
82         /**
83          * Override to customize.
84          * 
85          * @param graph
86          * @param element
87          * @return
88          * @throws DatabaseException
89          */
90         protected String getConfigurationComponentNameForElement(ReadGraph graph, Resource element) throws DatabaseException {
91                 Resource config = getConfigurationComponent(graph, element);
92                 if (config == null)
93                         return null;
94                 String name = graph.getPossibleRelatedValue(config, getPropertyRelation(graph,element), Bindings.STRING);
95                 return name;
96         }
97
98         public AffineTransform getTransform(INode node, AffineTransform parentTransform, Rectangle2D elementBounds, int location, boolean up) {
99                 return getTransform(parentTransform, elementBounds, location, up);
100         }
101
102         public AffineTransform getTransform(AffineTransform parentTransform, Rectangle2D elementBounds, int location, boolean up) {
103
104                 double scale = GeometryUtils.getScale(parentTransform);
105
106                 AffineTransform at = AffineTransform.getTranslateInstance(
107                                 parentTransform.getTranslateX() + elementBounds.getCenterX() * scale + xOffset,
108                                 parentTransform.getTranslateY() + elementBounds.getMinY() * scale + yOffset*(location-1) + (up ? 0.0 : (2.0*yOffset) + elementBounds.getHeight() * scale) 
109                                 );
110                 at.scale(0.15, 0.15);
111
112                 return at;
113
114         }
115
116         protected String rowId() {
117                 return "";
118         }
119
120         @Override
121         public MonitorTextGridResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration)
122                         throws DatabaseException {
123                 String name = getConfigurationComponentNameForElement(graph, element);
124                 if (name == null)
125                         return null;
126                 AffineTransform transform = DiagramGraphUtil.getDynamicAffineTransform(graph, runtimeDiagram, element);
127                 Vec2d offset = DiagramGraphUtil.getOffset(graph, element);
128                 boolean enabled = !DiagramGraphUtil.getProfileMonitorsHidden(graph, element);
129                 boolean up = DiagramGraphUtil.getProfileMonitorsUp(graph, element);
130                 double spacing = DiagramGraphUtil.getProfileMonitorSpacing(graph, element);
131                 return new MonitorTextGridResult(rowId(), name, "", "", enabled, up, spacing, null, null, null, transform, offset);
132         }
133
134         // Not to be modified
135         protected final static Point2D[] DEFAULT_CELL_OFFSETS = new Point2D[] {
136                         new Point2D.Double(-45, 0),
137                         new Point2D.Double(22, 0),
138                         new Point2D.Double(24, 0)
139         };
140
141         // Not to be modified
142         protected final static Point2D[] ZERO_CELL_OFFSETS = new Point2D[] {
143                         new Point2D.Double(0, 0),
144                         new Point2D.Double(0, 0),
145                         new Point2D.Double(0, 0)
146         };
147
148         protected Point2D[] getCellOffsets() {
149                 return DEFAULT_CELL_OFFSETS;
150         }
151
152         @Override
153         public void applyStyleForNode(EvaluationContext observer, INode _node, MonitorTextGridResult result) {
154
155                 String value = result != null ? result.getText1() : null;
156                 boolean enabled = result != null ? result.getEnabled() : false;
157
158                 if (value != null && enabled) {
159
160                         //        if (value != null && !value.isEmpty() && !value.trim().isEmpty()) {
161
162                         String value2 = result != null ? result.getText2() : null;
163                         String value3 = result != null ? result.getText3() : null;
164
165                         double spacing = result.getSpacing();
166
167                         final Function1<String, String> modifier = result != null ? result.getModifier() : null;
168                         final Function1<String, String> validator = result != null ? result.getValidator() : null;
169                         final Function1<Vec2d, Boolean> translator = result != null ? result.getTranslator() : null;
170                         final RVI rvi = result != null ? result.getRVI() : null;
171
172                         final TextGridNode node = ProfileVariables.claimChild(_node, "", "TextGridStyle", TextGridNode.class, observer);
173                         if (node == null)
174                                 return;
175
176                         // This assumes that this TextGridStyle instance will be devoted to
177                         // this row ID until the end of its life.
178                         String id = result.getRowId();
179                         //System.out.println(this + " ID: " + id);
180                         if (!id.equals(this.id)) {
181                                 //System.out.println(this + " SET ID: " + this.id + " -> " + id);
182                                 this.id = id;
183                         }
184
185                         Integer newRow = observer.getTemporaryProperty(_node, "location");
186                         if (newRow == null)
187                                 newRow = 1;
188
189                         // Remove from existing row to add to another row if necessary.
190                         Integer row = observer.getProperty(_node, id);
191                         if (row != null && row != newRow) {
192                                 String actualId = node.getRowId(row);
193                                 if (id.equals(actualId)) {
194                                         node.removeRow(row);
195                                 }
196                         }
197                         row = newRow;
198
199                         node.setRowId(row, id);
200                         observer.setProperty(_node, id, row);
201                         observer.setTemporaryProperty(_node, "location", row + 1);
202
203                         node.setText(2, row, value2);
204                         node.setUp(result.getUp());
205
206                         MonitorTextGridResult cache = node.getCache(1, row);
207                         if(cache != null && cache.sameStructure(result)) return;
208
209                         node.setCache(1, row, result);
210
211                         boolean isConnection = _node instanceof ConnectionNode;
212
213                         Rectangle2D elementBounds = isConnection ? EMPTY_BOUNDS : NodeUtil.getLocalElementBounds(_node);
214                         if(elementBounds == null) {
215                                 new Exception("Cannot get local element bounds for node " + _node.toString()).printStackTrace();
216                                 // This is here for checking why getLocalElementBounds failed in the debugger.
217                                 NodeUtil.getLocalElementBounds(_node);
218                                 return;
219                         }
220
221                         //            System.err.println("elementBounds " + elementBounds);
222                         //            System.err.println("parentTransform " + result.getParentTransform());
223
224                         AffineTransform at = getTransform(_node,result.getParentTransform(), elementBounds, row, result.getUp());
225                         Vec2d offset = result.getOffset();
226
227                         Point2D[] cellOffsets = getCellOffsets();
228
229                         AffineTransform at1 = new AffineTransform(at);
230                         at1.translate(cellOffsets[0].getX(),cellOffsets[0].getY());
231                         AffineTransform at2 = new AffineTransform(at);
232                         at2.translate(cellOffsets[1].getX()+spacing,cellOffsets[1].getY());
233                         AffineTransform at3 = new AffineTransform(at);
234                         at3.translate(cellOffsets[2].getX()+spacing,cellOffsets[2].getY());
235
236                         at1.translate(offset.x, offset.y);
237                         at2.translate(offset.x, offset.y);
238                         at3.translate(offset.x, offset.y);
239
240                         node.setTransform(1, row, at1);
241                         node.setTransform(2, row, at2);
242                         node.setTransform(3, row, at3);
243
244                         Alignment[] alignments = result.getAlignments();
245                         if(alignments != null) {
246                                 node.setHorizontalAlignment(1, row, (byte) alignments[0].ordinal());
247                                 node.setHorizontalAlignment(2, row, (byte) alignments[1].ordinal());
248                                 node.setHorizontalAlignment(3, row, (byte) alignments[2].ordinal());
249                         } else {
250                                 node.setHorizontalAlignment(1, row, (byte) getAlignment(1).ordinal());
251                                 node.setHorizontalAlignment(2, row, (byte) getAlignment(2).ordinal());
252                                 node.setHorizontalAlignment(3, row, (byte) getAlignment(3).ordinal());
253                         }
254
255                         Alignment[] verticalAlignments = result.getVerticalAlignments();
256                         if(verticalAlignments != null) {
257                                 node.setVerticalAlignment(1, row, (byte) verticalAlignments[0].ordinal());
258                                 node.setVerticalAlignment(2, row, (byte) verticalAlignments[1].ordinal());
259                                 node.setVerticalAlignment(3, row, (byte) verticalAlignments[2].ordinal());
260                         } else {
261                                 node.setVerticalAlignment(1, row, (byte) getVerticalAlignment(1).ordinal());
262                                 node.setVerticalAlignment(2, row, (byte) getVerticalAlignment(2).ordinal());
263                                 node.setVerticalAlignment(3, row, (byte) getVerticalAlignment(3).ordinal());
264                         }
265
266                         node.setZIndex(3000);
267
268                         org.simantics.common.color.Color color = result.getColor();
269                         Color awtColor = color != null ? Colors.awt(color) : Color.DARK_GRAY;
270                         Color bgColor = getBackgroundColor();
271                         Font font = getFont();
272
273                         setTextNodeData(node, 1, row, value, font, awtColor, bgColor);
274                         setTextNodeData(node, 2, row, value2, result.getPending(), font, awtColor, bgColor);
275                         setTextNodeData(node, 3, row, value3, font, awtColor, bgColor);
276
277                         node.setEditable(1, row, false);
278                         node.setForceEventListening(2, row, true);
279                         node.setEditable(2, row, modifier != null);
280                         node.setEditable(3, row, false);
281
282                         final int finalRow = row;
283
284                         if (modifier != null) {
285                                 node.setTextListener(2, row, new ITextListener() {
286                                         @Override
287                                         public void textChanged() {}
288
289                                         @Override
290                                         public void textEditingStarted() {}
291
292                                         @Override
293                                         public void textEditingCancelled() {
294                                         }
295
296                                         @Override
297                                         public void textEditingEnded() {
298
299                                                 TextNode t = node.get(2, finalRow);
300                                                 if (t == null)
301                                                         return;
302
303                                                 if(!t.getText().equals(t.getTextBeforeEdit()))
304                                                         modifier.apply(t.getText());
305
306                                         }
307                                 });
308                         } else {
309                                 node.setTextListener(2,  row,  null);
310                         }
311
312                         node.setInputValidator(2, row, validator);
313                         node.setTranslator(translator);
314
315                         node.setRVI(2, row, rvi);
316
317                         postProcessNode(node, row);
318
319                 } else {
320                         cleanupStyleForNode(observer, _node);
321                 }
322         }
323
324         private void setTextNodeData(TextGridNode node, int x, int y, String text, Font font, Color fgColor, Color bgColor) {
325                 if (text != null) {
326                         node.setText(x, y, text);
327                         node.setFont(x, y, font);
328                         node.setColor(x, y, fgColor);
329                         node.setBackgroundColor(x, y, bgColor);
330                 } else {
331                         // Prevent rendering of the node.
332                         node.setFont(x, y, null);
333                 }
334         }
335
336         private void setTextNodeData(TextGridNode node, int x, int y, String text, boolean pending, Font font, Color fgColor, Color bgColor) {
337                 setTextNodeData(node, x, y, text, font, fgColor, bgColor);
338                 node.setPending(x, y, pending);
339         }
340
341         protected Font getFont() {
342                 return FONT;
343         }
344
345         protected Color getBackgroundColor() {
346                 return BACKGROUND_COLOR;
347         }
348
349         protected Alignment getAlignment(int column) {
350                 switch(column) {
351                 case 1: return Alignment.TRAILING;
352                 case 2: return Alignment.TRAILING;
353                 case 3: return Alignment.LEADING;
354                 default: return Alignment.LEADING;
355                 }
356         }
357
358         protected Alignment getVerticalAlignment(int column) {
359                 return Alignment.TRAILING;
360         }
361
362         @Override
363         protected void cleanupStyleForNode(EvaluationContext observer, INode _node) {
364                 Integer row = observer.getProperty(_node, id);
365                 //System.out.println(this + " cleanup(" + id + ", " + row + ")");
366                 //System.out.println(element);
367                 if (row == null)
368                         return;
369                 observer.setProperty(_node, id, null);
370                 TextGridNode node = ProfileVariables.browseChild(_node, "TextGridStyle");
371                 if (node != null)
372                         node.removeRow(row);
373         }
374
375         protected void postProcessNode(TextGridNode node, int row) {
376         }
377
378 }