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 org.vectomatic.dom.svg.utils;
19  
20  import org.vectomatic.dom.svg.utils.DOMHelper;
21  
22  import com.google.gwt.dom.client.Document;
23  import com.google.gwt.dom.client.Element;
24  import com.google.gwt.dom.client.IFrameElement;
25  import com.google.gwt.dom.client.NativeEvent;
26  import com.google.gwt.user.client.ui.RootPanel;
27  
28  /**
29   * Implementation of the AsyncXmlLoader based on iframes.
30   */
31  public class IFrameXmlLoader implements AsyncXmlLoader {
32  	
33  	public IFrameXmlLoader() {
34  	}
35  
36  	public void loadResource(String resourceName, AsyncXmlLoaderCallback callback) {
37  		IFrameElement iframe = Document.get().createIFrameElement();
38  		iframe.setAttribute("tabIndex", "-1");
39  		iframe.setAttribute("style", "position: absolute; width: 0; height: 0; border: 0");
40  		iframe.setAttribute("src", "javascript:''");
41  		Element body = RootPanel.get().getElement();
42  		body.appendChild(iframe);
43  		setFrameLocation(resourceName, iframe, callback);
44  	}
45  	
46  	@SuppressWarnings("unused")
47  	private void dispatch(NativeEvent event, AsyncXmlLoaderCallback callback) {
48  		IFrameElement iframe = event.getCurrentEventTarget().cast();
49  		if (iframe != null) {
50  			String href = getHref(iframe);
51  			if (!"about:blank".equals(href)) {
52  				Element root = iframe.getContentDocument().getDocumentElement();
53  				Element localRoot = DOMHelper.importNode(DOMHelper.getCurrentDocument(), root, true).cast();
54  				callback.onSuccess(href, localRoot);
55  			}
56  			Element body = RootPanel.get().getElement();
57  			body.removeChild(iframe);
58  		}
59  	}
60  	
61  	private native String getHref(IFrameElement iframe) /*-{
62  		return iframe.contentDocument.location.href;
63  	}-*/;
64  	
65  	private native void setFrameLocation(String resourceName, IFrameElement iframe, AsyncXmlLoaderCallback callback) /*-{
66  		var gwtThis = this;
67  		iframe.onload = function(evt) {
68  		  gwtThis.@org.vectomatic.dom.svg.utils.IFrameXmlLoader::dispatch(Lcom/google/gwt/dom/client/NativeEvent;Lorg/vectomatic/dom/svg/utils/AsyncXmlLoaderCallback;)(evt,callback);
69  		}
70  		iframe.contentDocument.location.href = resourceName;
71  	}-*/;
72  }