1. /*
  2. * @(#)SynthToolTipUI.java 1.5 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.gtk;
  8. import java.awt.*;
  9. import java.beans.PropertyChangeEvent;
  10. import java.beans.PropertyChangeListener;
  11. import javax.swing.*;
  12. import javax.swing.BorderFactory;
  13. import javax.swing.border.Border;
  14. import javax.swing.plaf.basic.BasicHTML;
  15. import javax.swing.plaf.ToolTipUI;
  16. import javax.swing.plaf.ComponentUI;
  17. import javax.swing.plaf.UIResource;
  18. import javax.swing.text.View;
  19. /**
  20. * Standard tool tip L&F.
  21. * <p>
  22. *
  23. * @version 1.5, 01/23/03 (based on BasicToolTipUI v 1.36)
  24. * @author Dave Moore
  25. */
  26. class SynthToolTipUI extends ToolTipUI implements PropertyChangeListener,
  27. SynthUI {
  28. private SynthStyle style;
  29. public static ComponentUI createUI(JComponent c) {
  30. return new SynthToolTipUI();
  31. }
  32. public SynthToolTipUI() {
  33. super();
  34. }
  35. public void installUI(JComponent c) {
  36. installDefaults(c);
  37. installComponents(c);
  38. installListeners(c);
  39. }
  40. public void uninstallUI(JComponent c) {
  41. // REMIND: this is NOT getting called
  42. uninstallDefaults(c);
  43. uninstallComponents(c);
  44. uninstallListeners(c);
  45. }
  46. protected void installDefaults(JComponent c){
  47. fetchStyle(c);
  48. componentChanged(c);
  49. }
  50. private void fetchStyle(JComponent c) {
  51. SynthContext context = getContext(c, ENABLED);
  52. style = SynthLookAndFeel.updateStyle(context, this);
  53. context.dispose();
  54. }
  55. protected void uninstallDefaults(JComponent c){
  56. SynthContext context = getContext(c, ENABLED);
  57. style.uninstallDefaults(context);
  58. context.dispose();
  59. style = null;
  60. }
  61. /* Unfortunately this has to remain private until we can make API additions.
  62. */
  63. private void installComponents(JComponent c){
  64. BasicHTML.updateRenderer(c, ((JToolTip)c).getTipText());
  65. }
  66. /* Unfortunately this has to remain private until we can make API additions.
  67. */
  68. private void uninstallComponents(JComponent c){
  69. BasicHTML.updateRenderer(c, "");
  70. }
  71. protected void installListeners(JComponent c) {
  72. c.addPropertyChangeListener(this);
  73. }
  74. protected void uninstallListeners(JComponent c) {
  75. c.removePropertyChangeListener(this);
  76. }
  77. public SynthContext getContext(JComponent c) {
  78. return getContext(c, getComponentState(c));
  79. }
  80. private SynthContext getContext(JComponent c, int state) {
  81. return SynthContext.getContext(SynthContext.class, c,
  82. SynthLookAndFeel.getRegion(c), style, state);
  83. }
  84. private Region getRegion(JComponent c) {
  85. return SynthLookAndFeel.getRegion(c);
  86. }
  87. private int getComponentState(JComponent c) {
  88. JComponent comp = ((JToolTip)c).getComponent();
  89. if (comp != null && !comp.isEnabled()) {
  90. return DISABLED;
  91. }
  92. return SynthLookAndFeel.getComponentState(c);
  93. }
  94. public void update(Graphics g, JComponent c) {
  95. SynthContext context = getContext(c);
  96. SynthLookAndFeel.update(context, g);
  97. paint(context, g);
  98. context.dispose();
  99. }
  100. public void paint(Graphics g, JComponent c) {
  101. SynthContext context = getContext(c);
  102. paint(context, g);
  103. context.dispose();
  104. }
  105. protected void paint(SynthContext context, Graphics g) {
  106. JToolTip tip = (JToolTip)context.getComponent();
  107. String tipText = tip.getToolTipText();
  108. Insets insets = tip.getInsets();
  109. View v = (View)tip.getClientProperty(BasicHTML.propertyKey);
  110. if (v != null) {
  111. Rectangle paintTextR = new Rectangle(insets.left, insets.top,
  112. tip.getWidth() - (insets.left + insets.right),
  113. tip.getHeight() - (insets.top + insets.bottom));
  114. v.paint(g, paintTextR);
  115. } else {
  116. g.setColor(context.getStyle().getColor(context,
  117. ColorType.TEXT_FOREGROUND));
  118. g.setFont(style.getFont(context));
  119. context.getStyle().getSynthGraphics(context).paintText(
  120. context, g, tip.getTipText(), insets.left, insets.top, -1);
  121. }
  122. }
  123. public Dimension getPreferredSize(JComponent c) {
  124. SynthContext context = getContext(c);
  125. Insets insets = c.getInsets();
  126. Dimension prefSize = new Dimension(insets.left+insets.right,
  127. insets.top+insets.bottom);
  128. String text = ((JToolTip)c).getTipText();
  129. if (text != null) {
  130. View v = (c != null) ? (View) c.getClientProperty("html") : null;
  131. if (v != null) {
  132. prefSize.width += (int) v.getPreferredSpan(View.X_AXIS);
  133. prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS);
  134. } else {
  135. Font font = context.getStyle().getFont(context);
  136. FontMetrics fm = Toolkit.getDefaultToolkit().
  137. getFontMetrics(font);
  138. prefSize.width += context.getStyle().getSynthGraphics(context).
  139. computeStringWidth(context, font, fm, text);
  140. prefSize.height += fm.getHeight();
  141. }
  142. }
  143. context.dispose();
  144. return prefSize;
  145. }
  146. public Dimension getMinimumSize(JComponent c) {
  147. Dimension d = getPreferredSize(c);
  148. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  149. if (v != null) {
  150. d.width -= v.getPreferredSpan(View.X_AXIS) -
  151. v.getMinimumSpan(View.X_AXIS);
  152. }
  153. return d;
  154. }
  155. public Dimension getMaximumSize(JComponent c) {
  156. Dimension d = getPreferredSize(c);
  157. View v = (View) c.getClientProperty(BasicHTML.propertyKey);
  158. if (v != null) {
  159. d.width += v.getMaximumSpan(View.X_AXIS) -
  160. v.getPreferredSpan(View.X_AXIS);
  161. }
  162. return d;
  163. }
  164. /**
  165. * Invoked when the <code>JCompoment</code> associated with the
  166. * <code>JToolTip</code> has changed, or at initialization time. This
  167. * should update any state dependant upon the <code>JComponent</code>.
  168. *
  169. * @param c the JToolTip the JComponent has changed on.
  170. */
  171. private void componentChanged(JComponent c) {
  172. }
  173. public void propertyChange(PropertyChangeEvent e) {
  174. String name = e.getPropertyName();
  175. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  176. fetchStyle((JToolTip)e.getSource());
  177. }
  178. if (name.equals("tiptext") || "font".equals(name) ||
  179. "foreground".equals(name)) {
  180. // remove the old html view client property if one
  181. // existed, and install a new one if the text installed
  182. // into the JLabel is html source.
  183. JToolTip tip = ((JToolTip) e.getSource());
  184. String text = tip.getTipText();
  185. BasicHTML.updateRenderer(tip, text);
  186. }
  187. else if ("component".equals(name)) {
  188. componentChanged((JToolTip)e.getSource());
  189. }
  190. }
  191. }