📜 ⬆️ ⬇️

Binding and Validation JSR295

Preamble

At leisure, I decided to disassemble the binding and validation in Java Beans Binding (JSR 295). The implementation of the proposed JSR 295 seemed easier and more convenient than the similar functionality provided by JGoodies.

Below I will consider two small examples of binding controls and an example of creating and using a validator.

Binding a text field (JTextField)
')
Define a simple class whose properties will be associated with form elements.

 class MyBean extends AbstractBean { private String strProperty = ""; // // ,    . // } 


Binding process of text fields is quite simple and practically does not need comments. Linking two properties of different elements is done by creating an object of the AutoBinding class. This class synchronizes the properties of objects, guided by one of three strategies:

1. READ_ONCE - synchronization is performed only once, during the binding of fields of objects.

2. READ - the target is synchronized with the source.

3. READ_WRITE - synchronization of objects with each other.

The process of creating AutoBinding objects can be simplified by using the Bindings class factory.

 // ...-   ... MyBean modelBean = new MyBean(); // ... JTextField textField = new JTextField(); // ... Bindings.createAutoBinding(READ_WRITE, modelBean, BeanProperty.create("strProperty"), textField, BeanProperty.create("text")).bind(); 


Everything is obvious.
Binding combo boxes (JComboBox)

Binding of combo boxes is carried out in two stages. First, the combo box is associated with a list of possible values, after which the selectedItem property is bound to the corresponding property of the target class.

Define a simple class that represents an item in the combo box value list.

 class ComboBoxVal { private int id = 0; private String text = ""; // // ,    . // } 


Add a field to the MyBean class with which the selectedItem property of the combo box will be associated

 class MyBean extends AbstractBean { private String stringProperty = "Hello!"; private ComboBoxVal comboProperty = new ComboBoxVal(0, "Item0"); // ... 


Create a combo box value list.

 // ...-   ... private List comboBoxData = new ArrayList(); // ... for (int i = 0; i < 10; i++) comboBoxData.add( new ComboBoxVal(i, "Item" + i)); 


The binding of complex Swing components is supported by the SwingBindings class factory. We associate combobox with a list of values

 JComboBox comboBox = new JComboBox(); // ... SwingBindings.createJComboBoxBinding( READ, comboBoxData, comboBox).bind(); 


It remains to associate the selectedItem property of the combo box with the corresponding property of the target class.

 Bindings.createAutoBinding(READ_WRITE, comboBox, BeanProperty.create("selectedItem"), modelBean, BeanProperty.create("comboProperty")).bind(); 


creation of a validator

As an example, we implement a simple validator for the textField element discussed above. Our validator will perform two checks - for non-emptiness and for the maximum number of characters.

All validators must inherit from the parameterized class Validator, where T is the data type with which the work will be performed.

 class StringValidator extends Validator { private int maxStrLength = 10; // ... // ,    . // ... public Result validate(String value) { if (value == null || value.length() == 0) return new StringEmptyResult(); if (value.length() > maxStrLength) return new StringTooLong(); return null; } public static class StringEmptyResult extends Result { public StringEmptyResult() { super(0, "Str empty msg"); } } public static class StringTooLong extends Result { public StringTooLong() { super(1, "Str too long msg"); } } } 


Validation is handled by the validate function. In case the value passed validation, null should be returned. Otherwise, return a Result class object containing a description of the problem and an error code.

Use validator

In order to use the validator, you need a little bit of the code that performs the binding.

 //...-   ... private Binding textBinding; // ... textBinding = Bindings.createAutoBinding( READ_WRITE, modelBean, BeanProperty.create("strProperty"), textField, BeanProperty.create("text")); textBinding.setValidator(validator); textBinding.bind(); 


It remains only to respond to the validation events

 textBinding.addBindingListener( new BindingListener(){ public void syncFailed(Binding binding, Binding.SyncFailure failure) { //    . } public void synced(Binding binding) { //    . } // ... 


The end.

Source: https://habr.com/ru/post/38211/


All Articles