Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordstadnik2008-07-30 17:34:56 -0400
committerdstadnik2008-07-30 17:34:56 -0400
commit0a8a90d18af413159f602eda1da6c34d6ddc1511 (patch)
treed4076f2bc8060188a0c1b303e5167eb956668dde
parent92fb393c45b9ed18f7d74f86a2bf13646ad287d5 (diff)
downloadorg.eclipse.gmf-tooling-0a8a90d18af413159f602eda1da6c34d6ddc1511.tar.gz
org.eclipse.gmf-tooling-0a8a90d18af413159f602eda1da6c34d6ddc1511.tar.xz
org.eclipse.gmf-tooling-0a8a90d18af413159f602eda1da6c34d6ddc1511.zip
[2410] support document properties editing
-rw-r--r--plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/internal/runtime/lite/svg/InferringNamespaceContext.java80
-rw-r--r--plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/runtime/lite/svg/SVGFigure.java30
-rw-r--r--plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/runtime/lite/svg/SVGUtils.java53
3 files changed, 162 insertions, 1 deletions
diff --git a/plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/internal/runtime/lite/svg/InferringNamespaceContext.java b/plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/internal/runtime/lite/svg/InferringNamespaceContext.java
new file mode 100644
index 000000000..a0f0d3093
--- /dev/null
+++ b/plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/internal/runtime/lite/svg/InferringNamespaceContext.java
@@ -0,0 +1,80 @@
+/**
+ * Copyright (c) 2008 Borland Software Corporation
+ *
+ * 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:
+ * Dmitry Stadnik - initial API and implementation
+ */
+package org.eclipse.gmf.internal.runtime.lite.svg;
+
+import java.util.Iterator;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+public class InferringNamespaceContext implements NamespaceContext {
+
+ private Node namespaceContext;
+
+ public InferringNamespaceContext(Node namespaceContext) {
+ this.namespaceContext = namespaceContext;
+ }
+
+ public String getNamespaceURI(String prefix) {
+ if (prefix.equals(XMLConstants.XML_NS_PREFIX)) {
+ return XMLConstants.XML_NS_URI;
+ } else if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
+ return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+ } else {
+ return inferNamespaceURI(prefix);
+ }
+ }
+
+ /*
+ * @see com.sun.org.apache.xml.internal.utils.PrefixResolverDefault
+ */
+ protected String inferNamespaceURI(String prefix) {
+ Node parent = namespaceContext;
+ String namespace = null;
+ int type;
+ while ((null != parent) && (null == namespace) && (((type = parent.getNodeType()) == Node.ELEMENT_NODE) || (type == Node.ENTITY_REFERENCE_NODE))) {
+ if (type == Node.ELEMENT_NODE) {
+ if (parent.getNodeName().indexOf(prefix + ":") == 0) {
+ return parent.getNamespaceURI();
+ }
+ NamedNodeMap nnm = parent.getAttributes();
+ for (int i = 0; i < nnm.getLength(); i++) {
+ Node attr = nnm.item(i);
+ String aname = attr.getNodeName();
+ boolean isPrefix = aname.startsWith("xmlns:");
+ if (isPrefix || aname.equals("xmlns")) {
+ int index = aname.indexOf(':');
+ String p = isPrefix ? aname.substring(index + 1) : "";
+ if (p.equals(prefix)) {
+ namespace = attr.getNodeValue();
+ break;
+ }
+ }
+ }
+ }
+ parent = parent.getParentNode();
+ }
+ return namespace;
+ }
+
+ public String getPrefix(String namespaceURI) {
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ public Iterator getPrefixes(String namespaceURI) {
+ return null;
+ }
+}
diff --git a/plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/runtime/lite/svg/SVGFigure.java b/plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/runtime/lite/svg/SVGFigure.java
index 6b5c7439a..5e9b9b554 100644
--- a/plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/runtime/lite/svg/SVGFigure.java
+++ b/plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/runtime/lite/svg/SVGFigure.java
@@ -15,6 +15,11 @@ import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
@@ -27,10 +32,12 @@ import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.gmf.internal.runtime.lite.svg.Activator;
import org.eclipse.gmf.internal.runtime.lite.svg.ImageTranscoderEx;
+import org.eclipse.gmf.internal.runtime.lite.svg.InferringNamespaceContext;
import org.eclipse.gmf.internal.runtime.lite.svg.SVGGraphics2D;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
public class SVGFigure extends Figure {
@@ -66,7 +73,7 @@ public class SVGFigure extends Figure {
}
}
- private Document getDocument() {
+ protected final Document getDocument() {
if (failedToLoadDocument) {
return null;
}
@@ -76,6 +83,27 @@ public class SVGFigure extends Figure {
return document;
}
+ private XPath getXPath() {
+ XPath xpath = XPathFactory.newInstance().newXPath();
+ xpath.setNamespaceContext(new InferringNamespaceContext(getDocument().getDocumentElement()));
+ return xpath;
+ }
+
+ /**
+ * Executes XPath query over the SVG document.
+ */
+ protected final NodeList getNodes(String query) {
+ Document document = getDocument();
+ if (document != null) {
+ try {
+ return (NodeList) getXPath().evaluate(query, document, XPathConstants.NODESET);
+ } catch (XPathExpressionException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ return null;
+ }
+
private void renderDocument(Transcoder transcoder, Document document) {
try {
transcoder.addTranscodingHint(ImageTranscoder.KEY_WIDTH, new Float(getBounds().width));
diff --git a/plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/runtime/lite/svg/SVGUtils.java b/plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/runtime/lite/svg/SVGUtils.java
new file mode 100644
index 000000000..e8abdea95
--- /dev/null
+++ b/plugins/org.eclipse.gmf.runtime.lite.svg/src/org/eclipse/gmf/runtime/lite/svg/SVGUtils.java
@@ -0,0 +1,53 @@
+/**
+ * Copyright (c) 2008 Borland Software Corporation
+ *
+ * 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:
+ * Dmitry Stadnik - initial API and implementation
+ */
+package org.eclipse.gmf.runtime.lite.svg;
+
+import org.apache.batik.svggen.SVGColor;
+import org.apache.batik.svggen.SVGGeneratorContext;
+import org.apache.batik.svggen.SVGPaintDescriptor;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.widgets.Display;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.css.CSSStyleDeclaration;
+import org.w3c.dom.css.CSSValue;
+import org.w3c.dom.css.RGBColor;
+import org.w3c.dom.css.ViewCSS;
+import org.w3c.dom.svg.SVGPaint;
+
+public class SVGUtils {
+
+ private SVGUtils() {
+ }
+
+ public static String toSVGColor(Document document, Color color) {
+ java.awt.Color awtColor = new java.awt.Color(color.getRed(), color.getGreen(), color.getBlue());
+ SVGGeneratorContext svgContext = SVGGeneratorContext.createDefault(document);
+ SVGPaintDescriptor paint = SVGColor.toSVG(awtColor, svgContext);
+ return paint.getPaintValue();
+ }
+
+ public static Color toSWTColor(Element element, String attributeName) {
+ Document document = element.getOwnerDocument();
+ ViewCSS viewCSS = (ViewCSS) document.getDocumentElement();
+ CSSStyleDeclaration computedStyle = viewCSS.getComputedStyle(element, null);
+ SVGPaint svgPaint = (SVGPaint) computedStyle.getPropertyCSSValue(attributeName);
+ if (svgPaint.getPaintType() == SVGPaint.SVG_PAINTTYPE_RGBCOLOR) {
+ RGBColor rgb = svgPaint.getRGBColor();
+ float red = rgb.getRed().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
+ float green = rgb.getGreen().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
+ float blue = rgb.getBlue().getFloatValue(CSSValue.CSS_PRIMITIVE_VALUE);
+ return new Color(Display.getCurrent(), (int) red, (int) green, (int) blue);
+ }
+ return null;
+ }
+}

Back to the top