]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/elements/SVGImage.java
Use element transform when doing pick check
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / elements / SVGImage.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.diagram.elements;
13
14 import java.awt.Point;
15
16 import org.simantics.g2d.image.Image;
17 import org.simantics.scenegraph.Node;
18 import org.simantics.scenegraph.g2d.G2DParentNode;
19 import org.simantics.utils.datastructures.cache.IFactory;
20 import org.simantics.utils.datastructures.cache.ProvisionException;
21
22 /**
23  * This is a SVG implementation to the PaintableSymbol interface.
24  */
25 public class SVGImage extends org.simantics.g2d.svg.SVGImage {
26
27     public SVGImage(String nodeIdentifier, String document, Point targetSize) {
28         super(nodeIdentifier, document, targetSize);
29     }
30
31     public SVGImage(String nodeIdentifier, String document) {
32         super(nodeIdentifier, document);
33         }
34
35     public static IFactory<Image> createFactoryFromString(String nodeIdentifier, String svgDocument, Point targetSize) {
36         return new SVGFactory(nodeIdentifier, svgDocument, targetSize);
37     }
38
39     static class SVGFactory implements IFactory<Image> {
40         String nodeIdentifier;
41         String document;
42         Point targetSize;
43         public SVGFactory(String nodeIdentifier, String document) {
44             this(nodeIdentifier, document, null);
45         }
46         public SVGFactory(String nodeIdentifier, String document, Point referenceSize) {
47             if (nodeIdentifier == null)
48                 throw new NullPointerException("nodeIdentifier is null");
49             if (document == null)
50                 throw new NullPointerException("document is null");
51
52             this.nodeIdentifier = nodeIdentifier;
53             this.document = document;
54             this.targetSize = referenceSize;
55         }
56         @Override
57         public Image get() throws ProvisionException {
58             return new SVGImage(nodeIdentifier, document, targetSize);
59         }
60         @Override
61         public boolean equals(Object obj) {
62             if (this == obj)
63                 return true;
64             if (obj == null)
65                 return false;
66             if (!obj.getClass().equals(getClass()))
67                 return false;
68
69             SVGFactory other = (SVGFactory)obj;
70
71             if (!nodeIdentifier.equals(other.nodeIdentifier))
72                 return false;
73             if (targetSize != null) {
74                 if (!targetSize.equals(other.targetSize))
75                     return false;
76             } else {
77                 if (other.targetSize != null)
78                     return false;
79             }
80
81             return document.equals(other.document);
82         }
83         @Override
84         public int hashCode() {
85             return nodeIdentifier.hashCode() * 31 + document.hashCode() + 123;
86         }
87     }
88
89     @Override
90     public Node init(G2DParentNode parent) {
91         // FIXME: mipmaps enabled here by default, since some apps just don't work without them.
92         // Figure out a way to pass the mipmap argument from above
93
94         SVGNode node = parent.getOrCreateNode(nodeIdentifier, SVGNode.class);
95         node.setData(svgDocument);
96         node.setTargetSize(targetSize);
97         node.useMipMap(true);
98
99         return node;
100     }
101
102 }