]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
2b583a4a7bcf4d584756871ff0fce1202d67e2c3
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.elements2;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Font;\r
16 import java.awt.geom.AffineTransform;\r
17 \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
39 \r
40 /**\r
41  * ElementHandler for text elements\r
42  * In-line editing supported.\r
43  * \r
44  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
45  */\r
46 public class HoverTextElementNoBounds extends TextElementNoBounds {\r
47 \r
48         private static final long serialVersionUID = -148784588840819612L;\r
49 \r
50         public static final HoverTextElementNoBounds INSTANCE         = new HoverTextElementNoBounds();\r
51 \r
52         private IHintListener hoverHintListener;\r
53 \r
54         public HoverTextElementNoBounds() {\r
55                 super(0, 0, Alignment.LEADING, 0);\r
56         }\r
57 \r
58         public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment) {\r
59                 super(originX, originY, horizontalAlignment, 0);\r
60         }\r
61 \r
62         public HoverTextElementNoBounds(double originX, double originY, Alignment horizontalAlignment, double borderWidth) {\r
63                 super(originX, originY, horizontalAlignment, borderWidth);\r
64         }\r
65 \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
68         }\r
69 \r
70         @Override\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
73                         @Override\r
74                         public void run(HoverTextNode node) {\r
75                                 node.setTextListener(new ITextListener() {\r
76 \r
77                                         String textBeforeEdit;\r
78                                         Resource component;\r
79 \r
80                                         @Override\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
85                                                 } else {\r
86                                                         node.setColor(Color.BLACK);\r
87                                                 }\r
88 \r
89 \r
90                                         }\r
91 \r
92                                         @Override\r
93                                         public void textEditingStarted() {\r
94                                                 TextNode node = (TextNode) e.getHint(SG_NODE);\r
95                                                 textBeforeEdit = node.getText();\r
96 \r
97                                                 if(component != null) return;\r
98                                                 \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
103 \r
104                                                                 @Override\r
105                                                                 public void perform(WriteGraph graph) throws DatabaseException {\r
106                                                                         component = graph.getPossibleObject(element, ModelingResources.getInstance(graph).ElementToComponent);\r
107                                                                 }\r
108                                                         });\r
109                                                 }\r
110                                         }\r
111 \r
112                                         @Override\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
118                                                         endEdit(node);\r
119                                                 }\r
120                                         }\r
121 \r
122                                         @Override\r
123                                         public void textEditingEnded() {\r
124                                                 TextNode node = (TextNode) e.getHint(SG_NODE);\r
125                                                 if (node == null)\r
126                                                         return;\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
133                                                 } else {\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
138                                                                         @Override\r
139                                                                         public void perform(WriteGraph graph) throws DatabaseException {\r
140                                                                                 VariableNameUtils.renameInEquations(graph, component, textBeforeEdit, textAfterEdit);\r
141                                                                         }\r
142                                                                 });\r
143                                                         }\r
144                                                 }\r
145                                                 ElementUtils.setText(e, text);\r
146                                                 IDiagram diagram = ElementUtils.getDiagram(e);\r
147                                                 DiagramUtils.synchronizeHintsToBackend(diagram, e);\r
148                                                 endEdit(node);\r
149                                         }\r
150                                 });\r
151                         }\r
152                 });\r
153 \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
168                 if(at != null)\r
169                         node.setTransform(at);\r
170 \r
171                 this.hoverHintListener = new IHintListener() {\r
172                         @Override\r
173                         public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {\r
174 \r
175                         }\r
176 \r
177                         @Override\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
182                                         if(name != null)\r
183                                                 name.setHover(Boolean.TRUE.equals(e.getHint(ElementHints.KEY_HOVER)));\r
184                                 }\r
185                         }\r
186                 };\r
187                 e.addHintListener(hoverHintListener);\r
188         }\r
189 \r
190         @Override\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
195         }\r
196 }