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