Ticket #7182: 7182-merge.patch
File 7182-merge.patch, 20.0 KB (added by , 14 years ago) |
---|
-
src/org/openstreetmap/josm/actions/AddImageryLayerAction.java
11 11 import org.openstreetmap.josm.data.imagery.ImageryInfo; 12 12 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; 13 13 import org.openstreetmap.josm.gui.layer.ImageryLayer; 14 import org.openstreetmap.josm.tools.Image Request;14 import org.openstreetmap.josm.tools.ImageProvider; 15 15 16 16 public class AddImageryLayerAction extends JosmAction implements AdaptableAction { 17 17 … … 27 27 // change toolbar icon from if specified 28 28 try { 29 29 if (info.getIcon() != null) { 30 ImageIcon i = new Image Request().setOptional(true).setName(info.getIcon()).30 ImageIcon i = new ImageProvider(info.getIcon()).setOptional(true). 31 31 setMaxHeight(MAX_ICON_SIZE).setMaxWidth(MAX_ICON_SIZE).get(); 32 32 if (i != null) { 33 33 putValue(Action.SMALL_ICON, i); -
src/org/openstreetmap/josm/tools/ImageRequest.java
1 // License: GPL. For details, see LICENSE file.2 package org.openstreetmap.josm.tools;3 4 import static org.openstreetmap.josm.tools.I18n.tr;5 6 import java.awt.Dimension;7 import java.io.File;8 import java.util.Collection;9 import javax.swing.ImageIcon;10 11 /**12 * Alternative way to request an image.13 * E.g.14 *15 * ImageIcon icon = new ImageRequest().setName(imgName).setWidth(100).setHeight(120).get();16 *17 * or in funky double-brace style18 *19 * ImageIcon icon = new ImageRequest(){{name=imgName; width=100; height=120;}}.get();20 */21 public class ImageRequest {22 protected Collection<String> dirs;23 protected String id;24 protected String subdir;25 protected String name;26 protected File archive;27 protected int width = -1;28 protected int height = -1;29 protected int maxWidth = -1;30 protected int maxHeight = -1;31 protected boolean sanitize;32 protected boolean optional;33 34 public ImageRequest setDirs(Collection<String> dirs) {35 this.dirs = dirs;36 return this;37 }38 39 public ImageRequest setId(String id) {40 this.id = id;41 return this;42 }43 44 public ImageRequest setSubdir(String subdir) {45 this.subdir = subdir;46 return this;47 }48 49 public ImageRequest setName(String name) {50 this.name = name;51 return this;52 }53 54 public ImageRequest setArchive(File archive) {55 this.archive = archive;56 return this;57 }58 59 public ImageRequest setWidth(int width) {60 this.width = width;61 return this;62 }63 64 public ImageRequest setHeight(int height) {65 this.height = height;66 return this;67 }68 69 public ImageRequest setMaxWidth(int maxWidth) {70 this.maxWidth = maxWidth;71 return this;72 }73 74 public ImageRequest setMaxHeight(int maxHeight) {75 this.maxHeight = maxHeight;76 return this;77 }78 79 public ImageRequest setSanitize(boolean sanitize) {80 this.sanitize = sanitize;81 return this;82 }83 84 public ImageRequest setOptional(boolean optional) {85 this.optional = optional;86 return this;87 }88 89 public ImageIcon get() {90 ImageResource ir = ImageProvider.getIfAvailableImpl(dirs, id, subdir, name, archive);91 if (ir == null) {92 if (!optional) {93 String ext = name.indexOf('.') != -1 ? "" : ".???";94 throw new RuntimeException(tr("Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", name + ext));95 } else {96 System.out.println(tr("Failed to locate image ''{0}''", name));97 return null;98 }99 }100 if (maxWidth != -1 || maxHeight != -1)101 return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight), sanitize);102 else103 return ir.getImageIcon(new Dimension(width, height), sanitize);104 }105 106 } -
src/org/openstreetmap/josm/tools/ImageProvider.java
54 54 55 55 /** 56 56 * Helper class to support the application with images. 57 * 58 * How to use: 59 * 60 * <code>ImageIcon icon = new ImageProvider(name).setMaxWidth(24).setMaxHeight(24).get();</code> 61 * (there are more options, see below) 62 * 63 * short form: 64 * <code>ImageIcon icon = ImageProvider.get(name);</code> 65 * 57 66 * @author imi 58 67 */ 59 68 public class ImageProvider { 60 69 61 private static SVGUniverse svgUniverse;62 63 70 /** 64 71 * Position of an overlay icon 65 72 * @author imi … … 73 80 OTHER // everything else, e.g. png, gif (must be supported by Java) 74 81 } 75 82 83 protected Collection<String> dirs; 84 protected String id; 85 protected String subdir; 86 protected String name; 87 protected File archive; 88 protected int width = -1; 89 protected int height = -1; 90 protected int maxWidth = -1; 91 protected int maxHeight = -1; 92 protected boolean sanitize; 93 protected boolean optional; 94 95 private static SVGUniverse svgUniverse; 96 76 97 /** 77 98 * The icon cache 78 99 */ 79 100 private static Map<String, ImageResource> cache = new HashMap<String, ImageResource>(); 80 101 81 102 /** 103 * @param subdir Subdirectory the image lies in. 104 * @param name The name of the image. If it does not end with '.png' or '.svg', 105 * both extensions are tried. 106 */ 107 public ImageProvider(String subdir, String name) { 108 this.subdir = subdir; 109 this.name = name; 110 } 111 112 public ImageProvider(String name) { 113 this.name = name; 114 } 115 116 /** 117 * Directories to look for the image. 118 */ 119 public ImageProvider setDirs(Collection<String> dirs) { 120 this.dirs = dirs; 121 return this; 122 } 123 124 /** 125 * An id used for caching. Id is not used for cache if name starts with http://. (URL is unique anyway.) 126 */ 127 public ImageProvider setId(String id) { 128 this.id = id; 129 return this; 130 } 131 132 /** 133 * A zip file where the image is located. 134 */ 135 public ImageProvider setArchive(File archive) { 136 this.archive = archive; 137 return this; 138 } 139 140 /** 141 * The dimensions of the image. 142 * 143 * If not specified, the original size of the image is used. 144 * The width part of the dimension can be -1. Then it will only set the height but 145 * keep the aspect ratio. (And the other way around.) 146 */ 147 public ImageProvider setSize(Dimension size) { 148 this.width = size.width; 149 this.height = size.height; 150 return this; 151 } 152 153 /** 154 * see setSize 155 */ 156 public ImageProvider setWidth(int width) { 157 this.width = width; 158 return this; 159 } 160 161 /** 162 * see setSize 163 */ 164 public ImageProvider setHeight(int height) { 165 this.height = height; 166 return this; 167 } 168 169 /** 170 * The maximum size of the image. 171 * 172 * It will shrink the image if necessary, but keep the aspect ratio. 173 * The given width or height can be -1 which means this direction is not bounded. 174 * 175 * 'size' and 'maxSize' are not compatible, you should set only one of them. 176 */ 177 public ImageProvider setMaxSize(Dimension maxSize) { 178 this.maxWidth = maxSize.width; 179 this.maxHeight = maxSize.height; 180 return this; 181 } 182 183 /** 184 * see setMaxSize 185 */ 186 public ImageProvider setMaxWidth(int maxWidth) { 187 this.maxWidth = maxWidth; 188 return this; 189 } 190 191 /** 192 * see setMaxSize 193 */ 194 public ImageProvider setMaxHeight(int maxHeight) { 195 this.maxHeight = maxHeight; 196 return this; 197 } 198 199 /** 200 * Set true, if the image should be repainted to a new BufferedImage in order to work around certain issues. 201 */ 202 public ImageProvider setSanitize(boolean sanitize) { 203 this.sanitize = sanitize; 204 return this; 205 } 206 207 /** 208 * The image URL comes from user data and the image may be missing. 209 * 210 * Set true, if JOSM should *not* throw a RuntimeException in case the image cannot be located. 211 */ 212 public ImageProvider setOptional(boolean optional) { 213 this.optional = optional; 214 return this; 215 } 216 217 /** 218 * Execute the image request. 219 */ 220 public ImageIcon get() { 221 ImageResource ir = getIfAvailableImpl(); 222 if (ir == null) { 223 if (!optional) { 224 String ext = name.indexOf('.') != -1 ? "" : ".???"; 225 throw new RuntimeException(tr("Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", name + ext)); 226 } else { 227 System.out.println(tr("Failed to locate image ''{0}''", name)); 228 return null; 229 } 230 } 231 if (maxWidth != -1 || maxHeight != -1) 232 return ir.getImageIconBounded(new Dimension(maxWidth, maxHeight), sanitize); 233 else 234 return ir.getImageIcon(new Dimension(width, height), sanitize); 235 } 236 237 /** 82 238 * Return an image from the specified location. Throws a RuntimeException if 83 239 * the image cannot be located. 84 240 * … … 87 243 * @return The requested Image. 88 244 */ 89 245 public static ImageIcon get(String subdir, String name) { 90 ImageIcon icon = getIfAvailable(subdir, name); 91 if (icon == null) { 92 String ext = name.indexOf('.') != -1 ? "" : ".???"; 93 throw new RuntimeException(tr( 94 "Fatal: failed to locate image ''{0}''. This is a serious configuration problem. JOSM will stop working.", 95 name+ext)); 96 } 97 return icon; 246 return new ImageProvider(subdir, name).get(); 98 247 } 99 248 100 /**101 * Shortcut for get("", name);102 */103 249 public static ImageIcon get(String name) { 104 return get("", name);250 return new ImageProvider(name).get(); 105 251 } 106 252 107 253 public static ImageIcon getIfAvailable(String name) { 108 return getIfAvailable((Collection<String>) null, null, "", name);254 return new ImageProvider(name).setOptional(true).get(); 109 255 } 110 256 111 257 public static ImageIcon getIfAvailable(String subdir, String name) { 112 return getIfAvailable((Collection<String>) null, null, subdir, name);258 return new ImageProvider(subdir, name).setOptional(true).get(); 113 259 } 114 260 261 @Deprecated 115 262 public static ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) { 116 263 return getIfAvailable(Arrays.asList(dirs), id, subdir, name); 117 264 } … … 121 268 * is found. Use this, if the image to retrieve is optional. Nevertheless a warning will 122 269 * be printed on the console if the image could not be found. 123 270 */ 271 @Deprecated 124 272 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) { 125 273 return getIfAvailable(dirs, id, subdir, name, null); 126 274 } 127 275 276 @Deprecated 128 277 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) { 129 278 return getIfAvailable(dirs, id, subdir, name, archive, false); 130 279 } 131 280 281 @Deprecated 132 282 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) { 133 283 return getIfAvailable(dirs, id, subdir, name, archive, null, sanitize); 134 284 } 135 285 286 @Deprecated 136 287 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) { 137 288 return getIfAvailable(dirs, id, subdir, name, archive, dim, null, sanitize); 138 289 } … … 158 309 * @param sanitize If the image should be repainted to a new BufferedImage to work 159 310 * around certain issues. 160 311 */ 312 @Deprecated 161 313 public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, 162 314 File archive, Dimension dim, Dimension maxSize, boolean sanitize) { 163 ImageResource ir = getIfAvailableImpl(dirs, id, subdir, name, archive); 164 if (ir == null) 165 return null; 166 if (maxSize != null) 167 return ir.getImageIconBounded(maxSize, sanitize); 168 else 169 return ir.getImageIcon(dim == null ? ImageResource.DEFAULT_DIMENSION : dim, sanitize); 315 ImageProvider p = new ImageProvider(subdir, name).setDirs(dirs).setId(id).setArchive(archive).setSanitize(sanitize).setOptional(true); 316 if (dim != null) { 317 p.setSize(dim); 318 } 319 if (maxSize != null) { 320 p.setMaxSize(maxSize); 321 } 322 return p.get(); 170 323 } 171 324 172 325 /** … … 176 329 private static final Pattern dataUrlPattern = Pattern.compile( 177 330 "^data:([a-zA-Z]+/[a-zA-Z+]+)?(;base64)?,(.+)$"); 178 331 179 static ImageResource getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) {332 private ImageResource getIfAvailableImpl() { 180 333 if (name == null) 181 334 return null; 182 335 -
src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java
243 243 } 244 244 for (PluginInformation pi : availablePlugins) { 245 245 if (pi.icon == null && pi.iconPath != null) { 246 pi.icon = ImageProvider.getIfAvailable(null, null, null, pi.name+".jar/"+pi.iconPath, destFile); 246 pi.icon = new ImageProvider(pi.name+".jar/"+pi.iconPath) 247 .setArchive(destFile) 248 .setMaxWidth(24) 249 .setMaxHeight(24) 250 .setOptional(true).get(); 247 251 } 248 252 } 249 253 } -
src/org/openstreetmap/josm/plugins/PluginInformation.java
190 190 iconPath = attr.getValue("Plugin-Icon"); 191 191 if (iconPath != null && file != null) { 192 192 // extract icon from the plugin jar file 193 icon = ImageProvider.getIfAvailable(null, null, null, iconPath, file);193 icon = new ImageProvider(iconPath).setArchive(file).setMaxWidth(24).setMaxHeight(24)setOptional(true).get(); 194 194 } 195 195 if(oldcheck && mainversion > Version.getInstance().getVersion()) 196 196 { -
src/org/openstreetmap/josm/plugins/ReadLocalPluginInformationTask.java
120 120 monitor.setCustomText(tr("Processing file ''{0}''", fname)); 121 121 for (PluginInformation pi : availablePlugins.values()) { 122 122 if (pi.icon == null && pi.iconPath != null) { 123 pi.icon = ImageProvider.getIfAvailable(null, null, null, pi.name+".jar/"+pi.iconPath, f); 123 pi.icon = new ImageProvider(pi.name+".jar/"+pi.iconPath) 124 .setArchive(f) 125 .setMaxWidth(24) 126 .setMaxHeight(24) 127 .setOptional(true).get(); 124 128 } 125 129 } 126 130 monitor.worked(1); -
src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
88 88 89 89 public static ImageIcon getIcon(IconReference ref, int width, int height, boolean sanitize) { 90 90 final String namespace = ref.source.getPrefName(); 91 ImageIcon i = ImageProvider.getIfAvailable(getIconSourceDirs(ref.source), "mappaint."+namespace, null, ref.iconName, ref.source.zipIcons, width == -1 && height == -1 ? null : new Dimension(width, height), sanitize); 91 ImageIcon i = new ImageProvider(ref.iconName) 92 .setDirs(getIconSourceDirs(ref.source)) 93 .setId("mappaint."+namespace) 94 .setArchive(ref.source.zipIcons) 95 .setWidth(width) 96 .setHeight(height) 97 .setSanitize(sanitize) 98 .setOptional(true).get(); 92 99 if(i == null) 93 100 { 94 101 System.out.println("Mappaint style \""+namespace+"\" ("+ref.source.getDisplayString()+") icon \"" + ref.iconName + "\" not found."); … … 106 113 * can be null if the defaults are turned off by user 107 114 */ 108 115 public static ImageIcon getNoIcon_Icon(StyleSource source, boolean sanitize) { 109 return ImageProvider.getIfAvailable(getIconSourceDirs(source), "mappaint."+source.getPrefName(), null, "misc/no_icon.png", source.zipIcons, sanitize); 116 return new ImageProvider("misc/no_icon.png") 117 .setDirs(getIconSourceDirs(source)) 118 .setId("mappaint."+source.getPrefName()) 119 .setArchive(source.zipIcons) 120 .setSanitize(sanitize) 121 .setOptional(true).get(); 110 122 } 111 123 112 124 private static List<String> getIconSourceDirs(StyleSource source) { -
src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
1057 1057 */ 1058 1058 public void setIcon(String iconName) { 1059 1059 Collection<String> s = Main.pref.getCollection("taggingpreset.icon.sources", null); 1060 ImageIcon icon = ImageProvider.getIfAvailable(s, "presets", null, iconName, zipIcons);1060 ImageIcon icon = new ImageProvider(iconName).setDirs(s).setId("presets").setArchive(zipIcons).setOptional(true).get(); 1061 1061 if (icon == null) 1062 1062 { 1063 1063 System.out.println("Could not get presets icon " + iconName); -
src/org/openstreetmap/josm/io/session/OsmDataSessionExporter.java
44 44 import org.openstreetmap.josm.io.OsmWriterFactory; 45 45 import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport; 46 46 import org.openstreetmap.josm.tools.GBC; 47 import org.openstreetmap.josm.tools.Image Request;47 import org.openstreetmap.josm.tools.ImageProvider; 48 48 49 49 public class OsmDataSessionExporter implements SessionLayerExporter { 50 50 … … 68 68 69 69 private class LayerSaveAction extends AbstractAction { 70 70 public LayerSaveAction() { 71 putValue(SMALL_ICON, new Image Request().setName("save").setWidth(16).get());71 putValue(SMALL_ICON, new ImageProvider("save").setWidth(16).get()); 72 72 putValue(SHORT_DESCRIPTION, tr("Layer contains unsaved data - save to file.")); 73 73 updateEnabledState(); 74 74 }