Binding in Java Beans Inspecting bean properties with a GUI - - PowerPoint PPT Presentation

binding in java beans
SMART_READER_LITE
LIVE PREVIEW

Binding in Java Beans Inspecting bean properties with a GUI - - PowerPoint PPT Presentation

Binding in Java Beans HOM,DOS beans? convention Binding in Java Beans Inspecting bean properties with a GUI binding veto GUI BEANS Pieter van den Hombergh Summary on Java Beans FX Properties Thijs Dorssers Fontys Hogeschool voor


slide-1
SLIDE 1

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Binding in Java Beans

Pieter van den Hombergh Thijs Dorssers

Fontys Hogeschool voor Techniek en Logistiek

June 15, 2017

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 1/18

slide-2
SLIDE 2

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

What are Java Beans

Alternate meaning: coffee beans of the java sort. In the java programming culture: Normal Java Classes that adhere to specific conventions:

Must have public parameter-less (default) constructor. Other constructors are allowed. Have setters and getters for properties with naming convention. Can have property change listeners with support for them. Must be serializable.

They often group or encapsulate many objects into a single object.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 2/18

slide-3
SLIDE 3

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Naming convention

All properties of the bean should have getters and setters. For member named someValue of SomeType getter getSomeValue. First letter capitalized and prefixed with get for getter: SomeType getSomeValue() setter setSomeValue. First letter capitalized and prefixed with set for setter: void setSomeValue(SomeType v) is for getter of boolean members: boolean isSomeBoolean() If these conventions are followed, it is said that the implementation follows the Bean Pattern. This naming convention eases understanding, but also allows for automated tools to introspect the classes and objects and help with tools for non programmatic (as in GUI supported)

  • perations on the “bean”. DEMO in Netbeans

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 3/18

slide-4
SLIDE 4

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Bean GUI in NetBeans

DEMO with FaceBean. Note that this version is slightly modified from the tutorial’s version, using the inherited property change support of java.awt.Component.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 4/18

slide-5
SLIDE 5

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Binding is like Observer

Binding means that a PropertyChangeListener is informed whenever a property changes value. This is similar to the Observable/Observer pattern we saw earlier. PropertyChangeListeners can be added and removed. The listeners are informed about the changes of the property by a call of their void propertyChange(PropertyChangeEvent evt) method. When a bean wants to use this mechanism it typically uses a member of type PropertyChangeSupport, which does the “Heavy Lifting”. In the FaceBean example mPcs. The listeners are triggered by the bean by calling mPcs.firePropertyChanged(propname, oldValue, newValue);

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 5/18

slide-6
SLIDE 6

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Vetoable changes

A constrained property gets special treatment. The beans has veto change listeners.

The listeners get a say if the change is allowed, based

  • n name and old and new value of the property.

The FaceBean example uses VetoableChangeSupport instance name mVcs. The way the listener is informed of the immanent change is similar to firePropertyChange call on an earlier slide: fireVetoableChange(propname, oldValue, newValue) However: call this method before you change the properties value.

Then when the listener vetoes the change, it will throw an exception, thereby typically preventing the execution

  • f the steps to actually set the property to the vetoed

value. This exception is checked, so you must catch it or forward it with a throws in the setter’s signature.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 6/18

slide-7
SLIDE 7

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Gui Components as beans

java.awt.Component is a JavaBean and support change listening for the properties: the component’s font (”font”) background color (”background”) foreground color (”foreground”) focusability (”focusable”) focus traversal keys enabled state (”focusTraversalKeysEnabled”) Set of FORWARD TRAVERSAL KEYS (”forwardFocusTraversalKeys”) Set of BACKWARD TRAVERSAL KEYS (”backwardFocusTraversalKeys”) Set of UP CYCLE TRAVERSAL KEYS (”upCycleFocusTraversalKeys”) preferred size (”preferredSize”) minimum size (”minimumSize”) maximum size (”maximumSize”) locale (”locale”) component orientation (left to right or right to left) (”componentOrientation”) And you can add your own property too.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 7/18

slide-8
SLIDE 8

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Java Bean Summary

A Java Bean is simple plain old java (as in: needs not implement or extend anything) with some conventions. Non args ctor. Setters and getters name name the properties. PropertyChangeListeners can be informed. (Name,

  • ldvalue, new value)

Changes can be vetoed. GUI (Swing, AWT) components are Java Beans. Auxiliary file (also using a name pattern) provides BeanInfo and a GUI based editor for the properties.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 8/18

slide-9
SLIDE 9

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

FX Beans vs Java Beans

more direct binding/ relationship of and between variables.

The work around triggering and binding does no longer take place in containing class. because the properties are “exposed”, so can manipulated from the outside.

new (extra) convention for getter of property. xXXXProperty() where xXXX is the name of the property.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 9/18

slide-10
SLIDE 10

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Meet Bill

Simple bill example

p u b l i c c l a s s Bill { // Define a variable to store the property p r i v a t e DoubleProperty amountDue = new ← ֓ SimpleDoubleProperty () ; // Define a getter for the property 's value p u b l i c f i n a l double getAmountDue () { r e t u r n amountDue . get () ; } // Define a setter for the property 's value p u b l i c f i n a l void setAmountDue ( double value ) { amountDue . set ( value ) ; } // Define a getter for the property itself p u b l i c DoubleProperty amountDueProperty () { r e t u r n amountDue ; } }

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 10/18

slide-11
SLIDE 11

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Improvements

JavaFX properties expands and improves the JavaBeans model. Binding is assembled from one or more sources, the dependencies. A binding observes its dependencies for changes and updates itself after change detection, but lazily. Lazy means, that the value is recomputed on demand (when getter is called) instead of every time it is set. The documentation also states that the implementer should minimize the work in event handlers: “Implementations of this class should strive to generate as few events as possible to avoid wasting too much time in event handlers.” So:

if the new value is the same as the previous: do not bother. If you are already invalid, do not propagate this news any further.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 11/18

slide-12
SLIDE 12

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Property APIs

JavaFX beans provide three way to deal with bound properties: High level API.

Fluent API Bindings utility class.

Low level API.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 12/18

slide-13
SLIDE 13

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

High level API, Fluent

Make use of the (computational) method in the property: NumberProperty and its children.

Fluent API

p u b l i c s t a t i c void main ( String [ ] args ) { IntegerProperty num1 = new SimpleIntegerProperty ( 1 ) ; IntegerProperty num2 = new SimpleIntegerProperty ( 2 ) ; IntegerProperty num3 = new SimpleIntegerProperty ( 5 ) ; NumberBinding sumMul = num1 . add ( num2 ) . multiply ( num3 ) ; System . out . println ( sumMul . getValue () ) ; num1 . set ( 2 ) ; System . err . println ( sumMul . getValue () ) ; }

Note the use of chaining the method calls. It is because the add etc. methods return an object which support the

  • peration.

This is called a fluent style of programming.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 13/18

slide-14
SLIDE 14

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

High level API, Bindings class

The same computation can be done using the Bindings class which provides factories for all kinds of binding.

Using Bindings

p u b l i c s t a t i c void main ( String [ ] args ) { IntegerProperty num1 = new SimpleIntegerProperty ( 1 ) ; IntegerProperty num2 = new SimpleIntegerProperty ( 2 ) ; IntegerProperty num3 = new SimpleIntegerProperty ( 5 ) ; // note the use of static import NumberBinding sum = m u l t i p l y ( num3 , add ( num1 , num2 ) ) ; System . out . println ( sum . getValue () ) ; num1 . setValue ( 2 ) ; System . err . println ( sum . getValue () ) ; }

The add and multiply methods stem from the Bindings class.

The import statement

import s t a t i c javafx . beans . binding . Bindings . ∗ ;

modified from javafx bindings tutorial.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 14/18

slide-15
SLIDE 15

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Combining Fluent API and Bindings

p u b l i c s t a t i c void main ( String [ ] args ) { IntegerProperty num1 = new SimpleIntegerProperty ( 1 ) ; IntegerProperty num2 = new SimpleIntegerProperty ( 2 ) ; IntegerProperty num3 = new SimpleIntegerProperty ( 3 ) ; IntegerProperty num4 = new SimpleIntegerProperty ( 4 ) ; NumberBinding total = Bindings . add ( num1 . multiply ( num2 ) , num3 . multiply ( num4 ) ) ; System . out . println ( total . getValue () ) ; num1 . setValue ( 2 ) ; System . err . println ( total . getValue () ) ; }

Convenient to transform a formula to binding. When mixing types (e.g. int, double), conversion (promotion) can take place. The rules are the same as in Java.

1

If one of the operands is a double, the result is a double.

2

If not and one of the operands is a float, the result is a float.

3

If not and one of the operands is a long, the result is a long.

4

The result is an integer otherwise. from javafx bindings tutorial.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 15/18

slide-16
SLIDE 16

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Lazy evaluation

Lazy evaluation in action

NumberBinding total = Bindings . add ( bill1 . amountDueProperty () . add ( bill2 . amountDueProperty () ) , bill3 . amountDueProperty () ) ; // First call makes the binding invalid bill1 . setAmountDue ( 200.00 ) ; // The binding is now invalid bill2 . setAmountDue ( 100.00 ) ; bill3 . setAmountDue ( 75.00 ) ; // Make the binding valid ... System . out . println ( total . getValue () ) ; // Make invalid ... bill3 . setAmountDue ( 150.00 ) ; // Make valid ... System . out . println ( total . getValue () ) ;

The title is probably an oxymoron.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 16/18

slide-17
SLIDE 17

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Low Level API

f i n a l DoubleProperty a = new SimpleDoubleProperty ( 1 ) ; f i n a l DoubleProperty b = new SimpleDoubleProperty ( 2 ) ; f i n a l DoubleProperty c = new SimpleDoubleProperty ( 3 ) ; f i n a l DoubleProperty d = new SimpleDoubleProperty ( 4 ) ; DoubleBinding db = new DoubleBinding () { { super . bind ( a , b , c , d ) ; } @Override p r o t e c t e d double computeValue () { r e t u r n ( a . get () ∗ b . get () ) + ( c . get () ∗ d . get () ) ; } }; System . out . println ( db . get () ) ; b . set ( 3 ) ; System . err . println ( db . get () ) ;

The low level API can give more control and performance ( avoids intermediate objects) in return for a little more work Note the use of the block, which effectively is the sole constructor. super of DoubleBinding takes care of invalidation behavior.

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 17/18

slide-18
SLIDE 18

Binding in Java Beans HOM,DOS beans?

convention Inspecting bean properties with a GUI binding veto

GUI BEANS

Summary on Java Beans

FX Properties

Any open issues?

Not all understood? see https://docs.oracle.com/javase/8/javafx/ properties-binding-tutorial/binding.htm

Questions?

Questions or remarks?

HOM,DOS/FHTenL Binding in Java Beans June 15, 2017 18/18