KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.swt.events.MouseEvent;
19 import org.eclipse.swt.events.MouseListener;
20 import org.eclipse.swt.events.MouseMoveListener;
21 import org.eclipse.swt.widgets.Control;
22
23 public final class Capture {
24
25     private List JavaDoc captureListeners;
26     private boolean capturing;
27     private Control control;
28     private int data;
29
30     private MouseListener mouseListener = new MouseListener() {
31         public void mouseDoubleClick(MouseEvent mouseEvent) {
32         }
33
34         public void mouseDown(MouseEvent mouseEvent) {
35             if (!capturing) {
36                 capturing = true;
37                 data = mouseEvent.stateMask;
38                 pen = mouseEvent.button;
39                 points.clear();
40                 points.add(new Point(mouseEvent.x, mouseEvent.y));
41                 control.addMouseMoveListener(mouseMoveListener);
42             }
43         }
44
45         public void mouseUp(MouseEvent mouseEvent) {
46             if (capturing && mouseEvent.button == pen) {
47                 control.removeMouseMoveListener(mouseMoveListener);
48                 points.add(new Point(mouseEvent.x, mouseEvent.y));
49                 CaptureEvent captureEvent =
50                     CaptureEvent.create(
51                         data,
52                         pen,
53                         (Point[]) points.toArray(new Point[points.size()]));
54                 capturing = false;
55                 data = 0;
56                 pen = 0;
57                 points.clear();
58                 Iterator JavaDoc iterator = captureListeners.iterator();
59
60                 while (iterator.hasNext())
61                     ((ICaptureListener) iterator.next()).capture(captureEvent);
62             }
63         }
64     };
65
66     private MouseMoveListener mouseMoveListener = new MouseMoveListener() {
67         public void mouseMove(MouseEvent mouseEvent) {
68             if (capturing)
69                 points.add(new Point(mouseEvent.x, mouseEvent.y));
70         }
71     };
72     private int pen;
73     private List JavaDoc points = new ArrayList JavaDoc();
74
75     public Capture() {
76     }
77
78     public void addCaptureListener(ICaptureListener captureListener) {
79         if (captureListener == null)
80             throw new NullPointerException JavaDoc();
81
82         if (captureListeners == null)
83             captureListeners = new ArrayList JavaDoc();
84
85         if (!captureListeners.contains(captureListener))
86             captureListeners.add(captureListener);
87     }
88
89     public Control getControl() {
90         return control;
91     }
92
93     public void removeCaptureListener(ICaptureListener captureListener) {
94         if (captureListener == null)
95             throw new NullPointerException JavaDoc();
96
97         if (captureListeners != null)
98             captureListeners.remove(captureListener);
99     }
100
101     public void setControl(Control control) {
102         if (this.control != control) {
103             if (this.control != null) {
104                 control.removeMouseMoveListener(mouseMoveListener);
105                 control.removeMouseListener(mouseListener);
106             }
107
108             this.control = control;
109             capturing = false;
110             data = 0;
111             pen = 0;
112             points.clear();
113
114             if (this.control != null)
115                 control.addMouseListener(mouseListener);
116         }
117     }
118 }
119
Popular Tags