Gridarta Editor
net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R > Class Template Reference

Caches icon and preview images for map files. More...

+ Inheritance diagram for net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >:
+ Collaboration diagram for net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >:

Public Member Functions

void addMapImageCacheListener (@NotNull final MapImageCacheListener< G, A, R > listener)
 Adds a MapImageCacheListener to be notified. More...
 
void flush (@NotNull final File mapFile)
 Removes the cached images for a map file. More...
 
Image getIcon (@NotNull final File mapFile)
 Returns the icon Image for a given map file. More...
 
Image getOrCreateIcon (@NotNull final File mapFile)
 Returns an icon Image for a given map file. More...
 
Image getOrCreateIcon (@NotNull final MapControl< G, A, R > mapControl)
 Returns an icon Image for a given map. More...
 
Image getOrCreatePreview (@NotNull final File mapFile)
 Returns a preview Image for a given map file. More...
 
Image getOrCreatePreview (@NotNull final MapControl< G, A, R > mapControl)
 Returns a preview Image for a given map. More...
 
Image getPreview (@NotNull final File mapFile)
 Returns a preview Image for a given map file. More...
 
 MapImageCache (@NotNull final MapManager< G, A, R > mapManager, @NotNull final Image defaultIcon, @NotNull final Image defaultPreview, @NotNull final RendererFactory< G, A, R > rendererFactory, @NotNull final CacheFiles cacheFiles)
 Creates a new instance. More...
 
void removeMapImageCacheListener (@NotNull final MapImageCacheListener< G, A, R > listener)
 Removes a MapControlListener to be notified. More...
 

Private Member Functions

Image getOrCreate (@NotNull final MapControl< G, A, R > mapControl, @NotNull final ImageType imageType)
 Returns an icon or preview Image for a given map. More...
 
Image getOrCreate (@NotNull final File mapFile, @NotNull final ImageType imageType)
 Returns a preview Image for a given map file. More...
 
MapRenderer getRenderer (@NotNull final MapControl< G, A, R > mapControl)
 Returns a MapRenderer for a map. More...
 
Image updateCaches (@NotNull final File mapFile, final ImageType imageType)
 Updates the cached icon and preview Images for one map file. More...
 
Image updateCaches (@Nullable final File mapFile, @NotNull final MapControl< G, A, R > mapControl, final ImageType imageType)
 Updates the cached icon and preview Images for one map file. More...
 

Private Attributes

final Map< ImageType, MapImageCacheEntryentries = new EnumMap<>(ImageType.class)
 The cache entries. More...
 
final EventListenerList2< MapImageCacheListener< G, A, R > > listenerList = new EventListenerList2<>(MapImageCacheListener.class)
 The registered event listeners. More...
 
final MapControlListener< G, A, R > mapControlListener
 The MapControlListener attached to all MapControls. More...
 
final MapManager< G, A, R > mapManager
 The MapManager for loading maps. More...
 
final Map< MapControl< G, A, R >, SoftReference< MapRenderer > > mapRendererReferences = new HashMap<>()
 Maps MapControl instance to MapRenderer which is only used to get images. More...
 
final RendererFactory< G, A, R > rendererFactory
 The RendererFactory for creating renderers. More...
 

Static Private Attributes

static final int ICON_HEIGHT = 23
 The height of icons in pixels. More...
 
static final int ICON_WIDTH = 48
 The width of icons in pixels. More...
 
static final int PREVIEW_SCALE = 8
 The scale factor for preview images. More...
 

Detailed Description

Caches icon and preview images for map files.

Cached images are cached both on disk and in memory using weak references. Icons have a fixed size (ICON_WIDTHxICON_HEIGHT pixels); previews are scaled down from the normal size by the factor PREVIEW_SCALE.

Author
Andreas Kirschbaum
Christian Hujer

Definition at line 53 of file MapImageCache.java.

Constructor & Destructor Documentation

◆ MapImageCache()

net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.MapImageCache ( @NotNull final MapManager< G, A, R >  mapManager,
@NotNull final Image  defaultIcon,
@NotNull final Image  defaultPreview,
@NotNull final RendererFactory< G, A, R >  rendererFactory,
@NotNull final CacheFiles  cacheFiles 
)

Creates a new instance.

Parameters
mapManagerthe map manager for loading maps
defaultIconthe default icon to return if no icon can be created for a map
defaultPreviewthe default preview image to return if no preview can be created for a map
rendererFactorythe renderer factory for creating renderers
cacheFilesthe cache files for generating files for cached icons and previews

Purges cached renderers from MapImageCache#mapRendererReferences when a map is closed.

Definition at line 130 of file MapImageCache.java.

Member Function Documentation

◆ addMapImageCacheListener()

void net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.addMapImageCacheListener ( @NotNull final MapImageCacheListener< G, A, R >  listener)

Adds a MapImageCacheListener to be notified.

Parameters
listenerthe listener to add

Definition at line 324 of file MapImageCache.java.

◆ flush()

void net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.flush ( @NotNull final File  mapFile)

Removes the cached images for a map file.

Parameters
mapFilethe map file to purge

Definition at line 172 of file MapImageCache.java.

Referenced by net.sf.gridarta.gui.map.MapPreviewAccessory.genPreview().

+ Here is the caller graph for this function:

◆ getIcon()

Image net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.getIcon ( @NotNull final File  mapFile)

Returns the icon Image for a given map file.

If no image is cached (or if the cached image is outdated), returns

null

.

Parameters
mapFilethe map file
Returns
the icon image or
null

Definition at line 185 of file MapImageCache.java.

Referenced by net.sf.gridarta.gui.map.MapPreviewAccessory.getMapIcon().

+ Here is the caller graph for this function:

◆ getOrCreate() [1/2]

Image net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.getOrCreate ( @NotNull final MapControl< G, A, R >  mapControl,
@NotNull final ImageType  imageType 
)
private

Returns an icon or preview Image for a given map.

If no image is cached (or if the cached image is outdated), (re-)creates the image.

Parameters
mapControlthe map
imageTypethe image type to use
Returns
the preview image

Definition at line 344 of file MapImageCache.java.

Referenced by net.sf.gridarta.gui.mapimagecache.MapImageCache<?, ?, ?>.getOrCreateIcon(), and net.sf.gridarta.gui.mapimagecache.MapImageCache<?, ?, ?>.getOrCreatePreview().

+ Here is the caller graph for this function:

◆ getOrCreate() [2/2]

Image net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.getOrCreate ( @NotNull final File  mapFile,
@NotNull final ImageType  imageType 
)
private

Returns a preview Image for a given map file.

If no image is cached (or if the cached image is outdated), loads the map and creates the image.

Parameters
mapFilethe map file
imageTypethe image type to return
Returns
the preview image or
null
for directories

Definition at line 365 of file MapImageCache.java.

◆ getOrCreateIcon() [1/2]

Image net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.getOrCreateIcon ( @NotNull final File  mapFile)

Returns an icon Image for a given map file.

If no image is cached (or if the cached image is outdated), loads the map and creates the icon.

Parameters
mapFilethe map file
Returns
the icon image or
null
for directories

Definition at line 197 of file MapImageCache.java.

Referenced by net.sf.gridarta.gui.map.MapPreviewAccessory.getMapIcon(), net.sf.gridarta.gui.mapdesktop.MapDesktop< G, A, R >.updateFrameIcon(), and net.sf.gridarta.gui.mapmenu.MapMenuEntryTreeCellRenderer.visit().

+ Here is the caller graph for this function:

◆ getOrCreateIcon() [2/2]

Image net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.getOrCreateIcon ( @NotNull final MapControl< G, A, R >  mapControl)

Returns an icon Image for a given map.

If no image is cached (or if the cached image is outdated), (re-)creates it.

Parameters
mapControlthe map
Returns
the icon image

Definition at line 208 of file MapImageCache.java.

◆ getOrCreatePreview() [1/2]

Image net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.getOrCreatePreview ( @NotNull final File  mapFile)

Returns a preview Image for a given map file.

If no image is cached (or if the cached image is outdated), loads the map and creates the image.

Parameters
mapFilethe map file
Returns
the preview image or
null
for directories

Definition at line 231 of file MapImageCache.java.

Referenced by net.sf.gridarta.gui.map.MapPreviewAccessory.genPreview(), net.sf.gridarta.gui.map.MapPreviewAccessory.getMapPreview(), and net.sf.gridarta.gui.dialog.bookmarks.MapMenuEntryIcons.visit().

+ Here is the caller graph for this function:

◆ getOrCreatePreview() [2/2]

Image net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.getOrCreatePreview ( @NotNull final MapControl< G, A, R >  mapControl)

Returns a preview Image for a given map.

If no image is cached (or if the cached image is outdated), (re-)creates the image.

Parameters
mapControlthe map
Returns
the preview image

Definition at line 242 of file MapImageCache.java.

◆ getPreview()

Image net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.getPreview ( @NotNull final File  mapFile)

Returns a preview Image for a given map file.

If no image is cached (or if the cached image is outdated), returns

null

.

Parameters
mapFilethe map file
Returns
the preview image or
null

Definition at line 219 of file MapImageCache.java.

Referenced by net.sf.gridarta.gui.map.MapPreviewAccessory.getMapPreview().

+ Here is the caller graph for this function:

◆ getRenderer()

MapRenderer net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.getRenderer ( @NotNull final MapControl< G, A, R >  mapControl)
private

Returns a MapRenderer for a map.

If no renderer is cached a new one is created and returned.

Parameters
mapControlthe map to get the renderer for
Returns
the map renderer

Definition at line 307 of file MapImageCache.java.

Referenced by net.sf.gridarta.gui.mapimagecache.MapImageCache<?, ?, ?>.updateCaches().

+ Here is the caller graph for this function:

◆ removeMapImageCacheListener()

void net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.removeMapImageCacheListener ( @NotNull final MapImageCacheListener< G, A, R >  listener)

Removes a MapControlListener to be notified.

Parameters
listenerthe listener to remove

Definition at line 332 of file MapImageCache.java.

◆ updateCaches() [1/2]

Image net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.updateCaches ( @NotNull final File  mapFile,
final ImageType  imageType 
)
private

Updates the cached icon and preview Images for one map file.

Parameters
mapFilethe map file
imageTypethe type of the image to return
Returns
the image or
null
if the image cannot be created

Definition at line 254 of file MapImageCache.java.

Referenced by net.sf.gridarta.gui.mapimagecache.MapImageCache<?, ?, ?>.getOrCreate(), and net.sf.gridarta.gui.mapimagecache.MapImageCache<?, ?, ?>.updateCaches().

+ Here is the caller graph for this function:

◆ updateCaches() [2/2]

Image net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.updateCaches ( @Nullable final File  mapFile,
@NotNull final MapControl< G, A, R >  mapControl,
final ImageType  imageType 
)
private

Updates the cached icon and preview Images for one map file.

Parameters
mapFilethe map file or
null
for unsaved maps
mapControlthe map control instance corresponding to
mapFile
imageTypethe type of the image to return
Returns
the image

Definition at line 279 of file MapImageCache.java.

Member Data Documentation

◆ entries

final Map<ImageType, MapImageCacheEntry> net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.entries = new EnumMap<>(ImageType.class)
private

The cache entries.

Maps ImageType to associated entry. All entries exist.

Definition at line 93 of file MapImageCache.java.

◆ ICON_HEIGHT

final int net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.ICON_HEIGHT = 23
staticprivate

The height of icons in pixels.

Definition at line 63 of file MapImageCache.java.

◆ ICON_WIDTH

final int net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.ICON_WIDTH = 48
staticprivate

The width of icons in pixels.

Definition at line 58 of file MapImageCache.java.

◆ listenerList

final EventListenerList2<MapImageCacheListener<G, A, R> > net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.listenerList = new EventListenerList2<>(MapImageCacheListener.class)
private

The registered event listeners.

Definition at line 86 of file MapImageCache.java.

◆ mapControlListener

final MapControlListener<G, A, R> net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.mapControlListener
private
Initial value:
= new MapControlListener<G, A, R>() {
@Override
public void saved(@NotNull final DefaultMapControl<G, A, R> mapControl) {
final MapFile mapFile = mapControl.getMapModel().getMapFile();
assert mapFile != null;
final File file = mapFile.getFile();
updateCaches(file, mapControl, ImageType.ICON);
updateCaches(file, mapControl, ImageType.PREVIEW);
}
}

The MapControlListener attached to all MapControls.

It updates the cached icon and preview for saved maps.

Definition at line 106 of file MapImageCache.java.

◆ mapManager

final MapManager<G, A, R> net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.mapManager
private

The MapManager for loading maps.

Definition at line 74 of file MapImageCache.java.

Referenced by net.sf.gridarta.gui.mapimagecache.MapImageCache<?, ?, ?>.MapImageCache().

◆ mapRendererReferences

final Map<MapControl<G, A, R>, SoftReference<MapRenderer> > net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.mapRendererReferences = new HashMap<>()
private

Maps MapControl instance to MapRenderer which is only used to get images.

Definition at line 100 of file MapImageCache.java.

◆ PREVIEW_SCALE

final int net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.PREVIEW_SCALE = 8
staticprivate

The scale factor for preview images.

Definition at line 68 of file MapImageCache.java.

◆ rendererFactory

final RendererFactory<G, A, R> net.sf.gridarta.gui.mapimagecache.MapImageCache< G extends GameObject< G, A, R, A extends MapArchObject< A, R extends Archetype< G, A, R >.rendererFactory
private

The RendererFactory for creating renderers.

Definition at line 80 of file MapImageCache.java.

Referenced by net.sf.gridarta.gui.mapimagecache.MapImageCache<?, ?, ?>.MapImageCache().


The documentation for this class was generated from the following file: