KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > SynthToolTipUI


1 /*
2  * @(#)SynthToolTipUI.java 1.9 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.synth;
9
10 import java.awt.*;
11 import java.beans.PropertyChangeEvent JavaDoc;
12 import java.beans.PropertyChangeListener JavaDoc;
13
14 import javax.swing.*;
15 import javax.swing.plaf.basic.BasicHTML JavaDoc;
16 import javax.swing.plaf.basic.BasicToolTipUI JavaDoc;
17 import javax.swing.plaf.ComponentUI JavaDoc;
18 import javax.swing.text.View JavaDoc;
19 import sun.swing.plaf.synth.SynthUI;
20
21
22 /**
23  * Synth's ToolTipUI.
24  *
25  * @version 1.9, 12/19/03
26  * @author Joshua Outwater
27  */

28 class SynthToolTipUI extends BasicToolTipUI JavaDoc implements PropertyChangeListener JavaDoc,
29                SynthUI {
30     private SynthStyle JavaDoc style;
31
32
33     public static ComponentUI JavaDoc createUI(JComponent c) {
34         return new SynthToolTipUI JavaDoc();
35     }
36
37     protected void installDefaults(JComponent c) {
38         updateStyle(c);
39     }
40
41     private void updateStyle(JComponent c) {
42         SynthContext JavaDoc context = getContext(c, ENABLED);
43         style = SynthLookAndFeel.updateStyle(context, this);
44         context.dispose();
45     }
46     
47     protected void uninstallDefaults(JComponent c) {
48         SynthContext JavaDoc context = getContext(c, ENABLED);
49         style.uninstallDefaults(context);
50         context.dispose();
51         style = null;
52     }
53
54     protected void installListeners(JComponent c) {
55         c.addPropertyChangeListener(this);
56     }
57
58     protected void uninstallListeners(JComponent c) {
59         c.removePropertyChangeListener(this);
60     }
61
62     public SynthContext JavaDoc getContext(JComponent c) {
63         return getContext(c, getComponentState(c));
64     }
65
66     private SynthContext JavaDoc getContext(JComponent c, int state) {
67         return SynthContext.getContext(SynthContext JavaDoc.class, c,
68                     SynthLookAndFeel.getRegion(c), style, state);
69     }
70
71     private Region JavaDoc getRegion(JComponent c) {
72         return SynthLookAndFeel.getRegion(c);
73     }
74
75     private int getComponentState(JComponent c) {
76         JComponent comp = ((JToolTip)c).getComponent();
77
78         if (comp != null && !comp.isEnabled()) {
79             return DISABLED;
80         }
81         return SynthLookAndFeel.getComponentState(c);
82     }
83
84     public void update(Graphics g, JComponent c) {
85         SynthContext JavaDoc context = getContext(c);
86
87         SynthLookAndFeel.update(context, g);
88         context.getPainter().paintToolTipBackground(context,
89                           g, 0, 0, c.getWidth(), c.getHeight());
90         paint(context, g);
91         context.dispose();
92     }
93
94     public void paintBorder(SynthContext JavaDoc context, Graphics g, int x,
95                             int y, int w, int h) {
96         context.getPainter().paintToolTipBorder(context, g, x, y, w, h);
97     }
98
99     public void paint(Graphics g, JComponent c) {
100         SynthContext JavaDoc context = getContext(c);
101
102         paint(context, g);
103         context.dispose();
104     }
105
106     protected void paint(SynthContext JavaDoc context, Graphics g) {
107         JToolTip tip = (JToolTip)context.getComponent();
108     String JavaDoc tipText = tip.getToolTipText();
109
110         Insets insets = tip.getInsets();
111     View JavaDoc v = (View JavaDoc)tip.getClientProperty(BasicHTML.propertyKey);
112     if (v != null) {
113             Rectangle paintTextR = new Rectangle(insets.left, insets.top,
114                   tip.getWidth() - (insets.left + insets.right),
115                   tip.getHeight() - (insets.top + insets.bottom));
116         v.paint(g, paintTextR);
117     } else {
118             g.setColor(context.getStyle().getColor(context,
119                                                    ColorType.TEXT_FOREGROUND));
120             g.setFont(style.getFont(context));
121             context.getStyle().getGraphicsUtils(context).paintText(
122                 context, g, tip.getTipText(), insets.left, insets.top, -1);
123     }
124     }
125
126     public Dimension getPreferredSize(JComponent c) {
127         SynthContext JavaDoc context = getContext(c);
128     Insets insets = c.getInsets();
129     Dimension prefSize = new Dimension(insets.left+insets.right,
130                        insets.top+insets.bottom);
131     String JavaDoc text = ((JToolTip)c).getTipText();
132
133     if (text != null) {
134         View JavaDoc v = (c != null) ? (View JavaDoc) c.getClientProperty("html") : null;
135         if (v != null) {
136         prefSize.width += (int) v.getPreferredSpan(View.X_AXIS);
137         prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
138         } else {
139                 Font font = context.getStyle().getFont(context);
140                 FontMetrics fm = c.getFontMetrics(font);
141         prefSize.width += context.getStyle().getGraphicsUtils(context).
142                                   computeStringWidth(context, font, fm, text);
143         prefSize.height += fm.getHeight();
144         }
145         }
146         context.dispose();
147     return prefSize;
148     }
149
150     public void propertyChange(PropertyChangeEvent JavaDoc e) {
151         if (SynthLookAndFeel.shouldUpdateStyle(e)) {
152             updateStyle((JToolTip)e.getSource());
153         }
154         String JavaDoc name = e.getPropertyName();
155         if (name.equals("tiptext") || "font".equals(name) ||
156                 "foreground".equals(name)) {
157             // remove the old html view client property if one
158
// existed, and install a new one if the text installed
159
// into the JLabel is html source.
160
JToolTip tip = ((JToolTip) e.getSource());
161             String JavaDoc text = tip.getTipText();
162             BasicHTML.updateRenderer(tip, text);
163         }
164     }
165 }
166
Popular Tags