package org.springframework.richclient.samples.simple.ui; import org.springframework.binding.form.ValidatingFormModel; import org.springframework.richclient.application.event.LifecycleApplicationEvent; import org.springframework.richclient.command.ActionCommandExecutor; import org.springframework.richclient.dialog.CloseAction; import org.springframework.richclient.dialog.ConfirmationDialog; import org.springframework.richclient.dialog.FormBackedDialogPage; import org.springframework.richclient.dialog.TitledPageApplicationDialog; import org.springframework.richclient.form.Form; import org.springframework.richclient.form.FormModelHelper; import west.pharmacie.dto.Fournisseur; import west.pharmacie.session.FournisseurSessionAble; /** * This is a dialog for editing the properties of a Fournisseur object. It is a simple "form * backed" dialog, meaning that the body of the dialog is provided from a "form backed" * dialog page. The Ok (finish) button will be wired into the "page complete" state of the * dialog page, which in turn gets its state from the automatic validation of the * properties on the form. * * @author Larry Streepy * @see FormBackedDialogPage * @see FournisseurForm * */ public class FournisseurPropertiesDialog extends TitledPageApplicationDialog implements ActionCommandExecutor { /** Our page Id, used for configuring messages. */ private static final String PROPERTIES_PAGE_ID = "fournisseurProperties"; /** The form model used to manage instances of the Fournisseur object. */ private ValidatingFormModel formModel; /** The form that displays the form model. */ private Form form; /** Are we creating a new Fournisseur or editing an existing one? */ private boolean creatingNew = false; /** The fournisseur object we are processing. */ private Fournisseur fournisseur; /** The data store holding all our fournisseurs. */ private FournisseurSessionAble fournisseurSession; /** * Constructor. */ public FournisseurPropertiesDialog() { setCloseAction(CloseAction.DISPOSE); setFournisseur(null); formModel = FormModelHelper.createFormModel(fournisseur); form = new FournisseurForm(formModel); FormBackedDialogPage page = new FormBackedDialogPage(form); setDialogPage(page); } /** * Initialize using the configured fournisseur. */ private void initFormObject() { form.setFormObject(fournisseur); if( creatingNew ) { getMessage("fournisseurProperties.new.title"); setTitle(getMessage("fournisseurProperties.new.title")); } else { String title = getMessage("fournisseurProperties.edit.title", new Object[] { fournisseur.getRaisonSociale() }); setTitle(title); } } /** * Set the Fournisseur object on which to operate. If the provided objec is null, then * configure to handle the creation of a new object. * * @param fournisseur to operate upon, may be null */ public void setFournisseur( Fournisseur fournisseur ) { if( fournisseur == null ) { creatingNew = true; this.fournisseur = new Fournisseur(); } else { creatingNew = false; this.fournisseur = fournisseur; } } /* * (non-Javadoc) * * @see org.springframework.richclient.dialog.ApplicationDialog#onFinish() */ protected boolean onFinish() { formModel.commit(); // Commit all changes to the model object Fournisseur fournisseur = (Fournisseur) formModel.getFormObject(); // Update the persistent store with the new/modified object. String eventType; if( creatingNew ) { eventType = LifecycleApplicationEvent.CREATED; fournisseurSession.saveFournisseur(fournisseur); } else { eventType = LifecycleApplicationEvent.MODIFIED; fournisseurSession.updateFournisseur(fournisseur); } // And notify the rest of the application of the change getApplicationContext().publishEvent(new LifecycleApplicationEvent(eventType, fournisseur)); return true; } /** * Handle a dialog cancellation request. Get use confirmation before discarding * unsaved changes. */ protected void onCancel() { // Warn the user if they are about to discard their changes if( formModel.isDirty() ) { String msg = getMessage(PROPERTIES_PAGE_ID + ".dirtyCancelMessage"); String title = getMessage(PROPERTIES_PAGE_ID + ".dirtyCancelTitle"); ConfirmationDialog dlg = new ConfirmationDialog(title, msg) { protected void onConfirm() { FournisseurPropertiesDialog.super.onCancel(); } }; dlg.showDialog(); } else { super.onCancel(); } } /** * Execute this dialog. Simply show the dialog using the configured object. Note that * when execute gets called from the newFournisseurCommand, no call to * {@link #setFournisseur(Fournisseur)} will have been made. So, this method resets the edit * state upon exit. */ public void execute() { setParent(getActiveWindow().getControl()); initFormObject(); showDialog(); // At this point, the user is done interacting with the dialog. So, we can // reset our object. setFournisseur(null); } /** * @return the fournisseurSession */ public FournisseurSessionAble getFournisseurSession() { return fournisseurSession; } /** * @param fournisseurSession the fournisseurSession to set */ public void setFournisseurSession(FournisseurSessionAble fournisseurSession) { this.fournisseurSession = fournisseurSession; } }