Ticket #6797: async.diff
File async.diff, 7.7 KB (added by , 13 years ago) |
---|
-
src/org/openstreetmap/josm/tools/ImageProvider.java
30 30 import java.util.Collection; 31 31 import java.util.HashMap; 32 32 import java.util.Map; 33 import java.util.concurrent.Executors; 34 import java.util.concurrent.ExecutorService; 33 35 import java.util.regex.Matcher; 34 36 import java.util.regex.Pattern; 35 37 import java.util.zip.ZipEntry; … … 107 109 */ 108 110 private static Map<String, ImageResource> cache = new HashMap<String, ImageResource>(); 109 111 112 private final static ExecutorService imageFetcher = Executors.newSingleThreadExecutor(); 113 114 public final static int STATUS_IN_BACKGROUND = 0; 115 public final static int STATUS_FINISHED_EARLY = 1; 116 117 public interface ImageCallback { 118 void finished(ImageIcon result, int status); 119 } 120 110 121 /** 111 122 * @param subdir Subdirectory the image lies in. 112 123 * @param name The name of the image. If it does not end with '.png' or '.svg', … … 243 254 } 244 255 245 256 /** 257 * Load the image in a background thread. 258 * 259 * There are 2 possible return values: 260 * - STATUS_FINISHED_EARLY 261 * This means that image is returned directly: The callback will have been called 262 * before this method returns. 263 * - STATUS_IN_BACKGROUND 264 * Image is fetched in the background and callback will be executed at any time. 265 */ 266 public int getInBackground(final ImageCallback callback) { 267 if (name.startsWith("http://") || name.startsWith("wiki://")) { 268 Runnable fetch = new Runnable() { 269 @Override 270 public void run() { 271 ImageIcon result = get(); 272 callback.finished(result, STATUS_IN_BACKGROUND); 273 } 274 }; 275 imageFetcher.submit(fetch); 276 return STATUS_IN_BACKGROUND; 277 } else { 278 ImageIcon result = get(); 279 callback.finished(result, STATUS_FINISHED_EARLY); 280 return STATUS_FINISHED_EARLY; 281 } 282 } 283 284 /** 246 285 * Return an image from the specified location. Throws a RuntimeException if 247 286 * the image cannot be located. 248 287 * -
src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java
19 19 import org.openstreetmap.josm.data.osm.visitor.paint.MapPainter; 20 20 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 21 21 import org.openstreetmap.josm.gui.mappaint.StyleCache.StyleList; 22 import org.openstreetmap.josm.tools.ImageProvider; 23 import org.openstreetmap.josm.tools.ImageProvider.ImageCallback; 22 24 import org.openstreetmap.josm.tools.Pair; 23 25 import org.openstreetmap.josm.tools.Utils; 24 26 … … 29 31 public MapImage<Image> mapImage; 30 32 public Symbol symbol; 31 33 32 private ImageIcon disabledIcon;33 34 34 public enum SymbolShape { SQUARE, CIRCLE, TRIANGLE, PENTAGON, HEXAGON, HEPTAGON, OCTAGON, NONAGON, DECAGON } 35 35 36 36 public static class Symbol { … … 125 125 Cascade c = env.mc.getCascade(env.layer); 126 126 Cascade c_def = env.mc.getCascade("default"); 127 127 128 IconReference iconRef = c.get("icon-image", null, IconReference.class);128 final IconReference iconRef = c.get("icon-image", null, IconReference.class); 129 129 if (iconRef == null) 130 130 return null; 131 131 … … 144 144 int width = widthF == null ? -1 : Math.round(widthF); 145 145 int height = heightF == null ? -1 : Math.round(heightF); 146 146 147 MapImage<Image> mapImage = new MapImage<Image>(iconRef.iconName, iconRef.source);147 final MapImage<Image> mapImage = new MapImage<Image>(iconRef.iconName, iconRef.source); 148 148 149 ImageIcon icon = MapPaintStyles.getIcon(iconRef, width, height); 150 if (icon == null) { 151 mapImage.img = MapPaintStyles.getNoIcon_Icon(iconRef.source).getImage(); 152 } else { 153 mapImage.img = icon.getImage(); 154 mapImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255)))); 155 Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class)); 156 if (pAlpha != null) { 157 mapImage.alpha = pAlpha; 149 synchronized (mapImage) { 150 int result = new ImageProvider(iconRef.iconName) 151 .setDirs(MapPaintStyles.getIconSourceDirs(iconRef.source)) 152 .setId("mappaint."+iconRef.source.getPrefName()) 153 .setArchive(iconRef.source.zipIcons) 154 .setWidth(width) 155 .setHeight(height) 156 .setOptional(true) 157 .getInBackground(new ImageCallback() { 158 @Override 159 public void finished(ImageIcon result, int status) { 160 if (status == ImageProvider.STATUS_FINISHED_EARLY) { 161 mapImage.img = result.getImage(); 162 } else if (status == ImageProvider.STATUS_IN_BACKGROUND) { 163 synchronized (mapImage) { 164 if (result == null) { 165 mapImage.img = MapPaintStyles.getNoIcon_Icon(iconRef.source).getImage(); 166 mapImage.alpha = 255; 167 } else { 168 mapImage.img = result.getImage(); 169 } 170 } 171 Main.map.mapView.preferenceChanged(null); // otherwise repaint is ignored, because layer hasn't changed 172 Main.map.mapView.repaint(); 173 } 174 } 175 } 176 ); 177 178 if (result == ImageProvider.STATUS_FINISHED_EARLY && mapImage.img == null) { 179 mapImage.img = MapPaintStyles.getNoIcon_Icon(iconRef.source).getImage(); 180 mapImage.alpha = 255; 181 } else { 182 mapImage.width = width; 183 mapImage.height = height; 184 185 mapImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255)))); 186 Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class)); 187 if (pAlpha != null) { 188 mapImage.alpha = pAlpha; 189 } 158 190 } 159 mapImage.width = width; 160 mapImage.height = height; 191 if (result == ImageProvider.STATUS_IN_BACKGROUND) { 192 mapImage.img = ImageProvider.get("clock").getImage(); 193 } 161 194 } 162 195 return mapImage; 163 196 } -
src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
130 130 .setOptional(true).get(); 131 131 } 132 132 133 p rivatestatic List<String> getIconSourceDirs(StyleSource source) {133 public static List<String> getIconSourceDirs(StyleSource source) { 134 134 List<String> dirs = new LinkedList<String>(); 135 135 136 136 String sourceDir = source.getLocalSourceDir();