svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / controls / MultiLineToolTip.java @ 40561
History | View | Annotate | Download (3.43 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.gui.beans.controls; |
25 |
|
26 |
import java.awt.Dimension; |
27 |
import java.awt.FontMetrics; |
28 |
import java.awt.Graphics; |
29 |
import java.io.BufferedReader; |
30 |
import java.io.IOException; |
31 |
import java.io.StringReader; |
32 |
import java.util.Enumeration; |
33 |
import java.util.Vector; |
34 |
|
35 |
import javax.swing.JComponent; |
36 |
import javax.swing.JToolTip; |
37 |
import javax.swing.SwingUtilities; |
38 |
import javax.swing.plaf.metal.MetalToolTipUI; |
39 |
/**
|
40 |
* <p>
|
41 |
* Allows using more than one line length tooltips. It also automatically
|
42 |
* calculates the size of the screen rectangle used and the preferred sized up
|
43 |
* on the contained text. It does NOT format the text to fit within a given
|
44 |
* size. The user must provide the text as she/he wishes to show it.
|
45 |
* </p>
|
46 |
* <p>
|
47 |
* It means that new line characters must be placed in the string as well as it
|
48 |
* was a System.out.println statement.
|
49 |
* </p>
|
50 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
51 |
*/
|
52 |
public class MultiLineToolTip extends JToolTip { |
53 |
private static final long serialVersionUID = -3727623742187461577L; |
54 |
|
55 |
public MultiLineToolTip() {
|
56 |
setUI(new MultiLineToolTipUI());
|
57 |
} |
58 |
|
59 |
private class MultiLineToolTipUI extends MetalToolTipUI { |
60 |
private String[] strs; |
61 |
|
62 |
public void paint(Graphics g, JComponent c) { |
63 |
FontMetrics metrics = g.getFontMetrics();
|
64 |
Dimension size = c.getSize();
|
65 |
g.setColor(c.getBackground()); |
66 |
g.fillRect(0, 0, size.width, size.height); |
67 |
g.setColor(c.getForeground()); |
68 |
if (strs != null) { |
69 |
for (int i = 0; i < strs.length; i++) { |
70 |
g.drawString(strs[i], 3, (metrics.getHeight()) * (i + 1)); |
71 |
} |
72 |
} |
73 |
} |
74 |
|
75 |
public Dimension getPreferredSize(JComponent c) { |
76 |
FontMetrics metrics = c.getFontMetrics(c.getFont());
|
77 |
String tipText = ((JToolTip) c).getTipText(); |
78 |
if (tipText == null) { |
79 |
tipText = "";
|
80 |
} |
81 |
BufferedReader br = new BufferedReader(new StringReader(tipText)); |
82 |
String line;
|
83 |
int _maxWidth = 0; |
84 |
Vector v = new Vector(); |
85 |
try {
|
86 |
while ((line = br.readLine()) != null) { |
87 |
int width = SwingUtilities.computeStringWidth(metrics, line); |
88 |
_maxWidth = (_maxWidth < width) ? width : _maxWidth; |
89 |
v.addElement(line); |
90 |
} |
91 |
} catch (IOException ex) { |
92 |
ex.printStackTrace(); |
93 |
} |
94 |
int lines = v.size();
|
95 |
if (lines < 1) { |
96 |
strs = null;
|
97 |
lines = 1;
|
98 |
} else {
|
99 |
strs = new String[lines]; |
100 |
int i = 0; |
101 |
for (Enumeration e = v.elements(); e.hasMoreElements(); i++) { |
102 |
strs[i] = (String) e.nextElement();
|
103 |
} |
104 |
} |
105 |
int height = metrics.getHeight() * lines;
|
106 |
return new Dimension(_maxWidth + 6, height + 4); |
107 |
} |
108 |
} |
109 |
} |