Changeset 4721 in josm


Ignore:
Timestamp:
2011-12-27T16:11:03+01:00 (12 years ago)
Author:
bastiK
Message:

use builder pattern for ImageProvider (see #7192)

Location:
trunk/src/org/openstreetmap/josm
Files:
1 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java

    r4715 r4721  
    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 {
     
    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) {
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java

    r4718 r4721  
    122122            if (active != null && active instanceof GpxLayer && (merge || ((GpxLayer)active).data.fromServer))
    123123                return active;
    124             for (Layer l : Main.map.mapView.getAllLayers())
     124            for (Layer l : Main.map.mapView.getAllLayers()) {
    125125                if (l instanceof GpxLayer &&  (merge || ((GpxLayer)l).data.fromServer))
    126126                    return l;
    127                     return null;
     127            }
     128            return null;
    128129        }
    129130
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r4272 r4721  
    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        {
     
    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
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r4528 r4721  
    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        {
  • trunk/src/org/openstreetmap/josm/io/session/OsmDataSessionExporter.java

    r4688 r4721  
    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 {
     
    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();
  • trunk/src/org/openstreetmap/josm/plugins/PluginInformation.java

    r4310 r4721  
    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())
  • trunk/src/org/openstreetmap/josm/plugins/ReadLocalPluginInformationTask.java

    r4310 r4721  
    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            }
  • trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java

    r4612 r4721  
    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        }
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r4713 r4721  
    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
     
    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>();
     101
     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    }
    80236
    81237    /**
     
    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;
    98     }
    99 
    100     /**
    101      * Shortcut for get("", name);
    102      */
     246        return new ImageProvider(subdir, name).get();
     247    }
     248
    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);
    113     }
    114 
     258        return new ImageProvider(subdir, name).setOptional(true).get();
     259    }
     260
     261    @Deprecated
    115262    public static ImageIcon getIfAvailable(String[] dirs, String id, String subdir, String name) {
    116263        return getIfAvailable(Arrays.asList(dirs), id, subdir, name);
     
    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);
     
    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
     
    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;
Note: See TracChangeset for help on using the changeset viewer.