jdk/src/share/classes/com/sun/security/auth/callback/DialogCallbackHandler.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7970 af1579474d16
child 13818 d94217d9f609
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7970
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.security.auth.callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/* JAAS imports */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.security.auth.callback.Callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.security.auth.callback.CallbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.security.auth.callback.ConfirmationCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.security.auth.callback.NameCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.security.auth.callback.PasswordCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.security.auth.callback.TextOutputCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.security.auth.callback.UnsupportedCallbackException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/* Java imports */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.Box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.JLabel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.JOptionPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.JPasswordField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.swing.JTextField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Uses a Swing dialog window to query the user for answers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * authentication questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * This can be used by a JAAS application to instantiate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * CallbackHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see javax.security.auth.callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class DialogCallbackHandler implements CallbackHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /* -- Fields -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /* The parent window, or null if using the default parent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private Component parentComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int JPasswordFieldLen = 8 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final int JTextFieldLen = 8 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* -- Methods -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Creates a callback dialog with the default parent window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public DialogCallbackHandler() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Creates a callback dialog and specify the parent window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param parentComponent the parent window -- specify <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * for the default parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public DialogCallbackHandler(Component parentComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.parentComponent = parentComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * An interface for recording actions to carry out if the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * clicks OK for the dialog.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static interface Action {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         void perform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Handles the specified set of callbacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param callbacks the callbacks to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @throws UnsupportedCallbackException if the callback is not an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * instance  of NameCallback or PasswordCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public void handle(Callback[] callbacks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        throws UnsupportedCallbackException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        /* Collect messages to display in the dialog */
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   102
        final List<Object> messages = new ArrayList<>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        /* Collection actions to perform if the user clicks OK */
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   105
        final List<Action> okActions = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ConfirmationInfo confirmation = new ConfirmationInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        for (int i = 0; i < callbacks.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (callbacks[i] instanceof TextOutputCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                TextOutputCallback tc = (TextOutputCallback) callbacks[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                switch (tc.getMessageType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                case TextOutputCallback.INFORMATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    confirmation.messageType = JOptionPane.INFORMATION_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                case TextOutputCallback.WARNING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    confirmation.messageType = JOptionPane.WARNING_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                case TextOutputCallback.ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    confirmation.messageType = JOptionPane.ERROR_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    throw new UnsupportedCallbackException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        callbacks[i], "Unrecognized message type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                messages.add(tc.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } else if (callbacks[i] instanceof NameCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                final NameCallback nc = (NameCallback) callbacks[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                JLabel prompt = new JLabel(nc.getPrompt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                final JTextField name = new JTextField(JTextFieldLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                String defaultName = nc.getDefaultName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (defaultName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    name.setText(defaultName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                 * Put the prompt and name in a horizontal box,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                 * and add that to the set of messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                Box namePanel = Box.createHorizontalBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                namePanel.add(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                namePanel.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                messages.add(namePanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                /* Store the name back into the callback if OK */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                okActions.add(new Action() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    public void perform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        nc.setName(name.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } else if (callbacks[i] instanceof PasswordCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                final PasswordCallback pc = (PasswordCallback) callbacks[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                JLabel prompt = new JLabel(pc.getPrompt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                final JPasswordField password =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                        new JPasswordField(JPasswordFieldLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (!pc.isEchoOn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    password.setEchoChar('*');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                Box passwordPanel = Box.createHorizontalBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                passwordPanel.add(prompt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                passwordPanel.add(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                messages.add(passwordPanel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                okActions.add(new Action() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    public void perform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        pc.setPassword(password.getPassword());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } else if (callbacks[i] instanceof ConfirmationCallback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                ConfirmationCallback cc = (ConfirmationCallback)callbacks[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                confirmation.setCallback(cc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (cc.getPrompt() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    messages.add(cc.getPrompt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                throw new UnsupportedCallbackException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    callbacks[i], "Unrecognized Callback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /* Display the dialog */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int result = JOptionPane.showOptionDialog(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            parentComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            messages.toArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            "Confirmation",                     /* title */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            confirmation.optionType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            confirmation.messageType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            null,                               /* icon */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            confirmation.options,               /* options */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            confirmation.initialValue);         /* initialValue */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        /* Perform the OK actions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (result == JOptionPane.OK_OPTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            || result == JOptionPane.YES_OPTION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            Iterator<Action> iterator = okActions.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            while (iterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                iterator.next().perform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        confirmation.handleResult(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Provides assistance with translating between JAAS and Swing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * confirmation dialogs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private static class ConfirmationInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        private int[] translations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int optionType = JOptionPane.OK_CANCEL_OPTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        Object[] options = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        Object initialValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int messageType = JOptionPane.QUESTION_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        private ConfirmationCallback callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        /* Set the confirmation callback handler */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        void setCallback(ConfirmationCallback callback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throws UnsupportedCallbackException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            this.callback = callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int confirmationOptionType = callback.getOptionType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            switch (confirmationOptionType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            case ConfirmationCallback.YES_NO_OPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                optionType = JOptionPane.YES_NO_OPTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                translations = new int[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    JOptionPane.YES_OPTION, ConfirmationCallback.YES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    JOptionPane.NO_OPTION, ConfirmationCallback.NO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    JOptionPane.CLOSED_OPTION, ConfirmationCallback.NO
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            case ConfirmationCallback.YES_NO_CANCEL_OPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                optionType = JOptionPane.YES_NO_CANCEL_OPTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                translations = new int[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    JOptionPane.YES_OPTION, ConfirmationCallback.YES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    JOptionPane.NO_OPTION, ConfirmationCallback.NO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    JOptionPane.CANCEL_OPTION, ConfirmationCallback.CANCEL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    JOptionPane.CLOSED_OPTION, ConfirmationCallback.CANCEL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            case ConfirmationCallback.OK_CANCEL_OPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                optionType = JOptionPane.OK_CANCEL_OPTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                translations = new int[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    JOptionPane.OK_OPTION, ConfirmationCallback.OK,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    JOptionPane.CANCEL_OPTION, ConfirmationCallback.CANCEL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    JOptionPane.CLOSED_OPTION, ConfirmationCallback.CANCEL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            case ConfirmationCallback.UNSPECIFIED_OPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                options = callback.getOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                 * There's no way to know if the default option means
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                 * to cancel the login, but there isn't a better way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                 * to guess this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                translations = new int[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    JOptionPane.CLOSED_OPTION, callback.getDefaultOption()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                throw new UnsupportedCallbackException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    callback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    "Unrecognized option type: " + confirmationOptionType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            int confirmationMessageType = callback.getMessageType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            switch (confirmationMessageType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            case ConfirmationCallback.WARNING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                messageType = JOptionPane.WARNING_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            case ConfirmationCallback.ERROR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                messageType = JOptionPane.ERROR_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            case ConfirmationCallback.INFORMATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                messageType = JOptionPane.INFORMATION_MESSAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                throw new UnsupportedCallbackException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    callback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    "Unrecognized message type: " + confirmationMessageType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        /* Process the result returned by the Swing dialog */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        void handleResult(int result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (callback == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            for (int i = 0; i < translations.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (translations[i] == result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    result = translations[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            callback.setSelectedIndex(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
}