Text provides a primary means for displaying information in the user interface. The WText class provides a simple way to add plain or markup text to the user interface.
The WText
widget displays text using an HTML <span>
or <div>
element (depending on whether it is inline
or not). It can display either XHTML formatted text or plain text.
The text contents is contained in a WString. This string class provides at the same time support for localization and internationalization:
In its most simple form, the WText
widget displays plain text
(escaping special characters as needed).
Of course, a WText
widget may also display XHTML formatted text.
void TextXHTML() {
WText text =
new WText(
"This is <b>XHTML</b> markup text. It supports a safe subset of XHTML tags and attributes, which have only decorative functions.");
}
XHTML text that is not read from a localized strings interface (which is
considered inherently safe), is protected against unwanted side effects
from Cross-Site Scripting (XSS) attacks. The text of an XHTML-formatted
WText
is filtered using an XML parser and all malicious tags are
removed (unless this feature is explicitly by-passed by using the
XHTMLUnsafeText
text format).
This XHTML text contains JavaScript, wich is filtered by the XSS filter.
A warning is printed in the logs.
void TextXSS() {
WText text =
new WText(
"<p>This XHTML text contains JavaScript, wich is filtered by the XSS filter.</p><script>alert(\"XSS Attack!\");</script><p>A warning is printed in the logs.</p>");
}
If you want to display a text label associated with a form field then a WLabel is more suitable as it can be linked to the input field, relaying focus to it when clicked.
If you want to display an HTML fragment which contains widgets or other bound contents, then a WTemplate widget is probably what you are looking for.
The functionality of WText
is very basic. As this widget derives
- like many widgets - from WInteractWidget it may respond
to mouse events and also keyboard events if it can be given keyboard
focus. A few mouse events are demonstrated below.
void TextEvents() {
WContainerWidget container = new WContainerWidget();
WText text1 = new WText("This text reacts to <tt>clicked()</tt>", (WContainerWidget) container);
text1.setStyleClass("reactive");
WText text2 =
new WText("This text reacts to <tt>doubleClicked()</tt>", (WContainerWidget) container);
text2.setStyleClass("reactive");
WText text3 =
new WText("This text reacts to <tt>mouseWentOver()</tt>", (WContainerWidget) container);
text3.setStyleClass("reactive");
WText text4 =
new WText("This text reacts to <tt>mouseWentOut()</tt>", (WContainerWidget) container);
text4.setStyleClass("reactive");
final WText out = new WText((WContainerWidget) container);
text1
.clicked()
.addListener(
this,
() -> {
out.setText("<p>Text was clicked.</p>");
});
text2
.doubleClicked()
.addListener(
this,
() -> {
out.setText("<p>Text was double clicked.</p>");
});
text3
.mouseWentOver()
.addListener(
this,
() -> {
out.setText("<p>Mouse went over text.</p>");
});
text4
.mouseWentOut()
.addListener(
this,
() -> {
out.setText("<p>Mouse went out text.</p>");
});
}
You may add a tooltip or deferred tooltip to WText
void TextToolTip() {
WText text = new WText("Some text", TextFormat.Plain);
text.setToolTip("ToolTip", TextFormat.XHTML);
}
class Text extends WText {
private static Logger logger = LoggerFactory.getLogger(Text.class);
Text(WContainerWidget parentContainer) {
super();
if (parentContainer != null) parentContainer.addWidget(this);
}
public Text() {
this((WContainerWidget) null);
}
WString getCalculateToolTip() {
return new WString("Deferred tooltip");
}
WString getToolTip() {
return this.getCalculateToolTip();
}
}
void TextDeferredToolTip() {
Text text = new Text();
text.setText("Text");
text.setDeferredToolTip(true);
}