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.diagram.elements;
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.utils.datastructures.cache.IFactory;
23 import org.simantics.utils.datastructures.cache.ProvisionException;
26 * This is a SVG implementation to the PaintableSymbol interface.
28 public class AnimatedSVGImage implements Image {
30 static EnumSet<Feature> caps = EnumSet.of(Feature.Vector);
32 private Rectangle2D bounds;
33 private final String nodeIdentifier;
34 private final String svgDocument;
35 private final String svgScript;
36 private Point targetSize;
38 // public static AnimatedSVGImage loadFromURL(String nodeIdentifier, URL url) throws IOException {
39 // InputStream in = url.openStream();
41 // return new AnimatedSVGImage(nodeIdentifier, in);
47 public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument) {
48 return createFactoryFromString(svgDocument, null);
51 public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument, String svgScript, Point targetSize) {
52 return new AnimatedSVGFactory(nodeIdentifier, svgDocument, svgScript, targetSize);
55 static class AnimatedSVGFactory implements IFactory<Image> {
56 String nodeIdentifier;
60 public AnimatedSVGFactory(String nodeIdentifier, String document, String script, Point referenceSize) {
61 if (nodeIdentifier == null)
62 throw new NullPointerException("nodeIdentifier is null");
64 throw new NullPointerException("document is null");
66 throw new NullPointerException("script is null");
68 this.nodeIdentifier = nodeIdentifier;
69 this.document = document;
71 this.targetSize = referenceSize;
73 public Image get() throws ProvisionException {
74 return new AnimatedSVGImage(nodeIdentifier, document, script, targetSize);
77 public boolean equals(Object obj) {
82 if (!obj.getClass().equals(getClass()))
85 AnimatedSVGFactory other = (AnimatedSVGFactory)obj;
87 if (!nodeIdentifier.equals(other.nodeIdentifier))
89 if (targetSize != null) {
90 if (!targetSize.equals(other.targetSize))
93 if (other.targetSize != null)
97 return document.equals(other.document);
100 public int hashCode() {
101 return nodeIdentifier.hashCode() * 31 + document.hashCode() + 123;
105 // public AnimatedSVGImage(String nodeIdentifier, String svgDocument)
107 // this(nodeIdentifier, svgDocument, null);
110 public AnimatedSVGImage(String nodeIdentifier, String svgDocument, String svgScript, Point targetSize)
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");
119 this.nodeIdentifier = nodeIdentifier;
120 this.svgDocument = svgDocument;
121 this.svgScript = svgScript;
122 this.targetSize = targetSize;
123 //this.bounds = SVGNode.getBounds(svgDocument);
126 // public AnimatedSVGImage(String nodeIdentifier, InputStream svgInput)
128 // if (nodeIdentifier == null)
129 // throw new NullPointerException("nodeIdentifier is null");
133 // BufferedReader reader = new BufferedReader(new InputStreamReader(svgInput));
135 // while((line = reader.readLine()) != null) {
136 // data += line+"\n";
138 // } catch(IOException e) {
141 // this.nodeIdentifier = nodeIdentifier;
142 // this.bounds = SVGNode.getBounds(data);
143 // this.svgDocument = data;
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
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);
161 public Rectangle2D getBounds() {
162 if(bounds == null) bounds = SVGNode.getBounds(svgDocument);
167 public Shape getOutline() {
172 public void addImageListener(ImageListener listener) {
176 public EnumSet<Feature> getFeatures() {
181 public void removeImageListener(ImageListener listener) {