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