jdk/src/share/classes/javax/swing/plaf/basic/BasicOptionPaneUI.java
author yan
Wed, 25 Sep 2013 17:35:22 +0400
changeset 20169 d7fa6d7586c9
parent 11809 d65aab67ee8a
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
8025085: [javadoc] some errors in javax/swing Reviewed-by: alexsch Contributed-by: Dmitry Zinkevich <dmitry.zinkevich@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
     2
 * Copyright (c) 1997, 2008, 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: 1299
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: 1299
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: 1299
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
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 javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.border.Border;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.border.EmptyBorder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.plaf.ActionMapUIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.ComponentUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.plaf.OptionPaneUI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Provides the basic look and feel for a <code>JOptionPane</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <code>BasicMessagePaneUI</code> provides a means to place an icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * message and buttons into a <code>Container</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Generally, the layout will look like:<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *        ------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *        | i | message    |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *        | c | message    |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *        | o | message    |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *        | n | message    |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *        ------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *        |     buttons    |
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *        |________________|
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * icon is an instance of <code>Icon</code> that is wrapped inside a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <code>JLabel</code>.  The message is an opaque object and is tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * for the following: if the message is a <code>Component</code> it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * added to the <code>Container</code>, if it is an <code>Icon</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * it is wrapped inside a <code>JLabel</code> and added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <code>Container</code> otherwise it is wrapped inside a <code>JLabel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * The above layout is used when the option pane's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <code>ComponentOrientation</code> property is horizontal, left-to-right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * The layout will be adjusted appropriately for other orientations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * The <code>Container</code>, message, icon, and buttons are all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * determined from abstract methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * @author James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * @author Amy Fowler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
public class BasicOptionPaneUI extends OptionPaneUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static final int MinimumWidth = 262;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public static final int MinimumHeight = 90;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static String newline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <code>JOptionPane</code> that the receiver is providing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * look and feel for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected JOptionPane         optionPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected Dimension minimumSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /** JComponent provide for input if optionPane.getWantsInput() returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * true. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    protected JComponent          inputComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /** Component to receive focus when messaged with selectInitialValue. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected Component           initialFocusComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /** This is set to true in validateComponent if a Component is contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * in either the message or the buttons. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected boolean             hasCustomComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    protected PropertyChangeListener propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   112
        newline = java.security.AccessController.doPrivileged(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                new GetPropertyAction("line.separator"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (newline == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            newline = "\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        map.put(new Actions(Actions.CLOSE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        BasicLookAndFeel.installAudioActionMap(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
      * Creates a new BasicOptionPaneUI instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public static ComponentUI createUI(JComponent x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return new BasicOptionPaneUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 11809
diff changeset
   134
      * Installs the receiver as the L&amp;F for the passed in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
      * <code>JOptionPane</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        optionPane = (JOptionPane)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        optionPane.setLayout(createLayoutManager());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        installComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 11809
diff changeset
   147
      * Removes the receiver from the L&amp;F controller of the passed in split
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
      * pane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        uninstallComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        optionPane.setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        optionPane = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        LookAndFeel.installColorsAndFont(optionPane, "OptionPane.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                         "OptionPane.foreground", "OptionPane.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        LookAndFeel.installBorder(optionPane, "OptionPane.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        minimumSize = UIManager.getDimension("OptionPane.minimumSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        LookAndFeel.installProperty(optionPane, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        LookAndFeel.uninstallBorder(optionPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected void installComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        optionPane.add(createMessageArea());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Container separator = createSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (separator != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            optionPane.add(separator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        optionPane.add(createButtonArea());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        optionPane.applyComponentOrientation(optionPane.getComponentOrientation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    protected void uninstallComponents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        hasCustomComponents = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        inputComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        initialFocusComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        optionPane.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    protected LayoutManager createLayoutManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return new BoxLayout(optionPane, BoxLayout.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if ((propertyChangeListener = createPropertyChangeListener()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            optionPane.addPropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (propertyChangeListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            optionPane.removePropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            propertyChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    protected PropertyChangeListener createPropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        InputMap map = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        SwingUtilities.replaceUIInputMap(optionPane, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                       WHEN_IN_FOCUSED_WINDOW, map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        LazyActionMap.installLazyActionMap(optionPane, BasicOptionPaneUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                           "OptionPane.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        SwingUtilities.replaceUIInputMap(optionPane, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                       WHEN_IN_FOCUSED_WINDOW, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        SwingUtilities.replaceUIActionMap(optionPane, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    InputMap getInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            Object[] bindings = (Object[])DefaultLookup.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                             optionPane, this, "OptionPane.windowBindings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (bindings != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                return LookAndFeel.makeComponentInputMap(optionPane, bindings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Returns the minimum size the option pane should be. Primarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * provided for subclassers wishing to offer a different minimum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public Dimension getMinimumOptionPaneSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (minimumSize == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            return new Dimension(MinimumWidth, MinimumHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return new Dimension(minimumSize.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                             minimumSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * If <code>c</code> is the <code>JOptionPane</code> the receiver
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * is contained in, the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * size that is returned is the maximum of the preferred size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * the <code>LayoutManager</code> for the <code>JOptionPane</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <code>getMinimumOptionPaneSize</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public Dimension getPreferredSize(JComponent c) {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   265
        if (c == optionPane) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            Dimension            ourMin = getMinimumOptionPaneSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            LayoutManager        lm = c.getLayout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (lm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                Dimension         lmSize = lm.preferredLayoutSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                if (ourMin != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    return new Dimension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        (Math.max(lmSize.width, ourMin.width),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                         Math.max(lmSize.height, ourMin.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                return lmSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return ourMin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Messaged from installComponents to create a Container containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * body of the message. The icon is the created by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <code>addIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    protected Container createMessageArea() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        JPanel top = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        Border topBorder = (Border)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                             "OptionPane.messageAreaBorder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (topBorder != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            top.setBorder(topBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        top.setLayout(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        /* Fill the body. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        Container          body = new JPanel(new GridBagLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        Container          realBody = new JPanel(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        body.setName("OptionPane.body");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        realBody.setName("OptionPane.realBody");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (getIcon() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            JPanel sep = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            sep.setName("OptionPane.separator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            sep.setPreferredSize(new Dimension(15, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            realBody.add(sep, BorderLayout.BEFORE_LINE_BEGINS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        realBody.add(body, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        GridBagConstraints cons = new GridBagConstraints();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        cons.gridx = cons.gridy = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        cons.gridwidth = GridBagConstraints.REMAINDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        cons.gridheight = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        cons.anchor = DefaultLookup.getInt(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                      "OptionPane.messageAnchor", GridBagConstraints.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        cons.insets = new Insets(0,0,3,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        addMessageComponents(body, cons, getMessage(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                          getMaxCharactersPerLineCount(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        top.add(realBody, BorderLayout.CENTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        addIcon(top);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        return top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Creates the appropriate object to represent <code>msg</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * places it into <code>container</code>. If <code>msg</code> is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * instance of Component, it is added directly, if it is an Icon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * a JLabel is created to represent it, otherwise a JLabel is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * created for the string, if <code>d</code> is an Object[], this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * method will be recursively invoked for the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <code>internallyCreated</code> is true if Objc is an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * of Component and was created internally by this method (this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * used to correctly set hasCustomComponents only if !internallyCreated).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    protected void addMessageComponents(Container container,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                     GridBagConstraints cons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                     Object msg, int maxll,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                     boolean internallyCreated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (msg == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (msg instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            // To workaround problem where Gridbad will set child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            // to its minimum size if its preferred size will not fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            // within allocated cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (msg instanceof JScrollPane || msg instanceof JPanel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                cons.fill = GridBagConstraints.BOTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                cons.weighty = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                cons.fill = GridBagConstraints.HORIZONTAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            cons.weightx = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            container.add((Component) msg, cons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            cons.weightx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            cons.weighty = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            cons.fill = GridBagConstraints.NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            cons.gridy++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (!internallyCreated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                hasCustomComponents = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } else if (msg instanceof Object[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            Object [] msgs = (Object[]) msg;
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   369
            for (Object o : msgs) {
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   370
                addMessageComponents(container, cons, o, maxll, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } else if (msg instanceof Icon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            JLabel label = new JLabel( (Icon)msg, SwingConstants.CENTER );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            configureMessageLabel(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            addMessageComponents(container, cons, label, maxll, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            String s = msg.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
   384
            int nl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            int nll = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if ((nl = s.indexOf(newline)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                nll = newline.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            } else if ((nl = s.indexOf("\r\n")) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                nll = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            } else if ((nl = s.indexOf('\n')) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                nll = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (nl >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                // break up newlines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (nl == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    JPanel breakPanel = new JPanel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                        public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                            Font       f = getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                            if (f != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                return new Dimension(1, f.getSize() + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                            return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    breakPanel.setName("OptionPane.break");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    addMessageComponents(container, cons, breakPanel, maxll,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                                         true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    addMessageComponents(container, cons, s.substring(0, nl),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                      maxll, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                addMessageComponents(container, cons, s.substring(nl + nll), maxll,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                  false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            } else if (len > maxll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                Container c = Box.createVerticalBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                c.setName("OptionPane.verticalBox");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                burstStringInto(c, s, maxll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                addMessageComponents(container, cons, c, maxll, true );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                JLabel label;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                label = new JLabel( s, JLabel.LEADING );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                label.setName("OptionPane.label");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                configureMessageLabel(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                addMessageComponents(container, cons, label, maxll, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * Returns the message to display from the JOptionPane the receiver is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * providing the look and feel for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    protected Object getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        inputComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (optionPane != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            if (optionPane.getWantsInput()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                /* Create a user component to capture the input. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                   selectionValues are non null the component and there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                   are < 20 values it'll be a combobox, if non null and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                   >= 20, it'll be a list, otherwise it'll be a textfield. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                Object             message = optionPane.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                Object[]           sValues = optionPane.getSelectionValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                Object             inputValue = optionPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                                           .getInitialSelectionValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                JComponent         toAdd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if (sValues != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    if (sValues.length < 20) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        JComboBox            cBox = new JComboBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        cBox.setName("OptionPane.comboBox");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        for(int counter = 0, maxCounter = sValues.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                            cBox.addItem(sValues[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        if (inputValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                            cBox.setSelectedItem(inputValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        inputComponent = cBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                        toAdd = cBox;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        JList                list = new JList(sValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        JScrollPane          sp = new JScrollPane(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        sp.setName("OptionPane.scrollPane");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        list.setName("OptionPane.list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                        list.setVisibleRowCount(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                        if(inputValue != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                            list.setSelectedValue(inputValue, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        list.addMouseListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                        toAdd = sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        inputComponent = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    MultiplexingTextField   tf = new MultiplexingTextField(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    tf.setName("OptionPane.textField");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    tf.setKeyStrokes(new KeyStroke[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                     KeyStroke.getKeyStroke("ENTER") } );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    if (inputValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        String inputString = inputValue.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        tf.setText(inputString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        tf.setSelectionStart(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        tf.setSelectionEnd(inputString.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    tf.addActionListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    toAdd = inputComponent = tf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                Object[]           newMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                if (message == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    newMessage = new Object[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    newMessage[0] = toAdd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    newMessage = new Object[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    newMessage[0] = message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    newMessage[1] = toAdd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                return newMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            return optionPane.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Creates and adds a JLabel representing the icon returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * <code>getIcon</code> to <code>top</code>. This is messaged from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * <code>createMessageArea</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    protected void addIcon(Container top) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        /* Create the icon. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        Icon                  sideIcon = getIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (sideIcon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            JLabel            iconLabel = new JLabel(sideIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            iconLabel.setName("OptionPane.iconLabel");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            iconLabel.setVerticalAlignment(SwingConstants.TOP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            top.add(iconLabel, BorderLayout.BEFORE_LINE_BEGINS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Returns the icon from the JOptionPane the receiver is providing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * the look and feel for, or the default icon as returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <code>getDefaultIcon</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    protected Icon getIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        Icon      mIcon = (optionPane == null ? null : optionPane.getIcon());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if(mIcon == null && optionPane != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            mIcon = getIconForType(optionPane.getMessageType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return mIcon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Returns the icon to use for the passed in type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    protected Icon getIconForType(int messageType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        if(messageType < 0 || messageType > 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        String propertyName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        switch(messageType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            propertyName = "OptionPane.errorIcon";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            propertyName = "OptionPane.informationIcon";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            propertyName = "OptionPane.warningIcon";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            propertyName = "OptionPane.questionIcon";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        if (propertyName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            return (Icon)DefaultLookup.get(optionPane, this, propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * Returns the maximum number of characters to place on a line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    protected int getMaxCharactersPerLineCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return optionPane.getMaxCharactersPerLineCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Recursively creates new JLabel instances to represent <code>d</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Each JLabel instance is added to <code>c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    protected void burstStringInto(Container c, String d, int maxll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        // Primitive line wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        int len = d.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (len <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (len > maxll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            int p = d.lastIndexOf(' ', maxll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            if (p <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                p = d.indexOf(' ', maxll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            if (p > 0 && p < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                burstStringInto(c, d.substring(0, p), maxll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                burstStringInto(c, d.substring(p + 1), maxll);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        JLabel label = new JLabel(d, JLabel.LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        label.setName("OptionPane.label");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        configureMessageLabel(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        c.add(label);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    protected Container createSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Creates and returns a Container containing the buttons. The buttons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * are created by calling <code>getButtons</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    protected Container createButtonArea() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        JPanel bottom = new JPanel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        Border border = (Border)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                                          "OptionPane.buttonAreaBorder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        bottom.setName("OptionPane.buttonArea");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (border != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            bottom.setBorder(border);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        bottom.setLayout(new ButtonAreaLayout(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
           DefaultLookup.getBoolean(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                    "OptionPane.sameSizeButtons", true),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
           DefaultLookup.getInt(optionPane, this, "OptionPane.buttonPadding",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                6),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
           DefaultLookup.getInt(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                        "OptionPane.buttonOrientation", SwingConstants.CENTER),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
           DefaultLookup.getBoolean(optionPane, this, "OptionPane.isYesLast",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                    false)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        addButtonComponents(bottom, getButtons(), getInitialValueIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Creates the appropriate object to represent each of the objects in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <code>buttons</code> and adds it to <code>container</code>. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * differs from addMessageComponents in that it will recurse on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * <code>buttons</code> and that if button is not a Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * it will create an instance of JButton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    protected void addButtonComponents(Container container, Object[] buttons,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                                 int initialIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (buttons != null && buttons.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            boolean            sizeButtonsToSame = getSizeButtonsToSameWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            boolean            createdAll = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            int                numButtons = buttons.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            JButton[]          createdButtons = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            int                maxWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            if (sizeButtonsToSame) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                createdButtons = new JButton[numButtons];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            for(int counter = 0; counter < numButtons; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                Object       button = buttons[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                Component    newComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                if (button instanceof Component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                    createdAll = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                    newComponent = (Component)button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                    container.add(newComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    hasCustomComponents = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    JButton      aButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                    if (button instanceof ButtonFactory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        aButton = ((ButtonFactory)button).createButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    else if (button instanceof Icon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        aButton = new JButton((Icon)button);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        aButton = new JButton(button.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    aButton.setName("OptionPane.button");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    aButton.setMultiClickThreshhold(DefaultLookup.getInt(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                          optionPane, this, "OptionPane.buttonClickThreshhold",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                          0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    configureButton(aButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    container.add(aButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    ActionListener buttonListener = createButtonActionListener(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    if (buttonListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        aButton.addActionListener(buttonListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    newComponent = aButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                if (sizeButtonsToSame && createdAll &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                   (newComponent instanceof JButton)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                    createdButtons[counter] = (JButton)newComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    maxWidth = Math.max(maxWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                                        newComponent.getMinimumSize().width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                if (counter == initialIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    initialFocusComponent = newComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                    if (initialFocusComponent instanceof JButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                        JButton defaultB = (JButton)initialFocusComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                        defaultB.addHierarchyListener(new HierarchyListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                            public void hierarchyChanged(HierarchyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                if ((e.getChangeFlags() &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                                        HierarchyEvent.PARENT_CHANGED) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                                    JButton defaultButton = (JButton) e.getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                                    JRootPane root =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                                            SwingUtilities.getRootPane(defaultButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                                    if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                                        root.setDefaultButton(defaultButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            ((ButtonAreaLayout)container.getLayout()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                              setSyncAllWidths((sizeButtonsToSame && createdAll));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            /* Set the padding, windows seems to use 8 if <= 2 components,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
               otherwise 4 is used. It may actually just be the size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
               buttons is always the same, not sure. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            if (DefaultLookup.getBoolean(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                   "OptionPane.setButtonMargin", true) && sizeButtonsToSame &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                   createdAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                JButton               aButton;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                int                   padSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                padSize = (numButtons <= 2? 8 : 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                for(int counter = 0; counter < numButtons; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    aButton = createdButtons[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    aButton.setMargin(new Insets(2, padSize, 2, padSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    protected ActionListener createButtonActionListener(int buttonIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        return new ButtonActionListener(buttonIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * Returns the buttons to display from the JOptionPane the receiver is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * providing the look and feel for. If the JOptionPane has options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * set, they will be provided, otherwise if the optionType is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * YES_NO_OPTION, yesNoOptions is returned, if the type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * YES_NO_CANCEL_OPTION yesNoCancelOptions is returned, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * defaultButtons are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    protected Object[] getButtons() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        if (optionPane != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            Object[] suppliedOptions = optionPane.getOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            if (suppliedOptions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                Object[] defaultOptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                int type = optionPane.getOptionType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                Locale l = optionPane.getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                int minimumWidth =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    DefaultLookup.getInt(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                                        "OptionPane.buttonMinimumWidth",-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                if (type == JOptionPane.YES_NO_OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    defaultOptions = new ButtonFactory[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                    defaultOptions[0] = new ButtonFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                        UIManager.getString("OptionPane.yesButtonText", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                        getMnemonic("OptionPane.yesButtonMnemonic", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                        (Icon)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                                          "OptionPane.yesIcon"), minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    defaultOptions[1] = new ButtonFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                        UIManager.getString("OptionPane.noButtonText", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                        getMnemonic("OptionPane.noButtonMnemonic", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                        (Icon)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                                          "OptionPane.noIcon"), minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                } else if (type == JOptionPane.YES_NO_CANCEL_OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    defaultOptions = new ButtonFactory[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                    defaultOptions[0] = new ButtonFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                        UIManager.getString("OptionPane.yesButtonText", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                        getMnemonic("OptionPane.yesButtonMnemonic", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                        (Icon)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                          "OptionPane.yesIcon"), minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    defaultOptions[1] = new ButtonFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                        UIManager.getString("OptionPane.noButtonText",l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                        getMnemonic("OptionPane.noButtonMnemonic", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                        (Icon)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                                          "OptionPane.noIcon"), minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    defaultOptions[2] = new ButtonFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                        UIManager.getString("OptionPane.cancelButtonText",l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                        getMnemonic("OptionPane.cancelButtonMnemonic", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                        (Icon)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                                          "OptionPane.cancelIcon"), minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                } else if (type == JOptionPane.OK_CANCEL_OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    defaultOptions = new ButtonFactory[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    defaultOptions[0] = new ButtonFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                        UIManager.getString("OptionPane.okButtonText",l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                        getMnemonic("OptionPane.okButtonMnemonic", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                        (Icon)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                                          "OptionPane.okIcon"), minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    defaultOptions[1] = new ButtonFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                        UIManager.getString("OptionPane.cancelButtonText",l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                        getMnemonic("OptionPane.cancelButtonMnemonic", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                        (Icon)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                                          "OptionPane.cancelIcon"), minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    defaultOptions = new ButtonFactory[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    defaultOptions[0] = new ButtonFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                        UIManager.getString("OptionPane.okButtonText",l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        getMnemonic("OptionPane.okButtonMnemonic", l),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                        (Icon)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                          "OptionPane.okIcon"), minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                return defaultOptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            return suppliedOptions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    private int getMnemonic(String key, Locale l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        String value = (String)UIManager.get(key, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return Integer.parseInt(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        catch (NumberFormatException nfe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    /**
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 11809
diff changeset
   830
     * Returns true, basic L&amp;F wants all the buttons to have the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    protected boolean getSizeButtonsToSameWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Returns the initial index into the buttons to select. The index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * is calculated from the initial value from the JOptionPane and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * options of the JOptionPane or 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    protected int getInitialValueIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        if (optionPane != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            Object             iv = optionPane.getInitialValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            Object[]           options = optionPane.getOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            if(options == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            else if(iv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                for(int counter = options.length - 1; counter >= 0; counter--){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    if(options[counter].equals(iv))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        return counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * Sets the input value in the option pane the receiver is providing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * the look and feel for based on the value in the inputComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    protected void resetInputValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        if(inputComponent != null && (inputComponent instanceof JTextField)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            optionPane.setInputValue(((JTextField)inputComponent).getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        } else if(inputComponent != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                  (inputComponent instanceof JComboBox)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            optionPane.setInputValue(((JComboBox)inputComponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                                     .getSelectedItem());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        } else if(inputComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            optionPane.setInputValue(((JList)inputComponent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                                     .getSelectedValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * If inputComponent is non-null, the focus is requested on that,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * otherwise request focus on the default value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    public void selectInitialValue(JOptionPane op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        if (inputComponent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            inputComponent.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            if (initialFocusComponent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                initialFocusComponent.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            if (initialFocusComponent instanceof JButton) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                JRootPane root = SwingUtilities.getRootPane(initialFocusComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                    root.setDefaultButton((JButton)initialFocusComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * Returns true if in the last call to validateComponent the message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * or buttons contained a subclass of Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    public boolean containsCustomComponents(JOptionPane op) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        return hasCustomComponents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * <code>ButtonAreaLayout</code> behaves in a similar manner to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <code>FlowLayout</code>. It lays out all components from left to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * right. If <code>syncAllWidths</code> is true, the widths of each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * component will be set to the largest preferred size width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * This class should be treated as a &quot;protected&quot; inner class.
9230
c5674561cb9e 6985329: 9 classes in swing.plaf contains words inappropriate for public spec - about some compiler bug
alexp
parents: 5506
diff changeset
   915
     * Instantiate it only within subclasses of {@code BasicOptionPaneUI}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    public static class ButtonAreaLayout implements LayoutManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        protected boolean           syncAllWidths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        protected int               padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        /** If true, children are lumped together in parent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        protected boolean           centersChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        private int orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        private boolean reverseButtons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
         * Indicates whether or not centersChildren should be used vs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
         * the orientation. This is done for backward compatability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * for subclassers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        private boolean useOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        public ButtonAreaLayout(boolean syncAllWidths, int padding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            this.syncAllWidths = syncAllWidths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            this.padding = padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            centersChildren = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            useOrientation = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        ButtonAreaLayout(boolean syncAllSizes, int padding, int orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                         boolean reverseButtons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            this(syncAllSizes, padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            useOrientation = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            this.orientation = orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            this.reverseButtons = reverseButtons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        public void setSyncAllWidths(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            syncAllWidths = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        public boolean getSyncAllWidths() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            return syncAllWidths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        public void setPadding(int newPadding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            this.padding = newPadding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        public int getPadding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            return padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        public void setCentersChildren(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            centersChildren = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            useOrientation = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        public boolean getCentersChildren() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            return centersChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        private int getOrientation(Container container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            if (!useOrientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                return SwingConstants.CENTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            if (container.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                return orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            switch (orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            case SwingConstants.LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                return SwingConstants.RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            case SwingConstants.RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                return SwingConstants.LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            case SwingConstants.CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                return SwingConstants.CENTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            return SwingConstants.LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        public void addLayoutComponent(String string, Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        public void layoutContainer(Container container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            Component[]      children = container.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            if(children != null && children.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                int               numChildren = children.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                Insets            insets = container.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                int maxWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                int maxHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                int totalButtonWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                int xOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                boolean ltr = container.getComponentOrientation().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                                        isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                boolean reverse = (ltr) ? reverseButtons : !reverseButtons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                for(int counter = 0; counter < numChildren; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                    Dimension pref = children[counter].getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                    maxWidth = Math.max(maxWidth, pref.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                    maxHeight = Math.max(maxHeight, pref.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                    totalButtonWidth += pref.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                if (getSyncAllWidths()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                    totalButtonWidth = maxWidth * numChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                totalButtonWidth += (numChildren - 1) * padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                switch (getOrientation(container)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                case SwingConstants.LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    x = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                case SwingConstants.RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                    x = container.getWidth() - insets.right - totalButtonWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                case SwingConstants.CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                    if (getCentersChildren() || numChildren < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        x = (container.getWidth() - totalButtonWidth) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                        x = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                        if (getSyncAllWidths()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                            xOffset = (container.getWidth() - insets.left -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                                       insets.right - totalButtonWidth) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                                (numChildren - 1) + maxWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                            xOffset = (container.getWidth() - insets.left -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                                       insets.right - totalButtonWidth) /
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                                      (numChildren - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                for (int counter = 0; counter < numChildren; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    int index = (reverse) ? numChildren - counter - 1 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                                counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                    Dimension pref = children[index].getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    if (getSyncAllWidths()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                        children[index].setBounds(x, insets.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                                                  maxWidth, maxHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                        children[index].setBounds(x, insets.top, pref.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                                                  pref.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    if (xOffset != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                        x += xOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                        x += children[index].getWidth() + padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        public Dimension minimumLayoutSize(Container c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            if(c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                Component[]       children = c.getComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                if(children != null && children.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    Dimension     aSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                    int           numChildren = children.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    int           height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    Insets        cInsets = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    int           extraHeight = cInsets.top + cInsets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    int           extraWidth = cInsets.left + cInsets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    if (syncAllWidths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        int              maxWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                        for(int counter = 0; counter < numChildren; counter++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                            aSize = children[counter].getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                            height = Math.max(height, aSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                            maxWidth = Math.max(maxWidth, aSize.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                        return new Dimension(extraWidth + (maxWidth * numChildren) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                                             (numChildren - 1) * padding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                                             extraHeight + height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                        int        totalWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                        for(int counter = 0; counter < numChildren; counter++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                            aSize = children[counter].getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                            height = Math.max(height, aSize.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                            totalWidth += aSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                        totalWidth += ((numChildren - 1) * padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                        return new Dimension(extraWidth + totalWidth, extraHeight + height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        public Dimension preferredLayoutSize(Container c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            return minimumLayoutSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        public void removeLayoutComponent(Component c) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * This class should be treated as a &quot;protected&quot; inner class.
9230
c5674561cb9e 6985329: 9 classes in swing.plaf contains words inappropriate for public spec - about some compiler bug
alexp
parents: 5506
diff changeset
  1118
     * Instantiate it only within subclasses of {@code BasicOptionPaneUI}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    public class PropertyChangeHandler implements PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
         * If the source of the PropertyChangeEvent <code>e</code> equals the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
         * optionPane and is one of the ICON_PROPERTY, MESSAGE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
         * OPTIONS_PROPERTY or INITIAL_VALUE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
         * validateComponent is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            getHandler().propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * Configures any necessary colors/fonts for the specified label
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * used representing the message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    private void configureMessageLabel(JLabel label) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        Color color = (Color)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                                               "OptionPane.messageForeground");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        if (color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            label.setForeground(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        Font messageFont = (Font)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                                                   "OptionPane.messageFont");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        if (messageFont != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            label.setFont(messageFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * Configures any necessary colors/fonts for the specified button
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * used representing the button portion of the optionpane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    private void configureButton(JButton button) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        Font buttonFont = (Font)DefaultLookup.get(optionPane, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                                            "OptionPane.buttonFont");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        if (buttonFont != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            button.setFont(buttonFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * This class should be treated as a &quot;protected&quot; inner class.
9230
c5674561cb9e 6985329: 9 classes in swing.plaf contains words inappropriate for public spec - about some compiler bug
alexp
parents: 5506
diff changeset
  1163
     * Instantiate it only within subclasses of {@code BasicOptionPaneUI}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    public class ButtonActionListener implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        protected int buttonIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        public ButtonActionListener(int buttonIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            this.buttonIndex = buttonIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            if (optionPane != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                int optionType = optionPane.getOptionType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                Object[] options = optionPane.getOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                /* If the option pane takes input, then store the input value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                 * if custom options were specified, if the option type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                 * DEFAULT_OPTION, OR if option type is set to a predefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                 * one and the user chose the affirmative answer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                if (inputComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                    if (options != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                        optionType == JOptionPane.DEFAULT_OPTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                        ((optionType == JOptionPane.YES_NO_OPTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                         optionType == JOptionPane.YES_NO_CANCEL_OPTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                         optionType == JOptionPane.OK_CANCEL_OPTION) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                         buttonIndex == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                        resetInputValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                if (options == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                    if (optionType == JOptionPane.OK_CANCEL_OPTION &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                        buttonIndex == 1) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1195
                        optionPane.setValue(Integer.valueOf(2));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    } else {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1198
                        optionPane.setValue(Integer.valueOf(buttonIndex));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                    optionPane.setValue(options[buttonIndex]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    private class Handler implements ActionListener, MouseListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                                     PropertyChangeListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        // ActionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            optionPane.setInputValue(((JTextField)e.getSource()).getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        // MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
            if (e.getClickCount() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                JList     list = (JList)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                int       index = list.locationToIndex(e.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                optionPane.setInputValue(list.getModel().getElementAt(index));
11809
d65aab67ee8a 7138665: JOptionPane.getValue() unexpected change between JRE 1.6 and JRE 1.7
rupashka
parents: 9230
diff changeset
  1239
                optionPane.setValue(JOptionPane.OK_OPTION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            if(e.getSource() == optionPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                // Option Pane Auditory Cue Activation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                // only respond to "ancestor" changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                // the idea being that a JOptionPane gets a JDialog when it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                // set to appear and loses it's JDialog when it is dismissed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                if ("ancestor" == e.getPropertyName()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                    JOptionPane op = (JOptionPane)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                    boolean isComingUp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    // if the old value is null, then the JOptionPane is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                    // created since it didn't previously have an ancestor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                    if (e.getOldValue() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                        isComingUp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                        isComingUp = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                    // figure out what to do based on the message type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                    switch (op.getMessageType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                    case JOptionPane.PLAIN_MESSAGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                        if (isComingUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                            BasicLookAndFeel.playSound(optionPane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                                               "OptionPane.informationSound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                    case JOptionPane.QUESTION_MESSAGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                        if (isComingUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                            BasicLookAndFeel.playSound(optionPane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                                             "OptionPane.questionSound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                    case JOptionPane.INFORMATION_MESSAGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                        if (isComingUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                            BasicLookAndFeel.playSound(optionPane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                                             "OptionPane.informationSound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    case JOptionPane.WARNING_MESSAGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                        if (isComingUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                            BasicLookAndFeel.playSound(optionPane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                                             "OptionPane.warningSound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                    case JOptionPane.ERROR_MESSAGE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                        if (isComingUp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                            BasicLookAndFeel.playSound(optionPane,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                                             "OptionPane.errorSound");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                        System.err.println("Undefined JOptionPane type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                                           op.getMessageType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                // Visual activity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                String         changeName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                if(changeName == JOptionPane.OPTIONS_PROPERTY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                   changeName == JOptionPane.INITIAL_VALUE_PROPERTY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                   changeName == JOptionPane.ICON_PROPERTY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                   changeName == JOptionPane.MESSAGE_TYPE_PROPERTY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                   changeName == JOptionPane.OPTION_TYPE_PROPERTY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                   changeName == JOptionPane.MESSAGE_PROPERTY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                   changeName == JOptionPane.SELECTION_VALUES_PROPERTY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                   changeName == JOptionPane.INITIAL_SELECTION_VALUE_PROPERTY ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                   changeName == JOptionPane.WANTS_INPUT_PROPERTY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                   uninstallComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                   installComponents();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                   optionPane.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                else if (changeName == "componentOrientation") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                    ComponentOrientation o = (ComponentOrientation)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                    JOptionPane op = (JOptionPane)e.getSource();
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
  1321
                    if (o != e.getOldValue()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                        op.applyComponentOrientation(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    // Classes used when optionPane.getWantsInput returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * A JTextField that allows you to specify an array of KeyStrokes that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * that will have their bindings processed regardless of whether or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * not they are registered on the JTextField. This is used as we really
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * want the ActionListener to be notified so that we can push the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * change to the JOptionPane, but we also want additional bindings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * (those of the JRootPane) to be processed as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    private static class MultiplexingTextField extends JTextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        private KeyStroke[] strokes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        MultiplexingTextField(int cols) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            super(cols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
         * Sets the KeyStrokes that will be additional processed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
         * ancestor bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        void setKeyStrokes(KeyStroke[] strokes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            this.strokes = strokes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                                            int condition, boolean pressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            boolean processed = super.processKeyBinding(ks, e, condition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                                                        pressed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            if (processed && condition != JComponent.WHEN_IN_FOCUSED_WINDOW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                for (int counter = strokes.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                         counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                    if (strokes[counter].equals(ks)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                        // Returning false will allow further processing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                        // of the bindings, eg our parent Containers will get a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                        // crack at them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
            return processed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * Registered in the ActionMap. Sets the value of the option pane
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * to <code>JOptionPane.CLOSED_OPTION</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        private static final String CLOSE = "close";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        Actions(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            super(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            if (getName() == CLOSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                JOptionPane optionPane = (JOptionPane)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
  1394
                optionPane.setValue(Integer.valueOf(JOptionPane.CLOSED_OPTION));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * This class is used to create the default buttons. This indirection is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * used so that addButtonComponents can tell which Buttons were created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * by us vs subclassers or from the JOptionPane itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    private static class ButtonFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        private String text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        private int mnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        private Icon icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        private int minimumWidth = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        ButtonFactory(String text, int mnemonic, Icon icon, int minimumWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            this.mnemonic = mnemonic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            this.icon = icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            this.minimumWidth = minimumWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        JButton createButton() {
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 438
diff changeset
  1419
            JButton button;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            if (minimumWidth > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                button = new ConstrainedButton(text, minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                button = new JButton(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            if (icon != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                button.setIcon(icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            if (mnemonic != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                button.setMnemonic(mnemonic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            return button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        private static class ConstrainedButton extends JButton {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            int minimumWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            ConstrainedButton(String text, int minimumWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                super(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                this.minimumWidth = minimumWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            public Dimension getMinimumSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                Dimension min = super.getMinimumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                min.width = Math.max(min.width, minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                return min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            public Dimension getPreferredSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                Dimension pref = super.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                pref.width = Math.max(pref.width, minimumWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                return pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
}