~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/ui/SubstanceToolTipUI.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 *  o Redistributions of source code must retain the above copyright notice,
 
8
 *    this list of conditions and the following disclaimer.
 
9
 *
 
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.
 
13
 *
 
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.
 
17
 *
 
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.
 
29
 */
 
30
package org.pushingpixels.substance.internal.ui;
 
31
 
 
32
import java.awt.*;
 
33
 
 
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;
 
40
 
 
41
import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities;
 
42
import org.pushingpixels.substance.internal.utils.SubstanceTextUtilities;
 
43
 
 
44
/**
 
45
 * UI for tool tips in <b>Substance</b> look and feel.
 
46
 * 
 
47
 * @author Kirill Grouchnikov
 
48
 */
 
49
public class SubstanceToolTipUI extends BasicToolTipUI {
 
50
        /**
 
51
         * Creates a UI delegate for the specified component.
 
52
         * 
 
53
         * @param comp
 
54
         *            Component.
 
55
         * @return UI delegate.
 
56
         */
 
57
        public static ComponentUI createUI(JComponent comp) {
 
58
                SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
 
59
                return new SubstanceToolTipUI();
 
60
        }
 
61
 
 
62
        /*
 
63
         * (non-Javadoc)
 
64
         * 
 
65
         * @see javax.swing.plaf.basic.BasicToolTipUI#paint(java.awt.Graphics,
 
66
         * javax.swing.JComponent)
 
67
         */
 
68
        @Override
 
69
        public void paint(Graphics g, JComponent c) {
 
70
                Font font = c.getFont();
 
71
                // FontMetrics metrics = c.getFontMetrics(font);
 
72
                Dimension size = c.getSize();
 
73
                if (c.isOpaque()) {
 
74
                        g.setColor(c.getBackground());
 
75
                        g.fillRect(0, 0, size.width, size.height);
 
76
                }
 
77
                g.setColor(c.getForeground());
 
78
                g.setFont(font);
 
79
                // fix for bug 4153892
 
80
                String tipText = ((JToolTip) c).getTipText();
 
81
                if (tipText == null) {
 
82
                        tipText = "";
 
83
                }
 
84
 
 
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);
 
90
                if (v != null) {
 
91
                        v.paint(g, paintTextR);
 
92
                } else {
 
93
                        SubstanceTextUtilities.paintText(g, c, paintTextR, tipText, -1,
 
94
                                        font, c.getForeground(), null);
 
95
                }
 
96
        }
 
97
 
 
98
        @Override
 
99
        public Dimension getPreferredSize(JComponent c) {
 
100
                Font font = c.getFont();
 
101
                Insets insets = c.getInsets();
 
102
 
 
103
                Dimension prefSize = new Dimension(insets.left + insets.right,
 
104
                                insets.top + insets.bottom);
 
105
                String text = ((JToolTip) c).getTipText();
 
106
 
 
107
                if ((text == null) || text.equals("")) {
 
108
                        text = "";
 
109
                } else {
 
110
                        View v = (c != null) ? (View) c.getClientProperty("html") : null;
 
111
                        if (v != 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);
 
115
                        } else {
 
116
                                FontMetrics fm = c.getFontMetrics(font);
 
117
                                prefSize.width += fm.stringWidth(text) + 6;
 
118
                                prefSize.height += fm.getHeight() + 2;
 
119
                        }
 
120
                }
 
121
                return prefSize;
 
122
        }
 
123
 
 
124
}