View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 Lukas Laag
3    * This file is part of lib-gwt-svg.
4    * 
5    * libgwtsvg is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * libgwtsvg is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with libgwtsvg.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  
19  package org.vectomatic.dom.svg.ui;
20  
21  import org.vectomatic.dom.svg.OMNode;
22  
23  import com.google.gwt.dom.client.Element;
24  import com.google.gwt.user.client.ui.Widget;
25  
26  /**
27   * Abstract base class for SVG Widgets
28   * @author laaglu
29   */
30  public abstract class SVGWidget extends Widget {
31  	/**
32  	 * Constructor
33  	 */
34  	public SVGWidget() {
35  	}
36  
37  	/**
38  	 * Method invoked by the UiBinder to retrieve a UiBinder field inside an SVG document
39  	 * @param svgElement
40  	 * The root element of the SVG document
41  	 * @param expr
42  	 * An XPath to reach the field
43  	 * @return the retrieved UiBinder field
44  	 */
45  	public static OMNode getUiBinderField(Element svgElement, String expr) {
46  		Element elt = getUiBinderField_(svgElement, expr);
47  		return OMNode.convert(elt);
48  	}
49  	
50  //	private static native Element getUiBinderField_(Element svgElement, String expr) /*-{
51  //		var result = $doc.evaluate(expr, svgElement, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE , null);
52  //		return result.iterateNext();
53  //	}-*/;
54  
55  	private static Element getUiBinderField_(Element svgElement, String expr) {
56  		for (String str : expr.split("([/\\.\\[\\]\\*])+")) {
57  			if (str.length() > 0) {
58  				int index = Integer.parseInt(str);
59  				svgElement = svgElement.getFirstChildElement();
60  				while (index > 1) {
61  					svgElement = svgElement.getNextSiblingElement();
62  					index--;
63  				}
64  			}
65  		}
66  		return svgElement;
67  	}
68  }