FileDocCategorySizeDatePackage
MetalToolTipUI.javaAPI DocJava SE 5 API5990Fri Aug 26 14:58:08 BST 2005javax.swing.plaf.metal

MetalToolTipUI

public class MetalToolTipUI extends BasicToolTipUI
A Metal L&F extension of BasicToolTipUI.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.28 12/19/03
author
Steve Wilson

Fields Summary
static MetalToolTipUI
sharedInstance
private Font
smallFont
private JToolTip
tip
public static final int
padSpaceBetweenStrings
private String
acceleratorDelimiter
Constructors Summary
public MetalToolTipUI()


      
        super();
    
Methods Summary
public static javax.swing.plaf.ComponentUIcreateUI(javax.swing.JComponent c)

        return sharedInstance;
    
private java.lang.StringgetAcceleratorString(javax.swing.JToolTip tip)

        this.tip = tip;

        String retValue = getAcceleratorString();

        this.tip = null;
        return retValue;
    
public java.lang.StringgetAcceleratorString()

        if (tip == null || isAcceleratorHidden()) {
            return "";
        }
        JComponent comp = tip.getComponent();
	if (comp == null) {
	    return "";
	}

	KeyStroke[] keys;

	if (comp instanceof JTabbedPane) {
	  TabbedPaneUI ui = ( (JTabbedPane)(comp) ).getUI();
	  if (ui instanceof MetalTabbedPaneUI) {		
	    // if comp is instance of JTabbedPane and have 'metal' look-and-feel
	    // detect tab the mouse is over now
	    int rolloverTabIndex = ( (MetalTabbedPaneUI)ui ).getRolloverTabIndex();
       	    if  (rolloverTabIndex == -1)
    	      keys = new KeyStroke[0];
    	    else {
    	      // detect mnemonic for this tab
    	      int mnemonic = ((JTabbedPane)comp).getMnemonicAt(rolloverTabIndex);
    	      if (mnemonic == -1)
 	        keys = new KeyStroke[0];
 	      else {
 	        // and store it as mnemonic for the component
                KeyStroke keyStroke = KeyStroke.getKeyStroke(mnemonic, Event.ALT_MASK);
                keys = new KeyStroke[1];
                keys[0] = keyStroke;
              }
            }
          }
          else
            keys = comp.getRegisteredKeyStrokes();
        }
	else
	  keys = comp.getRegisteredKeyStrokes();

	String controlKeyStr = "";

	for (int i = 0; i < keys.length; i++) {
	  int mod = keys[i].getModifiers();
	  int condition =  comp.getConditionForKeyStroke(keys[i]);

	  if ( condition == JComponent.WHEN_IN_FOCUSED_WINDOW )
	  {
	      controlKeyStr = KeyEvent.getKeyModifiersText(mod) +
                              acceleratorDelimiter +
                              KeyEvent.getKeyText(keys[i].getKeyCode());
	      break;
	  }
	}

	/* Special case for menu item since they do not register a
	   keyboard action for their mnemonics and they always use Alt */
	if ( controlKeyStr.equals("") && comp instanceof JMenuItem )
	{
	    int mnemonic = ((JMenuItem) comp).getMnemonic();
	    if ( mnemonic != 0 )
	    {
	        controlKeyStr =
                    KeyEvent.getKeyModifiersText(KeyEvent.ALT_MASK) +
                    acceleratorDelimiter + (char)mnemonic;
	    }
	}

	return controlKeyStr;
    
public java.awt.DimensiongetPreferredSize(javax.swing.JComponent c)

	Dimension d = super.getPreferredSize(c);

	String key = getAcceleratorString((JToolTip)c);
	if (! (key.equals(""))) {
            FontMetrics fm = c.getFontMetrics(smallFont);	
	    d.width += SwingUtilities2.stringWidth(c, fm, key) +
                            padSpaceBetweenStrings;
	}
        return d;
    
public voidinstallUI(javax.swing.JComponent c)

        super.installUI(c);
        tip = (JToolTip)c;
	Font f = c.getFont();
	smallFont = new Font( f.getName(), f.getStyle(), f.getSize() - 2 );
	acceleratorDelimiter = UIManager.getString( "MenuItem.acceleratorDelimiter" );
	if ( acceleratorDelimiter == null ) { acceleratorDelimiter = "-"; }
    
protected booleanisAcceleratorHidden()

        Boolean b = (Boolean)UIManager.get("ToolTip.hideAccelerator");
        return b != null && b.booleanValue();
    
public voidpaint(java.awt.Graphics g, javax.swing.JComponent c)

        JToolTip tip = (JToolTip)c;

	super.paint(g, c);

        Font font = c.getFont();
        FontMetrics metrics = SwingUtilities2.getFontMetrics(c, g, font);
	String keyText = getAcceleratorString(tip);
	String tipText = tip.getTipText();
	if (tipText == null) {
	    tipText = "";
	}
	if (! (keyText.equals(""))) {  // only draw control key if there is one
	    g.setFont(smallFont);
	    g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
	    SwingUtilities2.drawString(tip, g, keyText,
                         SwingUtilities2.stringWidth(
                         tip, metrics, tipText) + padSpaceBetweenStrings, 
		         2 + metrics.getAscent());
	}
    
public voiduninstallUI(javax.swing.JComponent c)

        super.uninstallUI(c);
        tip = null;