1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package org.openslx.dozmod.gui.control;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.Icon;
import javax.swing.JLabel;
/**
* A label that will use automatic word wrapping.
*/
public class WordWrapLabel extends JLabel {
private static final long serialVersionUID = -2454459023556745689L;
private String realText = "";
private boolean bold = false;
private boolean italic = false;
public WordWrapLabel(String text, Icon icon, int horizontalAlignment) {
super(text, icon, horizontalAlignment);
}
public WordWrapLabel(String text, int horizontalAlignment) {
this(text, null, horizontalAlignment);
}
public WordWrapLabel(String text) {
this(text, null, LEADING);
}
public WordWrapLabel(String text, boolean bold, boolean italic) {
this(null, null, LEADING);
this.bold = bold;
this.italic = italic;
setText(text);
}
public WordWrapLabel(Icon image, int horizontalAlignment) {
this(null, image, horizontalAlignment);
}
public WordWrapLabel(Icon image) {
this(null, image, CENTER);
}
public WordWrapLabel() {
this("", null, LEADING);
}
@Override
public void setText(String text) {
realText = text;
if (text != null && !text.isEmpty()) {
if (text.indexOf('&') != -1) {
text = text.replace("&", "&");
}
if (text.indexOf('<') != -1) {
text = text.replace("<", "<");
}
if (text.indexOf('>') != -1) {
text = text.replace(">", ">");
}
String tmp = "<html>";
if (bold) {
tmp += "<b>";
}
if (italic) {
tmp += "<i>";
}
text = tmp + text;
if (italic) {
text += "</i>";
}
if (bold) {
text += "</b>";
}
if (text.contains("\n")) {
text = text.replace("\n", "<br>");
}
}
super.setText(text);
}
@Override
public Dimension getMaximumSize() {
Component parent = this.getParent();
if (parent == null)
return super.getMaximumSize();
return parent.getMaximumSize();
}
public String getPlainText() {
return realText;
}
}
|