org.openstreetmap.josm.tools
Class ImageProvider

java.lang.Object
  extended by org.openstreetmap.josm.tools.ImageProvider

public class ImageProvider
extends java.lang.Object

Helper class to support the application with images. How to use: ImageIcon icon = new ImageProvider(name).setMaxWidth(24).setMaxHeight(24).get(); (there are more options, see below) short form: ImageIcon icon = ImageProvider.get(name);


Nested Class Summary
static interface ImageProvider.ImageCallback
           
static class ImageProvider.ImageType
           
static class ImageProvider.OverlayPosition
          Position of an overlay icon
 
Field Summary
protected  java.util.Collection<java.lang.ClassLoader> additionalClassLoaders
           
protected  java.io.File archive
           
private static java.util.Map<java.lang.String,ImageResource> cache
          The icon cache
private static java.util.regex.Pattern dataUrlPattern
          data:[<mediatype>][;base64],<data>
(package private) static double DEGREE_90
          90 degrees in radians units
protected  java.util.Collection<java.lang.String> dirs
           
protected  int height
           
protected  java.lang.String id
           
private static java.util.concurrent.ExecutorService imageFetcher
           
protected  int maxHeight
           
protected  int maxWidth
           
protected  java.lang.String name
           
protected  boolean optional
           
protected  java.lang.String subdir
           
protected  boolean suppressWarnings
           
private static SVGUniverse svgUniverse
           
protected  int width
           
 
Constructor Summary
ImageProvider(java.lang.String name)
           
ImageProvider(java.lang.String subdir, java.lang.String name)
           
 
Method Summary
static java.awt.image.BufferedImage createImageFromSvg(SVGDiagram svg, java.awt.Dimension dim)
           
static java.awt.Image createRotatedImage(java.awt.Component c, java.awt.Image img, double rotatedAngle)
          Creates a rotated version of the input image.
 javax.swing.ImageIcon get()
          Execute the image request.
static javax.swing.ImageIcon get(OsmPrimitiveType type)
          Replies the icon for an OSM primitive type
static javax.swing.ImageIcon get(java.lang.String name)
           
static javax.swing.ImageIcon get(java.lang.String subdir, java.lang.String name)
          Load an image with a given file name.
static java.awt.Cursor getCursor(java.lang.String name, java.lang.String overlay)
           
static javax.swing.ImageIcon getIfAvailable(java.lang.String name)
           
static javax.swing.ImageIcon getIfAvailable(java.lang.String subdir, java.lang.String name)
          Load an image with a given file name, but do not throw an exception when the image cannot be found.
private static ImageResource getIfAvailableHttp(java.lang.String url, ImageProvider.ImageType type)
           
private  ImageResource getIfAvailableImpl(java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
           
private static ImageResource getIfAvailableLocalURL(java.net.URL path, ImageProvider.ImageType type)
           
private static ImageResource getIfAvailableWiki(java.lang.String name, ImageProvider.ImageType type)
           
private static ImageResource getIfAvailableZip(java.lang.String full_name, java.io.File archive, ImageProvider.ImageType type)
           
private static java.net.URL getImageUrl(java.lang.String imageName, java.util.Collection<java.lang.String> dirs, java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
           
private static java.net.URL getImageUrl(java.lang.String path, java.lang.String name, java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
           
private static java.lang.String getImgUrlFromWikiInfoPage(java.lang.String base, java.lang.String fn)
          Reads the wiki page on a certain file in html format in order to find the real image URL.
 void getInBackground(ImageProvider.ImageCallback callback)
          Load the image in a background thread.
private static SVGUniverse getSvgUniverse()
           
static javax.swing.ImageIcon overlay(javax.swing.Icon ground, javax.swing.Icon overlay, ImageProvider.OverlayPosition pos)
          Decorate one icon with an overlay icon.
static javax.swing.ImageIcon overlay(javax.swing.Icon ground, java.lang.String overlayImage, ImageProvider.OverlayPosition pos)
          Deprecated. 
 ImageProvider setAdditionalClassLoaders(java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
          Add a collection of additional class loaders to search image for.
 ImageProvider setArchive(java.io.File archive)
          Specify a zip file where the image is located.
 ImageProvider setDirs(java.util.Collection<java.lang.String> dirs)
          Directories to look for the image.
 ImageProvider setHeight(int height)
           
 ImageProvider setId(java.lang.String id)
          Set an id used for caching.
 ImageProvider setMaxHeight(int maxHeight)
           
 ImageProvider setMaxSize(java.awt.Dimension maxSize)
          Limit the maximum size of the image.
 ImageProvider setMaxWidth(int maxWidth)
           
 ImageProvider setOptional(boolean optional)
          Decide, if an exception should be thrown, when the image cannot be located.
 ImageProvider setSize(java.awt.Dimension size)
          Set the dimensions of the image.
 ImageProvider setSuppressWarnings(boolean suppressWarnings)
          Suppresses warning on the command line in case the image cannot be found.
 ImageProvider setWidth(int width)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dirs

protected java.util.Collection<java.lang.String> dirs

id

protected java.lang.String id

subdir

protected java.lang.String subdir

name

protected java.lang.String name

archive

protected java.io.File archive

width

protected int width

height

protected int height

maxWidth

protected int maxWidth

maxHeight

protected int maxHeight

optional

protected boolean optional

suppressWarnings

protected boolean suppressWarnings

additionalClassLoaders

protected java.util.Collection<java.lang.ClassLoader> additionalClassLoaders

svgUniverse

private static SVGUniverse svgUniverse

cache

private static java.util.Map<java.lang.String,ImageResource> cache
The icon cache


imageFetcher

private static final java.util.concurrent.ExecutorService imageFetcher

dataUrlPattern

private static final java.util.regex.Pattern dataUrlPattern
data:[<mediatype>][;base64],<data>

See Also:
RFC2397

DEGREE_90

static final double DEGREE_90
90 degrees in radians units

See Also:
Constant Field Values
Constructor Detail

ImageProvider

public ImageProvider(java.lang.String subdir,
                     java.lang.String name)
Parameters:
subdir - subdirectory the image lies in
name - the name of the image. If it does not end with '.png' or '.svg', both extensions are tried.

ImageProvider

public ImageProvider(java.lang.String name)
Method Detail

setDirs

public ImageProvider setDirs(java.util.Collection<java.lang.String> dirs)
Directories to look for the image.


setId

public ImageProvider setId(java.lang.String id)
Set an id used for caching. If name starts with http:// Id is not used for the cache. (A URL is unique anyway.)


setArchive

public ImageProvider setArchive(java.io.File archive)
Specify a zip file where the image is located. (optional)


setSize

public ImageProvider setSize(java.awt.Dimension size)
Set the dimensions of the image. If not specified, the original size of the image is used. The width part of the dimension can be -1. Then it will only set the height but keep the aspect ratio. (And the other way around.)


setWidth

public ImageProvider setWidth(int width)
See Also:
setSize(java.awt.Dimension)

setHeight

public ImageProvider setHeight(int height)
See Also:
setSize(java.awt.Dimension)

setMaxSize

public ImageProvider setMaxSize(java.awt.Dimension maxSize)
Limit the maximum size of the image. It will shrink the image if necessary, but keep the aspect ratio. The given width or height can be -1 which means this direction is not bounded. 'size' and 'maxSize' are not compatible, you should set only one of them.


setMaxWidth

public ImageProvider setMaxWidth(int maxWidth)
See Also:
setMaxSize(java.awt.Dimension)

setMaxHeight

public ImageProvider setMaxHeight(int maxHeight)
See Also:
setMaxSize(java.awt.Dimension)

setOptional

public ImageProvider setOptional(boolean optional)
Decide, if an exception should be thrown, when the image cannot be located. Set to true, when the image URL comes from user data and the image may be missing.

Parameters:
optional - true, if JOSM should not throw a RuntimeException in case the image cannot be located.
Returns:
the current object, for convenience

setSuppressWarnings

public ImageProvider setSuppressWarnings(boolean suppressWarnings)
Suppresses warning on the command line in case the image cannot be found. In combination with setOptional(true);


setAdditionalClassLoaders

public ImageProvider setAdditionalClassLoaders(java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)
Add a collection of additional class loaders to search image for.


get

public javax.swing.ImageIcon get()
Execute the image request.

Returns:
the requested image or null if the request failed

getInBackground

public void getInBackground(ImageProvider.ImageCallback callback)
Load the image in a background thread. This method returns immediately and runs the image request asynchronously.

Parameters:
callback - a callback. It is called, when the image is ready. This can happen before the call to this method returns or it may be invoked some time (seconds) later. If no image is available, a null value is returned to callback (just like get()).

get

public static javax.swing.ImageIcon get(java.lang.String subdir,
                                        java.lang.String name)
Load an image with a given file name.

Parameters:
subdir - subdirectory the image lies in
name - The icon name (base name with or without '.png' or '.svg' extension)
Returns:
The requested Image.
Throws:
java.lang.RuntimeException - if the image cannot be located

get

public static javax.swing.ImageIcon get(java.lang.String name)
See Also:
get(java.lang.String, java.lang.String)

getIfAvailable

public static javax.swing.ImageIcon getIfAvailable(java.lang.String subdir,
                                                   java.lang.String name)
Load an image with a given file name, but do not throw an exception when the image cannot be found.

See Also:
get(java.lang.String, java.lang.String)

getIfAvailable

public static javax.swing.ImageIcon getIfAvailable(java.lang.String name)
See Also:
getIfAvailable(java.lang.String, java.lang.String)

getIfAvailableImpl

private ImageResource getIfAvailableImpl(java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)

getIfAvailableHttp

private static ImageResource getIfAvailableHttp(java.lang.String url,
                                                ImageProvider.ImageType type)

getIfAvailableWiki

private static ImageResource getIfAvailableWiki(java.lang.String name,
                                                ImageProvider.ImageType type)

getIfAvailableZip

private static ImageResource getIfAvailableZip(java.lang.String full_name,
                                               java.io.File archive,
                                               ImageProvider.ImageType type)

getIfAvailableLocalURL

private static ImageResource getIfAvailableLocalURL(java.net.URL path,
                                                    ImageProvider.ImageType type)

getImageUrl

private static java.net.URL getImageUrl(java.lang.String path,
                                        java.lang.String name,
                                        java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)

getImageUrl

private static java.net.URL getImageUrl(java.lang.String imageName,
                                        java.util.Collection<java.lang.String> dirs,
                                        java.util.Collection<java.lang.ClassLoader> additionalClassLoaders)

getImgUrlFromWikiInfoPage

private static java.lang.String getImgUrlFromWikiInfoPage(java.lang.String base,
                                                          java.lang.String fn)
Reads the wiki page on a certain file in html format in order to find the real image URL.


getCursor

public static java.awt.Cursor getCursor(java.lang.String name,
                                        java.lang.String overlay)

overlay

@Deprecated
public static javax.swing.ImageIcon overlay(javax.swing.Icon ground,
                                                       java.lang.String overlayImage,
                                                       ImageProvider.OverlayPosition pos)
Deprecated. 


overlay

public static javax.swing.ImageIcon overlay(javax.swing.Icon ground,
                                            javax.swing.Icon overlay,
                                            ImageProvider.OverlayPosition pos)
Decorate one icon with an overlay icon.

Parameters:
ground - the base image
overlay - the overlay image (can be smaller than the base image)
pos - position of the overlay image inside the base image (positioned in one of the corners)
Returns:
an icon that represent the overlay of the two given icons. The second icon is layed on the first relative to the given position.

createRotatedImage

public static java.awt.Image createRotatedImage(java.awt.Component c,
                                                java.awt.Image img,
                                                double rotatedAngle)
Creates a rotated version of the input image.

Parameters:
c - The component to get properties useful for painting, e.g. the foreground or background color.
img - the image to be rotated.
rotatedAngle - the rotated angle, in degree, clockwise. It could be any double but we will mod it with 360 before using it.
Returns:
the image after rotating.

get

public static javax.swing.ImageIcon get(OsmPrimitiveType type)
Replies the icon for an OSM primitive type

Parameters:
type - the type
Returns:
the icon

createImageFromSvg

public static java.awt.image.BufferedImage createImageFromSvg(SVGDiagram svg,
                                                              java.awt.Dimension dim)

getSvgUniverse

private static SVGUniverse getSvgUniverse()


JOSM