001 /* 002 * Copyright 2002-2006 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.samples.simple.ui; 017 018 import org.springframework.richclient.application.event.LifecycleApplicationEvent; 019 import org.springframework.richclient.dialog.CloseAction; 020 import org.springframework.richclient.dialog.ConfirmationDialog; 021 import org.springframework.richclient.dialog.FormBackedDialogPage; 022 import org.springframework.richclient.dialog.TitledPageApplicationDialog; 023 import org.springframework.richclient.form.Form; 024 import org.springframework.richclient.samples.simple.domain.Contact; 025 import org.springframework.richclient.samples.simple.domain.ContactDataStore; 026 import org.springframework.util.Assert; 027 028 /** 029 * This is a dialog for editing the properties of a Contact object. It is a simple "form backed" dialog, meaning that 030 * the body of the dialog is provided from a "form backed" dialog page. The Ok (finish) button will be wired into the 031 * "page complete" state of the dialog page, which in turn gets its state from the automatic validation of the 032 * properties on the form. 033 * @author Larry Streepy 034 * @see FormBackedDialogPage 035 * @see ContactForm 036 */ 037 public class ContactPropertiesDialog extends TitledPageApplicationDialog { 038 039 /** The form that allows for editing the contact. */ 040 private Form form; 041 042 /** Are we creating a new Contact or editing an existing one? */ 043 private boolean creatingNew = false; 044 045 /** The data store holding all our contacts, used to add a new contact. */ 046 private ContactDataStore dataStore; 047 048 public ContactPropertiesDialog(ContactDataStore dataStore) { 049 this(null, dataStore); 050 } 051 052 public ContactPropertiesDialog(Contact contact, ContactDataStore dataStore) { 053 Assert.notNull(dataStore, "The data store is required to edit a contact"); 054 if (contact == null) { 055 creatingNew = true; 056 contact = new Contact(); 057 } 058 setCloseAction(CloseAction.DISPOSE); 059 form = new ContactForm(contact); 060 setDialogPage(new FormBackedDialogPage(form)); 061 this.dataStore = dataStore; 062 } 063 064 private Contact getEditingContact() { 065 return (Contact) form.getFormModel().getFormObject(); 066 } 067 068 protected void onAboutToShow() { 069 if (creatingNew) { 070 getMessage("contactProperties.new.title"); 071 setTitle(getMessage("contactProperties.new.title")); 072 } 073 else { 074 Contact contact = getEditingContact(); 075 String title = getMessage("contactProperties.edit.title", new Object[] { contact.getFirstName(), 076 contact.getLastName() }); 077 setTitle(title); 078 } 079 } 080 081 protected boolean onFinish() { 082 // commit any buffered edits to the model 083 form.getFormModel().commit(); 084 // Update the persistent store with the new/modified object. 085 String eventType; 086 if (creatingNew) { 087 eventType = LifecycleApplicationEvent.CREATED; 088 dataStore.add(getEditingContact()); 089 } 090 else { 091 eventType = LifecycleApplicationEvent.MODIFIED; 092 } 093 // And notify the rest of the application of the change 094 getApplicationContext().publishEvent(new LifecycleApplicationEvent(eventType, getEditingContact())); 095 return true; 096 } 097 098 protected void onCancel() { 099 // Warn the user if they are about to discard their changes 100 if (form.getFormModel().isDirty()) { 101 String msg = getMessage("contactProperties.dirtyCancelMessage"); 102 String title = getMessage("contactProperties.dirtyCancelTitle"); 103 ConfirmationDialog dlg = new ConfirmationDialog(title, msg) { 104 protected void onConfirm() { 105 ContactPropertiesDialog.super.onCancel(); 106 } 107 }; 108 dlg.showDialog(); 109 } 110 else { 111 super.onCancel(); 112 } 113 } 114 }