The EntityEditModel binds an EntityEditor to a connection, and is the model an EntityEditPanel is based on. The editing itself — values, validation, dirty state, insert, update and delete — is the EntityEditor's job, accessed via editor().
The simplest edit model requires nothing but a constructor:
public class CustomerEditModel extends SwingEntityEditModel {
public CustomerEditModel(EntityConnectionProvider connectionProvider) {
super(Customer.TYPE, connectionProvider);
}
}
Application logic belongs in the edit model (or its editor), not in the UI: default values, value dependencies and custom persistence configured here work identically whether the entity is edited through an edit panel, a table or a dialog.
public final class InvoiceEditModel extends SwingEntityEditModel {
public InvoiceEditModel(EntityConnectionProvider connectionProvider) {
super(Invoice.TYPE, connectionProvider);
EditorValue<Entity> customer = editor().value(Invoice.CUSTOMER_FK);
// By default, foreign key values persist when the model
// is cleared, here we disable that for CUSTOMER_FK
customer.persist().set(false);
// We populate the invoice address fields with
// the customer address when the customer is edited
customer.propagate(Invoice.BILLINGADDRESS, cust -> valueOrNull(cust, Customer.ADDRESS));
customer.propagate(Invoice.BILLINGCITY, cust -> valueOrNull(cust, Customer.CITY));
customer.propagate(Invoice.BILLINGPOSTALCODE, cust -> valueOrNull(cust, Customer.POSTALCODE));
customer.propagate(Invoice.BILLINGSTATE, cust -> valueOrNull(cust, Customer.STATE));
customer.propagate(Invoice.BILLINGCOUNTRY, cust -> valueOrNull(cust, Customer.COUNTRY));
}
private static @Nullable <T> T valueOrNull(Entity customer, Attribute<T> attribute) {
return customer == null ? null : customer.get(attribute);
}
}
See EntityEditor for the editor API this builds on: editing values, default values, value propagation, custom persistence and detail editors.
1. Combo box models
The Swing implementation, SwingEntityEditModel, provides combo box models for foreign keys and column values, shared by the input components bound to them. A combo box model can be initialized eagerly in the constructor — otherwise it is created and refreshed on first use, by the component requesting it.
public final class TrackEditModel extends SwingEntityEditModel {
public TrackEditModel(EntityConnectionProvider connectionProvider) {
super(Track.TYPE, connectionProvider);
// Creates and populates the combo box models for the given foreign keys, otherwise this
// would happen when the associated combo boxes are created, as the UI is initialized.
editor().comboBoxModels().initialize(Track.MEDIATYPE_FK, Track.GENRE_FK);
}
}
Combo box models based on entities stay consistent automatically: entities inserted, updated or deleted elsewhere in the application are added to, replaced in or removed from the combo box model, via the editor’s insert, update and delete events.