]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/image/impl/ImageURLFactory.java
Merge "ShapeNode with separate stroke and fill paints"
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / image / impl / ImageURLFactory.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.image.impl;
13
14 import java.awt.image.BufferedImage;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.URL;
18
19 import javax.imageio.ImageIO;
20
21 import org.simantics.g2d.image.Image;
22 import org.simantics.g2d.image.ImageUtils;
23 import org.simantics.g2d.svg.SVGImage;
24 import org.simantics.utils.datastructures.cache.IFactory;
25 import org.simantics.utils.datastructures.cache.ProvisionException;
26
27 /**
28  * Creates images at an URL.
29  * 
30  * @See {@link ImageUtils}
31  * @author Toni Kalajainen
32  * @deprecated does not work with org.simantics.scenegraph
33  */
34 @Deprecated
35 public class ImageURLFactory implements IFactory<Image> {
36
37     URL url;
38
39     public ImageURLFactory(URL url)
40     {
41         if (url==null) throw new IllegalArgumentException("null arg");
42         this.url = url;
43     }
44     
45     public static Image loadFromURL(String nodeIdentifier, URL url) throws IOException {
46         try (InputStream in = url.openStream()) {
47             return new SVGImage(nodeIdentifier, in);
48         }
49     }
50
51     
52     @Override
53     public Image get() throws ProvisionException {
54         boolean svg = url.getFile().toLowerCase().endsWith(".svg");
55
56         if (svg) {
57             try {
58                 // FIXME: provide nodeIdentifier argument from outside of the factory
59                 return SVGImage.loadFromURL(url.toString(), url);
60             } catch (IOException e) {
61                 throw new ProvisionException(e);
62             }
63         }
64
65         // Try opening with ImageIO
66         try {
67             BufferedImage bi = ImageIO.read(url);
68             return new AWTImage(bi);
69         } catch (IOException e) {
70             throw new ProvisionException(e);
71         }
72     }
73
74     @Override
75     public int hashCode() {
76         return url.hashCode() + 243243243;
77     }
78
79     @Override
80     public boolean equals(Object obj) {
81         if (obj == null) return false;
82         if (!obj.getClass().equals(getClass())) return false;
83         return ((ImageURLFactory)obj).url.equals(url);
84     }
85
86 }