View Javadoc
1   /*
2    * Copyright 2011 Daniel Kurka
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package com.googlecode.mgwt.ui.client.widget.touch;
17  
18  import com.google.gwt.core.client.GWT;
19  import com.google.gwt.event.shared.GwtEvent;
20  import com.google.gwt.event.shared.HandlerRegistration;
21  import com.google.gwt.user.client.ui.Widget;
22  import com.googlecode.mgwt.dom.client.event.mouse.HandlerRegistrationCollection;
23  import com.googlecode.mgwt.dom.client.event.tap.HasTapHandlers;
24  import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
25  import com.googlecode.mgwt.dom.client.event.tap.TapHandler;
26  import com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers;
27  import com.googlecode.mgwt.dom.client.event.touch.TouchCancelHandler;
28  import com.googlecode.mgwt.dom.client.event.touch.TouchEndHandler;
29  import com.googlecode.mgwt.dom.client.event.touch.TouchHandler;
30  import com.googlecode.mgwt.dom.client.event.touch.TouchMoveHandler;
31  import com.googlecode.mgwt.dom.client.event.touch.TouchStartHandler;
32  import com.googlecode.mgwt.dom.client.recognizer.longtap.HasLongTapHandlers;
33  import com.googlecode.mgwt.dom.client.recognizer.longtap.LongTapEvent;
34  import com.googlecode.mgwt.dom.client.recognizer.longtap.LongTapHandler;
35  import com.googlecode.mgwt.dom.client.recognizer.pinch.HasPinchHandlers;
36  import com.googlecode.mgwt.dom.client.recognizer.pinch.PinchEvent;
37  import com.googlecode.mgwt.dom.client.recognizer.pinch.PinchHandler;
38  import com.googlecode.mgwt.dom.client.recognizer.swipe.HasSwipeHandlers;
39  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeEndEvent;
40  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeEndHandler;
41  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeMoveEvent;
42  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeMoveHandler;
43  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeStartEvent;
44  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeStartHandler;
45  
46  /**
47   * A TouchDelegate can be used to source touch events from a widget that does
48   * not support {@link HasTouchHandlers}
49   * 
50   * @author Daniel Kurka
51   * @version $Id: $
52   */
53  
54  public class TouchDelegate implements HasTouchHandlers, HasTapHandlers, HasSwipeHandlers, HasPinchHandlers, HasLongTapHandlers {
55  
56  	protected final GestureUtility gestureUtility;
57  	private static final TouchWidgetImpl impl = GWT.create(TouchWidgetImpl.class);
58  	private final Widget w;
59  
60  	/**
61  	 * Construct a touchDelegate
62  	 * 
63  	 * @param w the widget to source touchevents from
64  	 */
65  	public TouchDelegate(Widget w) {
66  		this.w = w;
67  		gestureUtility = new GestureUtility(this);
68  	}
69  
70  	/*
71  	 * (non-Javadoc)
72  	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchStartHandler(com.googlecode.mgwt.dom.client.event.touch.TouchStartHandler)
73  	 */
74  	/** {@inheritDoc} */
75  	@Override
76  	public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
77  		return impl.addTouchStartHandler(w, handler);
78  
79  	}
80  
81  	/*
82  	 * (non-Javadoc)
83  	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchMoveHandler(com.googlecode.mgwt.dom.client.event.touch.TouchMoveHandler)
84  	 */
85  	/** {@inheritDoc} */
86  	@Override
87  	public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
88  		return impl.addTouchMoveHandler(w, handler);
89  
90  	}
91  
92  	/*
93  	 * (non-Javadoc)
94  	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchCancelHandler(com.googlecode.mgwt.dom.client.event.touch.TouchCancelHandler)
95  	 */
96  	/** {@inheritDoc} */
97  	@Override
98  	public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
99  		return impl.addTouchCancelHandler(w, handler);
100 
101 	}
102 
103 	/*
104 	 * (non-Javadoc)
105 	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchEndHandler(com.googlecode.mgwt.dom.client.event.touch.TouchEndHandler)
106 	 */
107 	/** {@inheritDoc} */
108 	@Override
109 	public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
110 		return impl.addTouchEndHandler(w, handler);
111 
112 	}
113 
114 	@Override
115 	public HandlerRegistration addTapHandler(TapHandler handler) {
116 		gestureUtility.ensureTapRecognizer();
117 		return w.addHandler(handler, TapEvent.getType());
118 	}
119 
120 	public HandlerRegistration addTouchHandler(TouchHandler handler) {
121 		HandlerRegistrationCollection handlerRegistrationCollection = new HandlerRegistrationCollection();
122 
123 		handlerRegistrationCollection.addHandlerRegistration(addTouchCancelHandler(handler));
124 		handlerRegistrationCollection.addHandlerRegistration(addTouchStartHandler(handler));
125 		handlerRegistrationCollection.addHandlerRegistration(addTouchEndHandler(handler));
126 		handlerRegistrationCollection.addHandlerRegistration(addTouchMoveHandler(handler));
127 		return handlerRegistrationCollection;
128 	}
129 
130 	@Override
131 	public void fireEvent(GwtEvent<?> event) {
132 		w.fireEvent(event);
133 
134 	}
135 
136 	public HandlerRegistration addSwipeStartHandler(SwipeStartHandler handler) {
137 		gestureUtility.ensureSwipeRecognizer();
138 		return w.addHandler(handler, SwipeStartEvent.getType());
139 	}
140 
141 	public HandlerRegistration addSwipeMoveHandler(SwipeMoveHandler handler) {
142 		gestureUtility.ensureSwipeRecognizer();
143 		return w.addHandler(handler, SwipeMoveEvent.getType());
144 	}
145 
146 	public HandlerRegistration addSwipeEndHandler(SwipeEndHandler handler) {
147 		gestureUtility.ensureSwipeRecognizer();
148 		return w.addHandler(handler, SwipeEndEvent.getType());
149 	}
150 
151 	@Override
152 	public HandlerRegistration addPinchHandler(PinchHandler handler) {
153 		gestureUtility.ensurePinchRecognizer(w);
154 		return w.addHandler(handler, PinchEvent.getType());
155 	}
156 
157 	@Override
158 	public HandlerRegistration addLongTapHandler(LongTapHandler handler) {
159 		gestureUtility.ensureLongTapHandler();
160 		return w.addHandler(handler, LongTapEvent.getType());
161 	}
162 
163 }