001 /*
002 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.plaf.synth;
027
028 import java.awt.*;
029 import java.beans.PropertyChangeEvent;
030 import java.beans.PropertyChangeListener;
031
032 import javax.swing.*;
033 import javax.swing.plaf.basic.BasicHTML;
034 import javax.swing.plaf.basic.BasicToolTipUI;
035 import javax.swing.plaf.ComponentUI;
036 import javax.swing.text.View;
037 import sun.swing.plaf.synth.SynthUI;
038
039 /**
040 * Synth's ToolTipUI.
041 *
042 * @version 1.16, 05/05/07
043 * @author Joshua Outwater
044 */
045 class SynthToolTipUI extends BasicToolTipUI implements
046 PropertyChangeListener, SynthUI {
047 private SynthStyle style;
048
049 public static ComponentUI createUI(JComponent c) {
050 return new SynthToolTipUI();
051 }
052
053 protected void installDefaults(JComponent c) {
054 updateStyle(c);
055 }
056
057 private void updateStyle(JComponent c) {
058 SynthContext context = getContext(c, ENABLED);
059 style = SynthLookAndFeel.updateStyle(context, this );
060 context.dispose();
061 }
062
063 protected void uninstallDefaults(JComponent c) {
064 SynthContext context = getContext(c, ENABLED);
065 style.uninstallDefaults(context);
066 context.dispose();
067 style = null;
068 }
069
070 protected void installListeners(JComponent c) {
071 c.addPropertyChangeListener(this );
072 }
073
074 protected void uninstallListeners(JComponent c) {
075 c.removePropertyChangeListener(this );
076 }
077
078 public SynthContext getContext(JComponent c) {
079 return getContext(c, getComponentState(c));
080 }
081
082 private SynthContext getContext(JComponent c, int state) {
083 return SynthContext.getContext(SynthContext.class, c,
084 SynthLookAndFeel.getRegion(c), style, state);
085 }
086
087 private Region getRegion(JComponent c) {
088 return SynthLookAndFeel.getRegion(c);
089 }
090
091 private int getComponentState(JComponent c) {
092 JComponent comp = ((JToolTip) c).getComponent();
093
094 if (comp != null && !comp.isEnabled()) {
095 return DISABLED;
096 }
097 return SynthLookAndFeel.getComponentState(c);
098 }
099
100 public void update(Graphics g, JComponent c) {
101 SynthContext context = getContext(c);
102
103 SynthLookAndFeel.update(context, g);
104 context.getPainter().paintToolTipBackground(context, g, 0, 0,
105 c.getWidth(), c.getHeight());
106 paint(context, g);
107 context.dispose();
108 }
109
110 public void paintBorder(SynthContext context, Graphics g, int x,
111 int y, int w, int h) {
112 context.getPainter().paintToolTipBorder(context, g, x, y, w, h);
113 }
114
115 public void paint(Graphics g, JComponent c) {
116 SynthContext context = getContext(c);
117
118 paint(context, g);
119 context.dispose();
120 }
121
122 protected void paint(SynthContext context, Graphics g) {
123 JToolTip tip = (JToolTip) context.getComponent();
124 String tipText = tip.getToolTipText();
125
126 Insets insets = tip.getInsets();
127 View v = (View) tip.getClientProperty(BasicHTML.propertyKey);
128 if (v != null) {
129 Rectangle paintTextR = new Rectangle(insets.left,
130 insets.top, tip.getWidth()
131 - (insets.left + insets.right), tip
132 .getHeight()
133 - (insets.top + insets.bottom));
134 v.paint(g, paintTextR);
135 } else {
136 g.setColor(context.getStyle().getColor(context,
137 ColorType.TEXT_FOREGROUND));
138 g.setFont(style.getFont(context));
139 context.getStyle().getGraphicsUtils(context).paintText(
140 context, g, tip.getTipText(), insets.left,
141 insets.top, -1);
142 }
143 }
144
145 public Dimension getPreferredSize(JComponent c) {
146 SynthContext context = getContext(c);
147 Insets insets = c.getInsets();
148 Dimension prefSize = new Dimension(insets.left + insets.right,
149 insets.top + insets.bottom);
150 String text = ((JToolTip) c).getTipText();
151
152 if (text != null) {
153 View v = (c != null) ? (View) c.getClientProperty("html")
154 : null;
155 if (v != null) {
156 prefSize.width += (int) v.getPreferredSpan(View.X_AXIS);
157 prefSize.height += (int) v
158 .getPreferredSpan(View.Y_AXIS);
159 } else {
160 Font font = context.getStyle().getFont(context);
161 FontMetrics fm = c.getFontMetrics(font);
162 prefSize.width += context.getStyle().getGraphicsUtils(
163 context).computeStringWidth(context, font, fm,
164 text);
165 prefSize.height += fm.getHeight();
166 }
167 }
168 context.dispose();
169 return prefSize;
170 }
171
172 public void propertyChange(PropertyChangeEvent e) {
173 if (SynthLookAndFeel.shouldUpdateStyle(e)) {
174 updateStyle((JToolTip) e.getSource());
175 }
176 String name = e.getPropertyName();
177 if (name.equals("tiptext") || "font".equals(name)
178 || "foreground".equals(name)) {
179 // remove the old html view client property if one
180 // existed, and install a new one if the text installed
181 // into the JLabel is html source.
182 JToolTip tip = ((JToolTip) e.getSource());
183 String text = tip.getTipText();
184 BasicHTML.updateRenderer(tip, text);
185 }
186 }
187 }
|