2
* Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* o Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* o Neither the name of Substance Kirill Grouchnikov nor the names of
15
* its contributors may be used to endorse or promote products derived
16
* from this software without specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
package org.pushingpixels.substance.internal.ui;
34
import javax.swing.JComponent;
35
import javax.swing.JToolTip;
36
import javax.swing.plaf.ComponentUI;
37
import javax.swing.plaf.basic.BasicHTML;
38
import javax.swing.plaf.basic.BasicToolTipUI;
39
import javax.swing.text.View;
41
import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities;
42
import org.pushingpixels.substance.internal.utils.SubstanceTextUtilities;
45
* UI for tool tips in <b>Substance</b> look and feel.
47
* @author Kirill Grouchnikov
49
public class SubstanceToolTipUI extends BasicToolTipUI {
51
* Creates a UI delegate for the specified component.
55
* @return UI delegate.
57
public static ComponentUI createUI(JComponent comp) {
58
SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
59
return new SubstanceToolTipUI();
65
* @see javax.swing.plaf.basic.BasicToolTipUI#paint(java.awt.Graphics,
66
* javax.swing.JComponent)
69
public void paint(Graphics g, JComponent c) {
70
Font font = c.getFont();
71
// FontMetrics metrics = c.getFontMetrics(font);
72
Dimension size = c.getSize();
74
g.setColor(c.getBackground());
75
g.fillRect(0, 0, size.width, size.height);
77
g.setColor(c.getForeground());
79
// fix for bug 4153892
80
String tipText = ((JToolTip) c).getTipText();
81
if (tipText == null) {
85
Insets insets = c.getInsets();
86
Rectangle paintTextR = new Rectangle(insets.left + 3, insets.top,
87
size.width - (insets.left + insets.right + 6), size.height
88
- (insets.top + insets.bottom + 2));
89
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
91
v.paint(g, paintTextR);
93
SubstanceTextUtilities.paintText(g, c, paintTextR, tipText, -1,
94
font, c.getForeground(), null);
99
public Dimension getPreferredSize(JComponent c) {
100
Font font = c.getFont();
101
Insets insets = c.getInsets();
103
Dimension prefSize = new Dimension(insets.left + insets.right,
104
insets.top + insets.bottom);
105
String text = ((JToolTip) c).getTipText();
107
if ((text == null) || text.equals("")) {
110
View v = (c != null) ? (View) c.getClientProperty("html") : null;
112
// fix for 302 - add extra pixels for the HTML view as well
113
prefSize.width += (int) (v.getPreferredSpan(View.X_AXIS) + 6);
114
prefSize.height += (int) (v.getPreferredSpan(View.Y_AXIS) + 2);
116
FontMetrics fm = c.getFontMetrics(font);
117
prefSize.width += fm.stringWidth(text) + 6;
118
prefSize.height += fm.getHeight() + 2;