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 package org.simantics.scenegraph.swing;
14 import java.awt.Rectangle;
15 import java.awt.Shape;
16 import java.awt.geom.AffineTransform;
17 import java.awt.geom.Path2D;
18 import java.awt.geom.Rectangle2D;
19 import java.util.Collection;
20 import java.util.EnumSet;
23 import org.simantics.g2d.element.ElementClass;
24 import org.simantics.g2d.element.ElementHints;
25 import org.simantics.g2d.element.IElement;
26 import org.simantics.g2d.element.SceneGraphNodeKey;
27 import org.simantics.g2d.element.handler.ElementHandler;
28 import org.simantics.g2d.element.handler.InternalSize;
29 import org.simantics.g2d.element.handler.Outline;
30 import org.simantics.g2d.element.handler.SceneGraph;
31 import org.simantics.g2d.element.handler.StaticSymbol;
32 import org.simantics.g2d.element.handler.Transform;
33 import org.simantics.g2d.element.handler.impl.SimpleElementLayers;
34 import org.simantics.g2d.element.handler.impl.StaticSymbolImpl;
35 import org.simantics.g2d.elementclass.MonitorHandler;
36 import org.simantics.g2d.image.Image;
37 import org.simantics.g2d.image.ProviderUtils;
38 import org.simantics.g2d.image.impl.AbstractImage;
39 import org.simantics.scenegraph.Node;
40 import org.simantics.scenegraph.g2d.G2DParentNode;
41 import org.simantics.utils.datastructures.cache.IFactory;
42 import org.simantics.utils.datastructures.cache.IProvider;
43 import org.simantics.utils.datastructures.cache.ProvisionException;
44 import org.simantics.utils.datastructures.hints.IHintContext.Key;
47 * @author Tuukka Lehtonen
49 public class SymbolMonitorClass {
51 public static final Key KEY_SG_NODE = new SceneGraphNodeKey(Node.class, "SYMBOL_MONITOR_SG_NODE");
53 public static class MonitorHandlerImpl implements MonitorHandler {
54 private static final long serialVersionUID = -4258875745321808416L;
55 public static final MonitorHandler INSTANCE = new MonitorHandlerImpl();
58 public static void update(IElement e) {
59 SymbolMonitorSGNode node = e.getElementClass().getAtMostOneItemOfClass(SymbolMonitorSGNode.class);
63 public static void cleanup(IElement e) {
64 SymbolMonitorSGNode node = e.getElementClass().getAtMostOneItemOfClass(SymbolMonitorSGNode.class);
68 static final Rectangle2D DEFAULT_BOX = new Rectangle2D.Double(0, 0, 0, 0);
70 static Shape createMonitor(IElement e) {
76 static Path2D makePath(double x, double y, double w, double h) {
77 Path2D path = new Path2D.Double();
80 path.lineTo(x+w, y+h);
86 public static final Shape BOX_SHAPE = new Rectangle(-1, -1, 2, 2);
88 public static class SymbolMonitorSGNode implements SceneGraph, InternalSize, Outline {
90 private static final long serialVersionUID = -106278359626957687L;
92 static final SymbolMonitorSGNode INSTANCE = new SymbolMonitorSGNode();
95 public void init(final IElement e, final G2DParentNode parent) {
96 // Create node if it doesn't exist yet
97 SymbolMonitorNode node = (SymbolMonitorNode)e.getHint(KEY_SG_NODE);
98 if(node == null || node.getBounds() == null || node.getParent() != parent) {
99 // FIXME : symbol monitor node is not disposed properly, so currently this code is commented out.
100 //node = parent.addNode(ElementUtils.generateNodeId(e), SymbolMonitorNode.class);
101 //e.setHint(KEY_SG_NODE, node);
106 public void update(IElement e) {
110 public void cleanup(IElement e) {
111 SymbolMonitorNode node = (SymbolMonitorNode)e.removeHint(KEY_SG_NODE);
117 public Rectangle2D getBounds(IElement e, Rectangle2D size) {
118 Rectangle2D shape = new Rectangle2D.Double(0, 0, 0, 0);
120 SymbolMonitorNode node = (SymbolMonitorNode)e.getHint(KEY_SG_NODE);
121 if(node != null && node.getBounds() != null) {
122 shape = node.getBounds().getBounds2D();
125 if(size != null) size.setRect(shape);
130 public Shape getElementShape(IElement e) {
131 Shape shape = new Rectangle2D.Double(0, 0, 0, 0);
133 SymbolMonitorNode node = (SymbolMonitorNode)e.getHint(KEY_SG_NODE);
134 if(node != null && node.getBounds() != null) {
135 shape = node.getBounds();
143 public static class Transformer implements Transform {
145 private static final long serialVersionUID = -3704887325602085677L;
147 public static final Transformer INSTANCE = new Transformer(null);
151 public Transformer() {
155 public Transformer(Double aspectRatio) {
156 this.aspectRatio = aspectRatio;
160 public AffineTransform getTransform(IElement e) {
161 AffineTransform at = e.getHint(ElementHints.KEY_TRANSFORM);
163 IElement parentElement = e.getHint(ElementHints.KEY_PARENT_ELEMENT);
164 if (parentElement == null)
167 Transform parentTransform = parentElement.getElementClass().getSingleItem(Transform.class);
168 assert(parentTransform!=null);
170 AffineTransform result = (AffineTransform)at.clone();
171 result.preConcatenate(parentTransform.getTransform(parentElement));
177 public void setTransform(IElement e, AffineTransform at) {
178 e.setHint(ElementHints.KEY_TRANSFORM, at.clone());
183 static class MonitorImageFactory implements IFactory<Image> {
185 private double staticScaleX = 1, staticScaleY = 1;
187 public MonitorImageFactory(double staticScaleX, double staticScaleY) {
188 this.staticScaleX = staticScaleX;
189 this.staticScaleY = staticScaleY;
193 public Image get() throws ProvisionException {
195 return new AbstractImage() {
197 Shape path = BOX_SHAPE;
200 public Rectangle2D getBounds() {
201 return path.getBounds2D();
205 public EnumSet<Feature> getFeatures() {
206 return EnumSet.of(Feature.Vector);
210 public Shape getOutline() {
215 public Node init(G2DParentNode parent) {
216 SymbolMonitorNode node = parent.getOrCreateNode(""+hashCode(), SymbolMonitorNode.class);
218 node.setBounds(new Rectangle2D.Double(0, 0, 50, 22));
219 node.setText("Drop Me");
220 node.setTransform(AffineTransform.getScaleInstance(staticScaleX, staticScaleY));
227 static final IProvider<Image> MONITOR_IMAGE =
228 ProviderUtils.reference(
230 ProviderUtils.rasterize(
231 new MonitorImageFactory(0.5, 0.5)
234 static final StaticSymbol MONITOR_SYMBOL = new StaticSymbolImpl( MONITOR_IMAGE.get() );
236 public static final ElementClass MONITOR_CLASS =
237 ElementClass.compile(
238 MonitorHandlerImpl.INSTANCE,
239 Transformer.INSTANCE,
240 SymbolMonitorSGNode.INSTANCE,
241 SimpleElementLayers.INSTANCE,
245 // staticScale{X,Y} define the scale of the static monitor image
246 public static ElementClass create(IElement parentElement, Map<String, String> substitutions, Collection<Object> path, double staticScaleX, double staticScaleY, ElementHandler... extraHandlers) {
247 // Bit of a hack to be able to define the scale
248 IProvider<Image> staticMonitorSymbolProvider = ProviderUtils.reference(
252 new MonitorImageFactory(staticScaleX, staticScaleY))));
253 StaticSymbol staticMonitorSymbol = new StaticSymbolImpl( staticMonitorSymbolProvider.get() );
254 return ElementClass.compile(
255 MonitorHandlerImpl.INSTANCE,
256 Transformer.INSTANCE,
257 SymbolMonitorSGNode.INSTANCE,
258 SimpleElementLayers.INSTANCE,
260 ).newClassWith(extraHandlers);