script - shannah/CodeRAD GitHub Wiki
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.
<script>
... java code ...
</script>
Tip
|
Characters with special meaning in XML must be encoded as XML entities. E.g. For example, the following two script tags are both valid. The first one encodes the less than sign the <script>
if (CN.getDisplayWidth() < 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 <script>
if (CN.getDisplayWidth() < 500) {
System.out.println("Display width is less than 500 px");
}
</script> |
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>
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>
thenit
will be aTextField
instance - not just aComponent
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 parentEntityList
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 theEntityView
for the current row. Otherwise it will be null. - EntityView view
-
If inside a
<row-template>
, this will refer to theEntityView
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 theview-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 tocontext.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.