/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.springframework.richclient.dialog;

import java.awt.Window;
import java.awt.event.KeyEvent;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.UIManager;

import org.springframework.richclient.core.Message;
import org.springframework.util.Assert;

public abstract class ConfirmationDialog extends ApplicationDialog {
    private static final String YES_FACE_DESCRIPTOR_ID = "yesCommand";

    private static final String NO_FACE_DESCRIPTOR_ID = "noCommand";

    private static final String CONFIRMATION_DIALOG_ICON = "confirmationDialog.icon";

    private DefaultMessageAreaPane messageAreaPane;

    private String confirmationMessage;

    public ConfirmationDialog() {
        this("Confirmation Required", null, "Are you sure you wish to perform this action?");
    }

    public ConfirmationDialog(String title, String message) {
        this(title, null, message);
    }

    public ConfirmationDialog(String title, Window parent, String message) {
        super(title, parent);
        setConfirmationMessage(message);
    }

    public void setConfirmationMessage(String message) {
        Assert.hasText(message, "The confirmation message is required");
        this.confirmationMessage = message;
    }

    protected String getFinishCommandId() {
        return YES_FACE_DESCRIPTOR_ID;
    }

    protected String getCancelCommandId() {
        return NO_FACE_DESCRIPTOR_ID;
    }

    protected void registerDefaultCommand() {
        registerCancelCommandAsDefault();
    }

    protected void onInitialized() {
        setupKeyBindings();
    }

    private void setupKeyBindings() {
        addActionKeyBinding(KeyStroke.getKeyStroke(getYesKey(), 0), getFinishCommand().getId());
        addActionKeyBinding(KeyStroke.getKeyStroke(getNoKey(), 0), getCancelCommand().getId());
    }

    protected int getYesKey() {
        return KeyEvent.VK_Y;
    }

    protected int getNoKey() {
        return KeyEvent.VK_N;
    }

    protected JComponent createDialogContentPane() {
        this.messageAreaPane = new DefaultMessageAreaPane();
        Icon icon = getIconSource().getIcon(CONFIRMATION_DIALOG_ICON);
        if (icon == null) {
            icon = UIManager.getIcon("OptionPane.questionIcon");
        }
        this.messageAreaPane.setDefaultIcon(icon);
        this.messageAreaPane.setMessage(new Message(confirmationMessage));
        return messageAreaPane.getControl();
    }

    protected final boolean onFinish() {
        onConfirm();
        return true;
    }

    protected abstract void onConfirm();

}
@@ -687,40 +687,8 @@
 dow;
-%0Aimport java.awt.event.KeyEvent;
 %0A%0Aim
@@ -895,16 +895,149 @@
 ssert;%0A%0A
+/**%0A * Dialog for asking confirmation to the user. The %3Ccode%3EonConfirm%3C/code%3E is%0A * called when the user presses the yes button.%0A */%0A
 public a
@@ -1097,16 +1097,17 @@
 ialog %7B%0A
+%0A
     priv
@@ -2632,21 +2632,40 @@
 urn 
-KeyEvent.VK_Y
+getFinishCommand().getMnemonic()
 ;%0A  
@@ -2719,21 +2719,40 @@
 urn 
-KeyEvent.VK_N
+getCancelCommand().getMnemonic()
 ;%0A  

fix for rcp-70 (ConfirmationDialog: i18n of ActionKeyBinding of Yes and No command)