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.diagram.adapter;
14 import java.awt.Shape;
15 import java.awt.geom.Rectangle2D;
16 import java.util.Collection;
17 import java.util.EnumSet;
18 import java.util.List;
20 import org.simantics.g2d.element.ElementClass;
21 import org.simantics.g2d.element.ElementUtils;
22 import org.simantics.g2d.element.IElement;
23 import org.simantics.g2d.element.SceneGraphNodeKey;
24 import org.simantics.g2d.element.handler.SceneGraph;
25 import org.simantics.g2d.image.Image;
26 import org.simantics.scenegraph.Node;
27 import org.simantics.scenegraph.g2d.G2DParentNode;
28 import org.simantics.scenegraph.g2d.nodes.SingleElementNode;
29 import org.simantics.utils.datastructures.hints.IHintContext.Key;
32 * Element Composition -> Image
34 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
36 public class CompositeImage implements Image {
38 Collection<IElement> elements;
40 private Rectangle2D bounds;
41 //IHintContext parentHints;
42 static EnumSet<Feature> feats = VOLATILE_VECTOR;
44 public static final Key KEY_SG_NODE = new SceneGraphNodeKey(Node.class, "COMPOSITE_IMAGE_SG_NODE");
46 public CompositeImage(Collection<IElement> elements)
48 //this.nodeIdentifier = nodeIdentifier;
49 this.elements = elements;
50 // ITask task = ThreadLogger.getInstance().begin("getElementShapesOnDiagram");
51 // getElementShapesOnDiagram is ridiculously slow with some input data..
52 // With Balas it took almost 2sec to calculate one Area.add() for the last terminal element.
53 //shape = ElementUtils.getElementShapesOnDiagram(elements);
54 // shape = ElementUtils.getElementBoundsOnDiagram(elements);
56 // bounds = shape.getBounds2D();
60 public Rectangle2D getBounds() {
62 Shape shape = getShape();
65 return new Rectangle2D.Double();
66 bounds = shape.getBounds2D();
72 public EnumSet<Feature> getFeatures() {
76 private final Shape getShape() {
78 shape = ElementUtils.getElementBoundsOnDiagram(elements);
84 public Shape getOutline() {
88 public Collection<IElement> getElements() {
93 public void addImageListener(ImageListener listener) {
97 public void removeImageListener(ImageListener listener) {
100 // public void setParentHints(IHintContext hints) {
101 // this.parentHints = hints;
104 // Rendering is single-threaded, this is used while rendering.
105 Rectangle2D tempBounds = new Rectangle2D.Double(0, 0, 0, 0);
108 public Node init(G2DParentNode parent) {
109 if (elements.size() < 2) {
110 // Optimization for 0..1 element composites
111 for (IElement e : elements) {
112 ElementClass ec = e.getElementClass();
113 G2DParentNode node = parent.getOrCreateNode("composite_image_"+this.hashCode(), G2DParentNode.class);
114 List<SceneGraph> nodeHandlers = ec.getItemsByClass(SceneGraph.class);
115 for (SceneGraph n : nodeHandlers) {
116 // n.init(e, parent);
119 return node; // Valid node must be returned because transform is set afterwards
123 // For N element composites
125 // Removed this grouping node as unnecessary, just use the given parent node
126 G2DParentNode node = parent.getOrCreateNode("composite_image_"+this.hashCode(), G2DParentNode.class);
128 // Rectangle2D bounds = tempBounds;
131 for (IElement e : elements) {
132 ElementClass ec = e.getElementClass();
133 // InternalSize size = ec.getSingleItem(InternalSize.class);
134 // size.getBounds(e, bounds);
136 // Transform transform = e.getElementClass().getSingleItem(Transform.class);
137 // AffineTransform at2 = transform.getTransform(e);
140 SingleElementNode holder = node.getOrCreateNode(ElementUtils.generateNodeId(e), SingleElementNode.class);
141 //SingleElementNode holder = parent.getOrCreateNode(ElementUtils.generateNodeId(e), SingleElementNode.class);
142 holder.setZIndex(++zIndex);
144 List<SceneGraph> nodeHandlers = ec.getItemsByClass(SceneGraph.class);
145 for(SceneGraph n : nodeHandlers) {
150 return node; // Valid node must be returned because transform is set afterwards
153 // public static class TypeProxyElement extends ProxyElement {
154 // IHintContext instanceHints;
155 // public TypeProxyElement(IElement orig, IHintContext instanceHints) {
157 // this.instanceHints = instanceHints;
160 // public <E> E getHint(Key key) {
161 // // TODO: need some mechanism to tell whether it is allowed to look for this key in the instance hints or not
162 // // This version is broken because some hints MUST come from the original element.
164 // if (instanceHints != null)
165 // e = instanceHints.getHint(key);
167 // e = orig.getHint(key);
171 // public Map<Key, Object> getHints() {
172 // throw new UnsupportedOperationException();
175 // public <E extends Key> Map<E, Object> getHintsOfClass(Class<E> clazz) {
176 // throw new UnsupportedOperationException();
179 // public void setHint(Key key, Object value) {
180 // if (instanceHints != null)
181 // instanceHints.setHint(key, value);
183 // orig.setHint(key, value);