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.BrowserService;
35  import org.utgenome.gwt.utgb.client.util.BrowserInfo;
36  
37  import com.google.gwt.event.shared.HandlerRegistration;
38  import com.google.gwt.user.client.ui.FocusPanel;
39  
40  
41  /**
42   * Extension of focus panel to add touch handlers.
43   *
44   * @author amoffat Alex Moffat
45   */
46  public class TouchableFocusPanel extends FocusPanel implements HasTouchHandlers {
47  
48  	private static interface Registrer {
49  	    public HandlerRegistration addTouchStartHandler(TouchStartHandler handler);
50  	    public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler);
51  	    public HandlerRegistration addTouchEndHandler(TouchEndHandler handler);
52  	    public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler);
53  	}
54  	
55  	
56  	private class DefaultRegistrerImpl implements Registrer {
57  	    public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
58  	    	return addDomHandler(handler, TouchStartEvent.getType());
59  	    }
60  
61  	    public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
62  	        return addDomHandler(handler, TouchMoveEvent.getType());
63  	    }
64  
65  	    public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
66  	        return addDomHandler(handler, TouchEndEvent.getType());
67  	    }
68  
69  	    public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
70  	        return addDomHandler(handler, TouchCancelEvent.getType());
71  	    }
72  	}
73  
74  	private class NonIPadRegistrerImpl implements Registrer {
75  	    public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
76  	    	return null;
77  	    }
78  
79  	    public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
80  	        return null;
81  	    }
82  
83  	    public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
84  	        return null;
85  	    }
86  
87  	    public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
88  	        return null;
89  	    }
90  	}
91  
92  	private Registrer impl;
93  	
94  	{
95  		if(BrowserInfo.isMobileSafari()) 
96  			impl = new DefaultRegistrerImpl();
97  		else
98  			impl = new NonIPadRegistrerImpl();
99  	}
100 	
101     public HandlerRegistration addTouchStartHandler(TouchStartHandler handler) {
102     	return impl.addTouchStartHandler(handler);
103     }
104 
105     public HandlerRegistration addTouchMoveHandler(TouchMoveHandler handler) {
106         return impl.addTouchMoveHandler(handler);
107     }
108 
109     public HandlerRegistration addTouchEndHandler(TouchEndHandler handler) {
110         return impl.addTouchEndHandler(handler);
111     }
112 
113     public HandlerRegistration addTouchCancelHandler(TouchCancelHandler handler) {
114         return impl.addTouchCancelHandler(handler);
115     }
116 }