View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU 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   * svgreal 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 General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.gxt.widget;
19  
20  import org.vectomatic.svg.edit.client.SvgrealApp;
21  import org.vectomatic.svg.edit.client.load.DndHandler;
22  
23  import com.extjs.gxt.ui.client.widget.Viewport;
24  import com.google.gwt.dom.client.NativeEvent;
25  import com.google.gwt.event.dom.client.DomEvent;
26  import com.google.gwt.event.dom.client.DragEnterEvent;
27  import com.google.gwt.event.dom.client.DragEnterHandler;
28  import com.google.gwt.event.dom.client.DragLeaveEvent;
29  import com.google.gwt.event.dom.client.DragLeaveHandler;
30  import com.google.gwt.event.dom.client.DragOverEvent;
31  import com.google.gwt.event.dom.client.DragOverHandler;
32  import com.google.gwt.event.dom.client.DropEvent;
33  import com.google.gwt.event.dom.client.DropHandler;
34  import com.google.gwt.event.dom.client.HasDragEnterHandlers;
35  import com.google.gwt.event.dom.client.HasDragLeaveHandlers;
36  import com.google.gwt.event.dom.client.HasDragOverHandlers;
37  import com.google.gwt.event.dom.client.HasDropHandlers;
38  import com.google.gwt.event.shared.GwtEvent;
39  import com.google.gwt.event.shared.HandlerRegistration;
40  import com.google.gwt.user.client.Element;
41  
42  /**
43   * A viewport class with support for drag and drop of external files.
44   * @author laaglu
45   */
46  public class ViewportExt extends Viewport implements HasDropHandlers, HasDragEnterHandlers, HasDragLeaveHandlers, HasDragOverHandlers {
47  	public ViewportExt() {
48  		new DndHandler(this);
49  	}
50  	public void setElement(Element elem) {
51  		super.setElement(elem);
52  		setup(elem);
53  	}
54  	
55  	private final native void setup(Element elem) /*-{
56  	    var x = this;
57  		var handler = function(evt) {
58  	        x.@org.vectomatic.svg.edit.client.gxt.widget.ViewportExt::dispatch(Lcom/google/gwt/dom/client/NativeEvent;)(evt);
59  		};
60  		elem.addEventListener("dragenter", handler, false);
61  		elem.addEventListener("dragleave", handler, false);
62  		elem.addEventListener("dragover", handler, false);
63  		elem.addEventListener("drop", handler, false);
64  	}-*/;
65  	
66  	public void fireEvent(GwtEvent<?> event) {
67  		revive(event);
68  		SvgrealApp.getApp().getEventBus().fireEventFromSource(event, this);
69  	}
70  	/**
71  	 * Revive the event. GWT does it by taking advantage of the
72  	 * fact that HandlerManager has package access to GwtEvent.
73  	 * Here we use a JSNI call to bypass scope restrictions
74  	 */
75  	private static final native void revive(GwtEvent<?> event) /*-{
76  	  event.@com.google.gwt.event.shared.GwtEvent::revive()();
77  	}-*/;
78  	
79  	/**
80  	 * Dispatches the specified event to this node
81  	 * event handlers
82  	 * @param event The event to dispatch
83  	 */
84  	public void dispatch(NativeEvent event) {
85  		// This call wraps the native event into a DomEvent
86  		// and invokes fireEvent
87  	    DomEvent.fireNativeEvent(event, this, (Element)event.getCurrentEventTarget().cast());
88  	}
89  
90  	@Override
91  	public HandlerRegistration addDropHandler(DropHandler handler) {
92  		return SvgrealApp.getApp().getEventBus().addHandlerToSource(DropEvent.getType(), this, handler);
93  	}
94  
95  	@Override
96  	public HandlerRegistration addDragLeaveHandler(DragLeaveHandler handler) {
97  		return SvgrealApp.getApp().getEventBus().addHandlerToSource(DragLeaveEvent.getType(), this, handler);
98  	}
99  
100 	@Override
101 	public HandlerRegistration addDragEnterHandler(DragEnterHandler handler) {
102 		return SvgrealApp.getApp().getEventBus().addHandlerToSource(DragEnterEvent.getType(), this, handler);
103 	}
104 
105 	@Override
106 	public HandlerRegistration addDragOverHandler(DragOverHandler handler) {
107 		return SvgrealApp.getApp().getEventBus().addHandlerToSource(DragOverEvent.getType(), this, handler);
108 	}
109 
110 }