diff options
author | Pierre-Charles David | 2015-09-17 15:17:38 +0000 |
---|---|---|
committer | Pierre-Charles David | 2016-02-19 16:12:03 +0000 |
commit | c008499e5956922de4d7091e890222b62eb4f979 (patch) | |
tree | de2bf5a2e693695d0a57901a4dda028069c95fcb | |
parent | 36ccc7b9f424913c5197af9148d24b9acbf80c30 (diff) | |
download | org.eclipse.sirius-c008499e5956922de4d7091e890222b62eb4f979.tar.gz org.eclipse.sirius-c008499e5956922de4d7091e890222b62eb4f979.tar.xz org.eclipse.sirius-c008499e5956922de4d7091e890222b62eb4f979.zip |
[442268] Move rendering and caching code in more logical places
- Move image cache removal logic directly into ImageCache helper class.
- Extract image rendering code from caching logic and move rendering
logic directly into SVGFigure.
- Move low-level rendering hint discovery into SVGUtils.
- Rename getCachedImage into getImage.
- Remove SVGFigure.paintFigure(): at runtime, only the version
overridden in AbstractSVGFigure is actually called, and it does not
invoke its super-method.
Bug: 442268
Change-Id: Id06ba35e522b027c7fc0acdc1f0538924be9b653
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
5 files changed, 101 insertions, 101 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 d9159dbe05..0d7d4b643c 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,21 +10,13 @@ *******************************************************************************/ package org.eclipse.sirius.diagram.ui.tools.api.figure; -import java.awt.image.BufferedImage; -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.TransparentFigureGraphicsModifier; import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.ImageCache; -import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.SVGUtils; 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.collect.Lists; /** * A {@link AbstractCachedSVGFigure} is a {@link SVGFigure} corresponding to a @@ -44,7 +36,7 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure { modifier.pushState(); Rectangle r = getClientArea(); - Image image = getCachedImage(getKey() + getContextKey(graphics), r, graphics); + Image image = getImage(getKey() + getContextKey(graphics), r, graphics); // Draw the image if (image != null) { graphics.drawImage(image, r.x, r.y); @@ -52,6 +44,16 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure { modifier.popState(); } + /** + * Compute a key for this figure. This key is used to store in cache the + * corresponding {@link org.eclipse.swt.graphics.Image}. + * + * The key must begin by the document key. + * + * @return The key corresponding to this BundleImageFigure. + */ + protected abstract String getKey(); + private String getContextKey(Graphics graphics) { // CHECKSTYLE:OFF int aaText = SWT.DEFAULT; @@ -84,37 +86,18 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure { * the graphical context * @return an image store in a cache */ - protected Image getCachedImage(final String key, Rectangle clientArea, Graphics graphics) { + protected Image getImage(String key, Rectangle clientArea, Graphics graphics) { Image result = AbstractCachedSVGFigure.CACHE.getIfPresent(key); if (result == null) { - /* Create the image if it does not exist */ - Document document = getDocument(); - if (document == null) { - return null; - } - getTranscoder().setCanvasSize(clientArea.width, clientArea.height); - updateRenderingHints(graphics); - BufferedImage awtImage = getTranscoder().getBufferedImage(); - if (awtImage != null) { - result = SVGUtils.toSWT(Display.getCurrent(), awtImage); + result = render(clientArea, graphics); + if (result != null) { AbstractCachedSVGFigure.CACHE.put(key, result); } } - // Get the image from the cache return result; } /** - * Compute a key for this figure. This key is used to store in cache the - * corresponding {@link org.eclipse.swt.graphics.Image}. - * - * The key must begin by the document key. - * - * @return The key corresponding to this BundleImageFigure. - */ - protected abstract String getKey(); - - /** * 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. @@ -125,20 +108,7 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure { */ protected static boolean doRemoveFromCache(final String documentKey) { if (!StringUtil.isEmpty(documentKey)) { - boolean remove = false; - Collection<String> keyToRemove = Lists.newArrayList(); - for (String key : AbstractCachedSVGFigure.CACHE.keySet()) { - if (key.startsWith(documentKey)) { - keyToRemove.add(key); - } - } - - for (String toRemove : keyToRemove) { - AbstractCachedSVGFigure.CACHE.invalidate(toRemove); - remove = true; - } - boolean removedFromDocumentsMap = SVGFigure.documentsMap.remove(documentKey) != null; - return remove || removedFromDocumentsMap; + 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/SVGFigure.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGFigure.java index 226437fa0b..d431fad787 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 @@ -29,7 +29,6 @@ import org.eclipse.sirius.diagram.ui.provider.DiagramUIPlugin; import org.eclipse.sirius.diagram.ui.provider.Messages; import org.eclipse.sirius.diagram.ui.tools.internal.figure.svg.SVGUtils; 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; @@ -57,7 +56,7 @@ public class SVGFigure extends Figure implements StyledFigure, ITransparentFigur private SimpleImageTranscoder transcoder; protected static WeakHashMap<String, Document> documentsMap = new WeakHashMap<String, Document>(); - + public SVGFigure() { this.setLayoutManager(new XYLayout()); } @@ -212,68 +211,29 @@ public class SVGFigure extends Figure implements StyledFigure, ITransparentFigur return uri; } - @Override - protected void paintFigure(Graphics graphics) { - super.paintFigure(graphics); - Document document = getDocument(); - if (document == null) { - return; - } - Image image = null; - try { - Rectangle r = getClientArea(); - transcoder.setCanvasSize(r.width, r.height); + protected Image render(Rectangle clientArea, Graphics graphics) { + Image result = null; + if (getDocument() != null) { + getTranscoder().setCanvasSize(clientArea.width, clientArea.height); updateRenderingHints(graphics); - BufferedImage awtImage = transcoder.getBufferedImage(); + BufferedImage awtImage = getTranscoder().getBufferedImage(); if (awtImage != null) { - image = SVGUtils.toSWT(Display.getCurrent(), awtImage); - graphics.drawImage(image, r.x, r.y); - } - } finally { - if (image != null) { - image.dispose(); + result = SVGUtils.toSWT(Display.getCurrent(), awtImage); } } + return result; } protected void updateRenderingHints(Graphics graphics) { - { - int aa = SWT.DEFAULT; - try { - aa = graphics.getAntialias(); - } catch (Exception e) { - // not supported - } - Object aaHint; - if (aa == SWT.ON) { - aaHint = RenderingHints.VALUE_ANTIALIAS_ON; - } else if (aa == SWT.OFF) { - aaHint = RenderingHints.VALUE_ANTIALIAS_OFF; - } else { - aaHint = RenderingHints.VALUE_ANTIALIAS_DEFAULT; - } - if (transcoder != null && transcoder.getRenderingHints().get(RenderingHints.KEY_ANTIALIASING) != aaHint) { - transcoder.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING, aaHint); + if (transcoder != null) { + Object antiAliasHint = SVGUtils.getAntialiasHint(graphics); + if (transcoder.getRenderingHints().get(RenderingHints.KEY_ANTIALIASING) != antiAliasHint) { + transcoder.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING, antiAliasHint); transcoder.contentChanged(); } - } - { - int aa = SWT.DEFAULT; - try { - aa = graphics.getTextAntialias(); - } catch (Exception e) { - // not supported - } - Object aaHint; - if (aa == SWT.ON) { - aaHint = RenderingHints.VALUE_TEXT_ANTIALIAS_ON; - } else if (aa == SWT.OFF) { - aaHint = RenderingHints.VALUE_TEXT_ANTIALIAS_OFF; - } else { - aaHint = RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT; - } - if (transcoder != null && transcoder.getRenderingHints().get(RenderingHints.KEY_TEXT_ANTIALIASING) != aaHint) { - transcoder.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, aaHint); + Object textAntiAliasHint = SVGUtils.getTextAntialiasHint(graphics); + if (transcoder.getRenderingHints().get(RenderingHints.KEY_TEXT_ANTIALIASING) != textAntiAliasHint) { + transcoder.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, textAntiAliasHint); transcoder.contentChanged(); } } 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 87df9e60f5..9036623654 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 @@ -235,7 +235,7 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements SVGWorkspaceImageFigure fig = new SVGWorkspaceImageFigure(); fig.updateImageURI(path); fig.contentChanged(); - return fig.getCachedImage(fig.getKey(), new Rectangle(0, 0, -1, -1), null); + return fig.getImage(fig.getKey(), new Rectangle(0, 0, -1, -1), 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 index 00506cc45c..9496c412c1 100644 --- 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 @@ -10,12 +10,15 @@ *******************************************************************************/ 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. @@ -70,4 +73,32 @@ public class ImageCache { 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; + } } diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SVGUtils.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SVGUtils.java index dcb935b154..cd601ba26a 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SVGUtils.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/internal/figure/svg/SVGUtils.java @@ -12,9 +12,12 @@ */ package org.eclipse.sirius.diagram.ui.tools.internal.figure.svg; +import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.awt.image.WritableRaster; +import org.eclipse.draw2d.Graphics; +import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Device; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.PaletteData; @@ -59,5 +62,41 @@ public class SVGUtils { } return new org.eclipse.swt.graphics.Image(device, swtImageData); } + + public static Object getAntialiasHint(Graphics graphics) { + int aa = SWT.DEFAULT; + try { + aa = graphics.getAntialias(); + } catch (Exception e) { + // not supported + } + Object aaHint; + if (aa == SWT.ON) { + aaHint = RenderingHints.VALUE_ANTIALIAS_ON; + } else if (aa == SWT.OFF) { + aaHint = RenderingHints.VALUE_ANTIALIAS_OFF; + } else { + aaHint = RenderingHints.VALUE_ANTIALIAS_DEFAULT; + } + return aaHint; + } + + public static Object getTextAntialiasHint(Graphics graphics) { + int aa = SWT.DEFAULT; + try { + aa = graphics.getTextAntialias(); + } catch (Exception e) { + // not supported + } + Object aaHint; + if (aa == SWT.ON) { + aaHint = RenderingHints.VALUE_TEXT_ANTIALIAS_ON; + } else if (aa == SWT.OFF) { + aaHint = RenderingHints.VALUE_TEXT_ANTIALIAS_OFF; + } else { + aaHint = RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT; + } + return aaHint; + } } // CHECKSTYLE:ON |