1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Cursor;
|
---|
8 | import java.awt.Graphics;
|
---|
9 | import java.awt.Graphics2D;
|
---|
10 | import java.awt.GraphicsConfiguration;
|
---|
11 | import java.awt.GraphicsEnvironment;
|
---|
12 | import java.awt.Image;
|
---|
13 | import java.awt.Point;
|
---|
14 | import java.awt.RenderingHints;
|
---|
15 | import java.awt.Toolkit;
|
---|
16 | import java.awt.Transparency;
|
---|
17 | import java.awt.image.BufferedImage;
|
---|
18 | import java.io.File;
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.net.MalformedURLException;
|
---|
21 | import java.net.URL;
|
---|
22 | import java.util.Arrays;
|
---|
23 | import java.util.Collection;
|
---|
24 | import java.util.HashMap;
|
---|
25 | import java.util.LinkedList;
|
---|
26 | import java.util.List;
|
---|
27 | import java.util.Map;
|
---|
28 |
|
---|
29 | import javax.swing.Icon;
|
---|
30 | import javax.swing.ImageIcon;
|
---|
31 |
|
---|
32 | import org.openstreetmap.josm.Main;
|
---|
33 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
34 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Helperclass to support the application with images.
|
---|
38 | * @author imi
|
---|
39 | */
|
---|
40 | public class ImageProvider {
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Position of an overlay icon
|
---|
44 | * @author imi
|
---|
45 | */
|
---|
46 | public static enum OverlayPosition {
|
---|
47 | NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * The icon cache
|
---|
52 | */
|
---|
53 | private static Map<String, Image> cache = new HashMap<String, Image>();
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Add here all ClassLoader whose ressource should be searched. Plugin's class loaders are added
|
---|
57 | * by main.
|
---|
58 | */
|
---|
59 | public static final List<ClassLoader> sources = new LinkedList<ClassLoader>();
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Return an image from the specified location.
|
---|
63 | *
|
---|
64 | * @param subdir The position of the directory, e.g. "layer"
|
---|
65 | * @param name The icons name (without the ending of ".png")
|
---|
66 | * @return The requested Image.
|
---|
67 | */
|
---|
68 | public static ImageIcon get(String subdir, String name) {
|
---|
69 | ImageIcon icon = getIfAvailable(subdir, name);
|
---|
70 | if (icon == null) {
|
---|
71 | String ext = name.indexOf('.') != -1 ? "" : ".png";
|
---|
72 | throw new NullPointerException("/images/" + subdir + "/" + name + ext + " not found");
|
---|
73 | }
|
---|
74 | return icon;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public static ImageIcon getIfAvailable(String subdir, String name) {
|
---|
78 | return getIfAvailable((Collection<String>) null, null, subdir, name);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public static final ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) {
|
---|
82 | return getIfAvailable(Arrays.asList(dirs), id, subdir, name);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Like {@link #get(String)}, but does not throw and return <code>null</code> in case of nothing
|
---|
87 | * is found. Use this, if the image to retrieve is optional.
|
---|
88 | */
|
---|
89 | public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) {
|
---|
90 | if (name == null)
|
---|
91 | return null;
|
---|
92 | if (name.startsWith("http://")) {
|
---|
93 | Image img = cache.get(name);
|
---|
94 | if (img == null) {
|
---|
95 | try {
|
---|
96 | MirroredInputStream is = new MirroredInputStream(name, new File(Main.pref.getPreferencesDir(),
|
---|
97 | "images").toString());
|
---|
98 | if (is != null) {
|
---|
99 | img = Toolkit.getDefaultToolkit().createImage(is.getFile().toURI().toURL());
|
---|
100 | cache.put(name, img);
|
---|
101 | }
|
---|
102 | } catch (IOException e) {
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return img == null ? null : new ImageIcon(img);
|
---|
106 | }
|
---|
107 | if (subdir == null) {
|
---|
108 | subdir = "";
|
---|
109 | } else if (!subdir.equals("")) {
|
---|
110 | subdir += "/";
|
---|
111 | }
|
---|
112 | String ext = name.indexOf('.') != -1 ? "" : ".png";
|
---|
113 | String full_name = subdir + name + ext;
|
---|
114 | String cache_name = full_name;
|
---|
115 | /* cache separately */
|
---|
116 | if (dirs != null && dirs.size() > 0) {
|
---|
117 | cache_name = "id:" + id + ":" + full_name;
|
---|
118 | }
|
---|
119 |
|
---|
120 | Image img = cache.get(cache_name);
|
---|
121 | if (img == null) {
|
---|
122 | // getImageUrl() does a ton of "stat()" calls and gets expensive
|
---|
123 | // and redundant when you have a whole ton of objects. So,
|
---|
124 | // index the cache by the name of the icon we're looking for
|
---|
125 | // and don't bother to create a URL unless we're actually
|
---|
126 | // creating the image.
|
---|
127 | URL path = getImageUrl(full_name, dirs);
|
---|
128 | if (path == null)
|
---|
129 | return null;
|
---|
130 | img = Toolkit.getDefaultToolkit().createImage(path);
|
---|
131 | cache.put(cache_name, img);
|
---|
132 | }
|
---|
133 |
|
---|
134 | return new ImageIcon(img);
|
---|
135 | }
|
---|
136 |
|
---|
137 | private static URL getImageUrl(String path, String name) {
|
---|
138 | if (path.startsWith("resource://")) {
|
---|
139 | String p = path.substring("resource://".length());
|
---|
140 | for (ClassLoader source : sources) {
|
---|
141 | URL res;
|
---|
142 | if ((res = source.getResource(p + name)) != null)
|
---|
143 | return res;
|
---|
144 | }
|
---|
145 | } else {
|
---|
146 | try {
|
---|
147 | File f = new File(path, name);
|
---|
148 | if (f.exists())
|
---|
149 | return f.toURI().toURL();
|
---|
150 | } catch (MalformedURLException e) {
|
---|
151 | }
|
---|
152 | }
|
---|
153 | return null;
|
---|
154 | }
|
---|
155 |
|
---|
156 | private static URL getImageUrl(String imageName, Collection<String> dirs) {
|
---|
157 | URL u = null;
|
---|
158 |
|
---|
159 | // Try passed directories first
|
---|
160 | if (dirs != null) {
|
---|
161 | for (String name : dirs) {
|
---|
162 | try {
|
---|
163 | u = getImageUrl(name, imageName);
|
---|
164 | if (u != null)
|
---|
165 | return u;
|
---|
166 | } catch (SecurityException e) {
|
---|
167 | System.out.println(tr(
|
---|
168 | "Warning: failed to acccess directory ''{0}'' for security reasons. Exception was: {1}",
|
---|
169 | name, e.toString()));
|
---|
170 | }
|
---|
171 |
|
---|
172 | }
|
---|
173 | }
|
---|
174 | // Try user-preference directory
|
---|
175 | String dir = Main.pref.getPreferencesDir() + "images";
|
---|
176 | try {
|
---|
177 | u = getImageUrl(dir, imageName);
|
---|
178 | if (u != null)
|
---|
179 | return u;
|
---|
180 | } catch (SecurityException e) {
|
---|
181 | System.out.println(tr(
|
---|
182 | "Warning: failed to acccess directory ''{0}'' for security reasons. Exception was: {1}", dir, e
|
---|
183 | .toString()));
|
---|
184 | }
|
---|
185 |
|
---|
186 | // Try plugins and josm classloader
|
---|
187 | u = getImageUrl("resource://images/", imageName);
|
---|
188 | if (u != null)
|
---|
189 | return u;
|
---|
190 |
|
---|
191 | // Try all other ressource directories
|
---|
192 | for (String location : Main.pref.getAllPossiblePreferenceDirs()) {
|
---|
193 | u = getImageUrl(location + "images", imageName);
|
---|
194 | if (u != null)
|
---|
195 | return u;
|
---|
196 | u = getImageUrl(location, imageName);
|
---|
197 | if (u != null)
|
---|
198 | return u;
|
---|
199 | }
|
---|
200 | System.out
|
---|
201 | .println(tr(
|
---|
202 | "Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.",
|
---|
203 | imageName));
|
---|
204 | return null;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Shortcut for get("", name);
|
---|
209 | */
|
---|
210 | public static ImageIcon get(String name) {
|
---|
211 | return get("", name);
|
---|
212 | }
|
---|
213 |
|
---|
214 | public static Cursor getCursor(String name, String overlay) {
|
---|
215 | ImageIcon img = get("cursor", name);
|
---|
216 | if (overlay != null) {
|
---|
217 | img = overlay(img, "cursor/modifier/" + overlay, OverlayPosition.SOUTHEAST);
|
---|
218 | }
|
---|
219 | Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(img.getImage(),
|
---|
220 | name.equals("crosshair") ? new Point(10, 10) : new Point(3, 2), "Cursor");
|
---|
221 | return c;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * @return an icon that represent the overlay of the two given icons. The second icon is layed
|
---|
226 | * on the first relative to the given position.
|
---|
227 | */
|
---|
228 | public static ImageIcon overlay(Icon ground, String overlayImage, OverlayPosition pos) {
|
---|
229 | GraphicsConfiguration conf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
|
---|
230 | .getDefaultConfiguration();
|
---|
231 | int w = ground.getIconWidth();
|
---|
232 | int h = ground.getIconHeight();
|
---|
233 | ImageIcon overlay = ImageProvider.get(overlayImage);
|
---|
234 | int wo = overlay.getIconWidth();
|
---|
235 | int ho = overlay.getIconHeight();
|
---|
236 | BufferedImage img = conf.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
|
---|
237 | Graphics g = img.createGraphics();
|
---|
238 | ground.paintIcon(null, g, 0, 0);
|
---|
239 | int x = 0, y = 0;
|
---|
240 | switch (pos) {
|
---|
241 | case NORTHWEST:
|
---|
242 | x = 0;
|
---|
243 | y = 0;
|
---|
244 | break;
|
---|
245 | case NORTHEAST:
|
---|
246 | x = w - wo;
|
---|
247 | y = 0;
|
---|
248 | break;
|
---|
249 | case SOUTHWEST:
|
---|
250 | x = 0;
|
---|
251 | y = h - ho;
|
---|
252 | break;
|
---|
253 | case SOUTHEAST:
|
---|
254 | x = w - wo;
|
---|
255 | y = h - ho;
|
---|
256 | break;
|
---|
257 | }
|
---|
258 | overlay.paintIcon(null, g, x, y);
|
---|
259 | return new ImageIcon(img);
|
---|
260 | }
|
---|
261 |
|
---|
262 | static {
|
---|
263 | try {
|
---|
264 | sources.add(ClassLoader.getSystemClassLoader());
|
---|
265 | sources.add(org.openstreetmap.josm.gui.MainApplication.class.getClassLoader());
|
---|
266 | } catch (SecurityException ex) {
|
---|
267 | sources.add(ImageProvider.class.getClassLoader());
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | /*
|
---|
272 | * from: http://www.jidesoft.com/blog/2008/02/29/rotate-an-icon-in-java/ License:
|
---|
273 | * "feel free to use"
|
---|
274 | */
|
---|
275 | final static double DEGREE_90 = 90.0 * Math.PI / 180.0;
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * Creates a rotated version of the input image.
|
---|
279 | *
|
---|
280 | * @param c The component to get properties useful for painting, e.g. the foreground or
|
---|
281 | * background color.
|
---|
282 | * @param icon the image to be rotated.
|
---|
283 | * @param rotatedAngle the rotated angle, in degree, clockwise. It could be any double but we
|
---|
284 | * will mod it with 360 before using it.
|
---|
285 | *
|
---|
286 | * @return the image after rotating.
|
---|
287 | */
|
---|
288 | public static ImageIcon createRotatedImage(Component c, Icon icon, double rotatedAngle) {
|
---|
289 | // convert rotatedAngle to a value from 0 to 360
|
---|
290 | double originalAngle = rotatedAngle % 360;
|
---|
291 | if (rotatedAngle != 0 && originalAngle == 0) {
|
---|
292 | originalAngle = 360.0;
|
---|
293 | }
|
---|
294 |
|
---|
295 | // convert originalAngle to a value from 0 to 90
|
---|
296 | double angle = originalAngle % 90;
|
---|
297 | if (originalAngle != 0.0 && angle == 0.0) {
|
---|
298 | angle = 90.0;
|
---|
299 | }
|
---|
300 |
|
---|
301 | double radian = Math.toRadians(angle);
|
---|
302 |
|
---|
303 | int iw = icon.getIconWidth();
|
---|
304 | int ih = icon.getIconHeight();
|
---|
305 | int w;
|
---|
306 | int h;
|
---|
307 |
|
---|
308 | if ((originalAngle >= 0 && originalAngle <= 90) || (originalAngle > 180 && originalAngle <= 270)) {
|
---|
309 | w = (int) (iw * Math.sin(DEGREE_90 - radian) + ih * Math.sin(radian));
|
---|
310 | h = (int) (iw * Math.sin(radian) + ih * Math.sin(DEGREE_90 - radian));
|
---|
311 | } else {
|
---|
312 | w = (int) (ih * Math.sin(DEGREE_90 - radian) + iw * Math.sin(radian));
|
---|
313 | h = (int) (ih * Math.sin(radian) + iw * Math.sin(DEGREE_90 - radian));
|
---|
314 | }
|
---|
315 | BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
|
---|
316 | Graphics g = image.getGraphics();
|
---|
317 | Graphics2D g2d = (Graphics2D) g.create();
|
---|
318 |
|
---|
319 | // calculate the center of the icon.
|
---|
320 | int cx = iw / 2;
|
---|
321 | int cy = ih / 2;
|
---|
322 |
|
---|
323 | // move the graphics center point to the center of the icon.
|
---|
324 | g2d.translate(w / 2, h / 2);
|
---|
325 |
|
---|
326 | // rotate the graphics about the center point of the icon
|
---|
327 | g2d.rotate(Math.toRadians(originalAngle));
|
---|
328 |
|
---|
329 | g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
---|
330 | icon.paintIcon(c, g2d, -cx, -cy);
|
---|
331 |
|
---|
332 | g2d.dispose();
|
---|
333 | return new ImageIcon(image);
|
---|
334 | }
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Replies the icon for an OSM primitive type
|
---|
338 | * @param type the type
|
---|
339 | * @return the icon
|
---|
340 | */
|
---|
341 | public static ImageIcon get(OsmPrimitiveType type) throws IllegalArgumentException {
|
---|
342 | if (type == null)
|
---|
343 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "type"));
|
---|
344 | return get("data", type.getAPIName());
|
---|
345 | }
|
---|
346 | }
|
---|