1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.Cursor;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.GraphicsConfiguration;
|
---|
7 | import java.awt.GraphicsEnvironment;
|
---|
8 | import java.awt.Image;
|
---|
9 | import java.awt.Point;
|
---|
10 | import java.awt.Toolkit;
|
---|
11 | import java.awt.Transparency;
|
---|
12 | import java.awt.image.BufferedImage;
|
---|
13 | import java.io.File;
|
---|
14 | import java.net.MalformedURLException;
|
---|
15 | import java.net.URL;
|
---|
16 | import java.util.HashMap;
|
---|
17 | import java.util.LinkedList;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Map;
|
---|
20 |
|
---|
21 | import javax.swing.Icon;
|
---|
22 | import javax.swing.ImageIcon;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Helperclass to support the application with images.
|
---|
28 | * @author imi
|
---|
29 | */
|
---|
30 | public class ImageProvider {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Position of an overlay icon
|
---|
34 | * @author imi
|
---|
35 | */
|
---|
36 | public static enum OverlayPosition {NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST}
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * The icon cache
|
---|
40 | */
|
---|
41 | private static Map<String, Image> cache = new HashMap<String, Image>();
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Add here all ClassLoader whose ressource should be searched.
|
---|
45 | * Plugin's class loaders are added by main.
|
---|
46 | */
|
---|
47 | public static final List<ClassLoader> sources = new LinkedList<ClassLoader>();
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Return an image from the specified location.
|
---|
51 | *
|
---|
52 | * @param subdir The position of the directory, e.g. "layer"
|
---|
53 | * @param name The icons name (without the ending of ".png")
|
---|
54 | * @return The requested Image.
|
---|
55 | */
|
---|
56 | public static ImageIcon get(String subdir, String name) {
|
---|
57 | ImageIcon icon = getIfAvailable(subdir, name);
|
---|
58 | if (icon == null) {
|
---|
59 | String ext = name.indexOf('.') != -1 ? "" : ".png";
|
---|
60 | throw new NullPointerException("/images/"+subdir+name+ext+" not found");
|
---|
61 | }
|
---|
62 | return icon;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Like {@link #get(String)}, but does not throw and return <code>null</code>
|
---|
67 | * in case of nothing is found. Use this, if the image to retrieve is optional.
|
---|
68 | */
|
---|
69 | public static ImageIcon getIfAvailable(String subdir, String name) {
|
---|
70 | if (name == null)
|
---|
71 | return null;
|
---|
72 | if (subdir == null)
|
---|
73 | subdir = "";
|
---|
74 | else if (!subdir.equals(""))
|
---|
75 | subdir += "/";
|
---|
76 | String ext = name.indexOf('.') != -1 ? "" : ".png";
|
---|
77 | String full_name = subdir+name+ext;
|
---|
78 |
|
---|
79 | Image img = cache.get(full_name);
|
---|
80 | if (img == null) {
|
---|
81 | // getImageUrl() does a ton of "stat()" calls and gets expensive
|
---|
82 | // and redundant when you have a whole ton of objects. So,
|
---|
83 | // index the cache by the name of the icon we're looking for
|
---|
84 | // and don't bother to create a URL unless we're actually
|
---|
85 | // creating the image.
|
---|
86 | URL path = getImageUrl(full_name);
|
---|
87 | if (path == null)
|
---|
88 | return null;
|
---|
89 | img = Toolkit.getDefaultToolkit().createImage(path);
|
---|
90 | cache.put(full_name, img);
|
---|
91 | }
|
---|
92 |
|
---|
93 | return new ImageIcon(img);
|
---|
94 | }
|
---|
95 |
|
---|
96 | private static URL getImageUrl(String imageName) {
|
---|
97 | URL path = null;
|
---|
98 | // Try user-preference directory first
|
---|
99 | try {
|
---|
100 | if (new File(Main.pref.getPreferencesDir()+"images/"+imageName).exists())
|
---|
101 | return new URL("file", "", Main.pref.getPreferencesDir()+"images/"+imageName);
|
---|
102 | } catch (MalformedURLException e) {
|
---|
103 | }
|
---|
104 |
|
---|
105 | // Try plugins and josm classloader
|
---|
106 | for (ClassLoader source : sources)
|
---|
107 | if ((path = source.getResource("images/"+imageName)) != null)
|
---|
108 | return path;
|
---|
109 |
|
---|
110 | // Try all other ressource directories
|
---|
111 | for (String location : Main.pref.getAllPossiblePreferenceDirs()) {
|
---|
112 | try {
|
---|
113 | if (new File(location+"images/"+imageName).exists())
|
---|
114 | return new URL("file", "", location+"images/"+imageName);
|
---|
115 | } catch (MalformedURLException e) {
|
---|
116 | }
|
---|
117 | }
|
---|
118 | return null;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Shortcut for get("", name);
|
---|
123 | */
|
---|
124 | public static ImageIcon get(String name) {
|
---|
125 | return get("", name);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public static Cursor getCursor(String name, String overlay) {
|
---|
129 | ImageIcon img = get("cursor",name);
|
---|
130 | if (overlay != null)
|
---|
131 | img = overlay(img, "cursor/modifier/"+overlay, OverlayPosition.SOUTHEAST);
|
---|
132 | Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(img.getImage(),
|
---|
133 | name.equals("crosshair") ? new Point(10,10) : new Point(3,2), "Cursor");
|
---|
134 | return c;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * @return an icon that represent the overlay of the two given icons. The
|
---|
139 | * second icon is layed on the first relative to the given position.
|
---|
140 | */
|
---|
141 | public static ImageIcon overlay(Icon ground, String overlayImage, OverlayPosition pos) {
|
---|
142 | GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
|
---|
143 | int w = ground.getIconWidth();
|
---|
144 | int h = ground.getIconHeight();
|
---|
145 | ImageIcon overlay = ImageProvider.get(overlayImage);
|
---|
146 | int wo = overlay.getIconWidth();
|
---|
147 | int ho = overlay.getIconHeight();
|
---|
148 | BufferedImage img = conf.createCompatibleImage(w,h, Transparency.TRANSLUCENT);
|
---|
149 | Graphics g = img.createGraphics();
|
---|
150 | ground.paintIcon(null, g, 0, 0);
|
---|
151 | int x = 0, y = 0;
|
---|
152 | switch (pos) {
|
---|
153 | case NORTHWEST:
|
---|
154 | x = 0;
|
---|
155 | y = 0;
|
---|
156 | break;
|
---|
157 | case NORTHEAST:
|
---|
158 | x = w-wo;
|
---|
159 | y = 0;
|
---|
160 | break;
|
---|
161 | case SOUTHWEST:
|
---|
162 | x = 0;
|
---|
163 | y = h-ho;
|
---|
164 | break;
|
---|
165 | case SOUTHEAST:
|
---|
166 | x = w-wo;
|
---|
167 | y = h-ho;
|
---|
168 | break;
|
---|
169 | }
|
---|
170 | overlay.paintIcon(null, g, x, y);
|
---|
171 | return new ImageIcon(img);
|
---|
172 | }
|
---|
173 |
|
---|
174 | static {
|
---|
175 | try {
|
---|
176 | sources.add(ClassLoader.getSystemClassLoader());
|
---|
177 | } catch (SecurityException ex) {
|
---|
178 | sources.add(ImageProvider.class.getClassLoader());
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|