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.dialog; 017 018 import java.awt.Window; 019 020 import javax.swing.Icon; 021 import javax.swing.JComponent; 022 import javax.swing.KeyStroke; 023 import javax.swing.UIManager; 024 025 import org.springframework.richclient.core.DefaultMessage; 026 import org.springframework.util.Assert; 027 028 /** 029 * Dialog for asking confirmation to the user. The <code>onConfirm</code> is 030 * called when the user presses the yes button. 031 */ 032 public abstract class ConfirmationDialog extends ApplicationDialog { 033 034 private static final String YES_FACE_DESCRIPTOR_ID = "yesCommand"; 035 036 private static final String NO_FACE_DESCRIPTOR_ID = "noCommand"; 037 038 private static final String CONFIRMATION_DIALOG_ICON = "confirmationDialog.icon"; 039 040 private DefaultMessageAreaPane messageAreaPane; 041 042 private String confirmationMessage; 043 044 public ConfirmationDialog() { 045 this("Confirmation Required", null, "Are you sure you wish to perform this action?"); 046 } 047 048 public ConfirmationDialog(String title, String message) { 049 this(title, null, message); 050 } 051 052 public ConfirmationDialog(String title, Window parent, String message) { 053 super(title, parent); 054 setConfirmationMessage(message); 055 } 056 057 public void setConfirmationMessage(String message) { 058 Assert.hasText(message, "The confirmation message is required"); 059 this.confirmationMessage = message; 060 if(this.messageAreaPane != null) { 061 messageAreaPane.setMessage(new DefaultMessage(message)); 062 } 063 } 064 065 protected String getFinishCommandId() { 066 return YES_FACE_DESCRIPTOR_ID; 067 } 068 069 protected String getCancelCommandId() { 070 return NO_FACE_DESCRIPTOR_ID; 071 } 072 073 protected void registerDefaultCommand() { 074 registerCancelCommandAsDefault(); 075 } 076 077 protected void onInitialized() { 078 setupKeyBindings(); 079 } 080 081 private void setupKeyBindings() { 082 addActionKeyBinding(KeyStroke.getKeyStroke(getYesKey(), 0), getFinishCommand().getId()); 083 addActionKeyBinding(KeyStroke.getKeyStroke(getNoKey(), 0), getCancelCommand().getId()); 084 } 085 086 protected int getYesKey() { 087 return getFinishCommand().getMnemonic(); 088 } 089 090 protected int getNoKey() { 091 return getCancelCommand().getMnemonic(); 092 } 093 094 protected JComponent createDialogContentPane() { 095 this.messageAreaPane = new DefaultMessageAreaPane(); 096 Icon icon = getIconSource().getIcon(CONFIRMATION_DIALOG_ICON); 097 if (icon == null) { 098 icon = UIManager.getIcon("OptionPane.questionIcon"); 099 } 100 this.messageAreaPane.setDefaultIcon(icon); 101 this.messageAreaPane.setMessage(new DefaultMessage(confirmationMessage)); 102 return messageAreaPane.getControl(); 103 } 104 105 protected void disposeDialogContentPane() { 106 messageAreaPane = null; 107 } 108 109 protected final boolean onFinish() { 110 onConfirm(); 111 return true; 112 } 113 114 protected abstract void onConfirm(); 115 116 }