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;
}
}