KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > navigation > base > TooltipHack


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
20 package org.netbeans.modules.java.navigation.base;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import javax.swing.Timer JavaDoc;
27 import javax.swing.ToolTipManager JavaDoc;
28
29 /**
30  * Hack to invoke tooltip on given component on given position immediatelly
31  * at a request.
32  *
33  * XXX - hack is not reliable, could stop working in future JDK releases.
34  * Navigator should better handle tooltips totally itself,
35  * without Swing TooltipManager, to get rid of such hacks.
36  *
37  * @author Dafe Simonek
38  */

39 public final class TooltipHack implements ActionListener JavaDoc {
40
41     private static TooltipHack instance;
42     
43     /** holds previous dismiss tooltip value */
44     private static int prevDismiss = -1;
45     
46     private TooltipHack() {
47     }
48
49     /** Hack to invoke tooltip on given JComponent, with given dismiss delay.
50      * Triggers <br>
51      * <code>comp.getToolTipText(MouseEvent)</code> and
52      * <code>comp.getToolTipLocation(MouseEvent)</code> with fake mousemoved
53      * MouseEvent, set to given coordinates.
54      */

55     public static void invokeTip (JComponent JavaDoc comp, int x, int y, int dismissDelay) {
56         final ToolTipManager JavaDoc ttm = ToolTipManager.sharedInstance();
57         final int prevInit = ttm.getInitialDelay();
58         prevDismiss = ttm.getDismissDelay();
59         ttm.setInitialDelay(0);
60         ttm.setDismissDelay(dismissDelay);
61         
62         MouseEvent JavaDoc fakeEvt = new MouseEvent JavaDoc(
63                 comp, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(),
64                 0, x, y, 0, false);
65         ttm.mouseMoved(fakeEvt);
66         
67         ttm.setInitialDelay(prevInit);
68         Timer JavaDoc timer = new Timer JavaDoc(20, instance());
69         timer.setRepeats(false);
70         timer.start();
71     }
72     
73     /** impl of ActionListener, reacts on timer and restores Dismiss value.
74      * Don't call from outside classes.
75      */

76     public void actionPerformed(ActionEvent JavaDoc e) {
77         if (prevDismiss > 0) {
78             ToolTipManager.sharedInstance().setDismissDelay(prevDismiss);
79             prevDismiss = -1;
80         }
81     }
82     
83     private static TooltipHack instance () {
84         if (instance == null) {
85             instance = new TooltipHack();
86         }
87         return instance;
88     }
89     
90 }
91
Popular Tags