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.g2d.svg;
14 import java.awt.Point;
15 import java.awt.Shape;
16 import java.awt.geom.Rectangle2D;
17 import java.util.EnumSet;
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;
28 * This is a SVG implementation to the PaintableSymbol interface.
30 public class AnimatedSVGImage implements Image {
32 static EnumSet<Feature> caps = EnumSet.of(Feature.Vector);
34 private Rectangle2D bounds;
35 private final String nodeIdentifier;
36 private final String svgDocument;
37 private final String svgScript;
38 private Point targetSize;
40 // public static AnimatedSVGImage loadFromURL(String nodeIdentifier, URL url) throws IOException {
41 // InputStream in = url.openStream();
43 // return new AnimatedSVGImage(nodeIdentifier, in);
49 public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument) {
50 return createFactoryFromString(svgDocument, null);
53 public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument, String svgScript, Point targetSize) {
54 return new AnimatedSVGFactory(nodeIdentifier, svgDocument, svgScript, targetSize);
57 static class AnimatedSVGFactory implements IFactory<Image> {
58 String nodeIdentifier;
62 public AnimatedSVGFactory(String nodeIdentifier, String document, String script, Point referenceSize) {
63 if (nodeIdentifier == null)
64 throw new NullPointerException("nodeIdentifier is null");
66 throw new NullPointerException("document is null");
68 throw new NullPointerException("script is null");
70 this.nodeIdentifier = nodeIdentifier;
71 this.document = document;
73 this.targetSize = referenceSize;
75 public Image get() throws ProvisionException {
76 return new AnimatedSVGImage(nodeIdentifier, document, script, targetSize);
79 public boolean equals(Object obj) {
84 if (!obj.getClass().equals(getClass()))
87 AnimatedSVGFactory other = (AnimatedSVGFactory)obj;
89 if (!nodeIdentifier.equals(other.nodeIdentifier))
91 if (targetSize != null) {
92 if (!targetSize.equals(other.targetSize))
95 if (other.targetSize != null)
99 return document.equals(other.document);
102 public int hashCode() {
103 return nodeIdentifier.hashCode() * 31 + document.hashCode() + 123;
107 // public AnimatedSVGImage(String nodeIdentifier, String svgDocument)
109 // this(nodeIdentifier, svgDocument, null);
112 public AnimatedSVGImage(String nodeIdentifier, String svgDocument, String svgScript, Point targetSize)
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");
121 this.nodeIdentifier = nodeIdentifier;
122 this.svgDocument = svgDocument;
123 this.svgScript = svgScript;
124 this.targetSize = targetSize;
125 //this.bounds = SVGNode.getBounds(svgDocument);
128 // public AnimatedSVGImage(String nodeIdentifier, InputStream svgInput)
130 // if (nodeIdentifier == null)
131 // throw new NullPointerException("nodeIdentifier is null");
135 // BufferedReader reader = new BufferedReader(new InputStreamReader(svgInput));
137 // while((line = reader.readLine()) != null) {
138 // data += line+"\n";
140 // } catch(IOException e) {
143 // this.nodeIdentifier = nodeIdentifier;
144 // this.bounds = SVGNode.getBounds(data);
145 // this.svgDocument = data;
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
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);
163 public Rectangle2D getBounds() {
164 if(bounds == null) bounds = SVGNode.getBounds(svgDocument);
169 public Shape getOutline() {
174 public void addImageListener(ImageListener listener) {
178 public EnumSet<Feature> getFeatures() {
183 public void removeImageListener(ImageListener listener) {