001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.jvnet.substance;
031:
032: import java.awt.*;
033:
034: import javax.swing.JComponent;
035: import javax.swing.JToolTip;
036: import javax.swing.plaf.ComponentUI;
037: import javax.swing.plaf.basic.BasicHTML;
038: import javax.swing.plaf.basic.BasicToolTipUI;
039: import javax.swing.text.View;
040:
041: import org.jvnet.substance.painter.text.SubstanceTextPainter;
042:
043: /**
044: * UI for tool tips in <b>Substance</b> look and feel.
045: *
046: * @author Kirill Grouchnikov
047: */
048: public class SubstanceToolTipUI extends BasicToolTipUI {
049: /**
050: * Creates a UI delegate for the specified component.
051: *
052: * @param c
053: * Component.
054: * @return UI delegate.
055: */
056: public static ComponentUI createUI(JComponent c) {
057: return new SubstanceToolTipUI();
058: }
059:
060: /*
061: * (non-Javadoc)
062: *
063: * @see javax.swing.plaf.basic.BasicToolTipUI#paint(java.awt.Graphics,
064: * javax.swing.JComponent)
065: */
066: @Override
067: public void paint(Graphics g, JComponent c) {
068: Font font = c.getFont();
069: // FontMetrics metrics = c.getFontMetrics(font);
070: Dimension size = c.getSize();
071: if (c.isOpaque()) {
072: g.setColor(c.getBackground());
073: g.fillRect(0, 0, size.width, size.height);
074: }
075: g.setColor(c.getForeground());
076: g.setFont(font);
077: // fix for bug 4153892
078: String tipText = ((JToolTip) c).getTipText();
079: if (tipText == null) {
080: tipText = "";
081: }
082:
083: Insets insets = c.getInsets();
084: Rectangle paintTextR = new Rectangle(insets.left + 3,
085: insets.top, size.width
086: - (insets.left + insets.right + 6), size.height
087: - (insets.top + insets.bottom + 2));
088: View v = (View) c.getClientProperty(BasicHTML.propertyKey);
089: if (v != null) {
090: v.paint(g, paintTextR);
091: } else {
092: SubstanceTextPainter textPainter = SubstanceLookAndFeel
093: .getCurrentTextPainter();
094: textPainter.init(c, null, true);
095: textPainter.setBackgroundFill(c, c.getBackground(), false,
096: 0, 0);
097: textPainter.attachText(c, paintTextR, tipText, -1, font, c
098: .getForeground(), null);
099: textPainter.renderSurface(g);
100: }
101: }
102:
103: @Override
104: public Dimension getPreferredSize(JComponent c) {
105: Font font = c.getFont();
106: Insets insets = c.getInsets();
107:
108: Dimension prefSize = new Dimension(insets.left + insets.right,
109: insets.top + insets.bottom);
110: String text = ((JToolTip) c).getTipText();
111:
112: if ((text == null) || text.equals("")) {
113: text = "";
114: } else {
115: View v = (c != null) ? (View) c.getClientProperty("html")
116: : null;
117: if (v != null) {
118: // fix for 302 - add extra pixels for the HTML view as well
119: prefSize.width += (int) (v
120: .getPreferredSpan(View.X_AXIS) + 6);
121: prefSize.height += (int) (v
122: .getPreferredSpan(View.Y_AXIS) + 2);
123: } else {
124: SubstanceTextPainter textPainter = SubstanceLookAndFeel
125: .getCurrentTextPainter();
126: Dimension textBounds = textPainter.getTextBounds(c,
127: font, text);
128: prefSize.width += textBounds.width + 6;
129: prefSize.height += textBounds.height + 2;
130: }
131: }
132: return prefSize;
133: }
134:
135: }
|