01: /*
02: * Copyright (C) 2004 TiongHiang Lee
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: *
18: * Email: thlee@onemindsoft.org
19: */
20:
21: package org.onemind.swingweb.client.gwt.widget;
22:
23: import com.google.gwt.user.client.Timer;
24: import com.google.gwt.user.client.ui.*;
25:
26: public class ClickDispatch {
27:
28: private static final int CLICK_TIME = 200;//ms
29:
30: private Timer _singleClickTimer = new Timer() {
31:
32: public void run() {
33: fireSingleClick();
34: }
35: };
36:
37: private int clickCount = 0;
38:
39: private ClickListenerCollection _clickListeners = new ClickListenerCollection();
40:
41: private DoubleClickListenerCollection _doubleClickListeners = new DoubleClickListenerCollection();
42:
43: private Widget _sender;
44:
45: /**
46: * Constructor
47: */
48: public ClickDispatch() {
49: super ();
50: }
51:
52: private void fireSingleClick() {
53: _singleClickTimer.cancel();
54: clickCount = 0;
55: _clickListeners.fireClick(_sender);
56: }
57:
58: public void addClickListener(ClickListener listener) {
59: _clickListeners.add(listener);
60: }
61:
62: public void removeClickListener(ClickListener listener) {
63: _clickListeners.remove(listener);
64: }
65:
66: public void addDoubleClickListener(DoubleClickListener listener) {
67: _doubleClickListeners.add(listener);
68: }
69:
70: public void removeDoubleClickListener(DoubleClickListener listener) {
71: _doubleClickListeners.remove(listener);
72: }
73:
74: public void fireDoubleClick(Widget widget) {
75: _doubleClickListeners.fireDoubleClick(widget);
76: }
77:
78: public void processClick(Widget widget) {
79: _sender = widget;
80: // TODO Auto-generated method stub
81: clickCount++;
82: if (clickCount >= 2) {
83: System.out.println("detected double click");
84: _singleClickTimer.cancel();
85: clickCount = 0;
86: _doubleClickListeners.fireDoubleClick(widget);
87: } else {
88: System.out.println("schedule single click");
89: _singleClickTimer.scheduleRepeating(CLICK_TIME);
90: }
91: }
92: }
|