KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > metal > MetalToolTipUI


1 /*
2  * @(#)MetalToolTipUI.java 1.28 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.metal;
9
10 import com.sun.java.swing.SwingUtilities2;
11 import java.awt.*;
12 import java.awt.event.*;
13 import javax.swing.*;
14 import javax.swing.BorderFactory JavaDoc;
15 import javax.swing.border.Border JavaDoc;
16 import javax.swing.plaf.*;
17 import javax.swing.plaf.basic.BasicToolTipUI JavaDoc;
18
19
20 /**
21  * A Metal L&F extension of BasicToolTipUI.
22  * <p>
23  * <strong>Warning:</strong>
24  * Serialized objects of this class will not be compatible with
25  * future Swing releases. The current serialization support is
26  * appropriate for short term storage or RMI between applications running
27  * the same version of Swing. As of 1.4, support for long term storage
28  * of all JavaBeans<sup><font size="-2">TM</font></sup>
29  * has been added to the <code>java.beans</code> package.
30  * Please see {@link java.beans.XMLEncoder}.
31  *
32  * @version 1.28 12/19/03
33  * @author Steve Wilson
34  */

35 public class MetalToolTipUI extends BasicToolTipUI JavaDoc {
36
37     static MetalToolTipUI JavaDoc sharedInstance = new MetalToolTipUI JavaDoc();
38     private Font smallFont;
39     // Refer to note in getAcceleratorString about this field.
40
private JToolTip tip;
41     public static final int padSpaceBetweenStrings = 12;
42     private String JavaDoc acceleratorDelimiter;
43
44     public MetalToolTipUI() {
45         super();
46     }
47
48     public static ComponentUI createUI(JComponent c) {
49         return sharedInstance;
50     }
51
52     public void installUI(JComponent c) {
53         super.installUI(c);
54         tip = (JToolTip)c;
55     Font f = c.getFont();
56     smallFont = new Font( f.getName(), f.getStyle(), f.getSize() - 2 );
57     acceleratorDelimiter = UIManager.getString( "MenuItem.acceleratorDelimiter" );
58     if ( acceleratorDelimiter == null ) { acceleratorDelimiter = "-"; }
59     }
60
61     public void uninstallUI(JComponent c) {
62         super.uninstallUI(c);
63         tip = null;
64     }
65
66     public void paint(Graphics g, JComponent c) {
67         JToolTip tip = (JToolTip)c;
68
69     super.paint(g, c);
70
71         Font font = c.getFont();
72         FontMetrics metrics = SwingUtilities2.getFontMetrics(c, g, font);
73     String JavaDoc keyText = getAcceleratorString(tip);
74     String JavaDoc tipText = tip.getTipText();
75     if (tipText == null) {
76         tipText = "";
77     }
78     if (! (keyText.equals(""))) { // only draw control key if there is one
79
g.setFont(smallFont);
80         g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
81         SwingUtilities2.drawString(tip, g, keyText,
82                          SwingUtilities2.stringWidth(
83                          tip, metrics, tipText) + padSpaceBetweenStrings,
84                  2 + metrics.getAscent());
85     }
86     }
87
88     public Dimension getPreferredSize(JComponent c) {
89     Dimension d = super.getPreferredSize(c);
90
91     String JavaDoc key = getAcceleratorString((JToolTip)c);
92     if (! (key.equals(""))) {
93             FontMetrics fm = c.getFontMetrics(smallFont);
94         d.width += SwingUtilities2.stringWidth(c, fm, key) +
95                             padSpaceBetweenStrings;
96     }
97         return d;
98     }
99
100     protected boolean isAcceleratorHidden() {
101         Boolean JavaDoc b = (Boolean JavaDoc)UIManager.get("ToolTip.hideAccelerator");
102         return b != null && b.booleanValue();
103     }
104
105     private String JavaDoc getAcceleratorString(JToolTip tip) {
106         this.tip = tip;
107
108         String JavaDoc retValue = getAcceleratorString();
109
110         this.tip = null;
111         return retValue;
112     }
113
114     // NOTE: This requires the tip field to be set before this is invoked.
115
// As MetalToolTipUI is shared between all JToolTips the tip field is
116
// set appropriately before this is invoked. Unfortunately this means
117
// that subclasses that randomly invoke this method will see varying
118
// results. If this becomes an issue, MetalToolTipUI should no longer be
119
// shared.
120
public String JavaDoc getAcceleratorString() {
121         if (tip == null || isAcceleratorHidden()) {
122             return "";
123         }
124         JComponent comp = tip.getComponent();
125     if (comp == null) {
126         return "";
127     }
128
129     KeyStroke[] keys;
130
131     if (comp instanceof JTabbedPane) {
132       TabbedPaneUI ui = ( (JTabbedPane)(comp) ).getUI();
133       if (ui instanceof MetalTabbedPaneUI JavaDoc) {
134         // if comp is instance of JTabbedPane and have 'metal' look-and-feel
135
// detect tab the mouse is over now
136
int rolloverTabIndex = ( (MetalTabbedPaneUI JavaDoc)ui ).getRolloverTabIndex();
137             if (rolloverTabIndex == -1)
138               keys = new KeyStroke[0];
139             else {
140               // detect mnemonic for this tab
141
int mnemonic = ((JTabbedPane)comp).getMnemonicAt(rolloverTabIndex);
142               if (mnemonic == -1)
143             keys = new KeyStroke[0];
144           else {
145             // and store it as mnemonic for the component
146
KeyStroke keyStroke = KeyStroke.getKeyStroke(mnemonic, Event.ALT_MASK);
147                 keys = new KeyStroke[1];
148                 keys[0] = keyStroke;
149               }
150             }
151           }
152           else
153             keys = comp.getRegisteredKeyStrokes();
154         }
155     else
156       keys = comp.getRegisteredKeyStrokes();
157
158     String JavaDoc controlKeyStr = "";
159
160     for (int i = 0; i < keys.length; i++) {
161       int mod = keys[i].getModifiers();
162       int condition = comp.getConditionForKeyStroke(keys[i]);
163
164       if ( condition == JComponent.WHEN_IN_FOCUSED_WINDOW )
165       {
166           controlKeyStr = KeyEvent.getKeyModifiersText(mod) +
167                               acceleratorDelimiter +
168                               KeyEvent.getKeyText(keys[i].getKeyCode());
169           break;
170       }
171     }
172
173     /* Special case for menu item since they do not register a
174        keyboard action for their mnemonics and they always use Alt */

175     if ( controlKeyStr.equals("") && comp instanceof JMenuItem )
176     {
177         int mnemonic = ((JMenuItem) comp).getMnemonic();
178         if ( mnemonic != 0 )
179         {
180             controlKeyStr =
181                     KeyEvent.getKeyModifiersText(KeyEvent.ALT_MASK) +
182                     acceleratorDelimiter + (char)mnemonic;
183         }
184     }
185
186     return controlKeyStr;
187     }
188
189 }
190
Popular Tags