]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/SheetClass.java
Use Consumer interface instead of deprecated Callback interface
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / SheetClass.java
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
7  *
8  * Contributors:
9  *     VTT Technical Research Centre of Finland - initial API and implementation
10  *******************************************************************************/
11 package org.simantics.spreadsheet.ui;
12
13 import java.awt.Shape;
14 import java.awt.geom.Rectangle2D;
15 import java.util.function.Consumer;
16
17 import org.simantics.db.Resource;
18 import org.simantics.g2d.element.ElementClass;
19 import org.simantics.g2d.element.ElementHints;
20 import org.simantics.g2d.element.ElementUtils;
21 import org.simantics.g2d.element.IElement;
22 import org.simantics.g2d.element.SceneGraphNodeKey;
23 import org.simantics.g2d.element.handler.InternalSize;
24 import org.simantics.g2d.element.handler.Outline;
25 import org.simantics.g2d.element.handler.SceneGraph;
26 import org.simantics.g2d.element.handler.impl.BorderColorImpl;
27 import org.simantics.g2d.element.handler.impl.DefaultTransform;
28 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
29 import org.simantics.scenegraph.Node;
30 import org.simantics.scenegraph.g2d.G2DParentNode;
31 import org.simantics.spreadsheet.Adaptable;
32 import org.simantics.utils.datastructures.hints.IHintContext.Key;
33 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
34
35 /**
36  * @author Antti Villberg
37  */
38 public class SheetClass {
39
40         public static final Key  KEY_SERVER_INTERFACE = new KeyOf(Adaptable.class, "SERVER_INTERFACE");
41
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");
45
46         public static class SheetSGNode implements SceneGraph, InternalSize, Outline {
47
48                 private static final long serialVersionUID = -5823585015844593347L;
49
50                 static final SheetSGNode INSTANCE = new SheetSGNode();
51
52                 @Override
53                 public void init(final IElement e, final G2DParentNode parent) {
54
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) {
58
59                                 node = parent.addNode(ElementUtils.generateNodeId(e), SheetNode.class);
60
61                                 e.setHint(KEY_SG_NODE, node);
62
63                                 Adaptable serverInterface = e.getHint(KEY_SERVER_INTERFACE);
64                                 node.init(serverInterface);
65
66                                 Consumer<Node> callback = e.getHint(ElementHints.KEY_SG_CALLBACK);
67                                 if(callback != null)
68                                         callback.accept(node);
69
70                                 System.out.println("SHEET PARENT NODE: " + parent);
71                                 node.setBounds(new Rectangle2D.Double(0, 0, 400, 200));
72
73                         }
74
75                         update(e);
76
77                 }
78
79                 public void update(IElement e) {
80                 }
81
82                 @Override
83                 public void cleanup(IElement e) {
84                         SheetNode node = (SheetNode)e.removeHint(KEY_SG_NODE);
85                         if (node != null)
86                                 node.remove();
87                 }
88
89                 @Override
90                 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
91                         Rectangle2D shape = new Rectangle2D.Double(0, 0, 0, 0);
92
93                         SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);
94                         if(node != null && node.getBounds() != null) {
95                                 shape = node.getBounds().getBounds2D();
96                         }
97
98                         if(size != null) size.setRect(shape);
99                         return shape;
100                 }
101
102                 @Override
103                 public Shape getElementShape(IElement e) {
104                         Shape shape = new Rectangle2D.Double(0, 0, 0, 0);
105
106                         SheetNode node = (SheetNode)e.getHint(KEY_SG_NODE);
107                         if(node != null && node.getBounds() != null) {
108                                 shape = node.getBounds();
109                         }
110
111                         return shape;
112                 }
113
114         }
115
116
117         public static final ElementClass INSTANCE =
118                         ElementClass.compile(
119                                         DefaultTransform.INSTANCE,
120                                         BorderColorImpl.BLACK,
121                                         SheetSGNode.INSTANCE,
122                                         SimpleElementLayers.INSTANCE
123                                         );
124
125 }