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

Last change on this file since 5257 was 5257, checked in by simon04, 12 years ago

see #7701 - NPE in MapImage

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