blob: b129f9bedff9caffaf15300f3a931ca9497d1bd4 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.swing.plaf.synth;
27
28import java.awt.*;
29import java.beans.PropertyChangeEvent;
30import java.beans.PropertyChangeListener;
31
32import javax.swing.*;
33import javax.swing.plaf.basic.BasicHTML;
34import javax.swing.plaf.basic.BasicToolTipUI;
35import javax.swing.plaf.ComponentUI;
36import javax.swing.text.View;
37import sun.swing.plaf.synth.SynthUI;
38
39
40/**
41 * Synth's ToolTipUI.
42 *
43 * @author Joshua Outwater
44 */
45class SynthToolTipUI extends BasicToolTipUI implements PropertyChangeListener,
46 SynthUI {
47 private SynthStyle style;
48
49
50 public static ComponentUI createUI(JComponent c) {
51 return new SynthToolTipUI();
52 }
53
54 protected void installDefaults(JComponent c) {
55 updateStyle(c);
56 }
57
58 private void updateStyle(JComponent c) {
59 SynthContext context = getContext(c, ENABLED);
60 style = SynthLookAndFeel.updateStyle(context, this);
61 context.dispose();
62 }
63
64 protected void uninstallDefaults(JComponent c) {
65 SynthContext context = getContext(c, ENABLED);
66 style.uninstallDefaults(context);
67 context.dispose();
68 style = null;
69 }
70
71 protected void installListeners(JComponent c) {
72 c.addPropertyChangeListener(this);
73 }
74
75 protected void uninstallListeners(JComponent c) {
76 c.removePropertyChangeListener(this);
77 }
78
79 public SynthContext getContext(JComponent c) {
80 return getContext(c, getComponentState(c));
81 }
82
83 private SynthContext getContext(JComponent c, int state) {
84 return SynthContext.getContext(SynthContext.class, c,
85 SynthLookAndFeel.getRegion(c), style, state);
86 }
87
88 private Region getRegion(JComponent c) {
89 return SynthLookAndFeel.getRegion(c);
90 }
91
92 private int getComponentState(JComponent c) {
93 JComponent comp = ((JToolTip)c).getComponent();
94
95 if (comp != null && !comp.isEnabled()) {
96 return DISABLED;
97 }
98 return SynthLookAndFeel.getComponentState(c);
99 }
100
101 public void update(Graphics g, JComponent c) {
102 SynthContext context = getContext(c);
103
104 SynthLookAndFeel.update(context, g);
105 context.getPainter().paintToolTipBackground(context,
106 g, 0, 0, c.getWidth(), c.getHeight());
107 paint(context, g);
108 context.dispose();
109 }
110
111 public void paintBorder(SynthContext context, Graphics g, int x,
112 int y, int w, int h) {
113 context.getPainter().paintToolTipBorder(context, g, x, y, w, h);
114 }
115
116 public void paint(Graphics g, JComponent c) {
117 SynthContext context = getContext(c);
118
119 paint(context, g);
120 context.dispose();
121 }
122
123 protected void paint(SynthContext context, Graphics g) {
124 JToolTip tip = (JToolTip)context.getComponent();
125 String tipText = tip.getToolTipText();
126
127 Insets insets = tip.getInsets();
128 View v = (View)tip.getClientProperty(BasicHTML.propertyKey);
129 if (v != null) {
130 Rectangle paintTextR = new Rectangle(insets.left, insets.top,
131 tip.getWidth() - (insets.left + insets.right),
132 tip.getHeight() - (insets.top + insets.bottom));
133 v.paint(g, paintTextR);
134 } else {
135 g.setColor(context.getStyle().getColor(context,
136 ColorType.TEXT_FOREGROUND));
137 g.setFont(style.getFont(context));
138 context.getStyle().getGraphicsUtils(context).paintText(
139 context, g, tip.getTipText(), insets.left, insets.top, -1);
140 }
141 }
142
143 public Dimension getPreferredSize(JComponent c) {
144 SynthContext context = getContext(c);
145 Insets insets = c.getInsets();
146 Dimension prefSize = new Dimension(insets.left+insets.right,
147 insets.top+insets.bottom);
148 String text = ((JToolTip)c).getTipText();
149
150 if (text != null) {
151 View v = (c != null) ? (View) c.getClientProperty("html") : null;
152 if (v != null) {
153 prefSize.width += (int) v.getPreferredSpan(View.X_AXIS);
154 prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
155 } else {
156 Font font = context.getStyle().getFont(context);
157 FontMetrics fm = c.getFontMetrics(font);
158 prefSize.width += context.getStyle().getGraphicsUtils(context).
159 computeStringWidth(context, font, fm, text);
160 prefSize.height += fm.getHeight();
161 }
162 }
163 context.dispose();
164 return prefSize;
165 }
166
167 public void propertyChange(PropertyChangeEvent e) {
168 if (SynthLookAndFeel.shouldUpdateStyle(e)) {
169 updateStyle((JToolTip)e.getSource());
170 }
171 String name = e.getPropertyName();
172 if (name.equals("tiptext") || "font".equals(name) ||
173 "foreground".equals(name)) {
174 // remove the old html view client property if one
175 // existed, and install a new one if the text installed
176 // into the JLabel is html source.
177 JToolTip tip = ((JToolTip) e.getSource());
178 String text = tip.getTipText();
179 BasicHTML.updateRenderer(tip, text);
180 }
181 }
182}