]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/elements/AnimatedSVGImage.java
Merge "(refs #7519) Added old constructor back to ConnectionVisuals."
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / elements / AnimatedSVGImage.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.elements;
13
14 import java.awt.Point;
15 import java.awt.Shape;
16 import java.awt.geom.Rectangle2D;
17 import java.util.EnumSet;
18
19 import org.simantics.g2d.image.Image;
20 import org.simantics.scenegraph.Node;
21 import org.simantics.scenegraph.g2d.G2DParentNode;
22 import org.simantics.utils.datastructures.cache.IFactory;
23 import org.simantics.utils.datastructures.cache.ProvisionException;
24
25 /**
26  * This is a SVG implementation to the PaintableSymbol interface.
27  */
28 public class AnimatedSVGImage implements Image {
29
30     static EnumSet<Feature> caps = EnumSet.of(Feature.Vector);
31
32     private Rectangle2D bounds;
33     private final String nodeIdentifier;
34     private final String svgDocument;
35     private final String svgScript;
36     private Point targetSize;
37
38 //    public static AnimatedSVGImage loadFromURL(String nodeIdentifier, URL url) throws IOException {
39 //        InputStream in = url.openStream();
40 //        try {
41 //            return new AnimatedSVGImage(nodeIdentifier, in);
42 //        } finally {
43 //            in.close();
44 //        }
45 //    }
46
47     public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument) {
48         return createFactoryFromString(svgDocument, null);
49     }
50
51     public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument, String svgScript, Point targetSize) {
52         return new AnimatedSVGFactory(nodeIdentifier, svgDocument, svgScript, targetSize);
53     }
54
55     static class AnimatedSVGFactory implements IFactory<Image> {
56         String nodeIdentifier;
57         String document;
58         String script;
59         Point targetSize;
60         public AnimatedSVGFactory(String nodeIdentifier, String document, String script, Point referenceSize) {
61             if (nodeIdentifier == null)
62                 throw new NullPointerException("nodeIdentifier is null");
63             if (document == null)
64                 throw new NullPointerException("document is null");
65             if (script == null)
66                 throw new NullPointerException("script is null");
67
68             this.nodeIdentifier = nodeIdentifier;
69             this.document = document;
70             this.script = script;
71             this.targetSize = referenceSize;
72         }
73         public Image get() throws ProvisionException {
74             return new AnimatedSVGImage(nodeIdentifier, document, script, targetSize);
75         }
76         @Override
77         public boolean equals(Object obj) {
78             if (this == obj)
79                 return true;
80             if (obj == null)
81                 return false;
82             if (!obj.getClass().equals(getClass()))
83                 return false;
84
85             AnimatedSVGFactory other = (AnimatedSVGFactory)obj;
86
87             if (!nodeIdentifier.equals(other.nodeIdentifier))
88                 return false;
89             if (targetSize != null) {
90                 if (!targetSize.equals(other.targetSize))
91                     return false;
92             } else {
93                 if (other.targetSize != null)
94                     return false;
95             }
96
97             return document.equals(other.document);
98         }
99         @Override
100         public int hashCode() {
101             return nodeIdentifier.hashCode() * 31 + document.hashCode() + 123;
102         }
103     }
104
105 //    public AnimatedSVGImage(String nodeIdentifier, String svgDocument)
106 //    {
107 //        this(nodeIdentifier, svgDocument, null);
108 //    }
109
110     public AnimatedSVGImage(String nodeIdentifier, String svgDocument, String svgScript, Point targetSize)
111     {
112         if (nodeIdentifier == null)
113             throw new NullPointerException("nodeIdentifier is null");
114         if (svgDocument == null)
115             throw new NullPointerException("svgDocument is null");
116         if (svgScript == null)
117             throw new NullPointerException("svgScript is null");
118
119         this.nodeIdentifier = nodeIdentifier;
120         this.svgDocument = svgDocument;
121         this.svgScript = svgScript;
122         this.targetSize = targetSize;
123         //this.bounds = SVGNode.getBounds(svgDocument);
124     }
125
126 //    public AnimatedSVGImage(String nodeIdentifier, InputStream svgInput)
127 //    {
128 //        if (nodeIdentifier == null)
129 //            throw new NullPointerException("nodeIdentifier is null");
130 //
131 //        String data = "";
132 //        try {
133 //            BufferedReader reader = new BufferedReader(new InputStreamReader(svgInput));
134 //            String line = "";
135 //            while((line = reader.readLine()) != null) {
136 //                data += line+"\n";
137 //            }
138 //        } catch(IOException e) {
139 //        }
140 //
141 //        this.nodeIdentifier = nodeIdentifier;
142 //        this.bounds = SVGNode.getBounds(data);
143 //        this.svgDocument = data;
144 //    }
145
146     @Override
147     public Node init(G2DParentNode parent) {
148         // FIXME: mipmaps enabled here by default, since some apps just don't work without them.
149         // Figure out a way to pass the mipmap argument from above
150
151         AnimatedSVGNode node = parent.getOrCreateNode(nodeIdentifier, AnimatedSVGNode.class);
152         node.setData(svgDocument);
153         node.setScript(svgScript);
154         node.setTargetSize(targetSize);
155         // If this is set to false, rendered graphics will pixelate and disppear during rotation. See #2396.
156         node.useMipMap(true);
157         return node;
158     }
159     
160         @Override
161         public Rectangle2D getBounds() {
162             if(bounds == null) bounds = SVGNode.getBounds(svgDocument);
163                 return bounds;
164         }
165     
166         @Override
167         public Shape getOutline() {
168         return getBounds();
169     }
170
171     @Override
172     public void addImageListener(ImageListener listener) {
173     }
174
175     @Override
176     public EnumSet<Feature> getFeatures() {
177         return caps;
178     }
179
180     @Override
181     public void removeImageListener(ImageListener listener) {
182     }
183
184 }