1 /*******************************************************************************
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.spreadsheet.ui;
13 import java.awt.Shape;
14 import java.awt.geom.Rectangle2D;
16 import org.simantics.db.Resource;
17 import org.simantics.g2d.element.ElementClass;
18 import org.simantics.g2d.element.ElementHints;
19 import org.simantics.g2d.element.ElementUtils;
20 import org.simantics.g2d.element.IElement;
21 import org.simantics.g2d.element.SceneGraphNodeKey;
22 import org.simantics.g2d.element.handler.InternalSize;
23 import org.simantics.g2d.element.handler.Outline;
24 import org.simantics.g2d.element.handler.SceneGraph;
25 import org.simantics.g2d.element.handler.impl.BorderColorImpl;
26 import org.simantics.g2d.element.handler.impl.DefaultTransform;
27 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
28 import org.simantics.scenegraph.Node;
29 import org.simantics.scenegraph.g2d.G2DParentNode;
30 import org.simantics.spreadsheet.Adaptable;
31 import org.simantics.utils.datastructures.Callback;
32 import org.simantics.utils.datastructures.hints.IHintContext.Key;
33 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
36 * @author Antti Villberg
38 public class SheetClass {
40 public static final Key KEY_SERVER_INTERFACE = new KeyOf(Adaptable.class, "SERVER_INTERFACE");
42 public static final Key KEY_SHEET = new KeyOf(Resource.class, "SHEET");
43 public static final Key KEY_RVI = new KeyOf(String.class, "RVI");
44 public static final Key KEY_SG_NODE = new SceneGraphNodeKey(Node.class, "SG_NODE");
46 public static class SheetSGNode implements SceneGraph, InternalSize, Outline {
48 private static final long serialVersionUID = -5823585015844593347L;
50 static final SheetSGNode INSTANCE = new SheetSGNode();
53 public void init(final IElement e, final G2DParentNode parent) {
55 // Create node if it doesn't exist yet
56 SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);
57 if(node == null || node.getBounds() == null || node.getParent() != parent) {
59 node = parent.addNode(ElementUtils.generateNodeId(e), SheetNode.class);
61 e.setHint(KEY_SG_NODE, node);
63 Adaptable serverInterface = e.getHint(KEY_SERVER_INTERFACE);
64 node.init(serverInterface);
66 Callback<Node> callback = e.getHint(ElementHints.KEY_SG_CALLBACK);
70 System.out.println("SHEET PARENT NODE: " + parent);
71 node.setBounds(new Rectangle2D.Double(0, 0, 400, 200));
79 public void update(IElement e) {
83 public void cleanup(IElement e) {
84 SheetNode node = (SheetNode)e.removeHint(KEY_SG_NODE);
90 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
91 Rectangle2D shape = new Rectangle2D.Double(0, 0, 0, 0);
93 SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);
94 if(node != null && node.getBounds() != null) {
95 shape = node.getBounds().getBounds2D();
98 if(size != null) size.setRect(shape);
103 public Shape getElementShape(IElement e) {
104 Shape shape = new Rectangle2D.Double(0, 0, 0, 0);
106 SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);
107 if(node != null && node.getBounds() != null) {
108 shape = node.getBounds();
117 public static final ElementClass INSTANCE =
118 ElementClass.compile(
119 DefaultTransform.INSTANCE,
120 BorderColorImpl.BLACK,
121 SheetSGNode.INSTANCE,
122 SimpleElementLayers.INSTANCE