]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SheetClass.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / SheetClass.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.spreadsheet.ui;\r
12 \r
13 import java.awt.Shape;\r
14 import java.awt.geom.Rectangle2D;\r
15 \r
16 import org.simantics.db.Resource;\r
17 import org.simantics.g2d.element.ElementClass;\r
18 import org.simantics.g2d.element.ElementHints;\r
19 import org.simantics.g2d.element.ElementUtils;\r
20 import org.simantics.g2d.element.IElement;\r
21 import org.simantics.g2d.element.SceneGraphNodeKey;\r
22 import org.simantics.g2d.element.handler.InternalSize;\r
23 import org.simantics.g2d.element.handler.Outline;\r
24 import org.simantics.g2d.element.handler.SceneGraph;\r
25 import org.simantics.g2d.element.handler.impl.BorderColorImpl;\r
26 import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
27 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;\r
28 import org.simantics.scenegraph.Node;\r
29 import org.simantics.scenegraph.g2d.G2DParentNode;\r
30 import org.simantics.spreadsheet.Adaptable;\r
31 import org.simantics.utils.datastructures.Callback;\r
32 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
33 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
34 \r
35 /**\r
36  * @author Antti Villberg\r
37  */\r
38 public class SheetClass {\r
39 \r
40         public static final Key  KEY_SERVER_INTERFACE = new KeyOf(Adaptable.class, "SERVER_INTERFACE");\r
41 \r
42         public static final Key  KEY_SHEET = new KeyOf(Resource.class, "SHEET");\r
43         public static final Key  KEY_RVI = new KeyOf(String.class, "RVI");\r
44         public static final Key  KEY_SG_NODE             = new SceneGraphNodeKey(Node.class, "SG_NODE");\r
45 \r
46         public static class SheetSGNode implements SceneGraph, InternalSize, Outline {\r
47 \r
48                 private static final long serialVersionUID = -5823585015844593347L;\r
49 \r
50                 static final SheetSGNode INSTANCE = new SheetSGNode();\r
51 \r
52                 @Override\r
53                 public void init(final IElement e, final G2DParentNode parent) {\r
54 \r
55                         // Create node if it doesn't exist yet\r
56                         SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);\r
57                         if(node == null || node.getBounds() == null || node.getParent() != parent) {\r
58 \r
59                                 node = parent.addNode(ElementUtils.generateNodeId(e), SheetNode.class);\r
60 \r
61                                 e.setHint(KEY_SG_NODE, node);\r
62 \r
63                                 Adaptable serverInterface = e.getHint(KEY_SERVER_INTERFACE);\r
64                                 node.init(serverInterface);\r
65 \r
66                                 Callback<Node> callback = e.getHint(ElementHints.KEY_SG_CALLBACK);\r
67                                 if(callback != null)\r
68                                         callback.run(node);\r
69 \r
70                                 System.out.println("SHEET PARENT NODE: " + parent);\r
71                                 node.setBounds(new Rectangle2D.Double(0, 0, 400, 200));\r
72 \r
73                         }\r
74 \r
75                         update(e);\r
76 \r
77                 }\r
78 \r
79                 public void update(IElement e) {\r
80                 }\r
81 \r
82                 @Override\r
83                 public void cleanup(IElement e) {\r
84                         SheetNode node = (SheetNode)e.removeHint(KEY_SG_NODE);\r
85                         if (node != null)\r
86                                 node.remove();\r
87                 }\r
88 \r
89                 @Override\r
90                 public Rectangle2D getBounds(IElement e, Rectangle2D size) {\r
91                         Rectangle2D shape = new Rectangle2D.Double(0, 0, 0, 0);\r
92 \r
93                         SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);\r
94                         if(node != null && node.getBounds() != null) {\r
95                                 shape = node.getBounds().getBounds2D();\r
96                         }\r
97 \r
98                         if(size != null) size.setRect(shape);\r
99                         return shape;\r
100                 }\r
101 \r
102                 @Override\r
103                 public Shape getElementShape(IElement e) {\r
104                         Shape shape = new Rectangle2D.Double(0, 0, 0, 0);\r
105 \r
106                         SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);\r
107                         if(node != null && node.getBounds() != null) {\r
108                                 shape = node.getBounds();\r
109                         }\r
110 \r
111                         return shape;\r
112                 }\r
113 \r
114         }\r
115 \r
116 \r
117         public static final ElementClass INSTANCE =\r
118                         ElementClass.compile(\r
119                                         DefaultTransform.INSTANCE,\r
120                                         BorderColorImpl.BLACK,\r
121                                         SheetSGNode.INSTANCE,\r
122                                         SimpleElementLayers.INSTANCE\r
123                                         );\r
124 \r
125 }\r