diff options
| author | Pierre-Charles David | 2015-09-03 14:38:04 +0000 |
|---|---|---|
| committer | Pierre-Charles David | 2016-02-10 09:11:23 +0000 |
| commit | 3406ee32a1f643604bdcf998aee00a16f2690fda (patch) | |
| tree | b28befd4bb2641aad972814d7c625d25dbacf7a5 | |
| parent | 1a5b1d6864a513df73ff680db3e53122c780b466 (diff) | |
| download | org.eclipse.sirius-3406ee32a1f643604bdcf998aee00a16f2690fda.tar.gz org.eclipse.sirius-3406ee32a1f643604bdcf998aee00a16f2690fda.tar.xz org.eclipse.sirius-3406ee32a1f643604bdcf998aee00a16f2690fda.zip | |
[442268] Replace explicit string concatenation with template formatting
* Simplify AbstractCachedSVGFigure.getImageNotFoundURI() and
BundledImageFigure.getImageFileURI() using a MessageFormat-based
template.
* Simplify BundledImageFigure.get*ToHexa() using String.printf().
* Also perform some source cleanup (applying our cleanup profile).
Bug: 442268
Change-Id: I258c011b64a32a3866daf6e977ee138c0b9a709b
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
3 files changed, 69 insertions, 147 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 5d1108712e..d800cb0ca8 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 @@ -11,6 +11,7 @@ package org.eclipse.sirius.diagram.ui.tools.api.figure; import java.awt.image.BufferedImage; +import java.text.MessageFormat; import java.util.Collection; import org.eclipse.draw2d.Graphics; @@ -34,7 +35,7 @@ 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 implements StyledFigure, ITransparentFigure, ImageFigureWithAlpha { @@ -43,6 +44,8 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style */ protected static final String SEPARATOR = "|"; //$NON-NLS-1$ + private static final String IMAGE_NOT_FOUND_URI = MessageFormat.format("platform:/plugin/{0}/images/NotFound.svg", DiagramUIPlugin.getPlugin().getSymbolicName()); //$NON-NLS-1$ + /** * Cache to store bitmaps of rendered SVGs. */ @@ -56,11 +59,6 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style } }).build(); - private static final String IMAGE_DIR = "images/"; //$NON-NLS-1$ - - private static final String IMAGE_EXT = ".svg"; //$NON-NLS-1$ - - private static final String IMAGE_NOT_FOUND = "NotFound"; //$NON-NLS-1$ private int viewpointAlpha = DEFAULT_ALPHA; @@ -68,46 +66,33 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style /** * Build a new {@link AbstractCachedSVGFigure} from an Image instance. - * + * */ public AbstractCachedSVGFigure() { this.setLayoutManager(new XYLayout()); } - /** - * {@inheritDoc} - */ + @Override public int getSiriusAlpha() { return viewpointAlpha; } - /** - * {@inheritDoc} - */ + @Override public boolean isTransparent() { return transparent; } - /** - * {@inheritDoc} - */ + @Override public void setSiriusAlpha(int alpha) { this.viewpointAlpha = alpha; } - /** - * {@inheritDoc} - */ + @Override public void setTransparent(boolean transparent) { this.transparent = transparent; } - /** - * {@inheritDoc} - * - * @see org.eclipse.sirius.diagram.ui.tools.api.figure.lite.svg.SVGFigure#paintFigure(org.eclipse.draw2d.Graphics) - */ @Override protected void paintFigure(Graphics graphics) { TransparentFigureGraphicsModifier modifier = new TransparentFigureGraphicsModifier(this, graphics); @@ -134,10 +119,10 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style StringBuilder result = new StringBuilder(); result.append(aaText); - result.append(SEPARATOR); + result.append(AbstractCachedSVGFigure.SEPARATOR); Rectangle r = getClientArea(); result.append(getSpecifyCanvasWidth() ? r.width : -1); - result.append(SEPARATOR); + result.append(AbstractCachedSVGFigure.SEPARATOR); result.append(getSpecifyCanvasHeight() ? r.height : -1); return result.toString(); @@ -145,7 +130,7 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style /** * Get the image cached or create new one and cache it. - * + * * @param key * the key * @param clientArea @@ -158,7 +143,6 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style Image result = AbstractCachedSVGFigure.SVG_IMG_CACHE.getIfPresent(key); if (result == null) { - /* Create the image if it does not exist */ Document document = getDocument(); if (document == null) { @@ -180,29 +164,27 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style /** * 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(); /** * The uri of the image to display when the file has not been found. - * + * * @return The uri of the image to display when the file has not been found. */ protected static String getImageNotFoundURI() { - final String path = new StringBuffer(IMAGE_DIR).append(IMAGE_NOT_FOUND).append(IMAGE_EXT).toString(); - String pluginId = DiagramUIPlugin.getPlugin().getSymbolicName(); - return "platform:/plugin/" + pluginId + "/" + path; //$NON-NLS-1$ //$NON-NLS-2$ + return IMAGE_NOT_FOUND_URI; } /** * 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. @@ -221,7 +203,7 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style AbstractCachedSVGFigure.SVG_IMG_CACHE.invalidate(toRemove); remove = true; } - boolean removedFromDocumentsMap = documentsMap.remove(documentKey) != null; + boolean removedFromDocumentsMap = SVGFigure.documentsMap.remove(documentKey) != null; return remove || removedFromDocumentsMap; } return false; @@ -267,7 +249,6 @@ public abstract class AbstractCachedSVGFigure extends SVGFigure implements Style return result[0]; } } - return 255; } } 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 1f53ff1510..41a6d412c1 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 @@ -10,6 +10,8 @@ *******************************************************************************/ package org.eclipse.sirius.diagram.ui.tools.api.figure; +import java.text.MessageFormat; + import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.draw2d.Border; import org.eclipse.draw2d.IFigure; @@ -28,7 +30,7 @@ import org.w3c.dom.Element; /** * A {@link BundledImageFigure} is a Figure corresponding to an Image defined in * a plugin. - * + * * @author cbrun */ public class BundledImageFigure extends AbstractCachedSVGFigure { @@ -113,14 +115,10 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { */ private static final String COLOR_IDENTIFIER = "colorIdentifier"; //$NON-NLS-1$ - private static final String IMAGE_DIR = "images/"; //$NON-NLS-1$ - - private static final String IMAGE_EXT = ".svg"; //$NON-NLS-1$ - private BundledImageExtensionQuery bundledImageExtensionQuery; /** - * * The actual shapeName use to draw the SVG figure + * The actual shapeName use to draw the SVG figure */ private String shapeName; @@ -151,7 +149,7 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { /** * Build a new {@link BundledImageFigure} from an Image instance. - * + * */ public BundledImageFigure() { this.setLayoutManager(new XYLayout()); @@ -160,7 +158,7 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { /** * Create the {@link BundledImageFigure} from a {@link BundledImage} * instance. - * + * * @param bundle * {@link BundledImage} specification. * @return new Figure. @@ -180,9 +178,6 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { } } - /** - * @param bundle - */ private boolean updateShape(BundledImage bundledImage) { boolean updated = false; if (bundledImage != null && bundledImage.getShape() != null) { @@ -211,7 +206,7 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { * @param bundledImage * @param force * If the color must be force to refresh (in case of shape update - * for sample) + * for example) */ private boolean updateColors(BundledImage bundledImage, boolean force) { boolean updated = updateColorFields(bundledImage); @@ -240,9 +235,9 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { Color newBorderLighterColor = ColorManager.getDefault().getLighterColor(borderColor); // Get Hexa values - String hexaColor = getRGBValuesColorToHexa(color); + String hexaColor = BundledImageFigure.getRGBValuesColorToHexa(color); String hexaLighterColor = getColorToHexa(newLighterColor); - String hexaBorderColor = getRGBValuesColorToHexa(borderColor); + String hexaBorderColor = BundledImageFigure.getRGBValuesColorToHexa(borderColor); String hexaLighterBorderColor = getColorToHexa(newBorderLighterColor); boolean updated = false; @@ -293,7 +288,8 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { Element gradientStep1 = findElementInDocument(bundledImage, document, BundledImageFigure.COLOR_IDENTIFIER, BundledImageFigure.SVG_STOP_LIGHTER_ID); if (gradientStep1 != null) { String gradientStep1Style = getAttributeValue(gradientStep1, bundledImage, BundledImageFigure.COLOR_ATTRIBUTE, BundledImageFigure.SVG_STYLE_ATTRIBUTE_NAME); - gradientStep1.setAttribute(BundledImageFigure.SVG_STYLE_ATTRIBUTE_NAME, getNewStyle(gradientStep1Style, BundledImageFigure.SVG_STOP_COLOR, getLighterGradientColor())); + gradientStep1.setAttribute(BundledImageFigure.SVG_STYLE_ATTRIBUTE_NAME, + BundledImageFigure.getNewStyle(gradientStep1Style, BundledImageFigure.SVG_STOP_COLOR, getLighterGradientColor())); updated = true; } @@ -301,15 +297,16 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { Element gradientStep2 = document.getElementById(BundledImageFigure.SVG_STOP_MAIN_ID); if (gradientStep2 != null) { String gradientStep2Style = gradientStep2.getAttribute(BundledImageFigure.SVG_STYLE_ATTRIBUTE_NAME); - gradientStep2.setAttribute(BundledImageFigure.SVG_STYLE_ATTRIBUTE_NAME, getNewStyle(gradientStep2Style, BundledImageFigure.SVG_STOP_COLOR, getMainGradientColor())); + gradientStep2.setAttribute(BundledImageFigure.SVG_STYLE_ATTRIBUTE_NAME, + BundledImageFigure.getNewStyle(gradientStep2Style, BundledImageFigure.SVG_STOP_COLOR, getMainGradientColor())); updated = true; } /* Update the shadow border (if exists). */ - Element shadow = document.getElementById(SVG_SHADOW_ELEMENT_ID); + Element shadow = document.getElementById(BundledImageFigure.SVG_SHADOW_ELEMENT_ID); if (shadow != null) { String shadowStyle = shadow.getAttribute(BundledImageFigure.SVG_STYLE_ATTRIBUTE_NAME); - shadow.setAttribute(BundledImageFigure.SVG_STYLE_ATTRIBUTE_NAME, getNewStyle(shadowStyle, SVG_FILL, getLighterBorderColor())); + shadow.setAttribute(BundledImageFigure.SVG_STYLE_ATTRIBUTE_NAME, BundledImageFigure.getNewStyle(shadowStyle, BundledImageFigure.SVG_FILL, getLighterBorderColor())); updated = true; } @@ -373,14 +370,8 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { return this.bundledImageExtensionQuery; } - /** - * @param shapeName - * @return - */ private static String getImageFileURI(String shapeName) { - final String path = new StringBuffer(IMAGE_DIR).append(shapeName).append(IMAGE_EXT).toString(); - String pluginId = DiagramUIPlugin.getPlugin().getSymbolicName(); - return "platform:/plugin/" + pluginId + "/" + path; //$NON-NLS-1$ //$NON-NLS-2$ + return MessageFormat.format("platform:/plugin/{0}/images/{1}.svg", DiagramUIPlugin.getPlugin().getSymbolicName(), shapeName); //$NON-NLS-1$ } /** @@ -389,24 +380,7 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { * @return The hexa representation of the color. */ private static String getRGBValuesColorToHexa(final RGBValues color) { - String blankDigit = "0"; //$NON-NLS-1$ - StringBuffer colorInHexa = new StringBuffer(); - String hexaColor = Integer.toHexString(color.getRed()); - if (hexaColor.length() == 1) { - colorInHexa.append(blankDigit); - } - colorInHexa.append(hexaColor); - hexaColor = Integer.toHexString(color.getGreen()); - if (hexaColor.length() == 1) { - colorInHexa.append(blankDigit); - } - colorInHexa.append(hexaColor); - hexaColor = Integer.toHexString(color.getBlue()); - if (hexaColor.length() == 1) { - colorInHexa.append(blankDigit); - } - colorInHexa.append(hexaColor); - return colorInHexa.toString(); + return String.format("%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue()); //$NON-NLS-1$ } /** @@ -414,25 +388,8 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { * The color to transform in hexa value * @return The hexa representation of the color. */ - private String getColorToHexa(Color color) { - String blankDigit = "0"; //$NON-NLS-1$ - StringBuffer colorInHexa = new StringBuffer(); - String hexaColor = Integer.toHexString(color.getRed()); - if (hexaColor.length() == 1) { - colorInHexa.append(blankDigit); - } - colorInHexa.append(hexaColor); - hexaColor = Integer.toHexString(color.getGreen()); - if (hexaColor.length() == 1) { - colorInHexa.append(blankDigit); - } - colorInHexa.append(hexaColor); - hexaColor = Integer.toHexString(color.getBlue()); - if (hexaColor.length() == 1) { - colorInHexa.append(blankDigit); - } - colorInHexa.append(hexaColor); - return colorInHexa.toString(); + private static String getColorToHexa(Color color) { + return String.format("%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue()); //$NON-NLS-1$ } private static String getNewStyle(String actualStyle, String colorAttribute, String newColor) { @@ -451,7 +408,7 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { /** * refresh the figure. - * + * * @param bundledImage * the image associated to the figure */ @@ -519,38 +476,38 @@ public class BundledImageFigure extends AbstractCachedSVGFigure { /** * Compute a key for this BundleImageFigure. This key is used to store in * cache the corresponding {@link org.eclipse.swt.graphics.Image}. - * + * * {@inheritDoc} - * + * * @return The key corresponding to this BundleImageFigure. */ @Override protected String getKey() { StringBuffer result = new StringBuffer(); result.append(getDocumentKey()); - result.append(SEPARATOR); + result.append(AbstractCachedSVGFigure.SEPARATOR); result.append(getSiriusAlpha()); - result.append(SEPARATOR); + result.append(AbstractCachedSVGFigure.SEPARATOR); return result.toString(); } /** * Compute a key for this BundleImageFigure. This key is used to store in * cache the corresponding {@link org.eclipse.swt.graphics.Image}. - * + * * {@inheritDoc} - * + * * @return The key corresponding to this BundleImageFigure. */ @Override protected String getDocumentKey() { StringBuffer result = new StringBuffer(); result.append(super.getDocumentKey()); - result.append(SEPARATOR); + result.append(AbstractCachedSVGFigure.SEPARATOR); result.append(shapeName); - result.append(SEPARATOR); + result.append(AbstractCachedSVGFigure.SEPARATOR); result.append(mainBorderColor); - result.append(SEPARATOR); + result.append(AbstractCachedSVGFigure.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/SVGWorkspaceImageFigure.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/tools/api/figure/SVGWorkspaceImageFigure.java index 46b7b34555..f2a7511684 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 @@ -30,9 +30,9 @@ import org.eclipse.swt.graphics.Image; * The {@link SVGWorkspaceImageFigure} is useful to load svg images using a * cache. The image can be in the workspace, or if it's not found in the * workspace it will be looked up in the plug-ins. - * + * * @author mporhel - * + * */ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements IWorkspaceImageFigure { @@ -51,7 +51,7 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements /** * Create the {@link SVGWorkspaceImageFigure} from a {@link WorkspaceImage} * instance. - * + * * @param image * {@link SVGWorkspaceImageFigure} specification. * @return new Figure. @@ -65,7 +65,7 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements /** * Create the {@link SVGWorkspaceImageFigure} from a {@link ContainerStyle} * instance. - * + * * @param containerStyle * {@link ContainerStyle} specification. * @return new Figure. @@ -81,11 +81,6 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements return null; } - /** - * {@inheritDoc} - * - * @see org.eclipse.draw2d.Figure#setSize(int, int) - */ @Override public void setSize(final int w, final int h) { if (keepAspectRatio) { @@ -96,31 +91,16 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements } } - /** - * {@inheritDoc} - * - * @see org.eclipse.draw2d.Figure#setMaximumSize(org.eclipse.draw2d.geometry.Dimension) - */ @Override public void setMaximumSize(final Dimension d) { super.setMaximumSize(this.getSize()); } - /** - * {@inheritDoc} - * - * @see org.eclipse.draw2d.Figure#setMinimumSize(org.eclipse.draw2d.geometry.Dimension) - */ @Override public void setMinimumSize(final Dimension d) { super.setMinimumSize(this.getSize()); } - /** - * {@inheritDoc} - * - * @see org.eclipse.draw2d.Figure#setPreferredSize(org.eclipse.draw2d.geometry.Dimension) - */ @Override public void setPreferredSize(final Dimension size) { super.setPreferredSize(this.getSize()); @@ -128,19 +108,21 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements /** * Get the image aspect ratio. - * + * * @return the image aspect ratio */ + @Override public double getImageAspectRatio() { return imageAspectRatio; } /** * Refreshes the figure. - * + * * @param containerStyle * the style of the container */ + @Override public void refreshFigure(final ContainerStyle containerStyle) { if (containerStyle instanceof FlatContainerStyle) { final FlatContainerStyle style = (FlatContainerStyle) containerStyle; @@ -157,10 +139,11 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements /** * refresh the figure. - * + * * @param workspaceImage * the image associated to the figure */ + @Override public void refreshFigure(final WorkspaceImage workspaceImage) { if (workspaceImage != null) { boolean updated = this.updateImageURI(workspaceImage.getWorkspacePath()); @@ -187,11 +170,11 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements private boolean updateImageURI(String workspacePath) { if (workspacePath != null) { - Option<String> existingImageUri = getImageUri(workspacePath, false); + Option<String> existingImageUri = SVGWorkspaceImageFigure.getImageUri(workspacePath, false); if (existingImageUri.some()) { setURI(existingImageUri.get()); } else { - setURI(getImageNotFoundURI()); + setURI(AbstractCachedSVGFigure.getImageNotFoundURI()); } return true; } @@ -200,7 +183,7 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements /** * Return an optional uri as used in the document key to read svg files. - * + * * @param workspacePath * the workspace path of the file. * @param force @@ -225,23 +208,24 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements * Compute a key for this {@link SVGWorkspaceImageFigure}. This key is used * to store in cache the corresponding * {@link org.eclipse.swt.graphics.Image}. - * + * * {@inheritDoc} - * + * * @return The key corresponding to this SVGWorkspaceImageFigure. */ + @Override protected String getKey() { StringBuffer result = new StringBuffer(); result.append(getDocumentKey()); - result.append(SEPARATOR); + result.append(AbstractCachedSVGFigure.SEPARATOR); result.append(getSiriusAlpha()); - result.append(SEPARATOR); + result.append(AbstractCachedSVGFigure.SEPARATOR); return result.toString(); } /** * Get an {@link Image} instance. The image will be stored in a cache. - * + * * @param path * the path is a "/project/file" path, if it's not found in the * workspace, the class will look for the file in the plug-ins. @@ -258,16 +242,16 @@ public class SVGWorkspaceImageFigure extends AbstractCachedSVGFigure implements * 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 workspacePath * the modified or deleted image file path. * @return an option with the document uri used as key for the svg file if a * corresponding element was removed. */ public static Option<String> removeFromCache(String workspacePath) { - Option<String> imageUri = getImageUri(workspacePath, true); + Option<String> imageUri = SVGWorkspaceImageFigure.getImageUri(workspacePath, true); if (imageUri.some()) { - if (doRemoveFromCache(imageUri.get())) { + if (AbstractCachedSVGFigure.doRemoveFromCache(imageUri.get())) { return imageUri; } } |
