KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > retouche > 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.retouche.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  * This file is originally from Retouche, the Java Support
31  * infrastructure in NetBeans. I have modified the file as little
32  * as possible to make merging Retouche fixes back as simple as
33  * possible.
34  * <p>
35  * Hack to invoke tooltip on given component on given position immediatelly
36  * at a request.
37  *
38  * XXX - hack is not reliable, could stop working in future JDK releases.
39  * Navigator should better handle tooltips totally itself,
40  * without Swing TooltipManager, to get rid of such hacks.
41  *
42  * @author Dafe Simonek
43  */

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

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

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