source: josm/trunk/src/org/openstreetmap/josm/tools/ImageRequest.java@ 4271

Last change on this file since 4271 was 4271, checked in by bastiK, 13 years ago

extend image caching and add support for scaled svg images (see #6560)

File size: 2.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Dimension;
7import java.io.File;
8import java.util.Collection;
9import javax.swing.ImageIcon;
10
11/**
12 * Alternative way to request an image.
13 * E.g.
14 *
15 * ImageIcon icon = new ImageRequest().setName(imgName).setWidth(100).setHeight(120).get();
16 *
17 * or in funky double-brace style
18 *
19 * ImageIcon icon = new ImageProvider.Request(){{name=imgName; width=100; height=120;}}.get();
20 */
21public class ImageRequest {
22 protected Collection<String> dirs;
23 protected String id;
24 protected String subdir;
25 protected String name;
26 protected File archive;
27 protected int width = -1;
28 protected int height = -1;
29 protected boolean sanitize;
30 protected boolean required = true;
31
32 public ImageRequest setDirs(Collection<String> dirs) {
33 this.dirs = dirs;
34 return this;
35 }
36
37 public ImageRequest setId(String id) {
38 this.id = id;
39 return this;
40 }
41
42 public ImageRequest setSubdir(String subdir) {
43 this.subdir = subdir;
44 return this;
45 }
46
47 public ImageRequest setName(String name) {
48 this.name = name;
49 return this;
50 }
51
52 public ImageRequest setArchive(File archive) {
53 this.archive = archive;
54 return this;
55 }
56
57 public ImageRequest setWidth(int width) {
58 this.width = width;
59 return this;
60 }
61
62 public ImageRequest setHeight(int height) {
63 this.height = height;
64 return this;
65 }
66
67 public ImageRequest setSanitize(boolean sanitize) {
68 this.sanitize = sanitize;
69 return this;
70 }
71
72 public ImageRequest setRequired(boolean required) {
73 this.required = required;
74 return this;
75 }
76
77 public ImageIcon get() {
78 ImageIcon icon = ImageProvider.getIfAvailable(dirs, id, subdir, name, archive, new Dimension(width, height), sanitize);
79 if (required && icon == null) {
80 String ext = name.indexOf('.') != -1 ? "" : ".???";
81 throw new NullPointerException(tr("Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", name + ext));
82 }
83 return icon;
84 }
85
86}
Note: See TracBrowser for help on using the repository browser.