Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/service/shape/AbstractShapeProvider.java')
-rw-r--r--plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/service/shape/AbstractShapeProvider.java76
1 files changed, 74 insertions, 2 deletions
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/service/shape/AbstractShapeProvider.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/service/shape/AbstractShapeProvider.java
index e73fc03ce5f..09d2f5d1511 100644
--- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/service/shape/AbstractShapeProvider.java
+++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/service/shape/AbstractShapeProvider.java
@@ -17,11 +17,14 @@ import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
+import java.util.WeakHashMap;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.dom.util.DOMUtilities;
import org.eclipse.core.runtime.IConfigurationElement;
+import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.gmf.runtime.common.core.service.AbstractProvider;
import org.eclipse.gmf.runtime.common.core.service.IOperation;
import org.eclipse.gmf.runtime.draw2d.ui.render.RenderedImage;
@@ -58,6 +61,11 @@ public abstract class AbstractShapeProvider extends AbstractProvider implements
/** description of the factory */
protected String description;
+ /**
+ * Maps of URIs for SVG files referred to with relative paths
+ */
+ private WeakHashMap<Resource, Map<String, String>> relativePaths;
+
/** Cache for the loaded SVG document */
private Map<String, SVGDocument> cache;
@@ -130,13 +138,32 @@ public abstract class AbstractShapeProvider extends AbstractProvider implements
* Loads a SVG document from the given location.
* This method uses a cache so that any given document is only loaded once.
*
+ * @param view
+ * The view object to retrieve a svg document for
+ * @param location
+ * The location to load the document from
+ * @return the Document SVG from its location, can return null if this is not a svg
+ */
+ protected synchronized SVGDocument getSVGDocument(EObject view, String location) {
+ if (relativePaths == null) {
+ relativePaths = new WeakHashMap<Resource, Map<String, String>>();
+ }
+ String canonical = getCanonicalURI(view, location);
+ return getSVGDocument(canonical);
+ }
+
+ /**
+ * Loads a SVG document from the given location.
+ * This method uses a cache so that any given document is only loaded once.
+ *
* @param location
* The location to load the document from
* @return the Document SVG from its location, can return null if this is not a svg
*/
protected synchronized SVGDocument getSVGDocument(String location) {
- if (cache == null)
+ if (cache == null) {
cache = new HashMap<String, SVGDocument>();
+ }
if (cache.containsKey(location))
return cache.get(location);
SVGDocument doc = doGetSVGDocument(location);
@@ -176,6 +203,47 @@ public abstract class AbstractShapeProvider extends AbstractProvider implements
return null;
}
+ /**
+ * Translates the given uri as a string to a canonical Eclipse URI
+ * The uri may be relative to the currently edited EMF resource
+ *
+ * @param model
+ * The model element used to retrieve the EMF resource that is currently edited
+ * @param uri
+ * The potentially relative URI of a svg file
+ * @return The canonical URI of the resource
+ */
+ private String getCanonicalURI(EObject model, String uri) {
+ if (uri.startsWith("platform:/")) {
+ return uri;
+ }
+
+ Map<String, String> resMap = relativePaths.get(model.eResource());
+ if (resMap == null) {
+ resMap = new HashMap<String, String>();
+ relativePaths.put(model.eResource(), resMap);
+ }
+ String canonical = resMap.get(uri);
+ if (canonical != null) {
+ return canonical;
+ }
+
+ URI resURI = model.eResource().getURI();
+ if (!resURI.isPlatform()) {
+ return null;
+ }
+ StringBuilder builder = new StringBuilder("platform:/");
+ String[] segments = resURI.segments();
+ for (int i = 0; i < segments.length - 1; i++) {
+ builder.append(segments[i]);
+ builder.append("/");
+ }
+ builder.append(uri);
+ canonical = builder.toString();
+ resMap.put(uri, canonical);
+ return canonical;
+ }
+
protected RenderedImage renderSVGDocument(EObject view, SVGDocument document) throws IOException {
postProcess(view, document);
String svgAsText = toString(document);
@@ -202,7 +270,11 @@ public abstract class AbstractShapeProvider extends AbstractProvider implements
public synchronized void refresh(IEditorPart editorPart) {
// Clears the cache of loaded SVG documents
// This will force their reloading
- if (cache != null)
+ if (cache != null) {
cache.clear();
+ }
+ if (relativePaths != null) {
+ relativePaths.clear();
+ }
}
}

Back to the top