This implements HTML support for tooltip. This is pretty barebones atm,
but good enough to not have HTML tags displaying in ToolTips. As with
all other Swing components, this will improve automagically as soon as
javax.swing.text.html is fixed. I am working on this atm and hope to get
it right before 0.93.
2006-08-13 Roman Kennke <[EMAIL PROTECTED]>
PR 28696
* javax/swing/plaf/basic/BasicHTML.java
(HTMLRootView.HTMLRootView): Trigger initial layout.
(HTMLRootView.setSize): Overridden to forward to real view.
* javax/swing/plaf/basic/BasicToolTipUI.java
(PropertyChangeHandler): New inner class. Updates the HTML
renderer.
(propertyChangeHandler): New field.
(getMaximumSize): Add HTML width delta.
(getMinimumSize): Add HTML width delta.
(getPreferredSize): Reimplemented to use HTML view for size
calculation if appropriate, otherwise use simple stringWidth()
measurement.
(installListeners): Install propertyChangeHandler.
(uninstallListeners): Uninstall propertyChangeHandler.
(installUI): Update HTML renderer.
(uninstallUI): Update HTML renderer.
(paint): Reimplemented to use HTML view for rendering if
appropriate, simple drawString otherwise.
* javax/swing/plaf/metal/MetalToolTipUI.java
(getPreferredSize): Call super and add accelerator delta.
(paint): Simply call super.
/Roman
Index: javax/swing/plaf/basic/BasicHTML.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicHTML.java,v
retrieving revision 1.4
diff -u -1 -2 -r1.4 BasicHTML.java
--- javax/swing/plaf/basic/BasicHTML.java 3 Mar 2006 11:06:11 -0000 1.4
+++ javax/swing/plaf/basic/BasicHTML.java 13 Aug 2006 21:33:13 -0000
@@ -98,24 +98,25 @@
private Document document;
/**
* Creates a new RootView.
*/
public HTMLRootView(JComponent c, View view, EditorKit kit, Document doc)
{
super(null);
component = c;
editorKit = kit;
document = doc;
setView(view);
+ setSize(view.getPreferredSpan(X_AXIS), view.getPreferredSpan(Y_AXIS));
}
/**
* Returns the ViewFactory for this RootView. If the current EditorKit
* provides a ViewFactory, this is used. Otherwise the TextUI itself
* is returned as a ViewFactory.
*
* @return the ViewFactory for this RootView
*/
public ViewFactory getViewFactory()
{
return editorKit.getViewFactory();
@@ -142,24 +143,32 @@
public void setView(View v)
{
if (view != null)
view.setParent(null);
if (v != null)
v.setParent(this);
view = v;
}
/**
+ * Overridden to forward to real view.
+ */
+ public void setSize(float w, float h)
+ {
+ view.setSize(w, h);
+ }
+
+ /**
* Returns the real root view, regardless of the index.
*
* @param index not used here
*
* @return the real root view, regardless of the index.
*/
public View getView(int index)
{
return view;
}
/**
Index: javax/swing/plaf/basic/BasicToolTipUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/basic/BasicToolTipUI.java,v
retrieving revision 1.11
diff -u -1 -2 -r1.11 BasicToolTipUI.java
--- javax/swing/plaf/basic/BasicToolTipUI.java 18 Oct 2005 22:10:32 -0000 1.11
+++ javax/swing/plaf/basic/BasicToolTipUI.java 13 Aug 2006 21:33:13 -0000
@@ -31,51 +31,79 @@
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing.plaf.basic;
import java.awt.Color;
import java.awt.Dimension;
+import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
-import java.awt.Toolkit;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
import javax.swing.JComponent;
import javax.swing.JToolTip;
import javax.swing.LookAndFeel;
-import javax.swing.SwingConstants;
-import javax.swing.SwingUtilities;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.ToolTipUI;
+import javax.swing.text.View;
/**
* This is the Basic Look and Feel UI class for JToolTip.
*/
public class BasicToolTipUI extends ToolTipUI
{
+ /**
+ * Receives notification when a property of the JToolTip changes.
+ * This updates the HTML renderer if appropriate.
+ */
+ private class PropertyChangeHandler
+ implements PropertyChangeListener
+ {
+
+ public void propertyChange(PropertyChangeEvent e)
+ {
+ String prop = e.getPropertyName();
+ if (prop.equals("tiptext") || prop.equals("font")
+ || prop.equals("foreground"))
+ {
+ JToolTip tip = (JToolTip) e.getSource();
+ String text = tip.getTipText();
+ BasicHTML.updateRenderer(tip, text);
+ }
+ }
+
+ }
+
/** The shared instance of BasicToolTipUI used for all ToolTips. */
private static BasicToolTipUI shared;
/** The tooltip's text */
private String text;
/**
+ * Handles property changes.
+ */
+ private PropertyChangeListener propertyChangeHandler;
+
+ /**
* Creates a new BasicToolTipUI object.
*/
public BasicToolTipUI()
{
super();
}
/**
* This method creates a new BasicToolTip UI for the given
* JComponent.
*
* @param c The JComponent to create a UI for.
@@ -89,157 +117,176 @@
return shared;
}
/**
* This method returns the msximum size of the given JComponent.
*
* @param c The JComponent to find a maximum size for.
*
* @return The maximum size.
*/
public Dimension getMaximumSize(JComponent c)
{
- return getPreferredSize(c);
+ Dimension d = getPreferredSize(c);
+ View view = (View) c.getClientProperty(BasicHTML.propertyKey);
+ if (view != null)
+ d.width += view.getMaximumSpan(View.X_AXIS)
+ - view.getPreferredSpan(View.X_AXIS);
+ return d;
}
/**
* This method returns the minimum size of the given JComponent.
*
* @param c The JComponent to find a minimum size for.
*
* @return The minimum size.
*/
public Dimension getMinimumSize(JComponent c)
{
- return getPreferredSize(c);
+ Dimension d = getPreferredSize(c);
+ View view = (View) c.getClientProperty(BasicHTML.propertyKey);
+ if (view != null)
+ d.width -= view.getPreferredSpan(View.X_AXIS)
+ - view.getMinimumSpan(View.X_AXIS);
+ return d;
}
/**
* This method returns the preferred size of the given JComponent.
*
* @param c The JComponent to find a preferred size for.
*
* @return The preferred size.
*/
public Dimension getPreferredSize(JComponent c)
{
JToolTip tip = (JToolTip) c;
- FontMetrics fm;
- Toolkit g = tip.getToolkit();
- text = tip.getTipText();
-
- Rectangle vr = new Rectangle();
- Rectangle ir = new Rectangle();
- Rectangle tr = new Rectangle();
- Insets insets = tip.getInsets();
- fm = g.getFontMetrics(tip.getFont());
- SwingUtilities.layoutCompoundLabel(tip, fm, text, null,
- SwingConstants.CENTER,
- SwingConstants.CENTER,
- SwingConstants.CENTER,
- SwingConstants.CENTER, vr, ir, tr, 0);
- return new Dimension(insets.left + tr.width + insets.right,
- insets.top + tr.height + insets.bottom);
+ String str = tip.getTipText();
+ FontMetrics fm = c.getFontMetrics(c.getFont());
+ Insets i = c.getInsets();
+ Dimension d = new Dimension(i.left + i.right, i.top + i.bottom);
+ if (str != null && ! str.equals(""))
+ {
+ View view = (View) c.getClientProperty(BasicHTML.propertyKey);
+ if (view != null)
+ {
+ d.width += (int) view.getPreferredSpan(View.X_AXIS);
+ d.height += (int) view.getPreferredSpan(View.Y_AXIS);
+ }
+ else
+ {
+ d.width += fm.stringWidth(str) + 6;
+ d.height += fm.getHeight();
+ }
+ }
+ return d;
}
/**
* This method installs the defaults for the given JComponent.
*
* @param c The JComponent to install defaults for.
*/
protected void installDefaults(JComponent c)
{
LookAndFeel.installColorsAndFont(c, "ToolTip.background",
"ToolTip.foreground", "ToolTip.font");
LookAndFeel.installBorder(c, "ToolTip.border");
}
/**
* This method installs the listeners for the given JComponent.
*
* @param c The JComponent to install listeners for.
*/
protected void installListeners(JComponent c)
{
- // TODO: Implement this properly.
+ propertyChangeHandler = new PropertyChangeHandler();
+ c.addPropertyChangeListener(propertyChangeHandler);
}
/**
* This method installs the UI for the given JComponent.
*
* @param c The JComponent to install the UI for.
*/
public void installUI(JComponent c)
{
c.setOpaque(true);
installDefaults(c);
+ BasicHTML.updateRenderer(c, ((JToolTip) c).getTipText());
installListeners(c);
}
/**
* This method paints the given JComponent with the given Graphics object.
*
* @param g The Graphics object to paint with.
* @param c The JComponent to paint.
*/
public void paint(Graphics g, JComponent c)
{
JToolTip tip = (JToolTip) c;
String text = tip.getTipText();
- Toolkit t = tip.getToolkit();
- if (text == null)
- return;
-
- Rectangle vr = new Rectangle();
- vr = SwingUtilities.calculateInnerArea(tip, vr);
- Rectangle ir = new Rectangle();
- Rectangle tr = new Rectangle();
- FontMetrics fm = t.getFontMetrics(tip.getFont());
+ Font font = c.getFont();
+ FontMetrics fm = c.getFontMetrics(font);
int ascent = fm.getAscent();
- SwingUtilities.layoutCompoundLabel(tip, fm, text, null,
- SwingConstants.CENTER,
- SwingConstants.CENTER,
- SwingConstants.CENTER,
- SwingConstants.CENTER, vr, ir, tr, 0);
+ Insets i = c.getInsets();
+ Dimension size = c.getSize();
+ Rectangle paintR = new Rectangle(i.left, i.top,
+ size.width - i.left - i.right,
+ size.height - i.top - i.bottom);
Color saved = g.getColor();
+ Font oldFont = g.getFont();
g.setColor(Color.BLACK);
- g.drawString(text, vr.x, vr.y + ascent);
+ View view = (View) c.getClientProperty(BasicHTML.propertyKey);
+ if (view != null)
+ view.paint(g, paintR);
+ else
+ g.drawString(text, paintR.x + 3, paintR.y + ascent);
+ g.setFont(oldFont);
g.setColor(saved);
}
/**
* This method uninstalls the defaults for the given JComponent.
*
* @param c The JComponent to uninstall defaults for.
*/
protected void uninstallDefaults(JComponent c)
{
c.setForeground(null);
c.setBackground(null);
c.setFont(null);
c.setBorder(null);
}
/**
* This method uninstalls listeners for the given JComponent.
*
* @param c The JComponent to uninstall listeners for.
*/
protected void uninstallListeners(JComponent c)
{
- // TODO: Implement this properly.
+ if (propertyChangeHandler != null)
+ {
+ c.removePropertyChangeListener(propertyChangeHandler);
+ propertyChangeHandler = null;
+ }
}
/**
* This method uninstalls the UI for the given JComponent.
*
* @param c The JComponent to uninstall.
*/
public void uninstallUI(JComponent c)
{
uninstallDefaults(c);
+ BasicHTML.updateRenderer(c, "");
uninstallListeners(c);
}
}
Index: javax/swing/plaf/metal/MetalToolTipUI.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/plaf/metal/MetalToolTipUI.java,v
retrieving revision 1.7
diff -u -1 -2 -r1.7 MetalToolTipUI.java
--- javax/swing/plaf/metal/MetalToolTipUI.java 11 May 2006 17:05:55 -0000 1.7
+++ javax/swing/plaf/metal/MetalToolTipUI.java 13 Aug 2006 21:33:13 -0000
@@ -34,37 +34,32 @@
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package javax.swing.plaf.metal;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
-import java.awt.Insets;
-import java.awt.Rectangle;
-import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JToolTip;
import javax.swing.KeyStroke;
-import javax.swing.SwingConstants;
-import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.UIResource;
import javax.swing.plaf.basic.BasicToolTipUI;
/**
* A UI delegate for the [EMAIL PROTECTED] JToolTip} component.
*/
public class MetalToolTipUI
extends BasicToolTipUI
{
@@ -183,93 +178,44 @@
return isAcceleratorHidden;
}
/**
* Returns the preferred size for the [EMAIL PROTECTED] JToolTip} component.
*
* @param c the component (a [EMAIL PROTECTED] JToolTip}).
*
* @return The preferred size.
*/
public Dimension getPreferredSize(JComponent c)
{
- if (isAcceleratorHidden())
- return super.getPreferredSize(c);
- else
+ Dimension d = super.getPreferredSize(c);
+ String acc = getAcceleratorString();
+ if (acc != null && ! acc.equals(""))
{
- Insets insets = c.getInsets();
- JToolTip tt = (JToolTip) c;
- String tipText = tt.getTipText();
- if (tipText != null)
- {
- FontMetrics fm = c.getFontMetrics(c.getFont());
- int prefH = fm.getHeight() + insets.top + insets.bottom;
- int prefW = fm.stringWidth(tipText) + insets.left + insets.right;
-
- // this seems to be the first opportunity we have to get the
- // accelerator string from the component (if it has one)
- acceleratorString = fetchAcceleratorString(c);
- if (acceleratorString != null)
- {
- prefW += padSpaceBetweenStrings;
- fm = c.getFontMetrics(acceleratorFont);
- prefW += fm.stringWidth(acceleratorString);
- }
- return new Dimension(prefW, prefH);
- }
- else return new Dimension(0, 0);
+ FontMetrics fm = c.getFontMetrics(c.getFont());
+ d.width += fm.stringWidth(acc);
}
+ return d;
}
/**
* Paints the tool tip.
*
* @param g the graphics context.
* @param c the [EMAIL PROTECTED] JToolTip} component.
*/
public void paint(Graphics g, JComponent c)
{
- JToolTip tip = (JToolTip) c;
-
- String text = tip.getTipText();
- Toolkit t = tip.getToolkit();
- if (text == null)
- return;
-
- Rectangle vr = new Rectangle();
- vr = SwingUtilities.calculateInnerArea(tip, vr);
- Rectangle ir = new Rectangle();
- Rectangle tr = new Rectangle();
- FontMetrics fm = t.getFontMetrics(tip.getFont());
- int ascent = fm.getAscent();
- SwingUtilities.layoutCompoundLabel(tip, fm, text, null,
- SwingConstants.CENTER, SwingConstants.LEFT,
- SwingConstants.CENTER, SwingConstants.CENTER, vr, ir, tr, 0);
- Color saved = g.getColor();
- g.setColor(Color.BLACK);
-
- g.drawString(text, vr.x, vr.y + ascent);
-
- // paint accelerator
- if (acceleratorString != null)
- {
- g.setFont(acceleratorFont);
- g.setColor(acceleratorForeground);
- fm = t.getFontMetrics(acceleratorFont);
- int width = fm.stringWidth(acceleratorString);
- g.drawString(acceleratorString, vr.x + vr.width - width
- - padSpaceBetweenStrings / 2, vr.y + vr.height - fm.getDescent());
- }
-
- g.setColor(saved);
+ super.paint(g, c);
+ // Somehow paint accelerator. Keep care for possible HTML rendering.
}
/**
* Returns a string representing the accelerator for the component, or
* <code>null</code> if the component has no accelerator.
*
* @param c the component.
*
* @return A string representing the accelerator (possibly
* <code>null</code>).
*/
private String fetchAcceleratorString(JComponent c)