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