KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > gestures > GestureSupport


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.gestures;
13
14 import org.eclipse.swt.graphics.Rectangle;
15 import org.eclipse.swt.widgets.Display;
16 import org.eclipse.swt.widgets.Shell;
17 import org.eclipse.ui.keys.SWTKeySupport;
18
19 public final class GestureSupport {
20
21     public static void main(String JavaDoc[] args) {
22         final int HEIGHT = 300;
23         final int WIDTH = 400;
24         Display display = new Display();
25         Rectangle bounds = display.getBounds();
26         Shell shell = new Shell(display);
27
28         if (bounds.height >= HEIGHT && bounds.width >= WIDTH)
29             shell.setBounds(
30                 (bounds.x + bounds.width - WIDTH) / 2,
31                 (bounds.y + bounds.height - HEIGHT) / 2,
32                 WIDTH,
33                 HEIGHT);
34
35         shell.setText(GestureSupport.class.getName());
36         shell.open();
37         Capture capture = new Capture();
38
39         capture.addCaptureListener(new ICaptureListener() {
40             public void capture(CaptureEvent captureEvent) {
41                 System.out.println("Pen: " + captureEvent.getPen() + //$NON-NLS-1$
42
" Key Stroke: " + SWTKeySupport.convertAcceleratorToKeyStroke(captureEvent.getData()) + //$NON-NLS-1$
43
" Points: " + captureEvent.getPoints().length + //$NON-NLS-1$
44
" Gesture: " + recognize(captureEvent.getPoints(), 20)); //$NON-NLS-1$
45
}
46         });
47
48         capture.setControl(shell);
49
50         while (!shell.isDisposed())
51             if (!display.readAndDispatch())
52                 display.sleep();
53
54         display.dispose();
55     }
56
57     public static String JavaDoc recognize(Point[] points, int sensitivity) {
58         char stroke = '\0';
59         StringBuffer JavaDoc sequence = new StringBuffer JavaDoc();
60         int x0 = 0;
61         int y0 = 0;
62
63         for (int i = 0; i < points.length; i++) {
64             Point point = points[i];
65
66             if (i == 0) {
67                 x0 = point.getX();
68                 y0 = point.getY();
69                 continue;
70             }
71
72             int x1 = point.getX();
73             int y1 = point.getY();
74             int dx = (x1 - x0) / sensitivity;
75             int dy = (y1 - y0) / sensitivity;
76
77             if (dx != 0 || dy != 0) {
78                 if (dx > 0 && stroke != 'E')
79                     sequence.append(stroke = 'E');
80                 else if (dx < 0 && stroke != 'W')
81                     sequence.append(stroke = 'W');
82                 else if (dy > 0 && stroke != 'S')
83                     sequence.append(stroke = 'S');
84                 else if (dy < 0 && stroke != 'N')
85                     sequence.append(stroke = 'N');
86
87                 x0 = x1;
88                 y0 = y1;
89             }
90         }
91
92         return sequence.toString();
93     }
94
95     private GestureSupport() {
96     }
97 }
Popular Tags