--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*******************************************************************************\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.elementclass.valve;\r
+\r
+import java.awt.BasicStroke;\r
+import java.awt.Color;\r
+import java.awt.Stroke;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Path2D;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.g2d.diagram.IDiagram;\r
+import org.simantics.g2d.element.ElementClass;\r
+import org.simantics.g2d.element.ElementUtils;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.SceneGraphNodeKey;\r
+import org.simantics.g2d.element.handler.LifeCycle;\r
+import org.simantics.g2d.element.handler.SceneGraph;\r
+import org.simantics.g2d.element.handler.Stateful;\r
+import org.simantics.g2d.element.handler.Clickable.PressStatus;\r
+import org.simantics.g2d.element.handler.impl.DefaultTransform;\r
+import org.simantics.g2d.element.handler.impl.ElementShapeImpl;\r
+import org.simantics.g2d.element.handler.impl.FillColorImpl;\r
+import org.simantics.g2d.element.handler.impl.FixedSize;\r
+import org.simantics.g2d.elementclass.slider.SliderColorProfile;\r
+import org.simantics.g2d.elementclass.slider.SliderHandle;\r
+import org.simantics.g2d.utils.PathUtils;\r
+import org.simantics.scenegraph.Node;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.nodes.ShapeNode;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+\r
+/**\r
+ * @author Toni Kalajainen\r
+ */\r
+public class ValveClass {\r
+\r
+ static final double M = 3;\r
+ public static final Stroke STROKE = new BasicStroke(1.5f);\r
+ public static final Stroke STROKE2 = new BasicStroke(1.f);\r
+ 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
+ public static final Path2D WHITE_LINE = PathUtils.path(-30*M,20*M, -30*M,-20*M, 0,-7*M, 30*M,-20*M);\r
+ public static final Path2D BLACK_LINE = PathUtils.path(30*M,-20*M, 30*M,20*M, 0,7*M, -30*M,20*M);\r
+ public final static Rectangle2D TRACK_RECT = new Rectangle2D.Double(-27*M, -5*M, 54*M, 10*M);\r
+ public static final Path2D TRACK_WHITE = PathUtils.path(-27*M,5*M, -27*M,-5*M, 27*M,-5*M);\r
+ public static final Path2D TRACK_BLACK = PathUtils.path(27*M,-5*M, 27*M,5*M, -27*M,5*M);\r
+\r
+ public static ElementClass INSTANCE =\r
+ ElementClass.compile(\r
+ new ValvePainter(),\r
+ new ValveHandle(),\r
+ FillColorImpl.handlerOf(new Color(150, 150, 180)),\r
+ //Clickable.INSTANCE,\r
+ Stateful.ENABLED_BY_DEFAULT,\r
+ DefaultTransform.INSTANCE,\r
+ ElementShapeImpl.shapeOf(SHAPE),\r
+ FixedSize.of(SHAPE.getBounds2D())\r
+ );\r
+\r
+ public static class ValveHandle extends SliderHandle implements LifeCycle {\r
+ private static final long serialVersionUID = 5605597689550675532L;\r
+ @Override\r
+ protected Rectangle2D getBounds(IElement e) {\r
+ return TRACK_RECT;\r
+ }\r
+\r
+ @Override\r
+ protected double getHandleWidth(IElement e) {\r
+ return super.getHandleWidth(e);\r
+ }\r
+\r
+ @Override\r
+ public void onElementActivated(IDiagram d, IElement e) {\r
+ }\r
+ @Override\r
+ public void onElementCreated(IElement e) {\r
+ e.setHint(SliderHandle.KEY_SLIDER_COLOR_PROFILE, SliderColorProfile.DEFAULT);\r
+ e.setHint(SliderHandle.KEY_SLIDER_POSITION, 50.0);\r
+ e.setHint(SliderHandle.KEY_SLIDER_MIN_VALUE, 0.0);\r
+ e.setHint(SliderHandle.KEY_SLIDER_MAX_VALUE, 100.0);\r
+ }\r
+ @Override\r
+ public void onElementDeactivated(IDiagram d, IElement e) {\r
+ }\r
+ @Override\r
+ public void onElementDestroyed(IElement e) {\r
+ }\r
+\r
+ }\r
+\r
+\r
+ private static class ValvePainter implements SceneGraph {\r
+ /**\r
+ * \r
+ */\r
+ private static final long serialVersionUID = -434582053817033874L;\r
+ public static final Key SG_NODE = new SceneGraphNodeKey(Node.class, "SUB_SG_NODE");\r
+\r
+ @Override\r
+ public void cleanup(IElement e) {\r
+ Node node = e.removeHint(SG_NODE);\r
+ if (node != null)\r
+ node.remove();\r
+ }\r
+\r
+ @Override\r
+ public void init(IElement e, G2DParentNode parent) {\r
+ G2DParentNode node = (G2DParentNode) e.getHint(SG_NODE);\r
+ if (node == null) {\r
+ node = parent.addNode(G2DParentNode.class);\r
+ e.setHint(SG_NODE, node);\r
+ }\r
+ Color fc = ElementUtils.getFillColor(e);\r
+ PressStatus press = null;//ElementUtils.getPressStatus(e, ctx); // FIXME: scenegraph does not support context\r
+ AffineTransform at = ElementUtils.getTransform(e);\r
+ if(at != null) node.setTransform(at);\r
+\r
+ ShapeNode shape1 = node.getOrCreateNode("shape1", ShapeNode.class);\r
+ shape1.setStroke(STROKE);\r
+ shape1.setColor(fc);\r
+ shape1.setFill(true);\r
+ shape1.setShape(SHAPE);\r
+\r
+ ShapeNode shape_press1 = node.getOrCreateNode("shape_press1", ShapeNode.class);\r
+ ShapeNode shape_press2 = node.getOrCreateNode("shape_press2", ShapeNode.class);\r
+\r
+ if (press==null || press==PressStatus.NORMAL || press == PressStatus.HOVER) {\r
+ shape_press1.setColor(Color.WHITE);\r
+ shape_press1.setShape(WHITE_LINE);\r
+ shape_press2.setColor(Color.BLACK);\r
+ shape_press2.setShape(BLACK_LINE);\r
+ } else if (press==PressStatus.PRESSED) {\r
+ shape_press1.setColor(Color.BLACK);\r
+ shape_press1.setShape(WHITE_LINE);\r
+ shape_press2.setColor(Color.WHITE);\r
+ shape_press2.setShape(BLACK_LINE);\r
+ } else if (press==PressStatus.HELD) {\r
+ shape_press1.setColor(new Color(50, 50, 50));\r
+ shape_press1.setShape(WHITE_LINE);\r
+ shape_press2.setColor(new Color(240, 240, 240));\r
+ shape_press2.setShape(BLACK_LINE);\r
+ }\r
+\r
+ ShapeNode shape2 = node.getOrCreateNode("shape2", ShapeNode.class);\r
+ shape2.setStroke(STROKE2);\r
+ shape2.setColor(Color.BLACK);\r
+ shape2.setShape(TRACK_BLACK);\r
+\r
+ ShapeNode shape3 = node.getOrCreateNode("shape3", ShapeNode.class);\r
+ shape3.setStroke(STROKE2);\r
+ shape3.setColor(Color.WHITE);\r
+ shape3.setShape(TRACK_WHITE);\r
+ }\r
+ }\r
+}\r