Ticket #7182: 7182-merge.patch

File 7182-merge.patch, 20.0 KB (added by bastiK, 14 years ago)
  • src/org/openstreetmap/josm/actions/AddImageryLayerAction.java

     
    1111import org.openstreetmap.josm.data.imagery.ImageryInfo;
    1212import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
    1313import org.openstreetmap.josm.gui.layer.ImageryLayer;
    14 import org.openstreetmap.josm.tools.ImageRequest;
     14import org.openstreetmap.josm.tools.ImageProvider;
    1515
    1616public class AddImageryLayerAction extends JosmAction implements AdaptableAction {
    1717
     
    2727        // change toolbar icon from if specified
    2828        try {
    2929            if (info.getIcon() != null) {
    30                 ImageIcon i = new ImageRequest().setOptional(true).setName(info.getIcon()).
     30                ImageIcon i = new ImageProvider(info.getIcon()).setOptional(true).
    3131                        setMaxHeight(MAX_ICON_SIZE).setMaxWidth(MAX_ICON_SIZE).get();
    3232                if (i != null) {
    3333                    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 style
    18  *
    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         else
    103             return ir.getImageIcon(new Dimension(width, height), sanitize);
    104     }
    105    
    106 }
  • src/org/openstreetmap/josm/tools/ImageProvider.java

     
    5454
    5555/**
    5656 * 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 *
    5766 * @author imi
    5867 */
    5968public class ImageProvider {
    6069
    61     private static SVGUniverse svgUniverse;
    62 
    6370    /**
    6471     * Position of an overlay icon
    6572     * @author imi
     
    7380        OTHER   // everything else, e.g. png, gif (must be supported by Java)
    7481    }
    7582
     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
    7697    /**
    7798     * The icon cache
    7899     */
    79100    private static Map<String, ImageResource> cache = new HashMap<String, ImageResource>();
    80101
    81102    /**
     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    /**
    82238     * Return an image from the specified location. Throws a RuntimeException if
    83239     * the image cannot be located.
    84240     *
     
    87243     * @return The requested Image.
    88244     */
    89245    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();
    98247    }
    99248
    100     /**
    101      * Shortcut for get("", name);
    102      */
    103249    public static ImageIcon get(String name) {
    104         return get("", name);
     250        return new ImageProvider(name).get();
    105251    }
    106252
    107253    public static ImageIcon getIfAvailable(String name) {
    108         return getIfAvailable((Collection<String>) null, null, "", name);
     254        return new ImageProvider(name).setOptional(true).get();
    109255    }
    110256
    111257    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();
    113259    }
    114260
     261    @Deprecated
    115262    public static ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) {
    116263        return getIfAvailable(Arrays.asList(dirs), id, subdir, name);
    117264    }
     
    121268     * is found. Use this, if the image to retrieve is optional. Nevertheless a warning will
    122269     * be printed on the console if the image could not be found.
    123270     */
     271    @Deprecated
    124272    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name) {
    125273        return getIfAvailable(dirs, id, subdir, name, null);
    126274    }
    127275
     276    @Deprecated
    128277    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive) {
    129278        return getIfAvailable(dirs, id, subdir, name, archive, false);
    130279    }
    131280
     281    @Deprecated
    132282    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, boolean sanitize) {
    133283        return getIfAvailable(dirs, id, subdir, name, archive, null, sanitize);
    134284    }
    135285
     286    @Deprecated
    136287    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name, File archive, Dimension dim, boolean sanitize) {
    137288        return getIfAvailable(dirs, id, subdir, name, archive, dim, null, sanitize);
    138289    }
     
    158309     * @param sanitize  If the image should be repainted to a new BufferedImage to work
    159310     *                  around certain issues.
    160311     */
     312    @Deprecated
    161313    public static ImageIcon getIfAvailable(Collection<String> dirs, String id, String subdir, String name,
    162314            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();
    170323    }
    171324
    172325    /**
     
    176329    private static final Pattern dataUrlPattern = Pattern.compile(
    177330            "^data:([a-zA-Z]+/[a-zA-Z+]+)?(;base64)?,(.+)$");
    178331
    179     static ImageResource getIfAvailableImpl(Collection<String> dirs, String id, String subdir, String name, File archive) {
     332    private ImageResource getIfAvailableImpl() {
    180333        if (name == null)
    181334            return null;
    182335
  • src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java

     
    243243        }
    244244        for (PluginInformation pi : availablePlugins) {
    245245            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();
    247251            }
    248252        }
    249253    }
  • src/org/openstreetmap/josm/plugins/PluginInformation.java

     
    190190        iconPath = attr.getValue("Plugin-Icon");
    191191        if (iconPath != null && file != null) {
    192192            // 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();
    194194        }
    195195        if(oldcheck && mainversion > Version.getInstance().getVersion())
    196196        {
  • src/org/openstreetmap/josm/plugins/ReadLocalPluginInformationTask.java

     
    120120            monitor.setCustomText(tr("Processing file ''{0}''", fname));
    121121            for (PluginInformation pi : availablePlugins.values()) {
    122122                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();
    124128                }
    125129            }
    126130            monitor.worked(1);
  • src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

     
    8888
    8989    public static ImageIcon getIcon(IconReference ref, int width, int height, boolean sanitize) {
    9090        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();
    9299        if(i == null)
    93100        {
    94101            System.out.println("Mappaint style \""+namespace+"\" ("+ref.source.getDisplayString()+") icon \"" + ref.iconName + "\" not found.");
     
    106113     *  can be null if the defaults are turned off by user
    107114     */
    108115    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();
    110122    }
    111123
    112124    private static List<String> getIconSourceDirs(StyleSource source) {
  • src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

     
    10571057     */
    10581058    public void setIcon(String iconName) {
    10591059        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();
    10611061        if (icon == null)
    10621062        {
    10631063            System.out.println("Could not get presets icon " + iconName);
  • src/org/openstreetmap/josm/io/session/OsmDataSessionExporter.java

     
    4444import org.openstreetmap.josm.io.OsmWriterFactory;
    4545import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport;
    4646import org.openstreetmap.josm.tools.GBC;
    47 import org.openstreetmap.josm.tools.ImageRequest;
     47import org.openstreetmap.josm.tools.ImageProvider;
    4848
    4949public class OsmDataSessionExporter implements SessionLayerExporter {
    5050
     
    6868
    6969    private class LayerSaveAction extends AbstractAction {
    7070        public LayerSaveAction() {
    71             putValue(SMALL_ICON, new ImageRequest().setName("save").setWidth(16).get());
     71            putValue(SMALL_ICON, new ImageProvider("save").setWidth(16).get());
    7272            putValue(SHORT_DESCRIPTION, tr("Layer contains unsaved data - save to file."));
    7373            updateEnabledState();
    7474        }