0N/A/*
2362N/A * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing.plaf.basic;
0N/A
0N/Aimport sun.swing.SwingUtilities2;
0N/Aimport java.awt.*;
0N/Aimport java.beans.PropertyChangeEvent;
0N/Aimport java.beans.PropertyChangeListener;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.BorderFactory;
0N/Aimport javax.swing.border.Border;
0N/Aimport javax.swing.plaf.ToolTipUI;
0N/Aimport javax.swing.plaf.ComponentUI;
0N/Aimport javax.swing.plaf.UIResource;
0N/Aimport javax.swing.text.View;
0N/A
0N/A
0N/A/**
0N/A * Standard tool tip L&F.
0N/A * <p>
0N/A *
0N/A * @author Dave Moore
0N/A */
0N/Apublic class BasicToolTipUI extends ToolTipUI
0N/A{
0N/A static BasicToolTipUI sharedInstance = new BasicToolTipUI();
0N/A /**
0N/A * Global <code>PropertyChangeListener</code> that
0N/A * <code>createPropertyChangeListener</code> returns.
0N/A */
0N/A private static PropertyChangeListener sharedPropertyChangedListener;
0N/A
0N/A private PropertyChangeListener propertyChangeListener;
0N/A
0N/A public static ComponentUI createUI(JComponent c) {
0N/A return sharedInstance;
0N/A }
0N/A
0N/A public BasicToolTipUI() {
0N/A super();
0N/A }
0N/A
0N/A public void installUI(JComponent c) {
0N/A installDefaults(c);
0N/A installComponents(c);
0N/A installListeners(c);
0N/A }
0N/A
0N/A public void uninstallUI(JComponent c) {
0N/A // REMIND: this is NOT getting called
0N/A uninstallDefaults(c);
0N/A uninstallComponents(c);
0N/A uninstallListeners(c);
0N/A }
0N/A
0N/A protected void installDefaults(JComponent c){
0N/A LookAndFeel.installColorsAndFont(c, "ToolTip.background",
0N/A "ToolTip.foreground",
0N/A "ToolTip.font");
0N/A LookAndFeel.installProperty(c, "opaque", Boolean.TRUE);
0N/A componentChanged(c);
0N/A }
0N/A
0N/A protected void uninstallDefaults(JComponent c){
0N/A LookAndFeel.uninstallBorder(c);
0N/A }
0N/A
0N/A /* Unfortunately this has to remain private until we can make API additions.
0N/A */
0N/A private void installComponents(JComponent c){
0N/A BasicHTML.updateRenderer(c, ((JToolTip)c).getTipText());
0N/A }
0N/A
0N/A /* Unfortunately this has to remain private until we can make API additions.
0N/A */
0N/A private void uninstallComponents(JComponent c){
0N/A BasicHTML.updateRenderer(c, "");
0N/A }
0N/A
0N/A protected void installListeners(JComponent c) {
0N/A propertyChangeListener = createPropertyChangeListener(c);
0N/A
0N/A c.addPropertyChangeListener(propertyChangeListener);
0N/A }
0N/A
0N/A protected void uninstallListeners(JComponent c) {
0N/A c.removePropertyChangeListener(propertyChangeListener);
0N/A
0N/A propertyChangeListener = null;
0N/A }
0N/A
0N/A /* Unfortunately this has to remain private until we can make API additions.
0N/A */
0N/A private PropertyChangeListener createPropertyChangeListener(JComponent c) {
0N/A if (sharedPropertyChangedListener == null) {
0N/A sharedPropertyChangedListener = new PropertyChangeHandler();
0N/A }
0N/A return sharedPropertyChangedListener;
0N/A }
0N/A
0N/A public void paint(Graphics g, JComponent c) {
0N/A Font font = c.getFont();
0N/A FontMetrics metrics = SwingUtilities2.getFontMetrics(c, g, font);
0N/A Dimension size = c.getSize();
0N/A
0N/A g.setColor(c.getForeground());
0N/A // fix for bug 4153892
0N/A String tipText = ((JToolTip)c).getTipText();
0N/A if (tipText == null) {
0N/A tipText = "";
0N/A }
0N/A
0N/A Insets insets = c.getInsets();
0N/A Rectangle paintTextR = new Rectangle(
0N/A insets.left + 3,
0N/A insets.top,
0N/A size.width - (insets.left + insets.right) - 6,
0N/A size.height - (insets.top + insets.bottom));
0N/A View v = (View) c.getClientProperty(BasicHTML.propertyKey);
0N/A if (v != null) {
0N/A v.paint(g, paintTextR);
0N/A } else {
0N/A g.setFont(font);
0N/A SwingUtilities2.drawString(c, g, tipText, paintTextR.x,
0N/A paintTextR.y + metrics.getAscent());
0N/A }
0N/A }
0N/A
0N/A public Dimension getPreferredSize(JComponent c) {
0N/A Font font = c.getFont();
0N/A FontMetrics fm = c.getFontMetrics(font);
0N/A Insets insets = c.getInsets();
0N/A
0N/A Dimension prefSize = new Dimension(insets.left+insets.right,
0N/A insets.top+insets.bottom);
0N/A String text = ((JToolTip)c).getTipText();
0N/A
0N/A if ((text == null) || text.equals("")) {
0N/A text = "";
0N/A }
0N/A else {
0N/A View v = (c != null) ? (View) c.getClientProperty("html") : null;
0N/A if (v != null) {
0N/A prefSize.width += (int) v.getPreferredSpan(View.X_AXIS) + 6;
0N/A prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
0N/A } else {
0N/A prefSize.width += SwingUtilities2.stringWidth(c,fm,text) + 6;
0N/A prefSize.height += fm.getHeight();
0N/A }
0N/A }
0N/A return prefSize;
0N/A }
0N/A
0N/A public Dimension getMinimumSize(JComponent c) {
0N/A Dimension d = getPreferredSize(c);
0N/A View v = (View) c.getClientProperty(BasicHTML.propertyKey);
0N/A if (v != null) {
0N/A d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
0N/A }
0N/A return d;
0N/A }
0N/A
0N/A public Dimension getMaximumSize(JComponent c) {
0N/A Dimension d = getPreferredSize(c);
0N/A View v = (View) c.getClientProperty(BasicHTML.propertyKey);
0N/A if (v != null) {
0N/A d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
0N/A }
0N/A return d;
0N/A }
0N/A
0N/A /**
0N/A * Invoked when the <code>JCompoment</code> associated with the
0N/A * <code>JToolTip</code> has changed, or at initialization time. This
0N/A * should update any state dependant upon the <code>JComponent</code>.
0N/A *
0N/A * @param c the JToolTip the JComponent has changed on.
0N/A */
0N/A private void componentChanged(JComponent c) {
0N/A JComponent comp = ((JToolTip)c).getComponent();
0N/A
0N/A if (comp != null && !(comp.isEnabled())) {
0N/A // For better backward compatability, only install inactive
0N/A // properties if they are defined.
0N/A if (UIManager.getBorder("ToolTip.borderInactive") != null) {
0N/A LookAndFeel.installBorder(c, "ToolTip.borderInactive");
0N/A }
0N/A else {
0N/A LookAndFeel.installBorder(c, "ToolTip.border");
0N/A }
0N/A if (UIManager.getColor("ToolTip.backgroundInactive") != null) {
0N/A LookAndFeel.installColors(c,"ToolTip.backgroundInactive",
0N/A "ToolTip.foregroundInactive");
0N/A }
0N/A else {
0N/A LookAndFeel.installColors(c,"ToolTip.background",
0N/A "ToolTip.foreground");
0N/A }
0N/A } else {
0N/A LookAndFeel.installBorder(c, "ToolTip.border");
0N/A LookAndFeel.installColors(c, "ToolTip.background",
0N/A "ToolTip.foreground");
0N/A }
0N/A }
0N/A
0N/A
0N/A private static class PropertyChangeHandler implements
0N/A PropertyChangeListener {
0N/A public void propertyChange(PropertyChangeEvent e) {
0N/A String name = e.getPropertyName();
0N/A if (name.equals("tiptext") || "font".equals(name) ||
0N/A "foreground".equals(name)) {
0N/A // remove the old html view client property if one
0N/A // existed, and install a new one if the text installed
0N/A // into the JLabel is html source.
0N/A JToolTip tip = ((JToolTip) e.getSource());
0N/A String text = tip.getTipText();
0N/A BasicHTML.updateRenderer(tip, text);
0N/A }
0N/A else if ("component".equals(name)) {
0N/A JToolTip tip = ((JToolTip) e.getSource());
0N/A
0N/A if (tip.getUI() instanceof BasicToolTipUI) {
0N/A ((BasicToolTipUI)tip.getUI()).componentChanged(tip);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A}