1. /*
  2. * @(#)SynthToolTipUI.java 1.9 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.synth;
  8. import java.awt.*;
  9. import java.beans.PropertyChangeEvent;
  10. import java.beans.PropertyChangeListener;
  11. import javax.swing.*;
  12. import javax.swing.plaf.basic.BasicHTML;
  13. import javax.swing.plaf.basic.BasicToolTipUI;
  14. import javax.swing.plaf.ComponentUI;
  15. import javax.swing.text.View;
  16. import sun.swing.plaf.synth.SynthUI;
  17. /**
  18. * Synth's ToolTipUI.
  19. *
  20. * @version 1.9, 12/19/03
  21. * @author Joshua Outwater
  22. */
  23. class SynthToolTipUI extends BasicToolTipUI implements PropertyChangeListener,
  24. SynthUI {
  25. private SynthStyle style;
  26. public static ComponentUI createUI(JComponent c) {
  27. return new SynthToolTipUI();
  28. }
  29. protected void installDefaults(JComponent c) {
  30. updateStyle(c);
  31. }
  32. private void updateStyle(JComponent c) {
  33. SynthContext context = getContext(c, ENABLED);
  34. style = SynthLookAndFeel.updateStyle(context, this);
  35. context.dispose();
  36. }
  37. protected void uninstallDefaults(JComponent c) {
  38. SynthContext context = getContext(c, ENABLED);
  39. style.uninstallDefaults(context);
  40. context.dispose();
  41. style = null;
  42. }
  43. protected void installListeners(JComponent c) {
  44. c.addPropertyChangeListener(this);
  45. }
  46. protected void uninstallListeners(JComponent c) {
  47. c.removePropertyChangeListener(this);
  48. }
  49. public SynthContext getContext(JComponent c) {
  50. return getContext(c, getComponentState(c));
  51. }
  52. private SynthContext getContext(JComponent c, int state) {
  53. return SynthContext.getContext(SynthContext.class, c,
  54. SynthLookAndFeel.getRegion(c), style, state);
  55. }
  56. private Region getRegion(JComponent c) {
  57. return SynthLookAndFeel.getRegion(c);
  58. }
  59. private int getComponentState(JComponent c) {
  60. JComponent comp = ((JToolTip)c).getComponent();
  61. if (comp != null && !comp.isEnabled()) {
  62. return DISABLED;
  63. }
  64. return SynthLookAndFeel.getComponentState(c);
  65. }
  66. public void update(Graphics g, JComponent c) {
  67. SynthContext context = getContext(c);
  68. SynthLookAndFeel.update(context, g);
  69. context.getPainter().paintToolTipBackground(context,
  70. g, 0, 0, c.getWidth(), c.getHeight());
  71. paint(context, g);
  72. context.dispose();
  73. }
  74. public void paintBorder(SynthContext context, Graphics g, int x,
  75. int y, int w, int h) {
  76. context.getPainter().paintToolTipBorder(context, g, x, y, w, h);
  77. }
  78. public void paint(Graphics g, JComponent c) {
  79. SynthContext context = getContext(c);
  80. paint(context, g);
  81. context.dispose();
  82. }
  83. protected void paint(SynthContext context, Graphics g) {
  84. JToolTip tip = (JToolTip)context.getComponent();
  85. String tipText = tip.getToolTipText();
  86. Insets insets = tip.getInsets();
  87. View v = (View)tip.getClientProperty(BasicHTML.propertyKey);
  88. if (v != null) {
  89. Rectangle paintTextR = new Rectangle(insets.left, insets.top,
  90. tip.getWidth() - (insets.left + insets.right),
  91. tip.getHeight() - (insets.top + insets.bottom));
  92. v.paint(g, paintTextR);
  93. } else {
  94. g.setColor(context.getStyle().getColor(context,
  95. ColorType.TEXT_FOREGROUND));
  96. g.setFont(style.getFont(context));
  97. context.getStyle().getGraphicsUtils(context).paintText(
  98. context, g, tip.getTipText(), insets.left, insets.top, -1);
  99. }
  100. }
  101. public Dimension getPreferredSize(JComponent c) {
  102. SynthContext context = getContext(c);
  103. Insets insets = c.getInsets();
  104. Dimension prefSize = new Dimension(insets.left+insets.right,
  105. insets.top+insets.bottom);
  106. String text = ((JToolTip)c).getTipText();
  107. if (text != null) {
  108. View v = (c != null) ? (View) c.getClientProperty("html") : null;
  109. if (v != null) {
  110. prefSize.width += (int) v.getPreferredSpan(View.X_AXIS);
  111. prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
  112. } else {
  113. Font font = context.getStyle().getFont(context);
  114. FontMetrics fm = c.getFontMetrics(font);
  115. prefSize.width += context.getStyle().getGraphicsUtils(context).
  116. computeStringWidth(context, font, fm, text);
  117. prefSize.height += fm.getHeight();
  118. }
  119. }
  120. context.dispose();
  121. return prefSize;
  122. }
  123. public void propertyChange(PropertyChangeEvent e) {
  124. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  125. updateStyle((JToolTip)e.getSource());
  126. }
  127. String name = e.getPropertyName();
  128. if (name.equals("tiptext") || "font".equals(name) ||
  129. "foreground".equals(name)) {
  130. // remove the old html view client property if one
  131. // existed, and install a new one if the text installed
  132. // into the JLabel is html source.
  133. JToolTip tip = ((JToolTip) e.getSource());
  134. String text = tip.getTipText();
  135. BasicHTML.updateRenderer(tip, text);
  136. }
  137. }
  138. }