1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.io.File;
|
---|
8 | import java.util.Collection;
|
---|
9 | import 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 | */
|
---|
21 | public 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 | }
|
---|