1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.elements;
14 import java.awt.Color;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Rectangle2D;
18 import java.util.function.Consumer;
20 import org.simantics.g2d.canvas.ICanvasContext;
21 import org.simantics.g2d.diagram.DiagramUtils;
22 import org.simantics.g2d.diagram.IDiagram;
23 import org.simantics.g2d.element.ElementHints;
24 import org.simantics.g2d.element.ElementUtils;
25 import org.simantics.g2d.element.IElement;
26 import org.simantics.g2d.element.SceneGraphNodeKey;
27 import org.simantics.g2d.element.handler.HandleMouseEvent;
28 import org.simantics.g2d.element.handler.SceneGraph;
29 import org.simantics.g2d.utils.Alignment;
30 import org.simantics.scenegraph.g2d.G2DParentNode;
31 import org.simantics.scenegraph.g2d.events.MouseEvent;
32 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseEnterEvent;
33 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseExitEvent;
34 import org.simantics.utils.datastructures.hints.IHintContext.Key;
37 * ElementHandler for text elements
38 * In-line editing supported.
40 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
42 public class TextElementNoBounds implements SceneGraph, HandleMouseEvent {
44 private static final long serialVersionUID = -148784588840819612L;
46 public static final TextElementNoBounds INSTANCE = new TextElementNoBounds();
48 public static final Key SG_NODE = new SceneGraphNodeKey(TextNode.class, "TEXT_SG_NODE");
50 protected static final double DEFAULT_PADDING_X = 0.5;
51 protected static final double DEFAULT_PADDING_Y = 0.5;
52 protected static final double DEFAULT_SCALE = 0.235;
54 protected final double originX;
55 protected final double originY;
56 protected final Alignment horizontalAlignment;
57 protected final Alignment verticalAlignment;
58 protected final double borderWidth;
59 protected final boolean editable;
60 protected final double paddingX;
61 protected final double paddingY;
62 protected final double scale;
65 public TextElementNoBounds() {
66 this(0, 0, Alignment.LEADING, 0);
69 public TextElementNoBounds(double originX, double originY, Alignment horizontalAlignment) {
70 this(originX, originY, horizontalAlignment, 0);
73 public TextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth) {
74 this(originX, originY, horizontalAlignment, borderWidth, DEFAULT_PADDING_X, DEFAULT_PADDING_Y, true);
77 public TextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth, double paddingX, double paddingY, boolean editable) {
78 this(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable, DEFAULT_SCALE);
81 public TextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth, double paddingX, double paddingY, boolean editable, double scale) {
82 this(originX, originY, horizontalAlignment, Alignment.BASELINE, borderWidth, paddingX, paddingY, editable, scale);
85 public TextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, Alignment verticalAlignment, double borderWidth, double paddingX, double paddingY, boolean editable, double scale) {
86 if (horizontalAlignment == null)
87 throw new NullPointerException("null horizontal alignment");
89 this.originX = originX;
90 this.originY = originY;
91 this.horizontalAlignment = horizontalAlignment;
92 this.verticalAlignment = verticalAlignment;
93 this.borderWidth = borderWidth;
94 this.editable = editable;
95 this.paddingX = paddingX;
96 this.paddingY = paddingY;
101 public void init(final IElement e, G2DParentNode parent) {
102 TextNode node = getOrCreateTextNode(e, parent);
104 Font font = ElementUtils.getTextFont(e);
105 Color color = ElementUtils.getTextColor(e);
106 Color fillColor = ElementUtils.getFillColor(e);
107 Color borderColor = ElementUtils.getBorderColor(e, Color.BLACK);
108 String text = ElementUtils.getText(e);
109 AffineTransform at = ElementUtils.getTransform(e);
110 Alignment hAlign = ElementUtils.getHintOrDefault(e, ElementHints.KEY_HORIZONTAL_ALIGN, horizontalAlignment);
111 Alignment vAlign = ElementUtils.getHintOrDefault(e, ElementHints.KEY_VERTICAL_ALIGN, verticalAlignment);
112 Double borderWidth = ElementUtils.getHintOrDefault(e, MonitorClass.KEY_BORDER_WIDTH, this.borderWidth);
114 node.init(text, font, color, originX, originY, scale);
115 node.setBackgroundColor(fillColor);
116 node.setBorderColor(borderColor);
117 node.setHorizontalAlignment((byte) hAlign.ordinal());
118 node.setVerticalAlignment((byte) vAlign.ordinal());
119 node.setPadding(paddingX, paddingY);
120 node.setBorderWidth(borderWidth.floatValue());
121 node.setEditable(editable);
123 node.setTransform(at);
125 if(Boolean.TRUE.equals(ElementUtils.getHintOrDefault(e, ElementHints.KEY_RESIZABLE, false))) {
126 Rectangle2D bounds = e.getHint(ElementHints.KEY_BOUNDS);
128 node.setTargetBounds(bounds);
129 node.setWrapText(true);
134 protected TextNode getOrCreateTextNode(IElement e, G2DParentNode parent) {
135 return ElementUtils.getOrCreateNode(e, parent, SG_NODE, "text", TextNode.class, new TextNodeCallBack(e));
138 private class TextNodeCallBack implements Consumer<TextNode> {
142 public TextNodeCallBack(IElement e) {
147 public void accept(TextNode node) {
148 node.setTextListener(new ITextListener() {
150 public void textChanged() {}
153 public void textEditingStarted() {}
156 public void textEditingCancelled() {
157 TextNode node = (TextNode) e.getHint(SG_NODE);
163 public void textEditingEnded() {
164 TextNode node = (TextNode) e.getHint(SG_NODE);
167 //System.out.println("Node text changed: " + node.getText());
168 ElementUtils.setText(e, node.getText());
169 IDiagram diagram = ElementUtils.getDiagram(e);
170 DiagramUtils.synchronizeHintsToBackend(diagram, e);
178 public void cleanup(IElement e) {
179 ElementUtils.removePossibleNode(e, SG_NODE);
182 // FIXME: hazardous with TextElementHandler.INSTANCE
183 TextEditActivation editActivation = null;
186 public boolean handleMouseEvent(IElement e, ICanvasContext ctx, MouseEvent me) {
187 if (me instanceof MouseEnterEvent) {
188 e.setHint(ElementHints.KEY_HOVER, true);
189 } else if (me instanceof MouseExitEvent) {
190 e.setHint(ElementHints.KEY_HOVER, false);
196 protected void endEdit(TextNode node) {
197 if (editActivation != null) {
198 editActivation.release();
199 editActivation = null;
201 node.setEditMode(false);
207 public int hashCode() {
208 final int prime = 31;
211 temp = Double.doubleToLongBits(borderWidth);
212 result = prime * result + (int) (temp ^ (temp >>> 32));
213 result = prime * result + horizontalAlignment.hashCode();
214 temp = Double.doubleToLongBits(originX);
215 result = prime * result + (int) (temp ^ (temp >>> 32));
216 temp = Double.doubleToLongBits(originY);
217 result = prime * result + (int) (temp ^ (temp >>> 32));
218 temp = Double.doubleToLongBits(paddingX);
219 result = prime * result + (int) (temp ^ (temp >>> 32));
220 temp = Double.doubleToLongBits(paddingY);
221 result = prime * result + (int) (temp ^ (temp >>> 32));
226 public boolean equals(Object obj) {
231 if (getClass() != obj.getClass())
233 TextElementNoBounds other = (TextElementNoBounds) obj;
234 if (Double.doubleToLongBits(borderWidth) != Double.doubleToLongBits(other.borderWidth))
236 if (horizontalAlignment != other.horizontalAlignment)
238 if (Double.doubleToLongBits(originX) != Double.doubleToLongBits(other.originX))
240 if (Double.doubleToLongBits(originY) != Double.doubleToLongBits(other.originY))
242 if (Double.doubleToLongBits(paddingX) != Double.doubleToLongBits(other.paddingX))
244 if (Double.doubleToLongBits(paddingY) != Double.doubleToLongBits(other.paddingY))