source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java@ 10634

Last change on this file since 10634 was 10634, checked in by Don-vip, 8 years ago

sonar - pmd:UselessQualifiedThis - Useless qualified this usage in the same class

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.styleelement;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Graphics;
7import java.awt.Image;
8import java.awt.Rectangle;
9import java.awt.image.BufferedImage;
10import java.util.Objects;
11
12import javax.swing.ImageIcon;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
16import org.openstreetmap.josm.gui.mappaint.StyleSource;
17import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.BoxProvider;
18import org.openstreetmap.josm.gui.mappaint.styleelement.BoxTextElement.BoxProviderResult;
19import org.openstreetmap.josm.gui.util.GuiHelper;
20import org.openstreetmap.josm.tools.ImageProvider;
21import org.openstreetmap.josm.tools.ImageProvider.ImageCallback;
22import org.openstreetmap.josm.tools.Utils;
23
24/**
25 * An image that will be displayed on the map.
26 */
27public class MapImage {
28
29 private static final int MAX_SIZE = 48;
30
31 /**
32 * ImageIcon can change while the image is loading.
33 */
34 private BufferedImage img;
35
36 public int alpha = 255;
37 public String name;
38 public StyleSource source;
39 public boolean autoRescale;
40 public int width = -1;
41 public int height = -1;
42 public int offsetX;
43 public int offsetY;
44
45 private boolean temporary;
46 private BufferedImage disabledImgCache;
47
48 public MapImage(String name, StyleSource source) {
49 this(name, source, true);
50 }
51
52 public MapImage(String name, StyleSource source, boolean autoRescale) {
53 this.name = name;
54 this.source = source;
55 this.autoRescale = autoRescale;
56 }
57
58 /**
59 * Get the image associated with this MapImage object.
60 *
61 * @param disabled {@code} true to request disabled version, {@code false} for the standard version
62 * @return the image
63 */
64 public BufferedImage getImage(boolean disabled) {
65 if (disabled) {
66 return getDisabled();
67 } else {
68 return getImage();
69 }
70 }
71
72 private BufferedImage getDisabled() {
73 if (disabledImgCache != null)
74 return disabledImgCache;
75 if (img == null)
76 getImage(); // fix #7498 ?
77 Image disImg = GuiHelper.getDisabledImage(img);
78 if (disImg instanceof BufferedImage) {
79 disabledImgCache = (BufferedImage) disImg;
80 } else {
81 disabledImgCache = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
82 Graphics g = disabledImgCache.getGraphics();
83 g.drawImage(disImg, 0, 0, null);
84 g.dispose();
85 }
86 return disabledImgCache;
87 }
88
89 private BufferedImage getImage() {
90 if (img != null)
91 return img;
92 temporary = false;
93 new ImageProvider(name)
94 .setDirs(MapPaintStyles.getIconSourceDirs(source))
95 .setId("mappaint."+source.getPrefName())
96 .setArchive(source.zipIcons)
97 .setInArchiveDir(source.getZipEntryDirName())
98 .setWidth(width)
99 .setHeight(height)
100 .setOptional(true)
101 .getInBackground((ImageCallback) result -> {
102 synchronized (this) {
103 if (result == null) {
104 source.logWarning(tr("Failed to locate image ''{0}''", name));
105 ImageIcon noIcon = MapPaintStyles.getNoIcon_Icon(source);
106 img = noIcon == null ? null : (BufferedImage) noIcon.getImage();
107 } else {
108 img = (BufferedImage) rescale(result.getImage());
109 }
110 if (temporary) {
111 disabledImgCache = null;
112 Main.map.mapView.preferenceChanged(null); // otherwise repaint is ignored, because layer hasn't changed
113 Main.map.mapView.repaint();
114 }
115 temporary = false;
116 }
117 }
118 );
119 synchronized (this) {
120 if (img == null) {
121 img = (BufferedImage) ImageProvider.get("clock").getImage();
122 temporary = true;
123 }
124 }
125 return img;
126 }
127
128 public int getWidth() {
129 return getImage().getWidth(null);
130 }
131
132 public int getHeight() {
133 return getImage().getHeight(null);
134 }
135
136 public float getAlphaFloat() {
137 return Utils.color_int2float(alpha);
138 }
139
140 /**
141 * Determines if image is not completely loaded and {@code getImage()} returns a temporary image.
142 * @return {@code true} if image is not completely loaded and getImage() returns a temporary image
143 */
144 public boolean isTemporary() {
145 return temporary;
146 }
147
148 protected class MapImageBoxProvider implements BoxProvider {
149 @Override
150 public BoxProviderResult get() {
151 return new BoxProviderResult(box(), temporary);
152 }
153
154 private Rectangle box() {
155 int w = getWidth(), h = getHeight();
156 if (mustRescale(getImage())) {
157 w = 16;
158 h = 16;
159 }
160 return new Rectangle(-w/2, -h/2, w, h);
161 }
162
163 private MapImage getParent() {
164 return MapImage.this;
165 }
166
167 @Override
168 public int hashCode() {
169 return MapImage.this.hashCode();
170 }
171
172 @Override
173 public boolean equals(Object obj) {
174 if (!(obj instanceof BoxProvider))
175 return false;
176 if (obj instanceof MapImageBoxProvider) {
177 MapImageBoxProvider other = (MapImageBoxProvider) obj;
178 return MapImage.this.equals(other.getParent());
179 } else if (temporary) {
180 return false;
181 } else {
182 final BoxProvider other = (BoxProvider) obj;
183 BoxProviderResult resultOther = other.get();
184 if (resultOther.isTemporary()) return false;
185 return box().equals(resultOther.getBox());
186 }
187 }
188 }
189
190 public BoxProvider getBoxProvider() {
191 return new MapImageBoxProvider();
192 }
193
194 /**
195 * Rescale excessively large images.
196 * @param image the unscaled image
197 * @return The scaled down version to 16x16 pixels if the image height and width exceeds 48 pixels and no size has been explicitely specified
198 */
199 private Image rescale(Image image) {
200 if (image == null) return null;
201 // Scale down large (.svg) images to 16x16 pixels if no size is explicitely specified
202 if (mustRescale(image)) {
203 return ImageProvider.createBoundedImage(image, 16);
204 } else {
205 return image;
206 }
207 }
208
209 private boolean mustRescale(Image image) {
210 return autoRescale && width == -1 && image.getWidth(null) > MAX_SIZE
211 && height == -1 && image.getHeight(null) > MAX_SIZE;
212 }
213
214 @Override
215 public boolean equals(Object obj) {
216 if (this == obj) return true;
217 if (obj == null || getClass() != obj.getClass()) return false;
218 MapImage mapImage = (MapImage) obj;
219 return alpha == mapImage.alpha &&
220 autoRescale == mapImage.autoRescale &&
221 width == mapImage.width &&
222 height == mapImage.height &&
223 Objects.equals(name, mapImage.name) &&
224 Objects.equals(source, mapImage.source);
225 }
226
227 @Override
228 public int hashCode() {
229 return Objects.hash(alpha, name, source, autoRescale, width, height);
230 }
231
232 @Override
233 public String toString() {
234 return name;
235 }
236}
Note: See TracBrowser for help on using the repository browser.