Binding is a powerful feature of connecting a data entity with a GUI component. It saves you a lot of 'plumbing' work and keeps your code clean, while on the other side it helps your team write graphical client code in a standardized way. The following example illustrates the process of binding a data field to a series of components: Note: BoundLabel and BoundTextField are specifically (Java Swing) components, which are not part of the Sopity runtime library. Depending on your GUI framework (like JSF, GWT, Echo etc.), similar approaches may apply. BoundLabel lblCustomer = new BoundLabel(SopOrder.customer);pnlOrders.add(lblCustomer);BoundTextField<Order> txtCustomer = new BoundTextField<Order>();txtCustomer.bind(SopOrder.customer);pnlOrders.add(txtCustomer);boundComponents.add(txtCustomer);Please note that both the label and the text field are bound to the same Soplet, which provides all necessary meta information. In the case of the label, the text to display (according to the current language settings), and in the case of the textfield, the datasource and other information related to the loading and saving the data. As the binding interface may define standard life cycle methods for all types of components (text field, combo, lists, tables etc.), the components can be easily iterated in the visitor pattern fashion: private void loadValues() { int selectedPos = lstOrders.getSelectedIndex(); Order order = (Order)mdlOrders.getElementAt(selectedPos); for (Bindable<Order> bindable : boundComponents) { bindable.load(order); } }Similar to the loading of values from the data object to the component, the user input can be saved back to the data layer: private void saveValues() { int selectedPos = lstOrders.getSelectedIndex(); Order order = (Order)mdlOrders.getElementAt(selectedPos); try { for (Bindable<Order> bindable : boundComponents) { bindable.save(order); } } catch (ValidationException ve) { MessageUtil.showWarning(ve); return; } getHibernateSession().update(order);}Note that the save() operation may throw a ValidationException - this happens when the user enters an invalid value as defined by the corresponding Soplet. For example, in the case of the customer field, a warning is displayed when the field does not have any value, as this field has been marked as mandatory: @SopDataDef( textEN = "Customer", textDE = "Kunde", description = "The person which ordered the pizza", dataType = SopSimpleDataType.string, mandatory = true, length = 50)customer,Note: For more sophisticated input validators please visit the Validators section |
