Skip to content
Steve Hannah edited this page Jul 13, 2021 · 1 revision

<script>

Synopsis

Embeds a block of Java code to be executed at the time the view is created. The script has access to the current environment, and it run at the point of the view creation where the script appears. E.g. If the <script> is inside a <textField> tag, then it will be run at the time that the TextField component is created, and can access the TextField object via the special it variable.

Usage

<script>
    ... java code ...
</script>
Tip

Characters with special meaning in XML must be encoded as XML entities. E.g. & must be encoded as &amp;, < must be &lt;, and > must be &gt;. You can avoid this by wrapping the script in a CDATA tag, which tells the XML parser that the content should be treated as raw character data and should not be parsed as XML. E.g.

For example, the following two script tags are both valid. The first one encodes the less than sign the &lt; XML entity. The second doesn’t need to encode it because it is inside a CDATA block.

<script>
if (CN.getDisplayWidth() &lt; 500) {
    System.out.println("Display width is less than 500 px");
}
</script>
<script><![CDATA[
if (CN.getDisplayWidth() < 500) {
    System.out.println("Display width is less than 500 px");
}
]]></script>

But the following will give a compile error because the < character is neither XML encoded, nor inside a CDATA section.

<script>
if (CN.getDisplayWidth() < 500) {
    System.out.println("Display width is less than 500 px");
}
</script>

Examples

The following example demonstrates the use of the it special variable to add an action listener to the parent <textField>.

<textField>
    <script><![CDATA[
        it.addActionListener(evt -> {
            System.out.println("Text field text is "+it.getText());
        });
        System.out.println("Creating textfield "+it);
    ]]></script>
</textField>

Referencing Components in View

You can reference components from a script if they have a rad-var attribute.

<label rad-var="currentTime"/>
<button text="Update time">
    <script><![CDATA[
        it.addActionListener(evt -> {
            currentTime.setText(new java.util.Date().toString());
        });
    ]]></script>
</button>

Script Environment

Tip
Java expressions in XML attributes have access to the same execution environment that is described in this section.

The script execution environment includes one special variable it that contains a reference to the parent component. In the above example, it refers to the TextField object created by the <textField> tag.

The following variables are also available:

Component it

A reference to the Component created by the parent tag of the script tag. This variable will be an instance of the specific component generated. There is no need to cast it to that type before using it. E.g. If the parent tag is <textField> then it will be a TextField instance - not just a Component instance.

boolean rowFocused

If inside a <row-template> tag, this refers to whether the current row is focused or not.

int rowIndex

If inside a <row-template> tag, this refers to the row index (zero-based).

EntityList rowList

If inside a <row-template> tag, this refers to the parent EntityList component.

Entity rowModel

If inside a <row-template> tag this refers to the view model for the current row. Otherwise it will be null.

boolean rowSelected

If inside a <row-template> tag, this indicates whether the row being rendered is selected or not.

EntityView rowView

If inside a <row-template>, this will refer to the EntityView for the current row. Otherwise it will be null.

EntityView view

If inside a <row-template>, this will refer to the EntityView for the current row. Otherwise this will refer to the view itself.

ViewContext context

The view context at the location of the <script> tag. This context will include the view model and controller of the nearest parent that specifies it. E.g. If inside a <row-template> tag, then this context will include the row model, rather than the root view model. If a parent tag specifies the view-controller attribute, then the context will have this controller instead of the root view’s controller.

FormController formController

A reference to the FormController object for the view.

ApplicationController applicationController

A reference to the application controller object.

ViewController viewController

A reference to the view controller to the nearest view controller at the location of the <script> tag. Equivalent to context.getController().

AppSectionController sectionController

A reference to the app section controller.

FormController parentFormController

A reference to the parent form controller for the current form. May be null.

Clone this wiki locally