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