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  package com.google.gwt.uibinder.elementparsers;
19  
20  import org.vectomatic.dom.svg.ui.SVGWidget;
21  import org.w3c.dom.Element;
22  import org.w3c.dom.Node;
23  
24  import com.google.gwt.core.ext.UnableToCompleteException;
25  import com.google.gwt.core.ext.typeinfo.JClassType;
26  import com.google.gwt.uibinder.rebind.FieldManager;
27  import com.google.gwt.uibinder.rebind.FieldWriter;
28  import com.google.gwt.uibinder.rebind.UiBinderWriter;
29  import com.google.gwt.uibinder.rebind.XMLElement;
30  
31  public class SvgFieldInterpreter implements XMLElement.Interpreter<String> {
32  
33  	private final UiBinderWriter writer;
34  	private final String ancestorExpression;
35  	private final Element root;
36  
37  	public SvgFieldInterpreter(UiBinderWriter writer, String ancestorExpression, Element root) {
38  		this.writer = writer;
39  		this.ancestorExpression = ancestorExpression;
40  		this.root = root;
41  	}
42  
43  	public String interpretElement(XMLElement elem) throws UnableToCompleteException {
44  		String fieldName = writer.declareFieldIfNeeded(elem);
45  		if (fieldName != null) {
46  			JClassType type = writer.findFieldType(elem);
47  			StringBuilder builder = new StringBuilder();
48  			builder.append("(");
49  			builder.append(type.getQualifiedSourceName());
50  			builder.append(")");
51  			builder.append(SVGWidget.class.getName());
52  			builder.append(".getUiBinderField(");
53  		    FieldManager fieldManager = writer.getFieldManager();
54  			builder.append(fieldManager.convertFieldToGetter(ancestorExpression));
55  			builder.append(".getSvgElement().getElement(), \"");
56  			builder.append(getXpath(elem.getElement()));
57  			builder.append("\")");
58  			FieldWriter ancestorWriter = fieldManager.require(ancestorExpression);
59  			ancestorWriter.setBuildPrecedence(1 + ancestorWriter.getBuildPrecedence());
60  		    writer.setFieldInitializer(fieldName, builder.toString());
61  //		    writer.addInitStatement(
62  //		        "%s = (" + type.getQualifiedSourceName() + ")" + SVGWidget.class.getName() + ".getUiBinderField(%s, \"%s\");",
63  //		        fieldName, ancestorExpression, getXpath(elem.getElement()));
64  		    return null;
65  		}
66  
67  		/*
68  		 * Return null because we don't want to replace the dom element with any
69  		 * particular string (though we may have consumed its id or gwt:field)
70  		 */
71  		return null;
72  	}
73  	
74  	private String getXpath(Element element) {
75  		StringBuilder buffer = new StringBuilder();
76  		while (!root.isSameNode(element)) {
77  			int index = 1;
78  			Node sibling = element.getPreviousSibling();
79  			while (sibling != null) {
80  				if (sibling.getNodeType() == Node.ELEMENT_NODE) {
81  					index++;
82  				}
83  				sibling = sibling.getPreviousSibling();
84  			}
85  			buffer.insert(0, "/*[" + index + "]");
86  			Node node = element.getParentNode();
87  			while (node.getNodeType() != Node.ELEMENT_NODE) {
88  				node = node.getParentNode();
89  			}
90  			element = (Element)node;
91  		}
92  		buffer.insert(0, ".");
93  		return buffer.toString();
94  	}
95  }