root/incubator/SimpleGesture/WebContent/WEB-INF/src/com/itmill/incubator/simplegesture/SimpleGesture.java

Revision 7521, 5.2 KB (checked in by marc.englund@…, 11 months ago)

SimpleGesture?: initial commit.

Line 
1/*
2 * Copyright 2009 Marc Englund <marc.englundATitmill.com>
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
17package com.itmill.incubator.simplegesture;
18
19import java.lang.reflect.Method;
20import java.util.HashMap;
21import java.util.Map;
22import java.util.Set;
23
24import com.itmill.incubator.simplegesture.gwt.client.Common;
25import com.itmill.toolkit.terminal.PaintException;
26import com.itmill.toolkit.terminal.PaintTarget;
27import com.itmill.toolkit.ui.AbstractComponent;
28import com.itmill.toolkit.ui.Component;
29
30// TODO 'block' gesture
31// TODO per-gesture maxDistance
32public class SimpleGesture extends AbstractComponent {
33        private static final Method GESTURE_METHOD;
34        static {
35                try {
36                        GESTURE_METHOD = GestureListener.class.getDeclaredMethod("gesture",
37                                        new Class[] { GestureEvent.class });
38                } catch (final java.lang.NoSuchMethodException e) {
39                        // This should never happen
40                        throw new java.lang.RuntimeException(
41                                        "Internal error finding methods in SimpleGesture");
42                }
43        }
44
45        private Component target = null;
46        private int maxDistance = Common.MAXDISTANCE;
47        private boolean recordMode = false;
48        private boolean normalizing = false;
49        private HashMap<String, Object> gestures = new HashMap<String, Object>();
50
51        public SimpleGesture() {
52                this(null);
53        }
54
55        public SimpleGesture(Component target) {
56                this.target = target;
57        }
58
59        public boolean isRecordMode() {
60                return recordMode;
61        }
62
63        public void setRecordMode(boolean recordMode) {
64                this.recordMode = recordMode;
65                requestRepaint();
66        }
67
68        public boolean isNormalizing() {
69                return normalizing;
70        }
71
72        public void setNormalizing(boolean normalizing) {
73                this.normalizing = normalizing;
74        }
75
76        public void addGesture(String gesture) {
77               
78        }
79
80        public void addGesture(String gesture, Object data) {
81                addGesture(gesture, data, maxDistance);
82        }
83       
84        public void addGesture(String gesture, Object data, int maxDistance) {
85                if (gesture == null) {
86                        return;
87                }
88                gestures.put(gesture, data);
89                requestRepaint();
90        }
91
92        public void removeGesture(String gesture) {
93                gestures.remove(gesture);
94                requestRepaint();
95        }
96       
97        public Set getAllGestures() {
98                return this.gestures.keySet();
99        }
100       
101        public void clear() {
102                this.gestures.clear();
103                requestRepaint();
104        }
105       
106        public void setDefaultMaxDistance(int maxDistance) {
107                this.maxDistance = maxDistance;
108                requestRepaint();
109        }
110       
111        public int getDefaultMaxDistance() {
112                return this.maxDistance;
113        }
114
115        @Override
116        public String getTag() {
117                return Common.TAG;
118        }
119
120        @Override
121        public void paintContent(PaintTarget target) throws PaintException {
122                super.paintContent(target);
123                target.addAttribute(Common.ATTR_MAXDISTANCE, maxDistance);
124                target.addAttribute(Common.ATTR_RECORD, recordMode);
125                target.addAttribute(Common.ATTR_NORMALIZE, normalizing);
126                if (this.target != null) {
127                        target.paintReference(this.target, Common.TARGET_REF);
128                }
129                target.addVariable(this, Common.VAR_MATCHED, "");
130                target.addVariable(this, Common.VAR_MOVED, "");
131                target.addVariable(this, Common.VAR_DISTANCE, "");
132                target.startTag(Common.GESTURES_TAG);
133                for (String g : gestures.keySet()) {
134                        target.startTag(Common.GESTURE_TAG);
135                        target.addAttribute(Common.GESTURE_ATTR_MOVES, g);
136                        target.endTag(Common.GESTURE_TAG);
137                }
138                target.endTag(Common.GESTURES_TAG);
139        }
140
141        @Override
142        public void changeVariables(Object source, Map variables) {
143                super.changeVariables(source, variables);
144                if (variables.containsKey(Common.VAR_MOVED)) {
145                        String moved = (String) variables.get(Common.VAR_MOVED);
146                        String matched = (String) variables.get(Common.VAR_MATCHED);
147                        int distance = (Integer)variables.get(Common.VAR_DISTANCE);
148                        if (recordMode || gestures.containsKey(matched)) {
149                                Object data = gestures.get(matched);
150                                GestureEvent evt = new GestureEvent(this, matched, moved,
151                                                distance, data);
152                                fireEvent(evt);
153                        }
154                }
155        }
156
157        public void addListener(GestureListener listener) {
158                super.addListener(GestureEvent.class, listener, GESTURE_METHOD);
159        }
160
161        public void removeListener(GestureListener listener) {
162                super.removeListener(GestureEvent.class, listener, GESTURE_METHOD);
163        }
164       
165        public class GestureEvent extends Event {
166                private static final long serialVersionUID = -4766249038615119101L;
167                private String matched;
168                private String moved;
169                private int distance;
170                private Object data;
171
172                public GestureEvent(SimpleGesture source, String matched, String moved,
173                                int distance, Object data) {
174                        super(source);
175                        this.data = data;
176                        this.matched = matched;
177                        this.moved = moved;
178                        this.distance = distance;
179                }
180
181                public String getMatched() {
182                        return matched;
183                }
184
185                public String getMoved() {
186                        return moved;
187                }
188
189                public int getDistance() {
190                        return distance;
191                }
192
193                public Object getData() {
194                        return data;
195                }
196
197        }
198
199        public interface GestureListener {
200                public void gesture(GestureEvent event);
201        }
202
203}
Note: See TracBrowser for help on using the browser.