KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > MouseUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.awt;
20
21 import java.awt.event.MouseAdapter JavaDoc;
22 import java.awt.event.MouseEvent JavaDoc;
23
24 import javax.swing.SwingUtilities JavaDoc;
25
26
27 /** A class that contains a set of utility classes and methods
28 * around mouse events and processing.
29 *
30 * @author Ian Formanek
31 */

32 public class MouseUtils extends Object JavaDoc {
33     private static int DOUBLE_CLICK_DELTA = 300;
34
35     /** variable for double click */
36     private static int tempx = 0;
37     private static int tempy = 0;
38     private static long temph = 0;
39     private static int tempm = 0;
40     private static MouseEvent JavaDoc tempe;
41
42     /** Determines if the event is originated from the right mouse button
43     * @param e the MouseEvent
44     * @return true if the event is originated from the right mouse button, false otherwise
45     * @deprecated Offers no advantages over the standard {@link SwingUtilities#isRightMouseButton}.
46     */

47     public static boolean isRightMouseButton(MouseEvent JavaDoc e) {
48         return SwingUtilities.isRightMouseButton(e);
49     }
50
51     /** Determines if the event is originated from a left mouse button
52     * @param e the MouseEvent
53     * @return true if the event is originated from the left mouse button, false otherwise
54     * @deprecated Offers no advantages over the standard {@link SwingUtilities#isLeftMouseButton}.
55     */

56     public static boolean isLeftMouseButton(MouseEvent JavaDoc e) {
57         return javax.swing.SwingUtilities.isLeftMouseButton(e);
58     }
59
60     /** Returns true if parametr is a 'doubleclick event'
61     * @param e MouseEvent
62     * @return true if the event is a doubleclick
63     */

64     public static boolean isDoubleClick(MouseEvent JavaDoc e) {
65         // even number of clicks is considered like doubleclick
66
// it works as well as 'normal testing against 2'
67
// but on solaris finaly works and on Win32 works better
68
//System.out.println ("Click COunt: "+e.getClickCount ()); // NOI18N
69
// If you don't do this, then if anyone calls isDoubleClick from
70
// say a mouseReleased method, then the immediately following mouseClicked
71
// method from a single mouse click will give isDoubleClick=true
72
if ((e.getID() != MouseEvent.MOUSE_CLICKED) || (e.getClickCount() == 0)) {
73             return false;
74         }
75
76         return ((e.getClickCount() % 2) == 0) || isDoubleClickImpl(e);
77     }
78
79     /** Tests the positions.
80     */

81     private static boolean isDoubleClickImpl(MouseEvent JavaDoc e) {
82         int x = e.getX();
83         int y = e.getY();
84         long h = e.getWhen();
85         int m = e.getModifiers();
86
87         //System.out.println ("When:: "+h); // NOI18N
88
// same position at short time
89
if ((tempx == x) && (tempy == y) && ((h - temph) < DOUBLE_CLICK_DELTA) && (
90             // Without this, calling isDoubleClick() twice on the same
91
// mouse event will return true the second time!
92
e != tempe) && (m == tempm)) {
93             // OK forget all
94
tempx = 0;
95             tempy = 0;
96             temph = 0;
97             tempm = 0;
98             tempe = null;
99
100             return true;
101         } else {
102             // remember
103
tempx = x;
104             tempy = y;
105             temph = h;
106             tempm = m;
107             tempe = e;
108
109             return false;
110         }
111     }
112
113     // ---------------------------------------------------------------------------
114
// Inner classes
115

116     /** The PopupMouseAdapter provides safe way to implement popup menu invocation
117      * mechanism. It should be used instead of invoking the popup in
118      * mouseClicked because the mouseClicked does not work as "often" as
119      * it should (i.e. sometimes it is not called).
120      * PopupMouseAdapter delegates to isPopupTrigger to get correct popup
121      * menu invocation gesture. Clients are supposed to extend this class and
122      * implement showPopup method by adding code that shows popup menu properly.<br>
123      *
124      * Please note that older implementation which used treshold is now
125      * deprecated, please use default constructor.
126     */

127     public static abstract class PopupMouseAdapter extends MouseAdapter JavaDoc {
128         /** @deprecated Obsoleted as of 3.4, PopupMouseAdapter now uses isPopupTrigger properly.
129             Threshold does nothing, please use default constructor without treshold.
130          */

131         public static final int DEFAULT_THRESHOLD = 5;
132
133         /** Creates a new PopupMouseAdapter with specified threshold
134          * @param threshold The threshold to be used
135          * @deprecated Obsoleted as of 3.4, by class rewrite to use isPopupTrigger.
136          * This constructor now just delegates to super constructor, please use
137          * default constructor instead.
138          */

139         public PopupMouseAdapter(int threshold) {
140             this();
141         }
142
143         /** Constructs PopupMouseAdapter. Just delegates to super constructor */
144         public PopupMouseAdapter() {
145             super();
146         }
147
148         public void mousePressed(MouseEvent JavaDoc e) {
149             maybePopup(e);
150         }
151
152         public void mouseReleased(MouseEvent JavaDoc e) {
153             maybePopup(e);
154         }
155
156         private void maybePopup(MouseEvent JavaDoc e) {
157             if (e.isPopupTrigger()) {
158                 showPopup(e);
159             }
160         }
161
162         /** Called when the sequnce of mouse events should lead to actual
163         * showing of the popup menu.
164         * Should be redefined to show the menu.
165         * param evt The mouse release event - should be used to obtain the
166         * position of the popup menu
167         */

168         abstract protected void showPopup(MouseEvent JavaDoc evt);
169     }
170 }
171
Popular Tags