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

Last change on this file since 7238 was 7083, checked in by Don-vip, 10 years ago

see #8465 - replace Utils.equal by Objects.equals, new in Java 7

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