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