1 7 8 package javax.swing.plaf.basic; 9 10 import com.sun.java.swing.SwingUtilities2; 11 import java.awt.*; 12 import java.beans.PropertyChangeEvent ; 13 import java.beans.PropertyChangeListener ; 14 15 import javax.swing.*; 16 import javax.swing.BorderFactory ; 17 import javax.swing.border.Border ; 18 import javax.swing.plaf.ToolTipUI ; 19 import javax.swing.plaf.ComponentUI ; 20 import javax.swing.plaf.UIResource ; 21 import javax.swing.text.View ; 22 23 24 31 public class BasicToolTipUI extends ToolTipUI 32 { 33 static BasicToolTipUI sharedInstance = new BasicToolTipUI (); 34 38 private static PropertyChangeListener sharedPropertyChangedListener; 39 40 private PropertyChangeListener propertyChangeListener; 41 42 public static ComponentUI createUI(JComponent c) { 43 return sharedInstance; 44 } 45 46 public BasicToolTipUI() { 47 super(); 48 } 49 50 public void installUI(JComponent c) { 51 installDefaults(c); 52 installComponents(c); 53 installListeners(c); 54 } 55 56 public void uninstallUI(JComponent c) { 57 uninstallDefaults(c); 59 uninstallComponents(c); 60 uninstallListeners(c); 61 } 62 63 protected void installDefaults(JComponent c){ 64 LookAndFeel.installColorsAndFont(c, "ToolTip.background", 65 "ToolTip.foreground", 66 "ToolTip.font"); 67 LookAndFeel.installProperty(c, "opaque", Boolean.TRUE); 68 componentChanged(c); 69 } 70 71 protected void uninstallDefaults(JComponent c){ 72 LookAndFeel.uninstallBorder(c); 73 } 74 75 77 private void installComponents(JComponent c){ 78 BasicHTML.updateRenderer(c, ((JToolTip)c).getTipText()); 79 } 80 81 83 private void uninstallComponents(JComponent c){ 84 BasicHTML.updateRenderer(c, ""); 85 } 86 87 protected void installListeners(JComponent c) { 88 propertyChangeListener = createPropertyChangeListener(c); 89 90 c.addPropertyChangeListener(propertyChangeListener); 91 } 92 93 protected void uninstallListeners(JComponent c) { 94 c.removePropertyChangeListener(propertyChangeListener); 95 96 propertyChangeListener = null; 97 } 98 99 101 private PropertyChangeListener createPropertyChangeListener(JComponent c) { 102 if (sharedPropertyChangedListener == null) { 103 sharedPropertyChangedListener = new PropertyChangeHandler(); 104 } 105 return sharedPropertyChangedListener; 106 } 107 108 public void paint(Graphics g, JComponent c) { 109 Font font = c.getFont(); 110 FontMetrics metrics = SwingUtilities2.getFontMetrics(c, g, font); 111 Dimension size = c.getSize(); 112 if (c.isOpaque()) { 113 g.setColor(c.getBackground()); 114 g.fillRect(0, 0, size.width, size.height); 115 } 116 g.setColor(c.getForeground()); 117 g.setFont(font); 118 String tipText = ((JToolTip)c).getTipText(); 120 if (tipText == null) { 121 tipText = ""; 122 } 123 124 Insets insets = c.getInsets(); 125 Rectangle paintTextR = new Rectangle( 126 insets.left, 127 insets.top, 128 size.width - (insets.left + insets.right), 129 size.height - (insets.top + insets.bottom)); 130 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 131 if (v != null) { 132 v.paint(g, paintTextR); 133 } else { 134 SwingUtilities2.drawString(c, g, tipText, paintTextR.x + 3, 135 paintTextR.y + metrics.getAscent()); 136 } 137 } 138 139 public Dimension getPreferredSize(JComponent c) { 140 Font font = c.getFont(); 141 FontMetrics fm = c.getFontMetrics(font); 142 Insets insets = c.getInsets(); 143 144 Dimension prefSize = new Dimension(insets.left+insets.right, 145 insets.top+insets.bottom); 146 String text = ((JToolTip)c).getTipText(); 147 148 if ((text == null) || text.equals("")) { 149 text = ""; 150 } 151 else { 152 View v = (c != null) ? (View ) c.getClientProperty("html") : null; 153 if (v != null) { 154 prefSize.width += (int) v.getPreferredSpan(View.X_AXIS); 155 prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS); 156 } else { 157 prefSize.width += SwingUtilities2.stringWidth(c,fm,text) + 6; 158 prefSize.height += fm.getHeight(); 159 } 160 } 161 return prefSize; 162 } 163 164 public Dimension getMinimumSize(JComponent c) { 165 Dimension d = getPreferredSize(c); 166 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 167 if (v != null) { 168 d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); 169 } 170 return d; 171 } 172 173 public Dimension getMaximumSize(JComponent c) { 174 Dimension d = getPreferredSize(c); 175 View v = (View ) c.getClientProperty(BasicHTML.propertyKey); 176 if (v != null) { 177 d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS); 178 } 179 return d; 180 } 181 182 189 private void componentChanged(JComponent c) { 190 JComponent comp = ((JToolTip)c).getComponent(); 191 192 if (comp != null && !(comp.isEnabled())) { 193 if (UIManager.getBorder("ToolTip.borderInactive") != null) { 196 LookAndFeel.installBorder(c, "ToolTip.borderInactive"); 197 } 198 else { 199 LookAndFeel.installBorder(c, "ToolTip.border"); 200 } 201 if (UIManager.getColor("ToolTip.backgroundInactive") != null) { 202 LookAndFeel.installColors(c,"ToolTip.backgroundInactive", 203 "ToolTip.foregroundInactive"); 204 } 205 else { 206 LookAndFeel.installColors(c,"ToolTip.background", 207 "ToolTip.foreground"); 208 } 209 } else { 210 LookAndFeel.installBorder(c, "ToolTip.border"); 211 LookAndFeel.installColors(c, "ToolTip.background", 212 "ToolTip.foreground"); 213 } 214 } 215 216 217 private static class PropertyChangeHandler implements 218 PropertyChangeListener { 219 public void propertyChange(PropertyChangeEvent e) { 220 String name = e.getPropertyName(); 221 if (name.equals("tiptext") || "font".equals(name) || 222 "foreground".equals(name)) { 223 JToolTip tip = ((JToolTip) e.getSource()); 227 String text = tip.getTipText(); 228 BasicHTML.updateRenderer(tip, text); 229 } 230 else if ("component".equals(name)) { 231 JToolTip tip = ((JToolTip) e.getSource()); 232 233 if (tip.getUI() instanceof BasicToolTipUI ) { 234 ((BasicToolTipUI )tip.getUI()).componentChanged(tip); 235 } 236 } 237 } 238 } 239 } 240 | Popular Tags |