jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicToolTipUI.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 25761 jdk/src/share/classes/javax/swing/plaf/basic/BasicToolTipUI.java@c408b10ef757
child 47020 2c55106dc37b
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.BorderFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.border.Border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.plaf.ToolTipUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.text.View;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
    43
 * Standard tool tip L&F.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Dave Moore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class BasicToolTipUI extends ToolTipUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static BasicToolTipUI sharedInstance = new BasicToolTipUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Global <code>PropertyChangeListener</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * <code>createPropertyChangeListener</code> returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static PropertyChangeListener sharedPropertyChangedListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private PropertyChangeListener propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    58
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    59
     * Returns the instance of {@code BasicToolTipUI}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    60
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    61
     * @param c a component
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    62
     * @return the instance of {@code BasicToolTipUI}
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    63
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return sharedInstance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    68
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    69
     * Constructs a new instance of {@code BasicToolTipUI}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    70
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public BasicToolTipUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        installDefaults(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        installComponents(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        installListeners(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // REMIND: this is NOT getting called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        uninstallDefaults(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        uninstallComponents(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        uninstallListeners(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    88
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    89
     * Installs default properties.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    90
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    91
     * @param c a component
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    92
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected void installDefaults(JComponent c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        LookAndFeel.installColorsAndFont(c, "ToolTip.background",
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    95
                "ToolTip.foreground",
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
    96
                "ToolTip.font");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        LookAndFeel.installProperty(c, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        componentChanged(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   101
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   102
     * Uninstalls default properties.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   103
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   104
     * @param c a component
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   105
     */
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   106
    protected void uninstallDefaults(JComponent c){
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        LookAndFeel.uninstallBorder(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /* Unfortunately this has to remain private until we can make API additions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private void installComponents(JComponent c){
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   113
        BasicHTML.updateRenderer(c, ((JToolTip) c).getTipText());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /* Unfortunately this has to remain private until we can make API additions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private void uninstallComponents(JComponent c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        BasicHTML.updateRenderer(c, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   122
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   123
     * Registers listeners.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   124
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   125
     * @param c a component
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   126
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected void installListeners(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        propertyChangeListener = createPropertyChangeListener(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        c.addPropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   133
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   134
     * Unregisters listeners.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   135
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   136
     * @param c a component
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 23715
diff changeset
   137
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    protected void uninstallListeners(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        c.removePropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        propertyChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /* Unfortunately this has to remain private until we can make API additions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private PropertyChangeListener createPropertyChangeListener(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (sharedPropertyChangedListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            sharedPropertyChangedListener = new PropertyChangeHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return sharedPropertyChangedListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Font font = c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        FontMetrics metrics = SwingUtilities2.getFontMetrics(c, g, font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Dimension size = c.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        g.setColor(c.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // fix for bug 4153892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        String tipText = ((JToolTip)c).getTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (tipText == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            tipText = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        Insets insets = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        Rectangle paintTextR = new Rectangle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            insets.left + 3,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            insets.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            size.width - (insets.left + insets.right) - 6,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            size.height - (insets.top + insets.bottom));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            v.paint(g, paintTextR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            g.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            SwingUtilities2.drawString(c, g, tipText, paintTextR.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                  paintTextR.y + metrics.getAscent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Font font = c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        FontMetrics fm = c.getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Insets insets = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        Dimension prefSize = new Dimension(insets.left+insets.right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                           insets.top+insets.bottom);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        String text = ((JToolTip)c).getTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if ((text == null) || text.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            text = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            View v = (c != null) ? (View) c.getClientProperty("html") : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                prefSize.width += (int) v.getPreferredSpan(View.X_AXIS) + 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                prefSize.width += SwingUtilities2.stringWidth(c,fm,text) + 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                prefSize.height += fm.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return prefSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        Dimension d = getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        View v = (View) c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Invoked when the <code>JCompoment</code> associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <code>JToolTip</code> has changed, or at initialization time. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * should update any state dependant upon the <code>JComponent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param c the JToolTip the JComponent has changed on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private void componentChanged(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        JComponent comp = ((JToolTip)c).getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (comp != null && !(comp.isEnabled())) {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20169
diff changeset
   235
            // For better backward compatibility, only install inactive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            // properties if they are defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (UIManager.getBorder("ToolTip.borderInactive") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                LookAndFeel.installBorder(c, "ToolTip.borderInactive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                LookAndFeel.installBorder(c, "ToolTip.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (UIManager.getColor("ToolTip.backgroundInactive") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                LookAndFeel.installColors(c,"ToolTip.backgroundInactive",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                          "ToolTip.foregroundInactive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                LookAndFeel.installColors(c,"ToolTip.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                          "ToolTip.foreground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            LookAndFeel.installBorder(c, "ToolTip.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            LookAndFeel.installColors(c, "ToolTip.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                      "ToolTip.foreground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private static class PropertyChangeHandler implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                 PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            String name = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (name.equals("tiptext") || "font".equals(name) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                "foreground".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                // remove the old html view client property if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                // existed, and install a new one if the text installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                // into the JLabel is html source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                JToolTip tip = ((JToolTip) e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                String text = tip.getTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                BasicHTML.updateRenderer(tip, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            else if ("component".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                JToolTip tip = ((JToolTip) e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if (tip.getUI() instanceof BasicToolTipUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    ((BasicToolTipUI)tip.getUI()).componentChanged(tip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}