View Javadoc
1   /*
2    * Copyright 2012 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.user.client.ui.UIObject;
19  import com.googlecode.mgwt.dom.client.event.touch.HasTouchHandlers;
20  import com.googlecode.mgwt.dom.client.recognizer.TapRecognizer;
21  import com.googlecode.mgwt.dom.client.recognizer.longtap.LongTapRecognizer;
22  import com.googlecode.mgwt.dom.client.recognizer.pinch.PinchRecognizer;
23  import com.googlecode.mgwt.dom.client.recognizer.pinch.UIObjectToOffsetProvider;
24  import com.googlecode.mgwt.dom.client.recognizer.swipe.SwipeRecognizer;
25  
26  /**
27   * {@link GestureUtility} is a utility class for adding standard recognizers to a widget
28   * 
29   * @author Daniel Kurka
30   * 
31   */
32  public class GestureUtility {
33  	private TapRecognizer tapRecognizer;
34  	private final HasTouchHandlers source;
35  	private LongTapRecognizer longTapRecognizer;
36  	private SwipeRecognizer swipeRecognizer;
37  	private PinchRecognizer pinchRecognizer;
38  
39    /**
40     * Construct a {@link GestureUtility} for a given source
41     * 
42     * @param source the source to use
43     */
44  	public GestureUtility(HasTouchHandlers source) {
45  		assert source != null;
46  		this.source = source;
47  	}
48  
49    /**
50     * ensure that there is a registered {@link TapRecognizer} on the source
51     */
52  	public void ensureTapRecognizer() {
53  		if (tapRecognizer != null)
54  			return;
55  
56  		tapRecognizer = new TapRecognizer(source);
57  		source.addTouchHandler(tapRecognizer);
58  	}
59  
60    /**
61     * ensure that there is a registered {@link LongTapRecognizer} on the source
62     */
63  	public void ensureLongTapRecognizer() {
64  		if (longTapRecognizer != null) {
65  			return;
66  		}
67  
68  		longTapRecognizer = new LongTapRecognizer(source);
69  		source.addTouchHandler(longTapRecognizer);
70  
71  	}
72  
73    /**
74     * ensure that there is a registered {@link SwipeRecognizer} on the source
75     */
76  	public void ensureSwipeRecognizer() {
77  		if (swipeRecognizer != null) {
78  			return;
79  		}
80  
81  		swipeRecognizer = new SwipeRecognizer(source);
82  		source.addTouchHandler(swipeRecognizer);
83  
84  	}
85  
86    /**
87     * ensure that there is a registered {@link PinchRecognizer} on the source
88     * 
89     * @param object the {@link UIObject} that is used for offset
90     * 
91     */
92  	public void ensurePinchRecognizer(UIObject object) {
93  		if (pinchRecognizer != null) {
94  			return;
95  		}
96  
97  		pinchRecognizer = new PinchRecognizer(source, new UIObjectToOffsetProvider(object));
98  		source.addTouchHandler(pinchRecognizer);
99  
100 	}
101 
102   /**
103    * ensure that there is a registered {@link LongTapRecognizer} on the source
104    */
105 	public void ensureLongTapHandler() {
106 		if (longTapRecognizer != null) {
107 			return;
108 		}
109 
110 		longTapRecognizer = new LongTapRecognizer(source);
111 		source.addTouchHandler(longTapRecognizer);
112 
113 	}
114 }