Skip to content

Commit 8071caf

Browse files
committed
add ToolTip with TextArea idea - did not work
1 parent 379f62d commit 8071caf

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package test;
2+
3+
import java.awt.Dimension;
4+
import java.awt.Graphics;
5+
6+
import javax.swing.BorderFactory;
7+
import javax.swing.JComponent;
8+
import javax.swing.JTextArea;
9+
import javax.swing.JToolTip;
10+
import javax.swing.ToolTipManager;
11+
12+
/**
13+
* A multiline tooltip based on open source code from http://code.ohloh.net.
14+
*
15+
* SwingJS abandons the UI idea in favor of simple HTML BR. Code borrowed from
16+
* Jalview-JS development.
17+
*
18+
*/
19+
public class JMultiLineToolTip extends JToolTip {
20+
21+
private static final String HTML_DIV_PREFIX = "<html><div style=\"width:250px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;\">";
22+
private static final String DIV_HTML_SUFFIX = "</div></html>";
23+
private static final int MAX_LEN = 40;
24+
25+
private boolean enclose = false;
26+
private String lastText = null;
27+
private String lastWrapped = null;
28+
29+
private JTextArea textArea;
30+
31+
private int maxChar = MAX_LEN;
32+
33+
private boolean asTextArea;
34+
35+
private static Dimension ZERO_DIM = new Dimension();
36+
37+
private static Dimension MAX_DIM = new Dimension(250, 1000);
38+
39+
/**
40+
* Allow for two options - a JTextArea or HTML-formatted text
41+
*
42+
* @param maxChar
43+
* @param asTextArea
44+
*/
45+
public JMultiLineToolTip(int maxChar, boolean asTextArea) {
46+
super();
47+
this.maxChar = maxChar;
48+
this.asTextArea = asTextArea;
49+
50+
if (asTextArea) {
51+
textArea = new JTextArea() {
52+
@Override
53+
public Dimension getMaximumSize() {
54+
return MAX_DIM;
55+
}
56+
};
57+
add(textArea);
58+
textArea.setBorder(BorderFactory.createEmptyBorder(0, 2, 2, 2));
59+
textArea.setWrapStyleWord(true);
60+
textArea.setLineWrap(true);
61+
} else {
62+
63+
}
64+
65+
}
66+
67+
@Override
68+
public Dimension getPreferredSize() {
69+
if (asTextArea) {
70+
String tipText = getTipText();
71+
if (tipText == null || tipText.length() == 0)
72+
return ZERO_DIM;
73+
return textArea.getPreferredSize();
74+
}
75+
return super.getPreferredSize();
76+
}
77+
78+
@Override
79+
public void setTipText(String tipText) {
80+
if (asTextArea) {
81+
textArea.setText(tipText);
82+
} else {
83+
tipText = wrapToolTip(tipText);
84+
}
85+
super.setTipText(tipText);
86+
}
87+
88+
@Override
89+
public void paintComponent(Graphics g) {
90+
if (asTextArea) {
91+
System.out.println(textArea.getText());
92+
textArea.paintComponents(g);
93+
} else {
94+
System.out.println("text=" + this.getTipText());
95+
super.paintComponent(g);
96+
}
97+
}
98+
99+
private StringBuilder sb = new StringBuilder();
100+
101+
private String wrapToolTip(String ttext) {
102+
if (ttext.equals(lastText))
103+
return lastWrapped;
104+
105+
ttext = ttext.trim();
106+
boolean addDiv = false;
107+
108+
if (ttext.contains("<br>")) {
109+
enclose = true;
110+
asTextArea = false;
111+
// String[] htmllines = ttext.split("<br>");
112+
// for (String line : htmllines) {
113+
// addDiv = line.length() > maxChar;
114+
// if (addDiv) {
115+
// break;
116+
// }
117+
// }
118+
} else {
119+
enclose = ttext.length() > maxChar;
120+
if (enclose) {
121+
String[] words = ttext.split(" ");
122+
sb.setLength(0);
123+
for (int i = 0, len = 0; i < words.length; i++) {
124+
if (len > 0)
125+
sb.append(" ");
126+
if ((len = len + words[i].length()) > maxChar) {
127+
sb.append("<br>");
128+
len = 0;
129+
}
130+
sb.append(words[i]);
131+
}
132+
ttext = sb.toString();
133+
}
134+
}
135+
lastText = ttext;
136+
lastWrapped = (!enclose ? ttext
137+
: addDiv ? HTML_DIV_PREFIX + ttext + DIV_HTML_SUFFIX : "<html>" + ttext + "</html>");
138+
System.out.println(lastWrapped);
139+
return lastWrapped;
140+
}
141+
142+
}

0 commit comments

Comments
 (0)