commit 82e763f51c86dacbbea890962ece8804cd6f489b
Author: Simon Legner <Simon.Legner@gmail.com>
Date: 2020-03-16 20:24:03 +0100
fix #18941 - MapImage.getImageResource: wait for image to load
diff --git a/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java b/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java
index aef68a6fa..238254cb6 100644
a
|
b
|
|
9 | 9 | import java.awt.Rectangle; |
10 | 10 | import java.awt.image.BufferedImage; |
11 | 11 | import java.util.Objects; |
| 12 | import java.util.concurrent.CompletableFuture; |
| 13 | import java.util.concurrent.ExecutionException; |
12 | 14 | |
13 | 15 | import javax.swing.ImageIcon; |
14 | 16 | |
… |
… |
|
21 | 23 | import org.openstreetmap.josm.gui.util.GuiHelper; |
22 | 24 | import org.openstreetmap.josm.tools.ImageProvider; |
23 | 25 | import org.openstreetmap.josm.tools.ImageResource; |
| 26 | import org.openstreetmap.josm.tools.Logging; |
24 | 27 | import org.openstreetmap.josm.tools.Utils; |
25 | 28 | |
26 | 29 | /** |
… |
… |
public Image getImage(boolean disabled) {
|
114 | 117 | |
115 | 118 | /** |
116 | 119 | * Get the image resource associated with this MapImage object. |
| 120 | * This method blocks until the image has been loaded. |
117 | 121 | * @return the image resource |
118 | 122 | */ |
119 | 123 | public ImageResource getImageResource() { |
| 124 | if (imageResource == null) { |
| 125 | try { |
| 126 | // load and wait for the image |
| 127 | loadImage().get(); |
| 128 | } catch (ExecutionException | InterruptedException e) { |
| 129 | Logging.warn(e); |
| 130 | Thread.currentThread().interrupt(); |
| 131 | } |
| 132 | } |
120 | 133 | return imageResource; |
121 | 134 | } |
122 | 135 | |
… |
… |
private Image getImage() {
|
141 | 154 | if (img != null) |
142 | 155 | return img; |
143 | 156 | temporary = false; |
144 | | new ImageProvider(name) |
| 157 | loadImage(); |
| 158 | synchronized (this) { |
| 159 | if (img == null) { |
| 160 | img = ImageProvider.get("clock").getImage(); |
| 161 | temporary = true; |
| 162 | } |
| 163 | } |
| 164 | return img; |
| 165 | } |
| 166 | |
| 167 | private CompletableFuture<Void> loadImage() { |
| 168 | return new ImageProvider(name) |
145 | 169 | .setDirs(MapPaintStyles.getIconSourceDirs(source)) |
146 | 170 | .setId("mappaint."+source.getPrefName()) |
147 | 171 | .setArchive(source.zipIcons) |
… |
… |
private Image getImage() {
|
167 | 191 | } |
168 | 192 | } |
169 | 193 | ); |
170 | | synchronized (this) { |
171 | | if (img == null) { |
172 | | img = ImageProvider.get("clock").getImage(); |
173 | | temporary = true; |
174 | | } |
175 | | } |
176 | | return img; |
177 | 194 | } |
178 | 195 | |
179 | 196 | /** |