001: /*
002: * AcceleratorToolTipUI.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.plaf.base;
023:
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.FontMetrics;
027: import java.awt.Graphics;
028: import java.awt.Insets;
029: import java.awt.Rectangle;
030: import java.awt.event.KeyEvent;
031: import javax.swing.AbstractButton;
032: import javax.swing.Action;
033: import javax.swing.JComponent;
034: import javax.swing.JToolTip;
035: import javax.swing.KeyStroke;
036: import javax.swing.plaf.basic.BasicToolTipUI;
037:
038: import org.underworldlabs.Constants;
039: import org.underworldlabs.util.MiscUtils;
040:
041: /* ----------------------------------------------------------
042: * CVS NOTE: Changes to the CVS repository prior to the
043: * release of version 3.0.0beta1 has meant a
044: * resetting of CVS revision numbers.
045: * ----------------------------------------------------------
046: */
047:
048: /**
049: *
050: * @author Takis Diakoumis
051: * @version $Revision: 1.4 $
052: * @date $Date: 2006/05/14 06:56:07 $
053: */
054: public class AcceleratorToolTipUI extends BasicToolTipUI {
055:
056: private static String delimiter = "+";
057:
058: public AcceleratorToolTipUI() {
059: super ();
060: }
061:
062: public void paint(Graphics g, JComponent c) {
063: Font font = c.getFont();
064: FontMetrics metrics = c.getFontMetrics(font);
065:
066: Dimension size = c.getSize();
067: if (c.isOpaque()) {
068: g.setColor(c.getBackground());
069: g.fillRect(0, 0, size.width + 20, size.height);
070: }
071:
072: g.setColor(c.getForeground());
073: g.setFont(font);
074:
075: JToolTip tip = (JToolTip) c;
076: String keyText = getAccelerator(tip);
077:
078: if (!MiscUtils.isNull(keyText)) {
079: Insets insets = c.getInsets();
080: Rectangle paintTextR = new Rectangle(insets.left,
081: insets.top, size.width
082: - (insets.left + insets.right), size.height
083: - (insets.top + insets.bottom));
084: g.drawString(keyText, paintTextR.x + 3, paintTextR.y
085: + metrics.getAscent());
086: }
087:
088: }
089:
090: public Dimension getPreferredSize(JComponent c) {
091: Dimension d = super .getPreferredSize(c);
092:
093: JToolTip tip = (JToolTip) c;
094: String keyText = getAccelerator(tip);
095:
096: if (!MiscUtils.isNull(keyText)) {
097: Font font = c.getFont();
098: FontMetrics fm = c.getFontMetrics(font);
099: d.width = fm.stringWidth(keyText) + 8;
100: }
101: return d;
102: }
103:
104: private String getAccelerator(JToolTip tip) {
105: String text = tip.getTipText();
106: if (text == null) {
107: text = Constants.EMPTY;
108: }
109:
110: Action action = ((AbstractButton) tip.getComponent())
111: .getAction();
112:
113: if (action != null) {
114: String modText = null;
115: KeyStroke keyStroke = (KeyStroke) action
116: .getValue(Action.ACCELERATOR_KEY);
117:
118: if (keyStroke != null) {
119: int mod = keyStroke.getModifiers();
120: modText = KeyEvent.getKeyModifiersText(mod);
121:
122: if (!MiscUtils.isNull(modText)) {
123: modText += delimiter;
124: }
125:
126: String keyText = KeyEvent.getKeyText(keyStroke
127: .getKeyCode());
128: if (!MiscUtils.isNull(keyText)) {
129: modText += keyText;
130: }
131:
132: }
133:
134: if (!MiscUtils.isNull(modText)) {
135: text = text + " (" + modText + ")";
136: }
137:
138: }
139: return text;
140: }
141:
142: }
|