View Javadoc

1   /*
2    * Copyright (c) 2010 Alex Moffat
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   * THE SOFTWARE.
21   */
22  
23  package org.utgenome.gwt.ipad.client;
24  
25  import org.utgenome.gwt.ipad.event.HasTouchHandlers;
26  import org.utgenome.gwt.ipad.event.TouchCancelEvent;
27  import org.utgenome.gwt.ipad.event.TouchCancelHandler;
28  import org.utgenome.gwt.ipad.event.TouchEndEvent;
29  import org.utgenome.gwt.ipad.event.TouchEndHandler;
30  import org.utgenome.gwt.ipad.event.TouchMoveEvent;
31  import org.utgenome.gwt.ipad.event.TouchMoveHandler;
32  import org.utgenome.gwt.ipad.event.TouchStartEvent;
33  import org.utgenome.gwt.ipad.event.TouchStartHandler;
34  import org.utgenome.gwt.utgb.client.util.BrowserInfo;
35  
36  import com.google.gwt.event.shared.HandlerRegistration;
37  import com.google.gwt.user.client.ui.Composite;
38  
39  
40  /**
41   * Extension of composite to add touch handlers.
42   *
43   * @author amoffat Alex Moffat
44   */
45  public class TouchableComposite extends Composite implements HasTouchHandlers {
46  
47  	private static interface Registrer {
48  	    public HandlerRegistration addTouchStartHandler(TouchStartHandler handler);
49  	    public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler);
50  	    public HandlerRegistration addTouchEndHandler(TouchEndHandler handler);
51  	    public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler);
52  	}
53  	
54  	
55  	private class DefaultRegistrerImpl implements Registrer {
56  	    public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
57  	    	return addDomHandler(handler, TouchStartEvent.getType());
58  	    }
59  
60  	    public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
61  	        return addDomHandler(handler, TouchMoveEvent.getType());
62  	    }
63  
64  	    public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
65  	        return addDomHandler(handler, TouchEndEvent.getType());
66  	    }
67  
68  	    public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
69  	        return addDomHandler(handler, TouchCancelEvent.getType());
70  	    }
71  	}
72  
73  	private class NonIPadRegistrerImpl implements Registrer {
74  	    public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
75  	    	return null;
76  	    }
77  
78  	    public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
79  	        return null;
80  	    }
81  
82  	    public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
83  	        return null;
84  	    }
85  
86  	    public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
87  	        return null;
88  	    }
89  	}
90  
91  	private Registrer impl;
92  	
93  	{
94  		if(BrowserInfo.isMobileSafari()) 
95  			impl = new DefaultRegistrerImpl();
96  		else
97  			impl = new NonIPadRegistrerImpl();
98  	}
99  	
100     public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
101     	return impl.addTouchStartHandler(handler);
102     }
103 
104     public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
105         return impl.addTouchMoveHandler(handler);
106     }
107 
108     public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
109         return impl.addTouchEndHandler(handler);
110     }
111 
112     public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
113         return impl.addTouchCancelHandler(handler);
114     }
115 }