1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.elements2;
\r
14 import java.awt.Color;
\r
15 import java.awt.Font;
\r
16 import java.awt.geom.AffineTransform;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.WriteGraph;
\r
20 import org.simantics.db.common.request.WriteRequest;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.diagram.elements.ITextListener;
\r
23 import org.simantics.diagram.elements.TextElementNoBounds;
\r
24 import org.simantics.diagram.elements.TextNode;
\r
25 import org.simantics.g2d.diagram.DiagramUtils;
\r
26 import org.simantics.g2d.diagram.IDiagram;
\r
27 import org.simantics.g2d.element.ElementHints;
\r
28 import org.simantics.g2d.element.ElementUtils;
\r
29 import org.simantics.g2d.element.IElement;
\r
30 import org.simantics.g2d.utils.Alignment;
\r
31 import org.simantics.modeling.ModelingResources;
\r
32 import org.simantics.scenegraph.g2d.G2DParentNode;
\r
33 import org.simantics.sysdyn.ui.utils.VariableNameUtils;
\r
34 import org.simantics.ui.SimanticsUI;
\r
35 import org.simantics.utils.datastructures.Callback;
\r
36 import org.simantics.utils.datastructures.hints.IHintListener;
\r
37 import org.simantics.utils.datastructures.hints.IHintObservable;
\r
38 import org.simantics.utils.datastructures.hints.IHintContext.Key;
\r
41 * ElementHandler for text elements
\r
42 * In-line editing supported.
\r
44 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
46 public class HoverTextElementNoBounds extends TextElementNoBounds {
\r
48 private static final long serialVersionUID = -148784588840819612L;
\r
50 public static final HoverTextElementNoBounds INSTANCE = new HoverTextElementNoBounds();
\r
52 private IHintListener hoverHintListener;
\r
54 public HoverTextElementNoBounds() {
\r
55 super(0, 0, Alignment.LEADING, 0);
\r
58 public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment) {
\r
59 super(originX, originY, horizontalAlignment, 0);
\r
62 public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth) {
\r
63 super(originX, originY, horizontalAlignment, borderWidth);
\r
66 public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth, double paddingX, double paddingY, boolean editable) {
\r
67 super(originX, originY, horizontalAlignment, borderWidth, paddingX, paddingY, editable);
\r
71 public void init(final IElement e, G2DParentNode parent) {
\r
72 HoverTextNode node = ElementUtils.getOrCreateNode(e, parent, SG_NODE, "text", HoverTextNode.class, new Callback<HoverTextNode>() {
\r
74 public void run(HoverTextNode node) {
\r
75 node.setTextListener(new ITextListener() {
\r
77 String textBeforeEdit;
\r
81 public void textChanged() {
\r
82 TextNode node = (TextNode) e.getHint(SG_NODE);
\r
83 if(!VariableNameUtils.isValid(component, node.getText(), false)) {
\r
84 node.setColor(Color.RED);
\r
86 node.setColor(Color.BLACK);
\r
93 public void textEditingStarted() {
\r
94 TextNode node = (TextNode) e.getHint(SG_NODE);
\r
95 textBeforeEdit = node.getText();
\r
97 if(component != null) return;
\r
99 Object o = e.getHint(ElementHints.KEY_OBJECT);
\r
100 if(o != null && o instanceof Resource) {
\r
101 final Resource element = (Resource)o;
\r
102 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
105 public void perform(WriteGraph graph) throws DatabaseException {
\r
106 component = graph.getPossibleObject(element, ModelingResources.getInstance(graph).ElementToComponent);
\r
113 public void textEditingCancelled() {
\r
114 TextNode node = (TextNode) e.getHint(SG_NODE);
\r
115 if (node != null) {
\r
116 if(VariableNameUtils.isValid(component, node.getText(), false))
\r
117 node.setColor(Color.BLACK);
\r
123 public void textEditingEnded() {
\r
124 TextNode node = (TextNode) e.getHint(SG_NODE);
\r
127 String text = node.getText();
\r
128 if(!VariableNameUtils.isValid(component, text, false)) {
\r
129 text = textBeforeEdit;
\r
130 node.setText(text);
\r
131 if(VariableNameUtils.isValid(component, text, false))
\r
132 node.setColor(Color.BLACK);
\r
134 Object o = e.getHint(ElementHints.KEY_OBJECT);
\r
135 final String textAfterEdit = text;
\r
136 if(o != null && o instanceof Resource) {
\r
137 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
139 public void perform(WriteGraph graph) throws DatabaseException {
\r
140 VariableNameUtils.renameInEquations(graph, component, textBeforeEdit, textAfterEdit);
\r
145 ElementUtils.setText(e, text);
\r
146 IDiagram diagram = ElementUtils.getDiagram(e);
\r
147 DiagramUtils.synchronizeHintsToBackend(diagram, e);
\r
154 //Font font = new Font("Tahoma", 0, 12);
\r
155 Font font = ElementUtils.getTextFont(e);
\r
156 Color color = ElementUtils.getTextColor(e);
\r
157 Color fillColor = ElementUtils.getFillColor(e);
\r
158 Color borderColor = ElementUtils.getBorderColor(e, Color.BLACK);
\r
159 String text = ElementUtils.getText(e);
\r
160 AffineTransform at = ElementUtils.getTransform(e);
\r
161 node.init(text, font, color, originX, originY, 0.235);
\r
162 node.setBackgroundColor(fillColor);
\r
163 node.setBorderColor(borderColor);
\r
164 node.setHorizontalAlignment((byte) horizontalAlignment.ordinal());
\r
165 node.setPadding(paddingX, paddingY);
\r
166 node.setBorderWidth((float) borderWidth);
\r
167 node.setEditable(editable);
\r
169 node.setTransform(at);
\r
171 this.hoverHintListener = new IHintListener() {
\r
173 public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {
\r
178 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
\r
179 if(key == ElementHints.KEY_HOVER) {
\r
180 IElement e = (IElement)sender;
\r
181 TextNode name = (TextNode) e.getHint(SG_NODE);
\r
183 name.setHover(Boolean.TRUE.equals(e.getHint(ElementHints.KEY_HOVER)));
\r
187 e.addHintListener(hoverHintListener);
\r
191 public void cleanup(IElement e) {
\r
192 if(hoverHintListener != null)
\r
193 e.removeHintListener(hoverHintListener);
\r
194 ElementUtils.removePossibleNode(e, SG_NODE);
\r