]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/CompositeImage.java
Make Write-interfaces as @FunctionalInterface for lambdas
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / CompositeImage.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 package org.simantics.diagram.adapter;
13
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;
19
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;
30
31 /**
32  * Element Composition -> Image
33  *
34  * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
35  */
36 public class CompositeImage implements Image {
37
38     Collection<IElement> elements;
39     private Shape shape;
40     private Rectangle2D bounds;
41     //IHintContext parentHints;
42     static EnumSet<Feature> feats = VOLATILE_VECTOR;
43
44     public static final Key  KEY_SG_NODE             = new SceneGraphNodeKey(Node.class, "COMPOSITE_IMAGE_SG_NODE");
45
46     public CompositeImage(Collection<IElement> elements)
47     {
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);
55   //      task.finish();
56 //        bounds = shape.getBounds2D();
57     }
58
59     @Override
60     public Rectangle2D getBounds() {
61         if(bounds == null) {
62             Shape shape = getShape();
63             // Shape may be null.
64             if (shape == null)
65                 return new Rectangle2D.Double();
66             bounds = shape.getBounds2D();
67         }
68         return bounds;
69     }
70
71     @Override
72     public EnumSet<Feature> getFeatures() {
73         return feats;
74     }
75
76     private final Shape getShape() {
77         if(shape == null) {
78             shape = ElementUtils.getElementBoundsOnDiagram(elements);
79         }
80         return shape;
81     }
82
83     @Override
84     public Shape getOutline() {
85         return getShape();
86     }
87
88     public Collection<IElement> getElements() {
89         return elements;
90     }
91
92     @Override
93     public void addImageListener(ImageListener listener) {
94     }
95
96     @Override
97     public void removeImageListener(ImageListener listener) {
98     }
99
100 //    public void setParentHints(IHintContext hints) {
101 //        this.parentHints = hints;
102 //    }
103
104     // Rendering is single-threaded, this is used while rendering.
105     Rectangle2D tempBounds = new Rectangle2D.Double(0, 0, 0, 0);
106
107     @Override
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);
117                     n.init(e, node);
118                 }
119                 return node; // Valid node must be returned because transform is set afterwards
120             }
121         }
122
123         // For N element composites
124
125         // Removed this grouping node as unnecessary, just use the given parent node
126         G2DParentNode node = parent.getOrCreateNode("composite_image_"+this.hashCode(), G2DParentNode.class);
127
128 //        Rectangle2D bounds = tempBounds;
129
130         int zIndex = 0;
131         for (IElement e : elements) {
132             ElementClass ec = e.getElementClass();
133 //            InternalSize size = ec.getSingleItem(InternalSize.class);
134 //            size.getBounds(e, bounds);
135
136 //            Transform transform = e.getElementClass().getSingleItem(Transform.class);
137 //            AffineTransform at2 = transform.getTransform(e);
138 //            if (at2 == null)
139 //                continue;
140             SingleElementNode holder = node.getOrCreateNode(ElementUtils.generateNodeId(e), SingleElementNode.class);
141             //SingleElementNode holder = parent.getOrCreateNode(ElementUtils.generateNodeId(e), SingleElementNode.class);
142             holder.setZIndex(++zIndex);
143
144             List<SceneGraph> nodeHandlers = ec.getItemsByClass(SceneGraph.class);
145             for(SceneGraph n : nodeHandlers) {
146                 n.init(e, holder);
147             }
148         }
149
150         return node; // Valid node must be returned because transform is set afterwards
151     }
152
153 //    public static class TypeProxyElement extends ProxyElement {
154 //        IHintContext instanceHints;
155 //        public TypeProxyElement(IElement orig, IHintContext instanceHints) {
156 //            super(orig);
157 //            this.instanceHints = instanceHints;
158 //        }
159 //        @Override
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.
163 //            E e = null;
164 //            if (instanceHints != null)
165 //                e = instanceHints.getHint(key);
166 //            if (e == null)
167 //                e = orig.getHint(key);
168 //            return e;
169 //        }
170 //        @Override
171 //        public Map<Key, Object> getHints() {
172 //            throw new UnsupportedOperationException();
173 //        }
174 //        @Override
175 //        public <E extends Key> Map<E, Object> getHintsOfClass(Class<E> clazz) {
176 //            throw new UnsupportedOperationException();
177 //        }
178 //        @Override
179 //        public void setHint(Key key, Object value) {
180 //            if (instanceHints != null)
181 //                instanceHints.setHint(key, value);
182 //            else
183 //                orig.setHint(key, value);
184 //        }
185 //    }
186
187 }