1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
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
19 * VTT Technical Research Centre of Finland - initial API and implementation
20 *******************************************************************************/
21 package org.simantics.g2d.elementclass.valve;
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;
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;
52 * @author Toni Kalajainen
54 public class ValveClass {
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);
66 public static ElementClass INSTANCE =
70 FillColorImpl.handlerOf(new Color(150, 150, 180)),
72 Stateful.ENABLED_BY_DEFAULT,
73 DefaultTransform.INSTANCE,
74 ElementShapeImpl.shapeOf(SHAPE),
75 FixedSize.of(SHAPE.getBounds2D())
78 public static class ValveHandle extends SliderHandle implements LifeCycle {
79 private static final long serialVersionUID = 5605597689550675532L;
81 protected Rectangle2D getBounds(IElement e) {
86 protected double getHandleWidth(IElement e) {
87 return super.getHandleWidth(e);
91 public void onElementActivated(IDiagram d, IElement e) {
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);
101 public void onElementDeactivated(IDiagram d, IElement e) {
104 public void onElementDestroyed(IElement e) {
110 private static class ValvePainter implements SceneGraph {
114 private static final long serialVersionUID = -434582053817033874L;
115 public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");
118 public void cleanup(IElement e) {
119 Node node = e.removeHint(SG_NODE);
125 public void init(IElement e, G2DParentNode parent) {
126 G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);
128 node = parent.addNode(G2DParentNode.class);
129 e.setHint(SG_NODE, node);
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);
136 ShapeNode shape1 = node.getOrCreateNode("shape1", ShapeNode.class);
137 shape1.setStroke(STROKE);
139 shape1.setFill(true);
140 shape1.setShape(SHAPE);
142 ShapeNode shape_press1 = node.getOrCreateNode("shape_press1", ShapeNode.class);
143 ShapeNode shape_press2 = node.getOrCreateNode("shape_press2", ShapeNode.class);
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);
162 ShapeNode shape2 = node.getOrCreateNode("shape2", ShapeNode.class);
163 shape2.setStroke(STROKE2);
164 shape2.setColor(Color.BLACK);
165 shape2.setShape(TRACK_BLACK);
167 ShapeNode shape3 = node.getOrCreateNode("shape3", ShapeNode.class);
168 shape3.setStroke(STROKE2);
169 shape3.setColor(Color.WHITE);
170 shape3.setShape(TRACK_WHITE);