diff options
| author | Pierre-Charles David | 2015-09-17 14:13:15 +0000 |
|---|---|---|
| committer | Pierre-Charles David | 2016-02-17 08:12:36 +0000 |
| commit | 208418a8db5681c36c52800da01163464e589409 (patch) | |
| tree | bac5ac76d5c69b72d100f5ebd2e432e1dd45f969 | |
| parent | 908235466db833029999ed5158b2a3d256aed75b (diff) | |
| download | org.eclipse.sirius-208418a8db5681c36c52800da01163464e589409.tar.gz org.eclipse.sirius-208418a8db5681c36c52800da01163464e589409.tar.xz org.eclipse.sirius-208418a8db5681c36c52800da01163464e589409.zip | |
[442268] Extract the actual cache into a separate class
Bug: 442268
Change-Id: I2c52597e096ca17b9761dea1d28a05558d64803a
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
2 files changed, 80 insertions, 19 deletions
diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/AbstractCachedSVGFigure.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/AbstractCachedSVGFigure.java index d800cb0ca8..03cb3478ad 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/AbstractCachedSVGFigure.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/AbstractCachedSVGFigure.java @@ -20,16 +20,13 @@ import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.sirius.common.tools.api.util.StringUtil; import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin; import org.eclipse.sirius.diagram.ui.tools.internal.figure.TransparentFigureGraphicsModifier; +import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.ImageCache; import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.SimpleImageTranscoder; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; import org.w3c.dom.Document; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; import com.google.common.collect.Lists; /** @@ -49,15 +46,7 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style /** * Cache to store bitmaps of rendered SVGs. */ - private static final Cache<String, Image> SVG_IMG_CACHE = CacheBuilder.newBuilder().softValues().removalListener(new RemovalListener<String, Image>() { - @Override - public void onRemoval(RemovalNotification<String, Image> notification) { - Image img = notification.getValue(); - if (img != null) { - img.dispose(); - } - } - }).build(); + private static final ImageCache CACHE = new ImageCache(); private int viewpointAlpha = DEFAULT_ALPHA; @@ -140,8 +129,7 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style * @return an image store in a cache */ protected Image getCachedImage(final String key, Rectangle clientArea, Graphics graphics) { - - Image result = AbstractCachedSVGFigure.SVG_IMG_CACHE.getIfPresent(key); + Image result = AbstractCachedSVGFigure.CACHE.getIfPresent(key); if (result == null) { /* Create the image if it does not exist */ Document document = getDocument(); @@ -154,7 +142,7 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style BufferedImage awtImage = getTranscoder().getBufferedImage(); if (awtImage != null) { result = toSWT(Display.getCurrent(), awtImage); - AbstractCachedSVGFigure.SVG_IMG_CACHE.put(key, result); + AbstractCachedSVGFigure.CACHE.put(key, result); } } // Get the image from the cache @@ -177,7 +165,7 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style * @return The uri of the image to display when the file has not been found. */ protected static String getImageNotFoundURI() { - return IMAGE_NOT_FOUND_URI; + return AbstractCachedSVGFigure.IMAGE_NOT_FOUND_URI; } /** @@ -193,14 +181,14 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style if (!StringUtil.isEmpty(documentKey)) { boolean remove = false; Collection<String> keyToRemove = Lists.newArrayList(); - for (String key : AbstractCachedSVGFigure.SVG_IMG_CACHE.asMap().keySet()) { + for (String key : AbstractCachedSVGFigure.CACHE.keySet()) { if (key.startsWith(documentKey)) { keyToRemove.add(key); } } for (String toRemove : keyToRemove) { - AbstractCachedSVGFigure.SVG_IMG_CACHE.invalidate(toRemove); + AbstractCachedSVGFigure.CACHE.invalidate(toRemove); remove = true; } boolean removedFromDocumentsMap = SVGFigure.documentsMap.remove(documentKey) != null; diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/ImageCache.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/ImageCache.java new file mode 100644 index 0000000000..00506cc45c --- /dev/null +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/ImageCache.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2016 Obeo. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.diagram.ui.tools.internal.figure.svg; + +import java.util.Set; + +import org.eclipse.swt.graphics.Image; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; + +/** + * Cache of pre-rendered images. + * + * @author pcdavid + */ +public class ImageCache { + /** + * The rendered bitmaps, organized by key.. + */ + private final Cache<String, Image> images = CacheBuilder.newBuilder().softValues().build(); + + /** + * Returns the bitmap associated to the specified key, or <code>null</code> + * if there is none. + * + * @param key + * the image key. + * @return the bitmap associated to the specified key. + */ + public Image getIfPresent(String key) { + return images.getIfPresent(key); + } + + /** + * Returns the set of keys for which we have cached bitmaps. + * + * @return the set of keys for which we have cached bitmaps. + */ + public Set<String> keySet() { + return images.asMap().keySet(); + } + + /** + * Associate a rendered bitmap to a key. + * + * @param key + * the key. + * @param img + * the rendered bitmap. + */ + public void put(String key, Image img) { + images.put(key, img); + } + + /** + * Remove the bitmap associated to the specified key from the cache, if any. + * + * @param key + * the key. + */ + public void invalidate(String key) { + images.invalidate(key); + } +} |
