View Javadoc
1   /*
2    * Copyright 2010 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.HandlerRegistration;
20  import com.google.gwt.user.client.ui.Widget;
21  import com.googlecode.mgwt.dom.client.event.mouse.HandlerRegistrationCollection;
22  import com.googlecode.mgwt.dom.client.event.tap.HasTapHandlers;
23  import com.googlecode.mgwt.dom.client.event.tap.TapEvent;
24  import com.googlecode.mgwt.dom.client.event.tap.TapHandler;
25  import com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers;
26  import com.googlecode.mgwt.dom.client.event.touch.TouchCancelHandler;
27  import com.googlecode.mgwt.dom.client.event.touch.TouchEndHandler;
28  import com.googlecode.mgwt.dom.client.event.touch.TouchHandler;
29  import com.googlecode.mgwt.dom.client.event.touch.TouchMoveHandler;
30  import com.googlecode.mgwt.dom.client.event.touch.TouchStartHandler;
31  import com.googlecode.mgwt.dom.client.recognizer.longtap.HasLongTapHandlers;
32  import com.googlecode.mgwt.dom.client.recognizer.longtap.LongTapEvent;
33  import com.googlecode.mgwt.dom.client.recognizer.longtap.LongTapHandler;
34  import com.googlecode.mgwt.dom.client.recognizer.pinch.HasPinchHandlers;
35  import com.googlecode.mgwt.dom.client.recognizer.pinch.PinchEvent;
36  import com.googlecode.mgwt.dom.client.recognizer.pinch.PinchHandler;
37  import com.googlecode.mgwt.dom.client.recognizer.swipe.HasSwipeHandlers;
38  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeEndEvent;
39  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeEndHandler;
40  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeMoveEvent;
41  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeMoveHandler;
42  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeStartEvent;
43  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeStartHandler;
44  
45  /**
46   * Base class for all widgets that support touch events Childclasses are
47   * responsible for setting the dom element
48   * 
49   * @author Daniel Kurka
50   */
51  
52  public abstract class TouchWidget extends Widget implements HasTouchHandlers, HasTapHandlers, HasSwipeHandlers, HasPinchHandlers, HasLongTapHandlers {
53  
54  	private static final TouchWidgetImpl impl = GWT.create(TouchWidgetImpl.class);
55  
56  	protected final GestureUtility gestureUtility;
57  
58  	/**
59  	 * <p>
60  	 * Constructor for TouchWidget.
61  	 * </p>
62  	 */
63  	public TouchWidget() {
64  		gestureUtility = new GestureUtility(this);
65  	}
66  
67  	/*
68  	 * (non-Javadoc)
69  	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchStartHandler(com.googlecode.mgwt.dom.client.event.touch.TouchStartHandler)
70  	 */
71  	/** {@inheritDoc} */
72  	@Override
73  	public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
74  		return impl.addTouchStartHandler(this, handler);
75  
76  	}
77  
78  	/*
79  	 * (non-Javadoc)
80  	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchMoveHandler(com.googlecode.mgwt.dom.client.event.touch.TouchMoveHandler)
81  	 */
82  	/** {@inheritDoc} */
83  	@Override
84  	public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
85  		return impl.addTouchMoveHandler(this, handler);
86  
87  	}
88  
89  	/*
90  	 * (non-Javadoc)
91  	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchCancelHandler(com.googlecode.mgwt.dom.client.event.touch.TouchCancelHandler)
92  	 */
93  	/** {@inheritDoc} */
94  	@Override
95  	public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
96  		return impl.addTouchCancelHandler(this, handler);
97  
98  	}
99  
100 	/*
101 	 * (non-Javadoc)
102 	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchEndHandler(com.googlecode.mgwt.dom.client.event.touch.TouchEndHandler)
103 	 */
104 	/** {@inheritDoc} */
105 	@Override
106 	public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
107 		return impl.addTouchEndHandler(this, handler);
108 
109 	}
110 
111 	/*
112 	 * (non-Javadoc)
113 	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchHandler(com.googlecode.mgwt.dom.client.event.touch.TouchHandler)
114 	 */
115 	/** {@inheritDoc} */
116 	@Override
117 	public HandlerRegistration addTouchHandler(TouchHandler handler) {
118 		HandlerRegistrationCollection handlerRegistrationCollection = new HandlerRegistrationCollection();
119 
120 		handlerRegistrationCollection.addHandlerRegistration(addTouchCancelHandler(handler));
121 		handlerRegistrationCollection.addHandlerRegistration(addTouchStartHandler(handler));
122 		handlerRegistrationCollection.addHandlerRegistration(addTouchEndHandler(handler));
123 		handlerRegistrationCollection.addHandlerRegistration(addTouchMoveHandler(handler));
124 		return handlerRegistrationCollection;
125 	}
126 
127 	/**
128 	 * <p>
129 	 * addTapHandler
130 	 * </p>
131 	 * 
132 	 * @param handler a
133 	 *            {@link com.googlecode.mgwt.dom.client.event.tap.TapHandler}
134 	 *            object.
135 	 * @return a {@link com.google.gwt.event.shared.HandlerRegistration} object.
136 	 */
137 
138 	public HandlerRegistration addTapHandler(TapHandler handler) {
139 		gestureUtility.ensureTapRecognizer();
140 		return addHandler(handler, TapEvent.getType());
141 	}
142 
143 	@Override
144 	public HandlerRegistration addSwipeStartHandler(SwipeStartHandler handler) {
145 		gestureUtility.ensureSwipeRecognizer();
146 		return addHandler(handler, SwipeStartEvent.getType());
147 	}
148 
149 	@Override
150 	public HandlerRegistration addSwipeMoveHandler(SwipeMoveHandler handler) {
151 		gestureUtility.ensureSwipeRecognizer();
152 		return addHandler(handler, SwipeMoveEvent.getType());
153 	}
154 
155 	@Override
156 	public HandlerRegistration addSwipeEndHandler(SwipeEndHandler handler) {
157 		gestureUtility.ensureSwipeRecognizer();
158 		return addHandler(handler, SwipeEndEvent.getType());
159 	}
160 
161 	@Override
162 	public HandlerRegistration addPinchHandler(PinchHandler handler) {
163 		gestureUtility.ensurePinchRecognizer(this);
164 		return addHandler(handler, PinchEvent.getType());
165 	}
166 
167 	@Override
168 	public HandlerRegistration addLongTapHandler(LongTapHandler handler) {
169 		gestureUtility.ensureLongTapHandler();
170 		return addHandler(handler, LongTapEvent.getType());
171 	}
172 
173 }