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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.svg;
\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
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
28 * This is a SVG implementation to the PaintableSymbol interface.
\r
30 public class AnimatedSVGImage implements Image {
\r
32 static EnumSet<Feature> caps = EnumSet.of(Feature.Vector);
\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
40 // public static AnimatedSVGImage loadFromURL(String nodeIdentifier, URL url) throws IOException {
\r
41 // InputStream in = url.openStream();
\r
43 // return new AnimatedSVGImage(nodeIdentifier, in);
\r
49 public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument) {
\r
50 return createFactoryFromString(svgDocument, null);
\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
57 static class AnimatedSVGFactory implements IFactory<Image> {
\r
58 String nodeIdentifier;
\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
68 throw new NullPointerException("script is null");
\r
70 this.nodeIdentifier = nodeIdentifier;
\r
71 this.document = document;
\r
72 this.script = script;
\r
73 this.targetSize = referenceSize;
\r
75 public Image get() throws ProvisionException {
\r
76 return new AnimatedSVGImage(nodeIdentifier, document, script, targetSize);
\r
79 public boolean equals(Object obj) {
\r
84 if (!obj.getClass().equals(getClass()))
\r
87 AnimatedSVGFactory other = (AnimatedSVGFactory)obj;
\r
89 if (!nodeIdentifier.equals(other.nodeIdentifier))
\r
91 if (targetSize != null) {
\r
92 if (!targetSize.equals(other.targetSize))
\r
95 if (other.targetSize != null)
\r
99 return document.equals(other.document);
\r
102 public int hashCode() {
\r
103 return nodeIdentifier.hashCode() * 31 + document.hashCode() + 123;
\r
107 // public AnimatedSVGImage(String nodeIdentifier, String svgDocument)
\r
109 // this(nodeIdentifier, svgDocument, null);
\r
112 public AnimatedSVGImage(String nodeIdentifier, String svgDocument, String svgScript, Point targetSize)
\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
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
128 // public AnimatedSVGImage(String nodeIdentifier, InputStream svgInput)
\r
130 // if (nodeIdentifier == null)
\r
131 // throw new NullPointerException("nodeIdentifier is null");
\r
133 // String data = "";
\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
140 // } catch(IOException e) {
\r
143 // this.nodeIdentifier = nodeIdentifier;
\r
144 // this.bounds = SVGNode.getBounds(data);
\r
145 // this.svgDocument = data;
\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
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
163 public Rectangle2D getBounds() {
\r
164 if(bounds == null) bounds = SVGNode.getBounds(svgDocument);
\r
169 public Shape getOutline() {
\r
170 return getBounds();
\r
174 public void addImageListener(ImageListener listener) {
\r
178 public EnumSet<Feature> getFeatures() {
\r
183 public void removeImageListener(ImageListener listener) {
\r