Ticket #6797: async2.diff
| File async2.diff, 19.1 KB (added by , 14 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 interface ImageCallback { 115 void finished(ImageIcon result); 116 } 117 110 118 /** 111 119 * @param subdir Subdirectory the image lies in. 112 120 * @param name The name of the image. If it does not end with '.png' or '.svg', … … 243 251 } 244 252 245 253 /** 254 * Load the image in a background thread. 255 */ 256 public void getInBackground(final ImageCallback callback) { 257 if (name.startsWith("http://") || name.startsWith("wiki://")) { 258 Runnable fetch = new Runnable() { 259 @Override 260 public void run() { 261 ImageIcon result = get(); 262 callback.finished(result); 263 } 264 }; 265 imageFetcher.submit(fetch); 266 } else { 267 ImageIcon result = get(); 268 callback.finished(result); 269 } 270 } 271 272 /** 246 273 * Return an image from the specified location. Throws a RuntimeException if 247 274 * the image cannot be located. 248 275 * -
src/org/openstreetmap/josm/gui/mappaint/LinePatternElemStyle.java
1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.mappaint; 3 3 4 import java.awt.Image;5 6 import javax.swing.ImageIcon;7 8 4 import org.openstreetmap.josm.data.osm.OsmPrimitive; 9 5 import org.openstreetmap.josm.data.osm.Way; 10 6 import org.openstreetmap.josm.data.osm.visitor.paint.MapPaintSettings; … … 16 12 */ 17 13 public class LinePatternElemStyle extends ElemStyle { 18 14 19 public MapImage <Image>pattern;15 public MapImage pattern; 20 16 21 public LinePatternElemStyle(Cascade c, MapImage <Image>pattern) {17 public LinePatternElemStyle(Cascade c, MapImage pattern) { 22 18 super(c, -1f); 23 19 this.pattern = pattern; 24 20 } … … 29 25 IconReference iconRef = c.get("pattern-image", null, IconReference.class); 30 26 if (iconRef == null) 31 27 return null; 32 ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1); 33 if (icon == null) 34 return null; 35 MapImage<Image> pattern = new MapImage<Image>(iconRef.iconName, iconRef.source); 36 pattern.img = icon.getImage(); 28 MapImage pattern = new MapImage(iconRef.iconName, iconRef.source); 37 29 return new LinePatternElemStyle(c, pattern); 38 30 } 39 31 40 32 @Override 41 33 public void paintPrimitive(OsmPrimitive primitive, MapPaintSettings paintSettings, MapPainter painter, boolean selected, boolean member) { 42 34 Way w = (Way)primitive; 43 painter.drawLinePattern(w, pattern. img);35 painter.drawLinePattern(w, pattern.getImage()); 44 36 } 45 37 46 38 @Override -
src/org/openstreetmap/josm/gui/mappaint/NodeElemStyle.java
5 5 6 6 import java.awt.BasicStroke; 7 7 import java.awt.Color; 8 import java.awt.Image;9 8 import java.awt.Rectangle; 10 9 import java.awt.Stroke; 11 10 12 import javax.swing.ImageIcon;13 14 11 import org.openstreetmap.josm.Main; 15 12 import org.openstreetmap.josm.data.osm.Node; 16 13 import org.openstreetmap.josm.data.osm.OsmPrimitive; … … 26 23 * applies for Nodes and turn restriction relations 27 24 */ 28 25 public class NodeElemStyle extends ElemStyle { 29 public MapImage <Image>mapImage;26 public MapImage mapImage; 30 27 public Symbol symbol; 31 28 32 private ImageIcon disabledIcon;33 34 29 public enum SymbolShape { SQUARE, CIRCLE, TRIANGLE, PENTAGON, HEXAGON, HEPTAGON, OCTAGON, NONAGON, DECAGON } 35 30 36 31 public static class Symbol { … … 94 89 public static final StyleList DEFAULT_NODE_STYLELIST = new StyleList(NodeElemStyle.SIMPLE_NODE_ELEMSTYLE); 95 90 public static final StyleList DEFAULT_NODE_STYLELIST_TEXT = new StyleList(NodeElemStyle.SIMPLE_NODE_ELEMSTYLE, BoxTextElemStyle.SIMPLE_NODE_TEXT_ELEMSTYLE); 96 91 97 protected NodeElemStyle(Cascade c, MapImage <Image>mapImage, Symbol symbol) {92 protected NodeElemStyle(Cascade c, MapImage mapImage, Symbol symbol) { 98 93 super(c, 1000f); 99 94 this.mapImage = mapImage; 100 95 this.symbol = symbol; … … 107 102 private static NodeElemStyle create(Environment env, boolean allowDefault) { 108 103 Cascade c = env.mc.getCascade(env.layer); 109 104 110 MapImage <Image>mapImage = createIcon(env);105 MapImage mapImage = createIcon(env); 111 106 Symbol symbol = null; 112 107 if (mapImage == null) { 113 108 symbol = createSymbol(env); … … 121 116 return new NodeElemStyle(c, mapImage, symbol); 122 117 } 123 118 124 private static MapImage <Image>createIcon(Environment env) {119 private static MapImage createIcon(Environment env) { 125 120 Cascade c = env.mc.getCascade(env.layer); 126 121 Cascade c_def = env.mc.getCascade("default"); 127 122 128 IconReference iconRef = c.get("icon-image", null, IconReference.class);123 final IconReference iconRef = c.get("icon-image", null, IconReference.class); 129 124 if (iconRef == null) 130 125 return null; 131 126 … … 144 139 int width = widthF == null ? -1 : Math.round(widthF); 145 140 int height = heightF == null ? -1 : Math.round(heightF); 146 141 147 MapImage<Image> mapImage = new MapImage<Image>(iconRef.iconName, iconRef.source);142 final MapImage mapImage = new MapImage(iconRef.iconName, iconRef.source); 148 143 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; 158 } 159 mapImage.width = width; 160 mapImage.height = height; 144 mapImage.width = width; 145 mapImage.height = height; 146 147 mapImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.icon-image-alpha", 255)))); 148 Integer pAlpha = Utils.color_float2int(c.get("icon-opacity", null, float.class)); 149 if (pAlpha != null) { 150 mapImage.alpha = pAlpha; 161 151 } 162 152 return mapImage; 163 153 } … … 242 232 if (primitive instanceof Node) { 243 233 Node n = (Node) primitive; 244 234 if (mapImage != null && painter.isShowIcons()) { 245 painter.drawNodeIcon(n, (painter.isInactiveMode() || n.isDisabled()) ? mapImage.getDisabled() : mapImage. img,235 painter.drawNodeIcon(n, (painter.isInactiveMode() || n.isDisabled()) ? mapImage.getDisabled() : mapImage.getImage(), 246 236 Utils.color_int2float(mapImage.alpha), selected, member); 247 237 } else if (symbol != null) { 248 238 Color fillColor = symbol.fillColor; … … 310 300 311 301 public Rectangle getBox() { 312 302 if (mapImage != null) { 313 int w = mapImage. img.getWidth(null), h = mapImage.img.getHeight(null);303 int w = mapImage.getWidth(), h = mapImage.getHeight(); 314 304 return new Rectangle(-w/2, -h/2, w, h); 315 305 } else if (symbol != null) { 316 306 return new Rectangle(-symbol.size/2, -symbol.size/2, symbol.size, symbol.size); -
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(); -
src/org/openstreetmap/josm/gui/mappaint/MapImage.java
2 2 package org.openstreetmap.josm.gui.mappaint; 3 3 4 4 import static org.openstreetmap.josm.tools.Utils.equal; 5 5 6 import java.awt.Image; 7 import java.awt.image.BufferedImage; 6 8 7 9 import javax.swing.GrayFilter; 8 10 import javax.swing.ImageIcon; 9 11 10 public class MapImage<I extends Image> { 12 import org.openstreetmap.josm.Main; 13 import org.openstreetmap.josm.tools.ImageProvider; 14 import org.openstreetmap.josm.tools.ImageProvider.ImageCallback; 15 import org.openstreetmap.josm.tools.Utils; 16 17 public class MapImage { 11 18 /** 12 19 * ImageIcon can chage while the image is loading. 13 20 */ 14 p ublic Iimg;21 private Image img; 15 22 16 23 public int alpha = 255; 17 24 … … 36 43 return disabledImg = GrayFilter.createDisabledImage(img); 37 44 } 38 45 46 public Image getImage() { 47 if (img != null) 48 return img; 49 new ImageProvider(name) 50 .setDirs(MapPaintStyles.getIconSourceDirs(source)) 51 .setId("mappaint."+source.getPrefName()) 52 .setArchive(source.zipIcons) 53 .setWidth(width) 54 .setHeight(height) 55 .setOptional(true) 56 .getInBackground(new ImageCallback() { 57 @Override 58 public void finished(ImageIcon result) { 59 synchronized (this) { 60 boolean repaint = img != null; 61 if (result == null) { 62 img = MapPaintStyles.getNoIcon_Icon(source).getImage(); 63 } else { 64 img = result.getImage(); 65 } 66 if (repaint) { 67 Main.map.mapView.preferenceChanged(null); // otherwise repaint is ignored, because layer hasn't changed 68 Main.map.mapView.repaint(); 69 } 70 } 71 } 72 } 73 ); 74 synchronized (this) { 75 if (img == null) { 76 img = ImageProvider.get("clock").getImage(); 77 } 78 } 79 return img; 80 } 81 82 public BufferedImage getBufferedImage() { 83 if (img == null) { 84 getImage(); 85 } 86 if (!(img instanceof BufferedImage)) { 87 img = ImageProvider.sanitize(img); 88 } 89 return (BufferedImage)img; 90 } 91 92 public int getWidth() { 93 return getImage().getWidth(null); 94 } 95 96 public int getHeight() { 97 return getImage().getHeight(null); 98 } 99 100 public float getAlphaFloat() { 101 return Utils.color_int2float(alpha); 102 } 103 39 104 // img changes when image is fully loaded and can't be used for equality check. 40 105 @Override 41 106 public boolean equals(Object obj) { -
src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java
4 4 import static org.openstreetmap.josm.tools.Utils.equal; 5 5 6 6 import java.awt.Color; 7 import java.awt.image.BufferedImage;8 7 9 import javax.swing.ImageIcon;10 11 8 import org.openstreetmap.josm.Main; 12 9 import org.openstreetmap.josm.data.osm.OsmPrimitive; 13 10 import org.openstreetmap.josm.data.osm.Relation; … … 17 14 import org.openstreetmap.josm.data.osm.visitor.paint.PaintColors; 18 15 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 19 16 import org.openstreetmap.josm.tools.CheckParameterUtil; 20 import org.openstreetmap.josm.tools.ImageProvider.SanitizeMode;21 17 import org.openstreetmap.josm.tools.Utils; 22 18 23 19 public class AreaElemStyle extends ElemStyle … … 27 23 * an arbitrary color value sampled from the fillImage 28 24 */ 29 25 public Color color; 30 public MapImage <BufferedImage>fillImage;26 public MapImage fillImage; 31 27 public TextElement text; 32 28 33 protected AreaElemStyle(Cascade c, Color color, MapImage <BufferedImage>fillImage, TextElement text) {29 protected AreaElemStyle(Cascade c, Color color, MapImage fillImage, TextElement text) { 34 30 super(c, -1000f); 35 31 CheckParameterUtil.ensureParameterNotNull(color); 36 32 this.color = color; … … 39 35 } 40 36 41 37 public static AreaElemStyle create(Cascade c) { 42 MapImage <BufferedImage>fillImage = null;38 MapImage fillImage = null; 43 39 Color color = null; 44 40 45 41 IconReference iconRef = c.get("fill-image", null, IconReference.class); 46 42 if (iconRef != null) { 47 ImageIcon icon = MapPaintStyles.getIcon(iconRef, -1, -1, SanitizeMode.MAKE_BUFFEREDIMAGE); 48 if (icon != null) { 49 if (!(icon.getImage() instanceof BufferedImage)) 50 throw new RuntimeException(); 51 fillImage = new MapImage<BufferedImage>(iconRef.iconName, iconRef.source); 52 fillImage.img = (BufferedImage) icon.getImage(); 43 fillImage = new MapImage(iconRef.iconName, iconRef.source); 44 fillImage.getBufferedImage(); 53 45 54 color = new Color(fillImage.img.getRGB(55 fillImage.img.getWidth() / 2, fillImage.img.getHeight() / 2)56 );46 color = new Color(fillImage.getBufferedImage().getRGB( 47 fillImage.getWidth() / 2, fillImage.getHeight() / 2) 48 ); 57 49 58 fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255)))); 59 Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class)); 60 if (pAlpha != null) { 61 fillImage.alpha = pAlpha; 62 } 50 fillImage.alpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fill-image-alpha", 255)))); 51 Integer pAlpha = Utils.color_float2int(c.get("fill-opacity", null, float.class)); 52 if (pAlpha != null) { 53 fillImage.alpha = pAlpha; 63 54 } 64 55 } else { 65 56 color = c.get("fill-color", null, Color.class); -
src/org/openstreetmap/josm/gui/MapView.java
459 459 /** 460 460 * Draw the component. 461 461 */ 462 @Override public void paint(Graphics g) {462 @Override public synchronized void paint(Graphics g) { 463 463 if (BugReportExceptionHandler.exceptionHandlingInProgress()) 464 464 return; 465 465 … … 832 832 } 833 833 } 834 834 835 public void preferenceChanged(PreferenceChangeEvent e) {835 public synchronized void preferenceChanged(PreferenceChangeEvent e) { 836 836 paintPreferencesChanged = true; 837 837 } 838 838 -
src/org/openstreetmap/josm/data/osm/visitor/paint/MapPainter.java
802 802 return path; 803 803 } 804 804 805 public void drawArea(Way w, Color color, MapImage <BufferedImage>fillImage, TextElement text) {805 public void drawArea(Way w, Color color, MapImage fillImage, TextElement text) { 806 806 drawArea(w, getPath(w), color, fillImage, text); 807 807 } 808 808 809 protected void drawArea(OsmPrimitive osm, Path2D.Double path, Color color, MapImage <BufferedImage>fillImage, TextElement text) {809 protected void drawArea(OsmPrimitive osm, Path2D.Double path, Color color, MapImage fillImage, TextElement text) { 810 810 811 811 Shape area = path.createTransformedShape(nc.getAffineTransform()); 812 812 … … 815 815 g.setColor(color); 816 816 g.fill(area); 817 817 } else { 818 TexturePaint texture = new TexturePaint(fillImage. img,818 TexturePaint texture = new TexturePaint(fillImage.getBufferedImage(), 819 819 // new Rectangle(polygon.xpoints[0], polygon.ypoints[0], fillImage.getWidth(), fillImage.getHeight())); 820 new Rectangle(0, 0, fillImage. img.getWidth(null), fillImage.img.getHeight(null)));820 new Rectangle(0, 0, fillImage.getWidth(), fillImage.getHeight())); 821 821 g.setPaint(texture); 822 if (fillImage. alpha!= 1f) {823 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fillImage. alpha));822 if (fillImage.getAlphaFloat() != 1f) { 823 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, fillImage.getAlphaFloat())); 824 824 } 825 825 g.fill(area); 826 826 g.setPaintMode(); … … 871 871 } 872 872 } 873 873 874 public void drawArea(Relation r, Color color, MapImage <BufferedImage>fillImage, TextElement text) {874 public void drawArea(Relation r, Color color, MapImage fillImage, TextElement text) { 875 875 Multipolygon multipolygon = MultipolygonCache.getInstance().get(nc, r); 876 876 if (!r.isDisabled() && !multipolygon.getOuterWays().isEmpty()) { 877 877 for (PolyData pd : multipolygon.getCombinedPolygons()) { … … 913 913 } 914 914 } 915 915 916 public void drawRestriction(Relation r, MapImage <Image>icon) {916 public void drawRestriction(Relation r, MapImage icon) { 917 917 Way fromWay = null; 918 918 Way toWay = null; 919 919 OsmPrimitive via = null; … … 1084 1084 iconAngle = 270-fromAngleDeg; 1085 1085 } 1086 1086 1087 drawRestriction(inactive || r.isDisabled() ? icon.getDisabled() : icon. img,1087 drawRestriction(inactive || r.isDisabled() ? icon.getDisabled() : icon.getImage(), 1088 1088 pVia, vx, vx2, vy, vy2, iconAngle, r.isSelected()); 1089 1089 } 1090 1090
