KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicToolTipUI


1 /*
2  * @(#)BasicToolTipUI.java 1.41 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.basic;
9
10 import com.sun.java.swing.SwingUtilities2;
11 import java.awt.*;
12 import java.beans.PropertyChangeEvent JavaDoc;
13 import java.beans.PropertyChangeListener JavaDoc;
14
15 import javax.swing.*;
16 import javax.swing.BorderFactory JavaDoc;
17 import javax.swing.border.Border JavaDoc;
18 import javax.swing.plaf.ToolTipUI JavaDoc;
19 import javax.swing.plaf.ComponentUI JavaDoc;
20 import javax.swing.plaf.UIResource JavaDoc;
21 import javax.swing.text.View JavaDoc;
22
23
24 /**
25  * Standard tool tip L&F.
26  * <p>
27  *
28  * @version 1.41 12/19/03
29  * @author Dave Moore
30  */

31 public class BasicToolTipUI extends ToolTipUI JavaDoc
32 {
33     static BasicToolTipUI JavaDoc sharedInstance = new BasicToolTipUI JavaDoc();
34     /**
35      * Global <code>PropertyChangeListener</code> that
36      * <code>createPropertyChangeListener</code> returns.
37      */

38     private static PropertyChangeListener JavaDoc sharedPropertyChangedListener;
39
40     private PropertyChangeListener JavaDoc propertyChangeListener;
41
42     public static ComponentUI JavaDoc createUI(JComponent c) {
43         return sharedInstance;
44     }
45
46     public BasicToolTipUI() {
47         super();
48     }
49
50     public void installUI(JComponent c) {
51     installDefaults(c);
52     installComponents(c);
53     installListeners(c);
54     }
55
56     public void uninstallUI(JComponent c) {
57     // REMIND: this is NOT getting called
58
uninstallDefaults(c);
59     uninstallComponents(c);
60     uninstallListeners(c);
61     }
62
63     protected void installDefaults(JComponent c){
64     LookAndFeel.installColorsAndFont(c, "ToolTip.background",
65                      "ToolTip.foreground",
66                      "ToolTip.font");
67         LookAndFeel.installProperty(c, "opaque", Boolean.TRUE);
68         componentChanged(c);
69     }
70     
71    protected void uninstallDefaults(JComponent c){
72     LookAndFeel.uninstallBorder(c);
73     }
74
75     /* Unfortunately this has to remain private until we can make API additions.
76      */

77     private void installComponents(JComponent c){
78     BasicHTML.updateRenderer(c, ((JToolTip)c).getTipText());
79     }
80      
81     /* Unfortunately this has to remain private until we can make API additions.
82      */

83     private void uninstallComponents(JComponent c){
84     BasicHTML.updateRenderer(c, "");
85     }
86
87     protected void installListeners(JComponent c) {
88     propertyChangeListener = createPropertyChangeListener(c);
89     
90         c.addPropertyChangeListener(propertyChangeListener);
91     }
92
93     protected void uninstallListeners(JComponent c) {
94         c.removePropertyChangeListener(propertyChangeListener);
95
96     propertyChangeListener = null;
97     }
98
99     /* Unfortunately this has to remain private until we can make API additions.
100      */

101     private PropertyChangeListener JavaDoc createPropertyChangeListener(JComponent c) {
102         if (sharedPropertyChangedListener == null) {
103             sharedPropertyChangedListener = new PropertyChangeHandler();
104         }
105         return sharedPropertyChangedListener;
106     }
107
108     public void paint(Graphics g, JComponent c) {
109         Font font = c.getFont();
110         FontMetrics metrics = SwingUtilities2.getFontMetrics(c, g, font);
111         Dimension size = c.getSize();
112         if (c.isOpaque()) {
113             g.setColor(c.getBackground());
114             g.fillRect(0, 0, size.width, size.height);
115         }
116         g.setColor(c.getForeground());
117         g.setFont(font);
118     // fix for bug 4153892
119
String JavaDoc tipText = ((JToolTip)c).getTipText();
120     if (tipText == null) {
121         tipText = "";
122     }
123
124         Insets insets = c.getInsets();
125         Rectangle paintTextR = new Rectangle(
126             insets.left,
127             insets.top,
128             size.width - (insets.left + insets.right),
129             size.height - (insets.top + insets.bottom));
130     View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
131     if (v != null) {
132         v.paint(g, paintTextR);
133     } else {
134         SwingUtilities2.drawString(c, g, tipText, paintTextR.x + 3,
135                                   paintTextR.y + metrics.getAscent());
136     }
137     }
138
139     public Dimension getPreferredSize(JComponent c) {
140         Font font = c.getFont();
141         FontMetrics fm = c.getFontMetrics(font);
142     Insets insets = c.getInsets();
143
144     Dimension prefSize = new Dimension(insets.left+insets.right,
145                        insets.top+insets.bottom);
146     String JavaDoc text = ((JToolTip)c).getTipText();
147
148     if ((text == null) || text.equals("")) {
149             text = "";
150         }
151         else {
152         View JavaDoc v = (c != null) ? (View JavaDoc) c.getClientProperty("html") : null;
153         if (v != null) {
154         prefSize.width += (int) v.getPreferredSpan(View.X_AXIS);
155         prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
156         } else {
157         prefSize.width += SwingUtilities2.stringWidth(c,fm,text) + 6;
158         prefSize.height += fm.getHeight();
159         }
160         }
161     return prefSize;
162     }
163
164     public Dimension getMinimumSize(JComponent c) {
165     Dimension d = getPreferredSize(c);
166     View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
167     if (v != null) {
168         d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
169     }
170     return d;
171     }
172
173     public Dimension getMaximumSize(JComponent c) {
174     Dimension d = getPreferredSize(c);
175     View JavaDoc v = (View JavaDoc) c.getClientProperty(BasicHTML.propertyKey);
176     if (v != null) {
177         d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
178     }
179     return d;
180     }
181
182     /**
183      * Invoked when the <code>JCompoment</code> associated with the
184      * <code>JToolTip</code> has changed, or at initialization time. This
185      * should update any state dependant upon the <code>JComponent</code>.
186      *
187      * @param c the JToolTip the JComponent has changed on.
188      */

189     private void componentChanged(JComponent c) {
190         JComponent comp = ((JToolTip)c).getComponent();
191
192         if (comp != null && !(comp.isEnabled())) {
193             // For better backward compatability, only install inactive
194
// properties if they are defined.
195
if (UIManager.getBorder("ToolTip.borderInactive") != null) {
196                 LookAndFeel.installBorder(c, "ToolTip.borderInactive");
197             }
198             else {
199                 LookAndFeel.installBorder(c, "ToolTip.border");
200             }
201             if (UIManager.getColor("ToolTip.backgroundInactive") != null) {
202                 LookAndFeel.installColors(c,"ToolTip.backgroundInactive",
203                                           "ToolTip.foregroundInactive");
204             }
205             else {
206                 LookAndFeel.installColors(c,"ToolTip.background",
207                                           "ToolTip.foreground");
208             }
209         } else {
210             LookAndFeel.installBorder(c, "ToolTip.border");
211             LookAndFeel.installColors(c, "ToolTip.background",
212                                       "ToolTip.foreground");
213         }
214     }
215
216
217     private static class PropertyChangeHandler implements
218                                  PropertyChangeListener JavaDoc {
219     public void propertyChange(PropertyChangeEvent JavaDoc e) {
220         String JavaDoc name = e.getPropertyName();
221         if (name.equals("tiptext") || "font".equals(name) ||
222                 "foreground".equals(name)) {
223         // remove the old html view client property if one
224
// existed, and install a new one if the text installed
225
// into the JLabel is html source.
226
JToolTip tip = ((JToolTip) e.getSource());
227         String JavaDoc text = tip.getTipText();
228         BasicHTML.updateRenderer(tip, text);
229         }
230             else if ("component".equals(name)) {
231         JToolTip tip = ((JToolTip) e.getSource());
232
233                 if (tip.getUI() instanceof BasicToolTipUI JavaDoc) {
234                     ((BasicToolTipUI JavaDoc)tip.getUI()).componentChanged(tip);
235                 }
236             }
237     }
238     }
239 }
240
Popular Tags