Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage
1. JavaFX 2.0 With Alternative Languages - Groovy, Clojure, Scala, Fantom, and Visage Stephen Chin Chief Agile Methodologist, GXS steveonjava@gmail.com tweet: @steveonjava
2. About the Presenter Stephen Chin Java Champion Family Man Chief Agile Methodologist, GXS Author, Pro JavaFX Platform OSCON Java Conference Chair Motorcyclist
3. Pro JavaFX 2 Platform Coming Soon! Coming 2nd half of this year All examples rewritten in Java Will cover the new JavaFX 2.0 APIs 3
4. Disclaimer: This is code-heavy THE FOLLOWING IS INTENDED TO STIMULATE CREATIVE USE OF JVM LANGUAGES. AFTER WATCHING THIS PRESENTATION YOU MAY FEEL COMPELLED TO START LEARNING A NEW JVM LANGUAGE AND WANT TO APPLY IT AT YOUR WORKPLACE. THE PRESENTER IS NOT LIABLE FOR ANY INNOVATION, BREAKTHROUGHS, OR NP-COMPLETE SOLUTIONS THAT MAY RESULT.
7. Programming Languages JavaFX 2.0 APIs are now in Java Pure Java APIs for all of JavaFX Expose JavaFX Binding, Sequences as Java APIs Embrace all JVM languages JRuby, Clojure, Groovy, Scala Fantom, Mira, Jython, etc. JavaFX Script is no longer supported by Oracle Existing JavaFX Script based applications will continue to run Visage is the open-source successor to the JavaFX Script language
8. JavaFX in Java JavaFX API follows JavaBeans approach Similar in feel to other UI toolkits (Swing, etc) Uses builder pattern to minimize boilerplate
9. Example Application public class HelloStage extends Application { @Override public void start(Stage stage) { stage.setTitle("Hello Stage"); stage.setWidth(600); stage.setHeight(450); Group root = new Group(); Scene scene = new Scene(root); scene.setFill(Color.LIGHTGREEN); stage.setScene(scene); stage.setVisible(true); } public static void main(String[] args) { launch(HelloStage.class, args); } }
10. Binding Unquestionably the biggest JavaFX Script innovation Supported via a PropertyBinding class Lazy invocation for high performance Static construction syntax for simple cases e.g.: bindTo(<property>)
11. Observable Pseudo-Properties Supports watching for changes to properties Implemented via anonymous inner classes Will take advantage of closures in the future
12. Observable Pseudo-Properties final Rectangle rect = new Rectangle(); rect.setX(40); rect.setY(40); rect.setWidth(100); rect.setHeight(200); rect.hoverProperty().addListener(new ChangeListener<Boolean>() { });
13. Observable Pseudo-Properties final Rectangle rect = new Rectangle(); rect.setX(40); rect.setY(40); rect.setWidth(100); rect.setHeight(200); rect.hoverProperty().addListener(new ChangeListener<Boolean>() { }); The property we want to watch
14. Observable Pseudo-Properties final Rectangle rect = new Rectangle(); rect.setX(40); rect.setY(40); rect.setWidth(100); rect.setHeight(200); rect.hoverProperty().addListener(new ChangeListener<Boolean>() { }); Only one listener used with generics to specify the data type
15. Observable Pseudo-Properties final Rectangle rect = new Rectangle(); rect.setX(40); rect.setY(40); rect.setWidth(100); rect.setHeight(200); rect.hoverProperty().addListener(new ChangeListener<Boolean>() { public void changed(ObservableValue<? extends Boolean> property, Boolean oldValue, Boolean value) { } }); Refers to the Rectangle.hoverProperty()
16. Observable Pseudo-Properties final Rectangle rect = new Rectangle(); rect.setX(40); rect.setY(40); rect.setWidth(100); rect.setHeight(200); rect.hoverProperty().addListener(new ChangeListener<Boolean>() { public void changed(ObservableValue<? extends Boolean> property, Boolean oldValue, Boolean value) { rect.setFill(rect.isHover() ? Color.GREEN : Color.RED); } });
17. Sequences in Java Replaced with an Observable List Public API is based on JavaFX sequences Internal code can use lighter collections API JavaFX 2.0 also has an Observable Map
25. JRuby Closure Conversion rect.hoverProperty.addListener() do |prop, oldVal, newVal| rect.fill = rect.hover ? Color::GREEN : Color::RED; end 23
26. JRubySwiby require 'swiby' class HelloWorldModel attr_accessor :saying end model = HelloWorldModel.new model.saying = "Hello World" Frame { title "Hello World“ width 200 content { Label { text bind(model,:saying) } } visible true } 24
27. 25 JavaFX With Clojure Artwork by Augusto Sellhorn http://sellmic.com/
28. A Little About Clojure Started in 2007 by Rich Hickey Functional Programming Language Derived from LISP Optimized for High Concurrency … and looks nothing like Java! 26 (def hello (fn [] "Hello world")) (hello)
29. Clojure Syntax in One Slide Symbols numbers – 2.178 ratios – 355/113 strings – “clojure”, “rocks” characters – symbols – a b c d keywords – :alpha :beta boolean – true, false null - nil Collections (commas optional) Lists (1, 2, 3, 4, 5) Vectors [1, 2, 3, 4, 5] Maps {:a 1, :b 2, :c 3, :d 4} Sets #{:a :b :c :d :e} 27 (plus macros that are syntactic sugar wrapping the above)
30. Clojure GUI Example (defnjavafxapp [] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (.setFill scene Color/LIGHTGREEN) (.setWidth stage 600) (.setHeight stage 450) (.setScene stage scene) (.setVisible stage true))) (javafxapp) 28
31. Clojure GUI Example (defnjavafxapp[] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (.setFill scene Color/LIGHTGREEN) (.setWidth stage 600) (.setHeight stage 450) (.setScene stage scene) (.setVisible stage true))) (javafxapp) 29 Create a Function for the Application
32. Clojure GUI Example (defnjavafxapp [] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (.setFill scene Color/LIGHTGREEN) (.setWidth stage 600) (.setHeight stage 450) (.setScene stage scene) (.setVisible stage true))) (javafxapp) 30 Initialize the Stage and Scene Variables
33. Clojure GUI Example (defnjavafxapp [] (let [stage (Stage. "JavaFX Stage") scene (Scene.)] (.setFill scene Color/LIGHTGREEN) (.setWidth stage 600) (.setHeight stage 450) (.setScene stage scene) (.setVisible stage true))) (javafxapp) 31 Call Setter Methods on Scene and Stage
41. Closures in Clojure 39 Inner classes can be created using proxy (.addListenerhoverProperty (proxy [ChangeListener] [] (changed [p, o, v] (.setFillrect (if (.isHoverrect) Color/GREEN Color/RED)))))
42. Closures in Clojure Inner classes can be created using proxy 40 Proxy form: (proxy [class] [args] fs+) f => (name [params*] body) (.addListenerhoverProperty (proxy[ChangeListener][] (changed [p, o, v] (.setFillrect (if (.isHoverrect) Color/GREEN Color/RED)))))
44. Features of Groovy Tight integration with Java Very easy to port from Java to Groovy Declarative syntax Familiar to JavaFX Script developers Builders
45. Step 1: Lazy conversion to Groovy class HelloStage extends Application { void start(stage) { stage.setTitle("Hello Stage (Groovy)"); stage.setWidth(600); stage.setHeight(450); Group root = new Group(); Scene scene = new Scene(root); scene.setFill(Color.LIGHTSKYBLUE); stage.setScene(scene); stage.setVisible(true); } static void main(args) { launch(HelloStage.class, args); } }
53. Step 2: Java-ish Groovy Animations final Timeline timeline = new Timeline( cycleCount: Timeline.INDEFINITE, autoReverse: true ) final KeyValue kv1 = new KeyValue (rect1.xProperty(), 200); final KeyValue kv2 = new KeyValue (rect2.yProperty(), 200); final KeyValue kv3 = new KeyValue (circle1.radiusProperty(), 200); final KeyFramekf = new KeyFrame(Duration.valueOf(750), kv1, kv2, kv3); timeline.getKeyFrames().add(kf); timeline.play();
54. Step 3: GroovyFX Animation timeline = timeline(cycleCount: Timeline.INDEFINITE, autoReverse: true) { at 750.ms { change (rect1, "y") { to 200 } change (rect2, "x") { to 200 } change (circle, "radius") { to 200 } } } timeline.play();
55. Groovy Closures - With interface coercion def f = { p, o, v -> rect.setFill(rect.isHover() ? Color.GREEN : Color.RED); } as ChangeListener; rect.hoverProperty().addListener(f);
57. What is Scala Started in 2001 by Martin Odersky Compiles to Java bytecodes Pure object-oriented language Also a functional programming language 55
58. Why Scala? Shares many language features with JavaFX Script that make GUI programming easier: Static type checking – Catch your errors at compile time Closures – Wrap behavior and pass it by reference Declarative – Express the UI by describing what it should look like Scala also supports DSLs! 56
59. Java vs. Scala DSL public class HelloStage extends Application { public void start(Stage stage) { stage.setTitle("Hello Stage"); stage.setWidth(600); stage.setHeight(450); Scene scene = new Scene(); scene.setFill(Color.LIGHTGREEN); Rectangle rect = new Rectangle(); rect.setX(25); rect.setY(40); rect.setWidth(100); rect.setHeight(50); rect.setFill(Color.RED); stage.add(rect); stage.setScene(scene); stage.setVisible(true); } public static void main(String[] args) { Launcher.launch(HelloStage.class, args); } } object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } } } 57 21 Lines 541 Characters 17 Lines 324 Characters
60. object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } } } 58
61. 59 object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } } } object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } } } Base class for JavaFX applications
62. 60 object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } } } Declarative Stage definition
63. 61 object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } } } Inline property definitions
64. 62 object HelloJavaFX extends JavaFXApplication { def stage = new Stage { title = "Hello Stage" width = 600 height = 450 scene = new Scene { fill = Color.LIGHTGREEN content = List(new Rectangle { x = 25 y = 40 width = 100 height = 50 fill = Color.RED }) } } } List Construction Syntax
65. Animation in Scala def timeline = new Timeline { repeatCount = INDEFINITE autoReverse = true keyFrames = List( new KeyFrame(time: 50) { values = List( new KeyValue(rect1.xProperty -> 300), new KeyValue(rect2.yProperty -> 500), new KeyValue(rect2.widthProperty -> 150) ) } ) } 63
66. def timeline = new Timeline { repeatCount = INDEFINITE autoReverse = true keyFrames = List( new KeyFrame(time: 50) { values = List( new KeyValue(rect1.xProperty -> 300), new KeyValue(rect2.yProperty -> 500), new KeyValue(rect2.widthProperty -> 150) ) } ) } Animation in Scala 64 Duration set by Constructor Parameter
67. Animation in Scala 65 def timeline = new Timeline { repeatCount = INDEFINITE autoReverse = true keyFrames = List( new KeyFrame(time: 50) { values = List( new KeyValue(rect1.xProperty -> 300), new KeyValue(rect2.yProperty -> 500), new KeyValue(rect2.widthProperty -> 150) ) } ) } Operator overloading for animation syntax
68. Closures in Scala 66 Closures are also supported in Scala And they are 100% type-safe rect.hoverProperty.addListener((p, o, v) => { rect.fill = if (rect.hover) Color.GREEN else Color.RED })
69. Closures in Scala Closures are also supported in Scala And they are 100% type-safe 67 rect.hoverProperty.addListener((p, o, v) => { rect.fill = if (rect.hover) Color.GREEN else Color.RED }) Compact syntax (params) => {body}
70. Other JVM Languages to Try Jython Started by Jim Hugunin High Performance Python Mirah Invented by Charles Nutter Originally called Duby Local Type Inference, Static and Dynamic Typing Fantom Created by Brian and Andy Frank Originally called Fan Built-in Declarative Syntax Portable to Java and .NET Local Type Inference, Static and Dynamic Typing 68
77. Visage on Android Curious? Drop by room 4 after this talk to find out more…
78. Conclusion You can write JavaFX applications in pure Java JavaFX is also usable in alternate languages Over time improved support is possible Groovy Builders, Scala DSL, Visage
There are two kinds of listener: ‘changedListener’ and ‘ChangingListener’. Being informed of the change before it happens allow for it to be vetoed.It is also possible to either watch a single property, or all properties belonging to a bean.Note that the value passed to the callback is the old value. This is to ensure that we aren’t eagerly computing the new value when it might not be required. To get the new value, you can call the function on the bean or via the propertyReference
There are two kinds of listener: ‘changedListener’ and ‘ChangingListener’. Being informed of the change before it happens allow for it to be vetoed.It is also possible to either watch a single property, or all properties belonging to a bean.Note that the value passed to the callback is the old value. This is to ensure that we aren’t eagerly computing the new value when it might not be required. To get the new value, you can call the function on the bean or via the propertyReference
There are two kinds of listener: ‘changedListener’ and ‘ChangingListener’. Being informed of the change before it happens allow for it to be vetoed.It is also possible to either watch a single property, or all properties belonging to a bean.Note that the value passed to the callback is the old value. This is to ensure that we aren’t eagerly computing the new value when it might not be required. To get the new value, you can call the function on the bean or via the propertyReference
There are two kinds of listener: ‘changedListener’ and ‘ChangingListener’. Being informed of the change before it happens allow for it to be vetoed.It is also possible to either watch a single property, or all properties belonging to a bean.Note that the value passed to the callback is the old value. This is to ensure that we aren’t eagerly computing the new value when it might not be required. To get the new value, you can call the function on the bean or via the propertyReference
There are two kinds of listener: ‘changedListener’ and ‘ChangingListener’. Being informed of the change before it happens allow for it to be vetoed.It is also possible to either watch a single property, or all properties belonging to a bean.Note that the value passed to the callback is the old value. This is to ensure that we aren’t eagerly computing the new value when it might not be required. To get the new value, you can call the function on the bean or via the propertyReference
There are two kinds of listener: ‘changedListener’ and ‘ChangingListener’. Being informed of the change before it happens allow for it to be vetoed.It is also possible to either watch a single property, or all properties belonging to a bean.Note that the value passed to the callback is the old value. This is to ensure that we aren’t eagerly computing the new value when it might not be required. To get the new value, you can call the function on the bean or via the propertyReference
Slight conversion to Groovy. This can be compiled by the Groovy compiler and run, but basically there is only one line difference (the ‘static void main’ line)
This is the same code as the previous slide, taking advantage of some of the Groovy syntax tricks. This is getting to look a lot more like JavaFX Script.
This DSL handles running on the EDT, and can actually be run as-is – there is no need for a class declaration, or anything else to ensure that we’re on the EDT. This is getting us fairly close to the simple JavaFX Script at the beginning
This DSL handles running on the EDT, and can actually be run as-is – there is no need for a class declaration, or anything else to ensure that we’re on the EDT. This is getting us fairly close to the simple JavaFX Script at the beginning