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.FlowPanel;
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.TouchEvent;
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 simple panel that supports {@link TouchEvent}
48   * 
49   * @author Daniel Kurka
50   * 
51   */
52  
53  public class TouchPanel extends FlowPanel implements HasTouchHandlers, HasTapHandlers, HasPinchHandlers, HasSwipeHandlers, HasLongTapHandlers {
54  
55  	private static final TouchWidgetImpl impl = GWT.create(TouchWidgetImpl.class);
56  
57  	protected final GestureUtility gestureUtility;
58  
59    /**
60     * Construct a touch panel
61     */
62  	public TouchPanel() {
63  		gestureUtility = new GestureUtility(this);
64  	}
65  
66  	/*
67  	 * (non-Javadoc)
68  	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchStartHandler(com.googlecode.mgwt.dom.client.event.touch.TouchStartHandler)
69  	 */
70  	/** {@inheritDoc} */
71  	@Override
72  	public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
73  		return impl.addTouchStartHandler(this, handler);
74  
75  	}
76  
77  	/*
78  	 * (non-Javadoc)
79  	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchMoveHandler(com.googlecode.mgwt.dom.client.event.touch.TouchMoveHandler)
80  	 */
81  	/** {@inheritDoc} */
82  	@Override
83  	public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
84  		return impl.addTouchMoveHandler(this, handler);
85  
86  	}
87  
88  	/*
89  	 * (non-Javadoc)
90  	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchCancelHandler(com.googlecode.mgwt.dom.client.event.touch.TouchCancelHandler)
91  	 */
92  	/** {@inheritDoc} */
93  	@Override
94  	public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
95  		return impl.addTouchCancelHandler(this, handler);
96  
97  	}
98  
99  	/*
100 	 * (non-Javadoc)
101 	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchEndHandler(com.googlecode.mgwt.dom.client.event.touch.TouchEndHandler)
102 	 */
103 	/** {@inheritDoc} */
104 	@Override
105 	public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
106 		return impl.addTouchEndHandler(this, handler);
107 
108 	}
109 
110 	/*
111 	 * (non-Javadoc)
112 	 * @see com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers#addTouchHandler(com.googlecode.mgwt.dom.client.event.touch.TouchHandler)
113 	 */
114 	/** {@inheritDoc} */
115 	@Override
116 	public HandlerRegistration addTouchHandler(TouchHandler handler) {
117 		HandlerRegistrationCollection handlerRegistrationCollection = new HandlerRegistrationCollection();
118 
119 		handlerRegistrationCollection.addHandlerRegistration(addTouchCancelHandler(handler));
120 		handlerRegistrationCollection.addHandlerRegistration(addTouchStartHandler(handler));
121 		handlerRegistrationCollection.addHandlerRegistration(addTouchEndHandler(handler));
122 		handlerRegistrationCollection.addHandlerRegistration(addTouchMoveHandler(handler));
123 		return handlerRegistrationCollection;
124 	}
125 
126 	/*
127 	 * (non-Javadoc)
128 	 * @see com.googlecode.mgwt.dom.client.recognizer.HasTapHandlers#addTapHandler(com.googlecode.mgwt.dom.client.recognizer.TapHandler)
129 	 */
130 	@Override
131 	public HandlerRegistration addTapHandler(TapHandler handler) {
132 		gestureUtility.ensureTapRecognizer();
133 		return addHandler(handler, TapEvent.getType());
134 	}
135 
136 	public HandlerRegistration addSwipeStartHandler(SwipeStartHandler handler) {
137 		gestureUtility.ensureSwipeRecognizer();
138 		return addHandler(handler, SwipeStartEvent.getType());
139 	}
140 
141 	public HandlerRegistration addSwipeMoveHandler(SwipeMoveHandler handler) {
142 		gestureUtility.ensureSwipeRecognizer();
143 		return addHandler(handler, SwipeMoveEvent.getType());
144 	}
145 
146 	public HandlerRegistration addSwipeEndHandler(SwipeEndHandler handler) {
147 		gestureUtility.ensureSwipeRecognizer();
148 		return addHandler(handler, SwipeEndEvent.getType());
149 	}
150 
151 	@Override
152 	public HandlerRegistration addPinchHandler(PinchHandler handler) {
153 		gestureUtility.ensurePinchRecognizer(this);
154 		return addHandler(handler, PinchEvent.getType());
155 	}
156 
157 	@Override
158 	public HandlerRegistration addLongTapHandler(LongTapHandler handler) {
159 		gestureUtility.ensureLongTapHandler();
160 		return addHandler(handler, LongTapEvent.getType());
161 	}
162 
163 }