< prev index next >

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicToolTipUI.java

Print this page




  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.plaf.basic;
  27 
  28 import sun.swing.SwingUtilities2;
  29 import java.awt.*;
  30 import java.beans.PropertyChangeEvent;
  31 import java.beans.PropertyChangeListener;
  32 
  33 import javax.swing.*;
  34 import javax.swing.BorderFactory;
  35 import javax.swing.border.Border;
  36 import javax.swing.plaf.ToolTipUI;
  37 import javax.swing.plaf.ComponentUI;

  38 import javax.swing.plaf.UIResource;
  39 import javax.swing.text.View;
  40 
  41 
  42 /**
  43  * Standard tool tip L&amp;F.
  44  *
  45  * @author Dave Moore
  46  */
  47 public class BasicToolTipUI extends ToolTipUI
  48 {
  49     static BasicToolTipUI sharedInstance = new BasicToolTipUI();
  50     /**
  51      * Global <code>PropertyChangeListener</code> that
  52      * <code>createPropertyChangeListener</code> returns.
  53      */
  54     private static PropertyChangeListener sharedPropertyChangedListener;
  55 
  56     private PropertyChangeListener propertyChangeListener;
  57 


  58     /**
  59      * Returns the instance of {@code BasicToolTipUI}.
  60      *
  61      * @param c a component
  62      * @return the instance of {@code BasicToolTipUI}
  63      */
  64     public static ComponentUI createUI(JComponent c) {
  65         return sharedInstance;
  66     }
  67 
  68     /**
  69      * Constructs a new instance of {@code BasicToolTipUI}.
  70      */
  71     public BasicToolTipUI() {
  72         super();
  73     }
  74 
  75     public void installUI(JComponent c) {
  76         installDefaults(c);
  77         installComponents(c);


  79     }
  80 
  81     public void uninstallUI(JComponent c) {
  82         // REMIND: this is NOT getting called
  83         uninstallDefaults(c);
  84         uninstallComponents(c);
  85         uninstallListeners(c);
  86     }
  87 
  88     /**
  89      * Installs default properties.
  90      *
  91      * @param c a component
  92      */
  93     protected void installDefaults(JComponent c){
  94         LookAndFeel.installColorsAndFont(c, "ToolTip.background",
  95                 "ToolTip.foreground",
  96                 "ToolTip.font");
  97         LookAndFeel.installProperty(c, "opaque", Boolean.TRUE);
  98         componentChanged(c);

  99     }
 100 
 101     /**
 102      * Uninstalls default properties.
 103      *
 104      * @param c a component
 105      */
 106     protected void uninstallDefaults(JComponent c){
 107         LookAndFeel.uninstallBorder(c);




 108     }
 109 
 110     /* Unfortunately this has to remain private until we can make API additions.
 111      */
 112     private void installComponents(JComponent c){
 113         BasicHTML.updateRenderer(c, ((JToolTip) c).getTipText());
 114     }
 115 
 116     /* Unfortunately this has to remain private until we can make API additions.
 117      */
 118     private void uninstallComponents(JComponent c){
 119         BasicHTML.updateRenderer(c, "");
 120     }
 121 
 122     /**
 123      * Registers listeners.
 124      *
 125      * @param c a component
 126      */
 127     protected void installListeners(JComponent c) {


 156         Dimension size = c.getSize();
 157 
 158         g.setColor(c.getForeground());
 159         // fix for bug 4153892
 160         String tipText = ((JToolTip)c).getTipText();
 161         if (tipText == null) {
 162             tipText = "";
 163         }
 164 
 165         Insets insets = c.getInsets();
 166         Rectangle paintTextR = new Rectangle(
 167             insets.left + 3,
 168             insets.top,
 169             size.width - (insets.left + insets.right) - 6,
 170             size.height - (insets.top + insets.bottom));
 171         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 172         if (v != null) {
 173             v.paint(g, paintTextR);
 174         } else {
 175             g.setFont(font);
 176             SwingUtilities2.drawString(c, g, tipText, paintTextR.x,
 177                                   paintTextR.y + metrics.getAscent());
 178         }
 179     }
 180 
 181     public Dimension getPreferredSize(JComponent c) {
 182         Font font = c.getFont();
 183         FontMetrics fm = c.getFontMetrics(font);
 184         Insets insets = c.getInsets();
 185 
 186         Dimension prefSize = new Dimension(insets.left+insets.right,
 187                                            insets.top+insets.bottom);
 188         String text = ((JToolTip)c).getTipText();
 189 
 190         if ((text == null) || text.equals("")) {
 191             text = "";
 192         }
 193         else {
 194             View v = (c != null) ? (View) c.getClientProperty("html") : null;
 195             if (v != null) {
 196                 prefSize.width += (int) v.getPreferredSpan(View.X_AXIS) + 6;
 197                 prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
 198             } else {
 199                 prefSize.width += SwingUtilities2.stringWidth(c,fm,text) + 6;
 200                 prefSize.height += fm.getHeight();
 201             }
 202         }
 203         return prefSize;
 204     }
 205 
 206     public Dimension getMinimumSize(JComponent c) {
 207         Dimension d = getPreferredSize(c);
 208         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 209         if (v != null) {
 210             d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
 211         }
 212         return d;
 213     }
 214 
 215     public Dimension getMaximumSize(JComponent c) {
 216         Dimension d = getPreferredSize(c);
 217         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 218         if (v != null) {
 219             d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);




  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.plaf.basic;
  27 
  28 import sun.swing.SwingUtilities2;
  29 import java.awt.*;
  30 import java.beans.PropertyChangeEvent;
  31 import java.beans.PropertyChangeListener;
  32 
  33 import javax.swing.*;
  34 import javax.swing.BorderFactory;
  35 import javax.swing.border.Border;
  36 import javax.swing.plaf.ToolTipUI;
  37 import javax.swing.plaf.ComponentUI;
  38 import javax.swing.plaf.TextUIDrawing;
  39 import javax.swing.plaf.UIResource;
  40 import javax.swing.text.View;
  41 
  42 
  43 /**
  44  * Standard tool tip L&amp;F.
  45  *
  46  * @author Dave Moore
  47  */
  48 public class BasicToolTipUI extends ToolTipUI
  49 {
  50     static BasicToolTipUI sharedInstance = new BasicToolTipUI();
  51     /**
  52      * Global <code>PropertyChangeListener</code> that
  53      * <code>createPropertyChangeListener</code> returns.
  54      */
  55     private static PropertyChangeListener sharedPropertyChangedListener;
  56 
  57     private PropertyChangeListener propertyChangeListener;
  58 
  59     private TextUIDrawing textUIDrawing;
  60 
  61     /**
  62      * Returns the instance of {@code BasicToolTipUI}.
  63      *
  64      * @param c a component
  65      * @return the instance of {@code BasicToolTipUI}
  66      */
  67     public static ComponentUI createUI(JComponent c) {
  68         return sharedInstance;
  69     }
  70 
  71     /**
  72      * Constructs a new instance of {@code BasicToolTipUI}.
  73      */
  74     public BasicToolTipUI() {
  75         super();
  76     }
  77 
  78     public void installUI(JComponent c) {
  79         installDefaults(c);
  80         installComponents(c);


  82     }
  83 
  84     public void uninstallUI(JComponent c) {
  85         // REMIND: this is NOT getting called
  86         uninstallDefaults(c);
  87         uninstallComponents(c);
  88         uninstallListeners(c);
  89     }
  90 
  91     /**
  92      * Installs default properties.
  93      *
  94      * @param c a component
  95      */
  96     protected void installDefaults(JComponent c){
  97         LookAndFeel.installColorsAndFont(c, "ToolTip.background",
  98                 "ToolTip.foreground",
  99                 "ToolTip.font");
 100         LookAndFeel.installProperty(c, "opaque", Boolean.TRUE);
 101         componentChanged(c);
 102         textUIDrawing = SwingUtilities2.getTextUIDrawing(textUIDrawing);
 103     }
 104 
 105     /**
 106      * Uninstalls default properties.
 107      *
 108      * @param c a component
 109      */
 110     protected void uninstallDefaults(JComponent c){
 111         LookAndFeel.uninstallBorder(c);
 112         if (textUIDrawing != SwingUtilities2.DEFAULT_UI_TEXT_DRAWING
 113                 && textUIDrawing instanceof UIResource) {
 114             textUIDrawing = SwingUtilities2.DEFAULT_UI_TEXT_DRAWING;
 115         }
 116     }
 117 
 118     /* Unfortunately this has to remain private until we can make API additions.
 119      */
 120     private void installComponents(JComponent c){
 121         BasicHTML.updateRenderer(c, ((JToolTip) c).getTipText());
 122     }
 123 
 124     /* Unfortunately this has to remain private until we can make API additions.
 125      */
 126     private void uninstallComponents(JComponent c){
 127         BasicHTML.updateRenderer(c, "");
 128     }
 129 
 130     /**
 131      * Registers listeners.
 132      *
 133      * @param c a component
 134      */
 135     protected void installListeners(JComponent c) {


 164         Dimension size = c.getSize();
 165 
 166         g.setColor(c.getForeground());
 167         // fix for bug 4153892
 168         String tipText = ((JToolTip)c).getTipText();
 169         if (tipText == null) {
 170             tipText = "";
 171         }
 172 
 173         Insets insets = c.getInsets();
 174         Rectangle paintTextR = new Rectangle(
 175             insets.left + 3,
 176             insets.top,
 177             size.width - (insets.left + insets.right) - 6,
 178             size.height - (insets.top + insets.bottom));
 179         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 180         if (v != null) {
 181             v.paint(g, paintTextR);
 182         } else {
 183             g.setFont(font);
 184             textUIDrawing.drawString(c, g, tipText, paintTextR.x,
 185                                   paintTextR.y + metrics.getAscent());
 186         }
 187     }
 188 
 189     public Dimension getPreferredSize(JComponent c) {
 190         Font font = c.getFont();
 191         FontMetrics fm = c.getFontMetrics(font);
 192         Insets insets = c.getInsets();
 193 
 194         Dimension prefSize = new Dimension(insets.left+insets.right,
 195                                            insets.top+insets.bottom);
 196         String text = ((JToolTip)c).getTipText();
 197 
 198         if ((text == null) || text.equals("")) {
 199             text = "";
 200         }
 201         else {
 202             View v = (c != null) ? (View) c.getClientProperty("html") : null;
 203             if (v != null) {
 204                 prefSize.width += (int) v.getPreferredSpan(View.X_AXIS) + 6;
 205                 prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
 206             } else {
 207                 prefSize.width += textUIDrawing.getStringWidth(c,fm,text) + 6;
 208                 prefSize.height += fm.getHeight();
 209             }
 210         }
 211         return prefSize;
 212     }
 213 
 214     public Dimension getMinimumSize(JComponent c) {
 215         Dimension d = getPreferredSize(c);
 216         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 217         if (v != null) {
 218             d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS);
 219         }
 220         return d;
 221     }
 222 
 223     public Dimension getMaximumSize(JComponent c) {
 224         Dimension d = getPreferredSize(c);
 225         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
 226         if (v != null) {
 227             d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS);


< prev index next >