Jim has posted 66 posts at DZone. You can read more from them at their website. View Full User Profile

Trigger Happy - Sequence Triggers in Compiled JavaFX Script

02.11.2008
| 5521 views |
  • submit to reddit

In the Binding to a Sequence in Compiled JavaFX Script article, we bound a ListBox to a sequence in the model. This binding causes the ListBox to stay in sync with the sequence. The example in today's article builds on the previous one, introducing the concept of sequence triggers. Here's a screenshot of the program, followed by the source code. I'll follow up with a brief discussion.

Sequencetriggers_2

 

/*
* SequenceTriggers.fx
*
* Developed 2008 by James L. Weaver (jim.weaver at lat-inc.com)
* to serve as a JavaFX Script example of using sequence triggers
*/
import javafx.ui.*;
import java.lang.System;

class Model {
attribute entries:String[] = ["Red", "Green", "Blue", "Yellow", "Purple"]
on replace [indx] (oldValue) {
System.out.println("replaced {oldValue} at {indx} with {entries[indx]}");
}
on insert [indx] (newValue) {
System.out.println("inserted {newValue} at {indx}");
}
on delete [indx] (oldValue) {
System.out.println("deleted {oldValue} from {indx}");
};
attribute selectedIndex:Number = 0 on replace {
System.out.println("selectedIndex={selectedIndex}");
};
attribute enteredText:String;
}

Frame {
var mdl = Model {}
title: "Sequence Triggers"
width: 400
height: 200
background: Color.WHITE
visible: true
content:
BorderPanel {
center:
FlowPanel {
content:
ListBox {
cells: bind for (entry in mdl.entries)
ListCell {
text: entry
}
selection: bind mdl.selectedIndex with inverse
fixedCellWidth: 100
visibleRowCount: 4
}
}
bottom:
FlowPanel {
content: [
TextField {
value: bind mdl.enteredText with inverse
columns: 10
},
Button {
text: "Insert"
defaultButton: true
enabled: bind mdl.enteredText.trim() <> ""
action:
function():Void {
insert mdl.enteredText before mdl.entries[mdl.selectedIndex.intValue()];
mdl.enteredText = "";
System.out.println("sizeof entries={sizeof mdl.entries}");
}
},
Button {
text: "Replace"
enabled: bind mdl.enteredText.trim() <> ""
action:
function():Void {
mdl.entries[mdl.selectedIndex.intValue()] = mdl.enteredText;
mdl.enteredText = "";
}
},
Button {
text: "Delete"
enabled: bind mdl.selectedIndex >= 0
action:
function():Void {
delete mdl.entries[mdl.selectedIndex.intValue()];
}
}
]
}
}
}

Sequence Triggers

Triggers can be created that execute when certain operations happen to a sequence. These operations are:

  • Inserting an element
  • Replacing an element
  • Deleting an element

The program example above demonstrates the use of each of these triggers by printing a message to the console when they execute. Notice the distinction between the replace trigger that applies to elements of a sequence, and the replace trigger that applies to variables (as shown in the selectedIndex attribute declaration).

Have fun with this example, and please post a comment if you have any questions.

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications

Immediate eBook (PDF) download available at the book's Apress site

Published at DZone with permission of its author, Jim Weaver.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: