diff options
| author | Pierre-Charles David | 2015-09-18 14:15:28 +0000 |
|---|---|---|
| committer | Pierre-Charles David | 2016-02-23 10:29:39 +0000 |
| commit | a7ecf8f13b7ecd7d4b17ef288a89965dc4c35657 (patch) | |
| tree | e61607f45e27ba0b15a72791c36ee750d84822b5 | |
| parent | 026e070ad198948a5d776c3e631d71ae0ac63b07 (diff) | |
| download | org.eclipse.sirius-a7ecf8f13b7ecd7d4b17ef288a89965dc4c35657.tar.gz org.eclipse.sirius-a7ecf8f13b7ecd7d4b17ef288a89965dc4c35657.tar.xz org.eclipse.sirius-a7ecf8f13b7ecd7d4b17ef288a89965dc4c35657.zip | |
[442268] Move all contents of AbstractCachedSVGFigure up into SVGFigure
Make caching enabled by default, but configurable by an internal flag.
Bug: 442268
Change-Id: Ib02001e872db615c8e7c32704be5e3faf64b9813
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
4 files changed, 103 insertions, 138 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 deleted file mode 100644 index 876d09f786..0000000000 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/AbstractCachedSVGFigure.java +++ /dev/null @@ -1,127 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007, 2015 THALES GLOBAL SERVICES. - * 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.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.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. - * - * @author mporhel - */ -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(); - - /** - * Get the image cached or create new one and cache it. - * - * @param clientArea - * the client area - * @param graphics - * the graphical context - * @return an image store in a cache - */ - @Override - protected Image getImage(Rectangle clientArea, Graphics graphics) { - return CACHE.getImage(this, clientArea, graphics); - } - - /** - * 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. - */ - protected static boolean doRemoveFromCache(final String documentKey) { - if (!StringUtil.isEmpty(documentKey)) { - return CACHE.doRemoveFromCache(documentKey) || SVGFigure.documentsMap.remove(documentKey) != null; - } - return false; - } - -} diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/BundledImageFigure.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/BundledImageFigure.java index 20f87baf56..9398c57664 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/BundledImageFigure.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/BundledImageFigure.java @@ -32,7 +32,7 @@ import org.w3c.dom.Element; * * @author cbrun */ -public class BundledImageFigure extends AbstractCachedSVGFigure { +public class BundledImageFigure extends SVGFigure { /** * The stroke tag in the SVG file. @@ -476,11 +476,11 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { protected String getDocumentKey() { StringBuffer result = new StringBuffer(); result.append(super.getDocumentKey()); - result.append(AbstractCachedSVGFigure.SEPARATOR); + result.append(SVGFigure.SEPARATOR); result.append(shapeName); - result.append(AbstractCachedSVGFigure.SEPARATOR); + result.append(SVGFigure.SEPARATOR); result.append(mainBorderColor); - result.append(AbstractCachedSVGFigure.SEPARATOR); + result.append(SVGFigure.SEPARATOR); result.append(mainGradientColor); result.append(SEPARATOR); result.append(mainBorderSize); diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java index 4fd218695f..01f7239489 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java @@ -15,6 +15,7 @@ package org.eclipse.sirius.diagram.ui.tools.api.figure; import java.awt.image.BufferedImage; import java.io.IOException; import java.text.MessageFormat; +import java.util.Collection; import java.util.WeakHashMap; import org.apache.batik.dom.svg.SAXSVGDocumentFactory; @@ -23,6 +24,7 @@ import org.eclipse.draw2d.Figure; import org.eclipse.draw2d.Graphics; import org.eclipse.draw2d.XYLayout; import org.eclipse.draw2d.geometry.Rectangle; +import org.eclipse.sirius.common.tools.api.util.StringUtil; import org.eclipse.sirius.diagram.DiagramPlugin; import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin; import org.eclipse.sirius.diagram.ui.provider.Messages; @@ -34,9 +36,78 @@ 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.collect.Lists; + //CHECKSTYLE:OFF public class SVGFigure extends Figure implements StyledFigure, ITransparentFigure, ImageFigureWithAlpha { /** + * 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; + } + } + + private static final ImageCache CACHE = new ImageCache(); + + private static final boolean CACHE_ENABLED = true; + + /** * The uri of the image to display when the file has not been found. */ protected static final String IMAGE_NOT_FOUND_URI = MessageFormat.format("platform:/plugin/{0}/images/NotFound.svg", DiagramUIPlugin.getPlugin().getSymbolicName()); //$NON-NLS-1$ @@ -216,14 +287,14 @@ public class SVGFigure extends Figure implements StyledFigure, ITransparentFigur StringBuffer result = new StringBuffer(); result.append(getDocumentKey()); - result.append(AbstractCachedSVGFigure.SEPARATOR); + result.append(SVGFigure.SEPARATOR); result.append(getSiriusAlpha()); - result.append(AbstractCachedSVGFigure.SEPARATOR); + result.append(SVGFigure.SEPARATOR); result.append(aaText); - result.append(AbstractCachedSVGFigure.SEPARATOR); + result.append(SVGFigure.SEPARATOR); Rectangle r = getClientArea(); result.append(r.width); - result.append(AbstractCachedSVGFigure.SEPARATOR); + result.append(SVGFigure.SEPARATOR); result.append(r.height); return result.toString(); @@ -251,7 +322,11 @@ public class SVGFigure extends Figure implements StyledFigure, ITransparentFigur * @return an image store in a cache */ protected Image getImage(Rectangle clientArea, Graphics graphics) { - return render(this, clientArea, graphics); + if (CACHE_ENABLED) { + return CACHE.getImage(this, clientArea, graphics); + } else { + return render(this, clientArea, graphics); + } } protected static Image render(SVGFigure fig, Rectangle clientArea, Graphics graphics) { @@ -266,5 +341,22 @@ public class SVGFigure extends Figure implements StyledFigure, ITransparentFigur } 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. + */ + protected static boolean doRemoveFromCache(final String documentKey) { + if (!StringUtil.isEmpty(documentKey)) { + return CACHE.doRemoveFromCache(documentKey) || SVGFigure.documentsMap.remove(documentKey) != null; + } + return false; + } + // CHECKSTYLE:ON } diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGWorkspaceImageFigure.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGWorkspaceImageFigure.java index bb4095afa0..4f976ffa78 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGWorkspaceImageFigure.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGWorkspaceImageFigure.java @@ -33,7 +33,7 @@ import org.eclipse.swt.graphics.Image; * @author mporhel * */ -public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements IWorkspaceImageFigure { +public class SVGWorkspaceImageFigure extends SVGFigure implements IWorkspaceImageFigure { private double imageAspectRatio = 1.0; @@ -202,7 +202,7 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements public static Option<String> removeFromCache(String workspacePath) { Option<String> imageUri = SVGWorkspaceImageFigure.getImageUri(workspacePath, true); if (imageUri.some()) { - if (AbstractCachedSVGFigure.doRemoveFromCache(imageUri.get())) { + if (SVGFigure.doRemoveFromCache(imageUri.get())) { return imageUri; } } |
