1 7 8 package javax.swing.plaf.synth; 9 10 import java.awt.*; 11 import java.beans.PropertyChangeEvent ; 12 import java.beans.PropertyChangeListener ; 13 14 import javax.swing.*; 15 import javax.swing.plaf.basic.BasicHTML ; 16 import javax.swing.plaf.basic.BasicToolTipUI ; 17 import javax.swing.plaf.ComponentUI ; 18 import javax.swing.text.View ; 19 import sun.swing.plaf.synth.SynthUI; 20 21 22 28 class SynthToolTipUI extends BasicToolTipUI implements PropertyChangeListener , 29 SynthUI { 30 private SynthStyle style; 31 32 33 public static ComponentUI createUI(JComponent c) { 34 return new SynthToolTipUI (); 35 } 36 37 protected void installDefaults(JComponent c) { 38 updateStyle(c); 39 } 40 41 private void updateStyle(JComponent c) { 42 SynthContext context = getContext(c, ENABLED); 43 style = SynthLookAndFeel.updateStyle(context, this); 44 context.dispose(); 45 } 46 47 protected void uninstallDefaults(JComponent c) { 48 SynthContext context = getContext(c, ENABLED); 49 style.uninstallDefaults(context); 50 context.dispose(); 51 style = null; 52 } 53 54 protected void installListeners(JComponent c) { 55 c.addPropertyChangeListener(this); 56 } 57 58 protected void uninstallListeners(JComponent c) { 59 c.removePropertyChangeListener(this); 60 } 61 62 public SynthContext getContext(JComponent c) { 63 return getContext(c, getComponentState(c)); 64 } 65 66 private SynthContext getContext(JComponent c, int state) { 67 return SynthContext.getContext(SynthContext .class, c, 68 SynthLookAndFeel.getRegion(c), style, state); 69 } 70 71 private Region getRegion(JComponent c) { 72 return SynthLookAndFeel.getRegion(c); 73 } 74 75 private int getComponentState(JComponent c) { 76 JComponent comp = ((JToolTip)c).getComponent(); 77 78 if (comp != null && !comp.isEnabled()) { 79 return DISABLED; 80 } 81 return SynthLookAndFeel.getComponentState(c); 82 } 83 84 public void update(Graphics g, JComponent c) { 85 SynthContext context = getContext(c); 86 87 SynthLookAndFeel.update(context, g); 88 context.getPainter().paintToolTipBackground(context, 89 g, 0, 0, c.getWidth(), c.getHeight()); 90 paint(context, g); 91 context.dispose(); 92 } 93 94 public void paintBorder(SynthContext context, Graphics g, int x, 95 int y, int w, int h) { 96 context.getPainter().paintToolTipBorder(context, g, x, y, w, h); 97 } 98 99 public void paint(Graphics g, JComponent c) { 100 SynthContext context = getContext(c); 101 102 paint(context, g); 103 context.dispose(); 104 } 105 106 protected void paint(SynthContext context, Graphics g) { 107 JToolTip tip = (JToolTip)context.getComponent(); 108 String tipText = tip.getToolTipText(); 109 110 Insets insets = tip.getInsets(); 111 View v = (View )tip.getClientProperty(BasicHTML.propertyKey); 112 if (v != null) { 113 Rectangle paintTextR = new Rectangle(insets.left, insets.top, 114 tip.getWidth() - (insets.left + insets.right), 115 tip.getHeight() - (insets.top + insets.bottom)); 116 v.paint(g, paintTextR); 117 } else { 118 g.setColor(context.getStyle().getColor(context, 119 ColorType.TEXT_FOREGROUND)); 120 g.setFont(style.getFont(context)); 121 context.getStyle().getGraphicsUtils(context).paintText( 122 context, g, tip.getTipText(), insets.left, insets.top, -1); 123 } 124 } 125 126 public Dimension getPreferredSize(JComponent c) { 127 SynthContext context = getContext(c); 128 Insets insets = c.getInsets(); 129 Dimension prefSize = new Dimension(insets.left+insets.right, 130 insets.top+insets.bottom); 131 String text = ((JToolTip)c).getTipText(); 132 133 if (text != null) { 134 View v = (c != null) ? (View ) c.getClientProperty("html") : null; 135 if (v != null) { 136 prefSize.width += (int) v.getPreferredSpan(View.X_AXIS); 137 prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS); 138 } else { 139 Font font = context.getStyle().getFont(context); 140 FontMetrics fm = c.getFontMetrics(font); 141 prefSize.width += context.getStyle().getGraphicsUtils(context). 142 computeStringWidth(context, font, fm, text); 143 prefSize.height += fm.getHeight(); 144 } 145 } 146 context.dispose(); 147 return prefSize; 148 } 149 150 public void propertyChange(PropertyChangeEvent e) { 151 if (SynthLookAndFeel.shouldUpdateStyle(e)) { 152 updateStyle((JToolTip)e.getSource()); 153 } 154 String name = e.getPropertyName(); 155 if (name.equals("tiptext") || "font".equals(name) || 156 "foreground".equals(name)) { 157 JToolTip tip = ((JToolTip) e.getSource()); 161 String text = tip.getTipText(); 162 BasicHTML.updateRenderer(tip, text); 163 } 164 } 165 } 166 | Popular Tags |