]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/elementclass/valve/ValveClass.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / elementclass / valve / ValveClass.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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 /*******************************************************************************\r
13  * All rights reserved. This program and the accompanying materials\r
14  * are made available under the terms of the Eclipse Public License v1.0\r
15  * which accompanies this distribution, and is available at\r
16  * http://www.eclipse.org/legal/epl-v10.html\r
17  *\r
18  * Contributors:\r
19  *     VTT Technical Research Centre of Finland - initial API and implementation\r
20  *******************************************************************************/\r
21 package org.simantics.g2d.elementclass.valve;\r
22 \r
23 import java.awt.BasicStroke;\r
24 import java.awt.Color;\r
25 import java.awt.Stroke;\r
26 import java.awt.geom.AffineTransform;\r
27 import java.awt.geom.Path2D;\r
28 import java.awt.geom.Rectangle2D;\r
29 \r
30 import org.simantics.g2d.diagram.IDiagram;\r
31 import org.simantics.g2d.element.ElementClass;\r
32 import org.simantics.g2d.element.ElementUtils;\r
33 import org.simantics.g2d.element.IElement;\r
34 import org.simantics.g2d.element.SceneGraphNodeKey;\r
35 import org.simantics.g2d.element.handler.LifeCycle;\r
36 import org.simantics.g2d.element.handler.SceneGraph;\r
37 import org.simantics.g2d.element.handler.Stateful;\r
38 import org.simantics.g2d.element.handler.Clickable.PressStatus;\r
39 import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
40 import org.simantics.g2d.element.handler.impl.ElementShapeImpl;\r
41 import org.simantics.g2d.element.handler.impl.FillColorImpl;\r
42 import org.simantics.g2d.element.handler.impl.FixedSize;\r
43 import org.simantics.g2d.elementclass.slider.SliderColorProfile;\r
44 import org.simantics.g2d.elementclass.slider.SliderHandle;\r
45 import org.simantics.g2d.utils.PathUtils;\r
46 import org.simantics.scenegraph.Node;\r
47 import org.simantics.scenegraph.g2d.G2DParentNode;\r
48 import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
49 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
50 \r
51 /**\r
52  * @author Toni Kalajainen\r
53  */\r
54 public class ValveClass {\r
55 \r
56     static final double M = 3;\r
57     public static final Stroke STROKE = new BasicStroke(1.5f);\r
58     public static final Stroke STROKE2 = new BasicStroke(1.f);\r
59     public static final Path2D SHAPE = PathUtils.closedPath(-30*M,20*M, -30*M,-20*M, 0,-7*M, 30*M,-20*M, 30*M,20*M, 0,7*M);\r
60     public static final Path2D WHITE_LINE = PathUtils.path(-30*M,20*M, -30*M,-20*M, 0,-7*M, 30*M,-20*M);\r
61     public static final Path2D BLACK_LINE = PathUtils.path(30*M,-20*M, 30*M,20*M, 0,7*M, -30*M,20*M);\r
62     public final static Rectangle2D TRACK_RECT = new Rectangle2D.Double(-27*M, -5*M, 54*M, 10*M);\r
63     public static final Path2D TRACK_WHITE = PathUtils.path(-27*M,5*M, -27*M,-5*M, 27*M,-5*M);\r
64     public static final Path2D TRACK_BLACK = PathUtils.path(27*M,-5*M, 27*M,5*M, -27*M,5*M);\r
65 \r
66     public static ElementClass INSTANCE =\r
67         ElementClass.compile(\r
68                 new ValvePainter(),\r
69                 new ValveHandle(),\r
70                 FillColorImpl.handlerOf(new Color(150, 150, 180)),\r
71                 //Clickable.INSTANCE,\r
72                 Stateful.ENABLED_BY_DEFAULT,\r
73                 DefaultTransform.INSTANCE,\r
74                 ElementShapeImpl.shapeOf(SHAPE),\r
75                 FixedSize.of(SHAPE.getBounds2D())\r
76         );\r
77 \r
78     public static class ValveHandle extends SliderHandle implements LifeCycle {\r
79         private static final long serialVersionUID = 5605597689550675532L;\r
80         @Override\r
81         protected Rectangle2D getBounds(IElement e) {\r
82             return TRACK_RECT;\r
83         }\r
84 \r
85         @Override\r
86         protected double getHandleWidth(IElement e) {\r
87             return super.getHandleWidth(e);\r
88         }\r
89 \r
90         @Override\r
91         public void onElementActivated(IDiagram d, IElement e) {\r
92         }\r
93         @Override\r
94         public void onElementCreated(IElement e) {\r
95             e.setHint(SliderHandle.KEY_SLIDER_COLOR_PROFILE, SliderColorProfile.DEFAULT);\r
96             e.setHint(SliderHandle.KEY_SLIDER_POSITION, 50.0);\r
97             e.setHint(SliderHandle.KEY_SLIDER_MIN_VALUE, 0.0);\r
98             e.setHint(SliderHandle.KEY_SLIDER_MAX_VALUE, 100.0);\r
99         }\r
100         @Override\r
101         public void onElementDeactivated(IDiagram d, IElement e) {\r
102         }\r
103         @Override\r
104         public void onElementDestroyed(IElement e) {\r
105         }\r
106 \r
107     }\r
108 \r
109 \r
110     private static class ValvePainter implements SceneGraph {\r
111         /**\r
112          * \r
113          */\r
114         private static final long serialVersionUID = -434582053817033874L;\r
115         public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");\r
116 \r
117         @Override\r
118         public void cleanup(IElement e) {\r
119             Node node = e.removeHint(SG_NODE);\r
120             if (node != null)\r
121                 node.remove();\r
122         }\r
123 \r
124         @Override\r
125         public void init(IElement e, G2DParentNode parent) {\r
126             G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);\r
127             if (node == null) {\r
128                 node = parent.addNode(G2DParentNode.class);\r
129                 e.setHint(SG_NODE, node);\r
130             }\r
131             Color                       fc              = ElementUtils.getFillColor(e);\r
132             PressStatus         press   = null;//ElementUtils.getPressStatus(e, ctx); // FIXME: scenegraph does not support context\r
133                         AffineTransform at      = ElementUtils.getTransform(e);\r
134                         if(at != null) node.setTransform(at);\r
135 \r
136             ShapeNode shape1 = node.getOrCreateNode("shape1", ShapeNode.class);\r
137             shape1.setStroke(STROKE);\r
138             shape1.setColor(fc);\r
139             shape1.setFill(true);\r
140             shape1.setShape(SHAPE);\r
141 \r
142             ShapeNode shape_press1 = node.getOrCreateNode("shape_press1", ShapeNode.class);\r
143             ShapeNode shape_press2 = node.getOrCreateNode("shape_press2", ShapeNode.class);\r
144 \r
145             if (press==null || press==PressStatus.NORMAL || press == PressStatus.HOVER) {\r
146                 shape_press1.setColor(Color.WHITE);\r
147                 shape_press1.setShape(WHITE_LINE);\r
148                 shape_press2.setColor(Color.BLACK);\r
149                 shape_press2.setShape(BLACK_LINE);\r
150             } else if (press==PressStatus.PRESSED) {\r
151                 shape_press1.setColor(Color.BLACK);\r
152                 shape_press1.setShape(WHITE_LINE);\r
153                 shape_press2.setColor(Color.WHITE);\r
154                 shape_press2.setShape(BLACK_LINE);\r
155             } else if (press==PressStatus.HELD) {\r
156                 shape_press1.setColor(new Color(50, 50, 50));\r
157                 shape_press1.setShape(WHITE_LINE);\r
158                 shape_press2.setColor(new Color(240, 240, 240));\r
159                 shape_press2.setShape(BLACK_LINE);\r
160             }\r
161 \r
162             ShapeNode shape2 = node.getOrCreateNode("shape2", ShapeNode.class);\r
163             shape2.setStroke(STROKE2);\r
164             shape2.setColor(Color.BLACK);\r
165             shape2.setShape(TRACK_BLACK);\r
166 \r
167             ShapeNode shape3 = node.getOrCreateNode("shape3", ShapeNode.class);\r
168             shape3.setStroke(STROKE2);\r
169             shape3.setColor(Color.WHITE);\r
170             shape3.setShape(TRACK_WHITE);\r
171         }\r
172     }\r
173 }\r