author | serb |
Mon, 07 Apr 2014 15:34:27 +0400 | |
changeset 24163 | d56df89854c4 |
parent 23715 | 54ae9dd9df73 |
child 25761 | c408b10ef757 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21278
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package javax.swing.plaf.basic; |
|
27 |
||
28 |
import sun.swing.SwingUtilities2; |
|
29 |
import java.awt.*; |
|
30 |
import java.beans.PropertyChangeEvent; |
|
31 |
import java.beans.PropertyChangeListener; |
|
32 |
||
33 |
import javax.swing.*; |
|
34 |
import javax.swing.BorderFactory; |
|
35 |
import javax.swing.border.Border; |
|
36 |
import javax.swing.plaf.ToolTipUI; |
|
37 |
import javax.swing.plaf.ComponentUI; |
|
38 |
import javax.swing.plaf.UIResource; |
|
39 |
import javax.swing.text.View; |
|
40 |
||
41 |
||
42 |
/** |
|
20169 | 43 |
* Standard tool tip L&F. |
2 | 44 |
* |
45 |
* @author Dave Moore |
|
46 |
*/ |
|
47 |
public class BasicToolTipUI extends ToolTipUI |
|
48 |
{ |
|
49 |
static BasicToolTipUI sharedInstance = new BasicToolTipUI(); |
|
50 |
/** |
|
51 |
* Global <code>PropertyChangeListener</code> that |
|
52 |
* <code>createPropertyChangeListener</code> returns. |
|
53 |
*/ |
|
54 |
private static PropertyChangeListener sharedPropertyChangedListener; |
|
55 |
||
56 |
private PropertyChangeListener propertyChangeListener; |
|
57 |
||
58 |
public static ComponentUI createUI(JComponent c) { |
|
59 |
return sharedInstance; |
|
60 |
} |
|
61 |
||
62 |
public BasicToolTipUI() { |
|
63 |
super(); |
|
64 |
} |
|
65 |
||
66 |
public void installUI(JComponent c) { |
|
67 |
installDefaults(c); |
|
68 |
installComponents(c); |
|
69 |
installListeners(c); |
|
70 |
} |
|
71 |
||
72 |
public void uninstallUI(JComponent c) { |
|
73 |
// REMIND: this is NOT getting called |
|
74 |
uninstallDefaults(c); |
|
75 |
uninstallComponents(c); |
|
76 |
uninstallListeners(c); |
|
77 |
} |
|
78 |
||
79 |
protected void installDefaults(JComponent c){ |
|
80 |
LookAndFeel.installColorsAndFont(c, "ToolTip.background", |
|
81 |
"ToolTip.foreground", |
|
82 |
"ToolTip.font"); |
|
83 |
LookAndFeel.installProperty(c, "opaque", Boolean.TRUE); |
|
84 |
componentChanged(c); |
|
85 |
} |
|
86 |
||
87 |
protected void uninstallDefaults(JComponent c){ |
|
88 |
LookAndFeel.uninstallBorder(c); |
|
89 |
} |
|
90 |
||
91 |
/* Unfortunately this has to remain private until we can make API additions. |
|
92 |
*/ |
|
93 |
private void installComponents(JComponent c){ |
|
94 |
BasicHTML.updateRenderer(c, ((JToolTip)c).getTipText()); |
|
95 |
} |
|
96 |
||
97 |
/* Unfortunately this has to remain private until we can make API additions. |
|
98 |
*/ |
|
99 |
private void uninstallComponents(JComponent c){ |
|
100 |
BasicHTML.updateRenderer(c, ""); |
|
101 |
} |
|
102 |
||
103 |
protected void installListeners(JComponent c) { |
|
104 |
propertyChangeListener = createPropertyChangeListener(c); |
|
105 |
||
106 |
c.addPropertyChangeListener(propertyChangeListener); |
|
107 |
} |
|
108 |
||
109 |
protected void uninstallListeners(JComponent c) { |
|
110 |
c.removePropertyChangeListener(propertyChangeListener); |
|
111 |
||
112 |
propertyChangeListener = null; |
|
113 |
} |
|
114 |
||
115 |
/* Unfortunately this has to remain private until we can make API additions. |
|
116 |
*/ |
|
117 |
private PropertyChangeListener createPropertyChangeListener(JComponent c) { |
|
118 |
if (sharedPropertyChangedListener == null) { |
|
119 |
sharedPropertyChangedListener = new PropertyChangeHandler(); |
|
120 |
} |
|
121 |
return sharedPropertyChangedListener; |
|
122 |
} |
|
123 |
||
124 |
public void paint(Graphics g, JComponent c) { |
|
125 |
Font font = c.getFont(); |
|
126 |
FontMetrics metrics = SwingUtilities2.getFontMetrics(c, g, font); |
|
127 |
Dimension size = c.getSize(); |
|
128 |
||
129 |
g.setColor(c.getForeground()); |
|
130 |
// fix for bug 4153892 |
|
131 |
String tipText = ((JToolTip)c).getTipText(); |
|
132 |
if (tipText == null) { |
|
133 |
tipText = ""; |
|
134 |
} |
|
135 |
||
136 |
Insets insets = c.getInsets(); |
|
137 |
Rectangle paintTextR = new Rectangle( |
|
138 |
insets.left + 3, |
|
139 |
insets.top, |
|
140 |
size.width - (insets.left + insets.right) - 6, |
|
141 |
size.height - (insets.top + insets.bottom)); |
|
142 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
|
143 |
if (v != null) { |
|
144 |
v.paint(g, paintTextR); |
|
145 |
} else { |
|
146 |
g.setFont(font); |
|
147 |
SwingUtilities2.drawString(c, g, tipText, paintTextR.x, |
|
148 |
paintTextR.y + metrics.getAscent()); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
public Dimension getPreferredSize(JComponent c) { |
|
153 |
Font font = c.getFont(); |
|
154 |
FontMetrics fm = c.getFontMetrics(font); |
|
155 |
Insets insets = c.getInsets(); |
|
156 |
||
157 |
Dimension prefSize = new Dimension(insets.left+insets.right, |
|
158 |
insets.top+insets.bottom); |
|
159 |
String text = ((JToolTip)c).getTipText(); |
|
160 |
||
161 |
if ((text == null) || text.equals("")) { |
|
162 |
text = ""; |
|
163 |
} |
|
164 |
else { |
|
165 |
View v = (c != null) ? (View) c.getClientProperty("html") : null; |
|
166 |
if (v != null) { |
|
167 |
prefSize.width += (int) v.getPreferredSpan(View.X_AXIS) + 6; |
|
168 |
prefSize.height += (int) v.getPreferredSpan(View.Y_AXIS); |
|
169 |
} else { |
|
170 |
prefSize.width += SwingUtilities2.stringWidth(c,fm,text) + 6; |
|
171 |
prefSize.height += fm.getHeight(); |
|
172 |
} |
|
173 |
} |
|
174 |
return prefSize; |
|
175 |
} |
|
176 |
||
177 |
public Dimension getMinimumSize(JComponent c) { |
|
178 |
Dimension d = getPreferredSize(c); |
|
179 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
|
180 |
if (v != null) { |
|
181 |
d.width -= v.getPreferredSpan(View.X_AXIS) - v.getMinimumSpan(View.X_AXIS); |
|
182 |
} |
|
183 |
return d; |
|
184 |
} |
|
185 |
||
186 |
public Dimension getMaximumSize(JComponent c) { |
|
187 |
Dimension d = getPreferredSize(c); |
|
188 |
View v = (View) c.getClientProperty(BasicHTML.propertyKey); |
|
189 |
if (v != null) { |
|
190 |
d.width += v.getMaximumSpan(View.X_AXIS) - v.getPreferredSpan(View.X_AXIS); |
|
191 |
} |
|
192 |
return d; |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* Invoked when the <code>JCompoment</code> associated with the |
|
197 |
* <code>JToolTip</code> has changed, or at initialization time. This |
|
198 |
* should update any state dependant upon the <code>JComponent</code>. |
|
199 |
* |
|
200 |
* @param c the JToolTip the JComponent has changed on. |
|
201 |
*/ |
|
202 |
private void componentChanged(JComponent c) { |
|
203 |
JComponent comp = ((JToolTip)c).getComponent(); |
|
204 |
||
205 |
if (comp != null && !(comp.isEnabled())) { |
|
21278 | 206 |
// For better backward compatibility, only install inactive |
2 | 207 |
// properties if they are defined. |
208 |
if (UIManager.getBorder("ToolTip.borderInactive") != null) { |
|
209 |
LookAndFeel.installBorder(c, "ToolTip.borderInactive"); |
|
210 |
} |
|
211 |
else { |
|
212 |
LookAndFeel.installBorder(c, "ToolTip.border"); |
|
213 |
} |
|
214 |
if (UIManager.getColor("ToolTip.backgroundInactive") != null) { |
|
215 |
LookAndFeel.installColors(c,"ToolTip.backgroundInactive", |
|
216 |
"ToolTip.foregroundInactive"); |
|
217 |
} |
|
218 |
else { |
|
219 |
LookAndFeel.installColors(c,"ToolTip.background", |
|
220 |
"ToolTip.foreground"); |
|
221 |
} |
|
222 |
} else { |
|
223 |
LookAndFeel.installBorder(c, "ToolTip.border"); |
|
224 |
LookAndFeel.installColors(c, "ToolTip.background", |
|
225 |
"ToolTip.foreground"); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
||
230 |
private static class PropertyChangeHandler implements |
|
231 |
PropertyChangeListener { |
|
232 |
public void propertyChange(PropertyChangeEvent e) { |
|
233 |
String name = e.getPropertyName(); |
|
234 |
if (name.equals("tiptext") || "font".equals(name) || |
|
235 |
"foreground".equals(name)) { |
|
236 |
// remove the old html view client property if one |
|
237 |
// existed, and install a new one if the text installed |
|
238 |
// into the JLabel is html source. |
|
239 |
JToolTip tip = ((JToolTip) e.getSource()); |
|
240 |
String text = tip.getTipText(); |
|
241 |
BasicHTML.updateRenderer(tip, text); |
|
242 |
} |
|
243 |
else if ("component".equals(name)) { |
|
244 |
JToolTip tip = ((JToolTip) e.getSource()); |
|
245 |
||
246 |
if (tip.getUI() instanceof BasicToolTipUI) { |
|
247 |
((BasicToolTipUI)tip.getUI()).componentChanged(tip); |
|
248 |
} |
|
249 |
} |
|
250 |
} |
|
251 |
} |
|
252 |
} |