1 package org.simantics.diagram.profile;
5 import java.awt.geom.AffineTransform;
6 import java.awt.geom.Point2D;
7 import java.awt.geom.Rectangle2D;
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;
32 * @author Antti Villberg
33 * @author Tuukka Lehtonen
35 public abstract class TextGridStyle extends StyleBase<MonitorTextGridResult> {
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);
41 // NOTE: this is a hack
44 protected double xOffset;
45 protected double yOffset;
47 public TextGridStyle() {
51 public TextGridStyle(double xOffset, double yOffset) {
52 this.xOffset = xOffset;
53 this.yOffset = yOffset;
56 public Resource getPropertyRelation(ReadGraph g, Resource module) {
57 throw new Error("Fix this");
61 * @return the name of the scene graph node to create to represent the text
62 * element created by this style
64 public String getNodeName() {
65 return getClass().getSimpleName();
69 * Override to customize.
74 * @throws DatabaseException
76 protected Resource getConfigurationComponent(ReadGraph graph, Resource element) throws DatabaseException {
77 ModelingResources mr = ModelingResources.getInstance(graph);
78 Resource config = graph.getPossibleObject(element, mr.ElementToComponent);
83 * Override to customize.
88 * @throws DatabaseException
90 protected String getConfigurationComponentNameForElement(ReadGraph graph, Resource element) throws DatabaseException {
91 Resource config = getConfigurationComponent(graph, element);
94 String name = graph.getPossibleRelatedValue(config, getPropertyRelation(graph,element), Bindings.STRING);
98 public AffineTransform getTransform(INode node, AffineTransform parentTransform, Rectangle2D elementBounds, int location, boolean up) {
99 return getTransform(parentTransform, elementBounds, location, up);
102 public AffineTransform getTransform(AffineTransform parentTransform, Rectangle2D elementBounds, int location, boolean up) {
104 double scale = GeometryUtils.getScale(parentTransform);
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)
110 at.scale(0.15, 0.15);
116 protected String rowId() {
121 public MonitorTextGridResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration)
122 throws DatabaseException {
123 String name = getConfigurationComponentNameForElement(graph, element);
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);
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)
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)
148 protected Point2D[] getCellOffsets() {
149 return DEFAULT_CELL_OFFSETS;
153 public void applyStyleForNode(EvaluationContext observer, INode _node, MonitorTextGridResult result) {
155 String value = result != null ? result.getText1() : null;
156 boolean enabled = result != null ? result.getEnabled() : false;
158 if (value != null && enabled) {
160 // if (value != null && !value.isEmpty() && !value.trim().isEmpty()) {
162 String value2 = result != null ? result.getText2() : null;
163 String value3 = result != null ? result.getText3() : null;
165 double spacing = result.getSpacing();
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;
172 final TextGridNode node = ProfileVariables.claimChild(_node, "", "TextGridStyle", TextGridNode.class, observer);
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);
185 Integer newRow = observer.getTemporaryProperty(_node, "location");
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)) {
199 node.setRowId(row, id);
200 observer.setProperty(_node, id, row);
201 observer.setTemporaryProperty(_node, "location", row + 1);
203 node.setText(2, row, value2);
204 node.setUp(result.getUp());
206 MonitorTextGridResult cache = node.getCache(1, row);
207 if(cache != null && cache.sameStructure(result)) return;
209 node.setCache(1, row, result);
211 boolean isConnection = _node instanceof ConnectionNode;
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);
221 // System.err.println("elementBounds " + elementBounds);
222 // System.err.println("parentTransform " + result.getParentTransform());
224 AffineTransform at = getTransform(_node,result.getParentTransform(), elementBounds, row, result.getUp());
225 Vec2d offset = result.getOffset();
227 Point2D[] cellOffsets = getCellOffsets();
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());
236 at1.translate(offset.x, offset.y);
237 at2.translate(offset.x, offset.y);
238 at3.translate(offset.x, offset.y);
240 node.setTransform(1, row, at1);
241 node.setTransform(2, row, at2);
242 node.setTransform(3, row, at3);
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());
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());
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());
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());
266 node.setZIndex(3000);
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();
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);
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);
282 final int finalRow = row;
284 if (modifier != null) {
285 node.setTextListener(2, row, new ITextListener() {
287 public void textChanged() {}
290 public void textEditingStarted() {}
293 public void textEditingCancelled() {
297 public void textEditingEnded() {
299 TextNode t = node.get(2, finalRow);
303 if(!t.getText().equals(t.getTextBeforeEdit()))
304 modifier.apply(t.getText());
309 node.setTextListener(2, row, null);
312 node.setInputValidator(2, row, validator);
313 node.setTranslator(translator);
315 node.setRVI(2, row, rvi);
317 postProcessNode(node, row);
320 cleanupStyleForNode(observer, _node);
324 private void setTextNodeData(TextGridNode node, int x, int y, String text, Font font, Color fgColor, Color bgColor) {
326 node.setText(x, y, text);
327 node.setFont(x, y, font);
328 node.setColor(x, y, fgColor);
329 node.setBackgroundColor(x, y, bgColor);
331 // Prevent rendering of the node.
332 node.setFont(x, y, null);
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);
341 protected Font getFont() {
345 protected Color getBackgroundColor() {
346 return BACKGROUND_COLOR;
349 protected Alignment getAlignment(int column) {
351 case 1: return Alignment.TRAILING;
352 case 2: return Alignment.TRAILING;
353 case 3: return Alignment.LEADING;
354 default: return Alignment.LEADING;
358 protected Alignment getVerticalAlignment(int column) {
359 return Alignment.TRAILING;
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);
369 observer.setProperty(_node, id, null);
370 TextGridNode node = ProfileVariables.browseChild(_node, "TextGridStyle");
375 protected void postProcessNode(TextGridNode node, int row) {