diff options
| author | Pierre-Charles David | 2015-09-18 13:56:27 +0000 |
|---|---|---|
| committer | Pierre-Charles David | 2016-02-23 10:29:39 +0000 |
| commit | 026e070ad198948a5d776c3e631d71ae0ac63b07 (patch) | |
| tree | c8632e847707be8a83dfe6f50ce4243cba57a653 | |
| parent | c530ac267dcf8e7779fbc06e4c710fa397d488b7 (diff) | |
| download | org.eclipse.sirius-026e070ad198948a5d776c3e631d71ae0ac63b07.tar.gz org.eclipse.sirius-026e070ad198948a5d776c3e631d71ae0ac63b07.tar.xz org.eclipse.sirius-026e070ad198948a5d776c3e631d71ae0ac63b07.zip | |
[442268] Make ImageCache an internal class of AbstractCachedSVGFigure
Also move getImage() into the new internal class and remove the
indirection code this makes obsolete.
Bug: 442268
Change-Id: Ic7a3b6dd76e4b596e9eff072c25a3576253afaf6
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
2 files changed, 68 insertions, 129 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 2dbffa6397..876d09f786 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 @@ -10,12 +10,17 @@ *******************************************************************************/ package org.eclipse.sirius.diagram.ui.tools.api.figure; +import java.util.Collection; + import org.eclipse.draw2d.Graphics; import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.sirius.common.tools.api.util.StringUtil; -import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.ImageCache; import org.eclipse.swt.graphics.Image; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.collect.Lists; + /** * A {@link AbstractCachedSVGFigure} is a {@link SVGFigure} corresponding to a * svg image. {@link Image} are store in a map with soft values. @@ -24,6 +29,67 @@ import org.eclipse.swt.graphics.Image; */ public abstract class AbstractCachedSVGFigure extends SVGFigure { /** + * Cache of pre-rendered images. + */ + private static class ImageCache { + /** + * The rendered bitmaps, organized by key.. + */ + private final Cache<String, Image> images = CacheBuilder.newBuilder().softValues().build(); + + /** + * Get the image cached or create new one and cache it. + * + * @param key + * the key + * @param clientArea + * the client area + * @param graphics + * the graphical context + * @return an image store in a cache + */ + public Image getImage(SVGFigure fig, Rectangle clientArea, Graphics graphics) { + String key = fig.getKey(graphics); + Image result = images.getIfPresent(key); + if (result == null) { + result = render(fig, clientArea, graphics); + if (result != null) { + images.put(key, result); + } + } + return result; + } + + /** + * Remove all entries whose key begins with the given key. Remove from + * the document map, the entries with the given keys to force to re-read + * the file. + * + * @param documentKey + * the document key. + * @return true of something was removed. + */ + public boolean doRemoveFromCache(String documentKey) { + if (!StringUtil.isEmpty(documentKey)) { + boolean remove = false; + Collection<String> keyToRemove = Lists.newArrayList(); + for (String key : images.asMap().keySet()) { + if (key.startsWith(documentKey)) { + keyToRemove.add(key); + } + } + + for (String toRemove : keyToRemove) { + images.invalidate(toRemove); + remove = true; + } + return remove; + } + return false; + } + } + + /** * Cache to store bitmaps of rendered SVGs. */ private static final ImageCache CACHE = new ImageCache(); @@ -39,30 +105,7 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure { */ @Override protected Image getImage(Rectangle clientArea, Graphics graphics) { - return getImage(this, clientArea, graphics); - } - - /** - * Get the image cached or create new one and cache it. - * - * @param key - * the key - * @param clientArea - * the client area - * @param graphics - * the graphical context - * @return an image store in a cache - */ - private static Image getImage(SVGFigure fig, Rectangle clientArea, Graphics graphics) { - String key = fig.getKey(graphics); - Image result = AbstractCachedSVGFigure.CACHE.getIfPresent(key); - if (result == null) { - result = render(fig, clientArea, graphics); - if (result != null) { - AbstractCachedSVGFigure.CACHE.put(key, result); - } - } - return result; + return CACHE.getImage(this, clientArea, graphics); } /** 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 deleted file mode 100644 index 9496c412c1..0000000000 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/ImageCache.java +++ /dev/null @@ -1,104 +0,0 @@ -/******************************************************************************* - * 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.Collection; -import java.util.Set; - -import org.eclipse.sirius.common.tools.api.util.StringUtil; -import org.eclipse.swt.graphics.Image; - -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.collect.Lists; - -/** - * 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); - } - - /** - * Remove all entries whose key begins with the given key. Remove from the - * document map, the entries with the given keys to force to re-read the - * file. - * - * @param documentKey - * the document key. - * @return true of something was removed. - */ - public boolean doRemoveFromCache(final String documentKey) { - if (!StringUtil.isEmpty(documentKey)) { - boolean remove = false; - Collection<String> keyToRemove = Lists.newArrayList(); - for (String key : keySet()) { - if (key.startsWith(documentKey)) { - keyToRemove.add(key); - } - } - - for (String toRemove : keyToRemove) { - invalidate(toRemove); - remove = true; - } - return remove; - } - return false; - } -} |
