KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicOptionPaneUI


1 /*
2  * @(#)BasicOptionPaneUI.java 1.58 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.plaf.basic;
9
10 import sun.swing.DefaultLookup;
11 import sun.swing.UIAction;
12 import javax.swing.border.Border JavaDoc;
13 import javax.swing.border.EmptyBorder JavaDoc;
14 import javax.swing.*;
15 import javax.swing.event.*;
16 import javax.swing.plaf.ActionMapUIResource JavaDoc;
17 import javax.swing.plaf.ComponentUI JavaDoc;
18 import javax.swing.plaf.OptionPaneUI JavaDoc;
19 import java.awt.*;
20 import java.awt.event.*;
21 import java.beans.PropertyChangeEvent JavaDoc;
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.security.AccessController JavaDoc;
25
26 import sun.security.action.GetPropertyAction;
27
28
29 /**
30  * Provides the basic look and feel for a <code>JOptionPane</code>.
31  * <code>BasicMessagePaneUI</code> provides a means to place an icon,
32  * message and buttons into a <code>Container</code>.
33  * Generally, the layout will look like:<p>
34  * <pre>
35  * ------------------
36  * | i | message |
37  * | c | message |
38  * | o | message |
39  * | n | message |
40  * ------------------
41  * | buttons |
42  * |________________|
43  * </pre>
44  * icon is an instance of <code>Icon</code> that is wrapped inside a
45  * <code>JLabel</code>. The message is an opaque object and is tested
46  * for the following: if the message is a <code>Component</code> it is
47  * added to the <code>Container</code>, if it is an <code>Icon</code>
48  * it is wrapped inside a <code>JLabel</code> and added to the
49  * <code>Container</code> otherwise it is wrapped inside a <code>JLabel</code>.
50  * <p>
51  * The above layout is used when the option pane's
52  * <code>ComponentOrientation</code> property is horizontal, left-to-right.
53  * The layout will be adjusted appropriately for other orientations.
54  * <p>
55  * The <code>Container</code>, message, icon, and buttons are all
56  * determined from abstract methods.
57  *
58  * @version 1.58 12/19/03
59  * @author James Gosling
60  * @author Scott Violet
61  * @author Amy Fowler
62  */

63 public class BasicOptionPaneUI extends OptionPaneUI JavaDoc {
64
65     public static final int MinimumWidth = 262;
66     public static final int MinimumHeight = 90;
67
68     private static String JavaDoc newline;
69
70     /**
71      * <code>JOptionPane</code> that the receiver is providing the
72      * look and feel for.
73      */

74     protected JOptionPane optionPane;
75
76     protected Dimension minimumSize;
77
78     /** JComponent provide for input if optionPane.getWantsInput() returns
79      * true. */

80     protected JComponent inputComponent;
81
82     /** Component to receive focus when messaged with selectInitialValue. */
83     protected Component initialFocusComponent;
84
85     /** This is set to true in validateComponent if a Component is contained
86      * in either the message or the buttons. */

87     protected boolean hasCustomComponents;
88
89     protected PropertyChangeListener JavaDoc propertyChangeListener;
90
91     private Handler handler;
92
93
94     static {
95     newline = (String JavaDoc)java.security.AccessController.doPrivileged(
96                                 new GetPropertyAction("line.separator"));
97         if (newline == null) {
98             newline = "\n";
99         }
100     }
101
102     static void loadActionMap(LazyActionMap JavaDoc map) {
103     map.put(new Actions(Actions.CLOSE));
104         BasicLookAndFeel.installAudioActionMap(map);
105     }
106
107
108
109     /**
110       * Creates a new BasicOptionPaneUI instance.
111       */

112     public static ComponentUI JavaDoc createUI(JComponent x) {
113     return new BasicOptionPaneUI JavaDoc();
114     }
115
116     /**
117       * Installs the receiver as the L&F for the passed in
118       * <code>JOptionPane</code>.
119       */

120     public void installUI(JComponent c) {
121     optionPane = (JOptionPane)c;
122         installDefaults();
123         optionPane.setLayout(createLayoutManager());
124     installComponents();
125         installListeners();
126         installKeyboardActions();
127     }
128
129     /**
130       * Removes the receiver from the L&F controller of the passed in split
131       * pane.
132       */

133     public void uninstallUI(JComponent c) {
134         uninstallComponents();
135         optionPane.setLayout(null);
136         uninstallKeyboardActions();
137         uninstallListeners();
138         uninstallDefaults();
139     optionPane = null;
140     }
141
142     protected void installDefaults() {
143         LookAndFeel.installColorsAndFont(optionPane, "OptionPane.background",
144                                          "OptionPane.foreground", "OptionPane.font");
145     LookAndFeel.installBorder(optionPane, "OptionPane.border");
146         minimumSize = UIManager.getDimension("OptionPane.minimumSize");
147         LookAndFeel.installProperty(optionPane, "opaque", Boolean.TRUE);
148     }
149
150     protected void uninstallDefaults() {
151     LookAndFeel.uninstallBorder(optionPane);
152     }
153
154     protected void installComponents() {
155     optionPane.add(createMessageArea());
156         
157         Container separator = createSeparator();
158         if (separator != null) {
159             optionPane.add(separator);
160         }
161     optionPane.add(createButtonArea());
162     optionPane.applyComponentOrientation(optionPane.getComponentOrientation());
163     }
164
165     protected void uninstallComponents() {
166     hasCustomComponents = false;
167         inputComponent = null;
168     initialFocusComponent = null;
169     optionPane.removeAll();
170     }
171
172     protected LayoutManager createLayoutManager() {
173         return new BoxLayout(optionPane, BoxLayout.Y_AXIS);
174     }
175
176     protected void installListeners() {
177         if ((propertyChangeListener = createPropertyChangeListener()) != null) {
178             optionPane.addPropertyChangeListener(propertyChangeListener);
179         }
180     }
181
182     protected void uninstallListeners() {
183         if (propertyChangeListener != null) {
184             optionPane.removePropertyChangeListener(propertyChangeListener);
185             propertyChangeListener = null;
186         }
187         handler = null;
188     }
189
190     protected PropertyChangeListener JavaDoc createPropertyChangeListener() {
191         return getHandler();
192     }
193
194     private Handler getHandler() {
195         if (handler == null) {
196             handler = new Handler();
197         }
198         return handler;
199     }
200
201     protected void installKeyboardActions() {
202     InputMap map = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
203
204     SwingUtilities.replaceUIInputMap(optionPane, JComponent.
205                        WHEN_IN_FOCUSED_WINDOW, map);
206
207         LazyActionMap.installLazyActionMap(optionPane, BasicOptionPaneUI JavaDoc.class,
208                                            "OptionPane.actionMap");
209     }
210
211     protected void uninstallKeyboardActions() {
212     SwingUtilities.replaceUIInputMap(optionPane, JComponent.
213                        WHEN_IN_FOCUSED_WINDOW, null);
214     SwingUtilities.replaceUIActionMap(optionPane, null);
215     }
216
217     InputMap getInputMap(int condition) {
218     if (condition == JComponent.WHEN_IN_FOCUSED_WINDOW) {
219         Object JavaDoc[] bindings = (Object JavaDoc[])DefaultLookup.get(
220                              optionPane, this, "OptionPane.windowBindings");
221         if (bindings != null) {
222         return LookAndFeel.makeComponentInputMap(optionPane, bindings);
223         }
224     }
225     return null;
226     }
227
228     /**
229      * Returns the minimum size the option pane should be. Primarily
230      * provided for subclassers wishing to offer a different minimum size.
231      */

232     public Dimension getMinimumOptionPaneSize() {
233         if (minimumSize == null) {
234             return new Dimension(MinimumWidth, MinimumHeight);
235         }
236     return new Dimension(minimumSize.width,
237                  minimumSize.height);
238     }
239
240     /**
241      * If <code>c</code> is the <code>JOptionPane</code> the receiver
242      * is contained in, the preferred
243      * size that is returned is the maximum of the preferred size of
244      * the <code>LayoutManager</code> for the <code>JOptionPane</code>, and
245      * <code>getMinimumOptionPaneSize</code>.
246      */

247     public Dimension getPreferredSize(JComponent c) {
248     if ((JOptionPane)c == optionPane) {
249         Dimension ourMin = getMinimumOptionPaneSize();
250         LayoutManager lm = c.getLayout();
251
252         if (lm != null) {
253         Dimension lmSize = lm.preferredLayoutSize(c);
254
255         if (ourMin != null)
256             return new Dimension
257             (Math.max(lmSize.width, ourMin.width),
258              Math.max(lmSize.height, ourMin.height));
259         return lmSize;
260         }
261         return ourMin;
262     }
263     return null;
264     }
265
266     /**
267      * Messaged from installComponents to create a Container containing the
268      * body of the message. The icon is the created by calling
269      * <code>addIcon</code>.
270      */

271     protected Container createMessageArea() {
272         JPanel top = new JPanel();
273         Border JavaDoc topBorder = (Border JavaDoc)DefaultLookup.get(optionPane, this,
274                                              "OptionPane.messageAreaBorder");
275         if (topBorder != null) {
276             top.setBorder(topBorder);
277         }
278     top.setLayout(new BorderLayout());
279
280     /* Fill the body. */
281     Container body = new JPanel(new GridBagLayout());
282     Container realBody = new JPanel(new BorderLayout());
283
284         body.setName("OptionPane.body");
285         realBody.setName("OptionPane.realBody");
286
287     if (getIcon() != null) {
288             JPanel sep = new JPanel();
289             sep.setName("OptionPane.separator");
290             sep.setPreferredSize(new Dimension(15, 1));
291         realBody.add(sep, BorderLayout.BEFORE_LINE_BEGINS);
292     }
293     realBody.add(body, BorderLayout.CENTER);
294
295     GridBagConstraints cons = new GridBagConstraints();
296     cons.gridx = cons.gridy = 0;
297     cons.gridwidth = GridBagConstraints.REMAINDER;
298     cons.gridheight = 1;
299     cons.anchor = DefaultLookup.getInt(optionPane, this,
300                       "OptionPane.messageAnchor", GridBagConstraints.CENTER);
301     cons.insets = new Insets(0,0,3,0);
302
303     addMessageComponents(body, cons, getMessage(),
304               getMaxCharactersPerLineCount(), false);
305     top.add(realBody, BorderLayout.CENTER);
306
307     addIcon(top);
308     return top;
309     }
310
311     /**
312      * Creates the appropriate object to represent <code>msg</code> and
313      * places it into <code>container</code>. If <code>msg</code> is an
314      * instance of Component, it is added directly, if it is an Icon,
315      * a JLabel is created to represent it, otherwise a JLabel is
316      * created for the string, if <code>d</code> is an Object[], this
317      * method will be recursively invoked for the children.
318      * <code>internallyCreated</code> is true if Objc is an instance
319      * of Component and was created internally by this method (this is
320      * used to correctly set hasCustomComponents only if !internallyCreated).
321      */

322     protected void addMessageComponents(Container container,
323                      GridBagConstraints cons,
324                      Object JavaDoc msg, int maxll,
325                      boolean internallyCreated) {
326     if (msg == null) {
327         return;
328         }
329     if (msg instanceof Component) {
330             // To workaround problem where Gridbad will set child
331
// to its minimum size if its preferred size will not fit
332
// within allocated cells
333
if (msg instanceof JScrollPane || msg instanceof JPanel) {
334                 cons.fill = GridBagConstraints.BOTH;
335                 cons.weighty = 1;
336             } else {
337             cons.fill = GridBagConstraints.HORIZONTAL;
338             }
339         cons.weightx = 1;
340
341         container.add((Component) msg, cons);
342         cons.weightx = 0;
343             cons.weighty = 0;
344         cons.fill = GridBagConstraints.NONE;
345         cons.gridy++;
346         if (!internallyCreated) {
347         hasCustomComponents = true;
348             }
349
350     } else if (msg instanceof Object JavaDoc[]) {
351         Object JavaDoc [] msgs = (Object JavaDoc[]) msg;
352         for (int i = 0; i < msgs.length; i++) {
353         addMessageComponents(container, cons, msgs[i], maxll, false);
354             }
355
356     } else if (msg instanceof Icon) {
357         JLabel label = new JLabel( (Icon)msg, SwingConstants.CENTER );
358             configureMessageLabel(label);
359         addMessageComponents(container, cons, label, maxll, true);
360
361     } else {
362         String JavaDoc s = msg.toString();
363         int len = s.length();
364         if (len <= 0) {
365         return;
366             }
367         int nl = -1;
368         int nll = 0;
369
370         if ((nl = s.indexOf(newline)) >= 0) {
371         nll = newline.length();
372         } else if ((nl = s.indexOf("\r\n")) >= 0) {
373             nll = 2;
374         } else if ((nl = s.indexOf('\n')) >= 0) {
375             nll = 1;
376         }
377         if (nl >= 0) {
378         // break up newlines
379
if (nl == 0) {
380                     JPanel breakPanel = new JPanel() {
381                 public Dimension getPreferredSize() {
382                 Font f = getFont();
383                 
384                 if (f != null) {
385                 return new Dimension(1, f.getSize() + 2);
386                             }
387                 return new Dimension(0, 0);
388                 }
389                     };
390                     breakPanel.setName("OptionPane.break");
391             addMessageComponents(container, cons, breakPanel, maxll,
392                                          true);
393         } else {
394             addMessageComponents(container, cons, s.substring(0, nl),
395                       maxll, false);
396                 }
397         addMessageComponents(container, cons, s.substring(nl + nll), maxll,
398                   false);
399
400         } else if (len > maxll) {
401         Container c = Box.createVerticalBox();
402                 c.setName("OptionPane.verticalBox");
403         burstStringInto(c, s, maxll);
404         addMessageComponents(container, cons, c, maxll, true );
405
406         } else {
407             JLabel label;
408         label = new JLabel( s, JLabel.LEADING );
409                 label.setName("OptionPane.label");
410                 configureMessageLabel(label);
411         addMessageComponents(container, cons, label, maxll, true);
412         }
413     }
414     }
415
416     /**
417      * Returns the message to display from the JOptionPane the receiver is
418      * providing the look and feel for.
419      */

420     protected Object JavaDoc getMessage() {
421     inputComponent = null;
422     if (optionPane != null) {
423         if (optionPane.getWantsInput()) {
424         /* Create a user component to capture the input. If the
425            selectionValues are non null the component and there
426            are < 20 values it'll be a combobox, if non null and
427            >= 20, it'll be a list, otherwise it'll be a textfield. */

428         Object JavaDoc message = optionPane.getMessage();
429         Object JavaDoc[] sValues = optionPane.getSelectionValues();
430         Object JavaDoc inputValue = optionPane
431                                    .getInitialSelectionValue();
432         JComponent toAdd;
433
434         if (sValues != null) {
435             if (sValues.length < 20) {
436             JComboBox cBox = new JComboBox();
437
438                         cBox.setName("OptionPane.comboBox");
439             for(int counter = 0, maxCounter = sValues.length;
440                 counter < maxCounter; counter++) {
441                 cBox.addItem(sValues[counter]);
442                         }
443             if (inputValue != null) {
444                 cBox.setSelectedItem(inputValue);
445                         }
446             inputComponent = cBox;
447             toAdd = cBox;
448
449             } else {
450             JList list = new JList(sValues);
451             JScrollPane sp = new JScrollPane(list);
452
453                         sp.setName("OptionPane.scrollPane");
454                         list.setName("OptionPane.list");
455             list.setVisibleRowCount(10);
456             list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
457             if(inputValue != null)
458                 list.setSelectedValue(inputValue, true);
459             list.addMouseListener(getHandler());
460             toAdd = sp;
461             inputComponent = list;
462             }
463
464         } else {
465             MultiplexingTextField tf = new MultiplexingTextField(20);
466
467                     tf.setName("OptionPane.textField");
468                     tf.setKeyStrokes(new KeyStroke[] {
469                                      KeyStroke.getKeyStroke("ENTER") } );
470             if (inputValue != null) {
471                         String JavaDoc inputString = inputValue.toString();
472             tf.setText(inputString);
473                         tf.setSelectionStart(0);
474                         tf.setSelectionEnd(inputString.length());
475                     }
476             tf.addActionListener(getHandler());
477             toAdd = inputComponent = tf;
478         }
479
480         Object JavaDoc[] newMessage;
481
482         if (message == null) {
483             newMessage = new Object JavaDoc[1];
484             newMessage[0] = toAdd;
485         
486         } else {
487             newMessage = new Object JavaDoc[2];
488             newMessage[0] = message;
489             newMessage[1] = toAdd;
490         }
491         return newMessage;
492         }
493         return optionPane.getMessage();
494     }
495     return null;
496     }
497
498     /**
499      * Creates and adds a JLabel representing the icon returned from
500      * <code>getIcon</code> to <code>top</code>. This is messaged from
501      * <code>createMessageArea</code>
502      */

503     protected void addIcon(Container top) {
504     /* Create the icon. */
505     Icon sideIcon = getIcon();
506
507     if (sideIcon != null) {
508         JLabel iconLabel = new JLabel(sideIcon);
509
510             iconLabel.setName("OptionPane.iconLabel");
511         iconLabel.setVerticalAlignment(SwingConstants.TOP);
512         top.add(iconLabel, BorderLayout.BEFORE_LINE_BEGINS);
513     }
514     }
515
516     /**
517      * Returns the icon from the JOptionPane the receiver is providing
518      * the look and feel for, or the default icon as returned from
519      * <code>getDefaultIcon</code>.
520      */

521     protected Icon getIcon() {
522     Icon mIcon = (optionPane == null ? null : optionPane.getIcon());
523
524     if(mIcon == null && optionPane != null)
525         mIcon = getIconForType(optionPane.getMessageType());
526     return mIcon;
527     }
528
529     /**
530      * Returns the icon to use for the passed in type.
531      */

532     protected Icon getIconForType(int messageType) {
533     if(messageType < 0 || messageType > 3)
534         return null;
535         String JavaDoc propertyName = null;
536     switch(messageType) {
537     case 0:
538         propertyName = "OptionPane.errorIcon";
539             break;
540     case 1:
541         propertyName = "OptionPane.informationIcon";
542             break;
543     case 2:
544         propertyName = "OptionPane.warningIcon";
545             break;
546     case 3:
547         propertyName = "OptionPane.questionIcon";
548             break;
549     }
550         if (propertyName != null) {
551             return (Icon)DefaultLookup.get(optionPane, this, propertyName);
552     }
553     return null;
554     }
555
556     /**
557      * Returns the maximum number of characters to place on a line.
558      */

559     protected int getMaxCharactersPerLineCount() {
560     return optionPane.getMaxCharactersPerLineCount();
561     }
562
563    /**
564      * Recursively creates new JLabel instances to represent <code>d</code>.
565      * Each JLabel instance is added to <code>c</code>.
566      */

567     protected void burstStringInto(Container c, String JavaDoc d, int maxll) {
568     // Primitive line wrapping
569
int len = d.length();
570     if (len <= 0)
571         return;
572     if (len > maxll) {
573         int p = d.lastIndexOf(' ', maxll);
574         if (p <= 0)
575         p = d.indexOf(' ', maxll);
576         if (p > 0 && p < len) {
577         burstStringInto(c, d.substring(0, p), maxll);
578         burstStringInto(c, d.substring(p + 1), maxll);
579         return;
580         }
581     }
582     JLabel label = new JLabel(d, JLabel.LEFT);
583         label.setName("OptionPane.label");
584         configureMessageLabel(label);
585     c.add(label);
586     }
587
588     protected Container createSeparator() {
589         return null;
590     }
591
592     /**
593      * Creates and returns a Container containing the buttons. The buttons
594      * are created by calling <code>getButtons</code>.
595      */

596     protected Container createButtonArea() {
597         JPanel bottom = new JPanel();
598         Border JavaDoc border = (Border JavaDoc)DefaultLookup.get(optionPane, this,
599                                           "OptionPane.buttonAreaBorder");
600         bottom.setName("OptionPane.buttonArea");
601         if (border != null) {
602             bottom.setBorder(border);
603         }
604     bottom.setLayout(new ButtonAreaLayout(
605            DefaultLookup.getBoolean(optionPane, this,
606                                     "OptionPane.sameSizeButtons", true),
607            DefaultLookup.getInt(optionPane, this, "OptionPane.buttonPadding",
608                                 6),
609            DefaultLookup.getInt(optionPane, this,
610                         "OptionPane.buttonOrientation", SwingConstants.CENTER),
611            DefaultLookup.getBoolean(optionPane, this, "OptionPane.isYesLast",
612                                     false)));
613     addButtonComponents(bottom, getButtons(), getInitialValueIndex());
614     return bottom;
615     }
616
617     /**
618      * Creates the appropriate object to represent each of the objects in
619      * <code>buttons</code> and adds it to <code>container</code>. This
620      * differs from addMessageComponents in that it will recurse on
621      * <code>buttons</code> and that if button is not a Component
622      * it will create an instance of JButton.
623      */

624     protected void addButtonComponents(Container container, Object JavaDoc[] buttons,
625                  int initialIndex) {
626     if (buttons != null && buttons.length > 0) {
627         boolean sizeButtonsToSame = getSizeButtonsToSameWidth();
628         boolean createdAll = true;
629         int numButtons = buttons.length;
630         JButton[] createdButtons = null;
631         int maxWidth = 0;
632
633         if (sizeButtonsToSame) {
634         createdButtons = new JButton[numButtons];
635             }
636
637         for(int counter = 0; counter < numButtons; counter++) {
638         Object JavaDoc button = buttons[counter];
639         Component newComponent;
640
641         if (button instanceof Component) {
642             createdAll = false;
643             newComponent = (Component)button;
644             container.add(newComponent);
645             hasCustomComponents = true;
646         
647         } else {
648             JButton aButton;
649
650                     if (button instanceof ButtonFactory) {
651                         aButton = ((ButtonFactory)button).createButton();
652                     }
653             else if (button instanceof Icon)
654             aButton = new JButton((Icon)button);
655             else
656             aButton = new JButton(button.toString());
657
658                     aButton.setName("OptionPane.button");
659             aButton.setMultiClickThreshhold(DefaultLookup.getInt(
660                           optionPane, this, "OptionPane.buttonClickThreshhold",
661                           0));
662                     configureButton(aButton);
663
664             container.add(aButton);
665
666                     ActionListener buttonListener = createButtonActionListener(counter);
667                     if (buttonListener != null) {
668                         aButton.addActionListener(buttonListener);
669                     }
670             newComponent = aButton;
671         }
672         if (sizeButtonsToSame && createdAll &&
673            (newComponent instanceof JButton)) {
674             createdButtons[counter] = (JButton)newComponent;
675             maxWidth = Math.max(maxWidth,
676                     newComponent.getMinimumSize().width);
677         }
678         if (counter == initialIndex) {
679             initialFocusComponent = newComponent;
680                     if (initialFocusComponent instanceof JButton) {
681                         JButton defaultB = (JButton)initialFocusComponent;
682                         defaultB.addAncestorListener(new AncestorListener() {
683                            public void ancestorAdded(AncestorEvent e) {
684                                JButton defaultButton = (JButton)e.getComponent();
685                                JRootPane root = SwingUtilities.getRootPane(defaultButton);
686                                if (root != null) {
687                                    root.setDefaultButton(defaultButton);
688                                }
689                            }
690                            public void ancestorRemoved(AncestorEvent event) {}
691                            public void ancestorMoved(AncestorEvent event) {}
692                         });
693                     }
694         }
695         }
696         ((ButtonAreaLayout)container.getLayout()).
697                       setSyncAllWidths((sizeButtonsToSame && createdAll));
698         /* Set the padding, windows seems to use 8 if <= 2 components,
699            otherwise 4 is used. It may actually just be the size of the
700            buttons is always the same, not sure. */

701         if (DefaultLookup.getBoolean(optionPane, this,
702                    "OptionPane.setButtonMargin", true) && sizeButtonsToSame &&
703                    createdAll) {
704         JButton aButton;
705         int padSize;
706
707         padSize = (numButtons <= 2? 8 : 4);
708
709         for(int counter = 0; counter < numButtons; counter++) {
710             aButton = createdButtons[counter];
711             aButton.setMargin(new Insets(2, padSize, 2, padSize));
712         }
713         }
714     }
715     }
716
717     protected ActionListener createButtonActionListener(int buttonIndex) {
718         return new ButtonActionListener(buttonIndex);
719     }
720
721     /**
722      * Returns the buttons to display from the JOptionPane the receiver is
723      * providing the look and feel for. If the JOptionPane has options
724      * set, they will be provided, otherwise if the optionType is
725      * YES_NO_OPTION, yesNoOptions is returned, if the type is
726      * YES_NO_CANCEL_OPTION yesNoCancelOptions is returned, otherwise
727      * defaultButtons are returned.
728      */

729     protected Object JavaDoc[] getButtons() {
730     if (optionPane != null) {
731         Object JavaDoc[] suppliedOptions = optionPane.getOptions();
732
733         if (suppliedOptions == null) {
734                 Object JavaDoc[] defaultOptions;
735         int type = optionPane.getOptionType();
736                 Locale JavaDoc l = optionPane.getLocale();
737         if (type == JOptionPane.YES_NO_OPTION) {
738                     defaultOptions = new ButtonFactory[2];
739                     defaultOptions[0] = new ButtonFactory(
740                         UIManager.getString("OptionPane.yesButtonText", l),
741                         getMnemonic("OptionPane.yesButtonMnemonic", l),
742                         (Icon)DefaultLookup.get(optionPane, this,
743                                           "OptionPane.yesIcon"));
744                     defaultOptions[1] = new ButtonFactory(
745                         UIManager.getString("OptionPane.noButtonText", l),
746                         getMnemonic("OptionPane.noButtonMnemonic", l),
747                         (Icon)DefaultLookup.get(optionPane, this,
748                                           "OptionPane.noIcon"));
749         } else if (type == JOptionPane.YES_NO_CANCEL_OPTION) {
750                     defaultOptions = new ButtonFactory[3];
751                     defaultOptions[0] = new ButtonFactory(
752                         UIManager.getString("OptionPane.yesButtonText", l),
753                         getMnemonic("OptionPane.yesButtonMnemonic", l),
754                         (Icon)DefaultLookup.get(optionPane, this,
755                                           "OptionPane.yesIcon"));
756                     defaultOptions[1] = new ButtonFactory(
757                         UIManager.getString("OptionPane.noButtonText",l),
758                         getMnemonic("OptionPane.noButtonMnemonic", l),
759                         (Icon)DefaultLookup.get(optionPane, this,
760                                           "OptionPane.noIcon"));
761                     defaultOptions[2] = new ButtonFactory(
762                         UIManager.getString("OptionPane.cancelButtonText",l),
763                         getMnemonic("OptionPane.cancelButtonMnemonic", l),
764                         (Icon)DefaultLookup.get(optionPane, this,
765                                           "OptionPane.cancelIcon"));
766         } else if (type == JOptionPane.OK_CANCEL_OPTION) {
767                     defaultOptions = new ButtonFactory[2];
768                     defaultOptions[0] = new ButtonFactory(
769                         UIManager.getString("OptionPane.okButtonText",l),
770                         getMnemonic("OptionPane.okButtonMnemonic", l),
771                         (Icon)DefaultLookup.get(optionPane, this,
772                                           "OptionPane.okIcon"));
773                     defaultOptions[1] = new ButtonFactory(
774                         UIManager.getString("OptionPane.cancelButtonText",l),
775                         getMnemonic("OptionPane.cancelButtonMnemonic", l),
776                         (Icon)DefaultLookup.get(optionPane, this,
777                                           "OptionPane.cancelIcon"));
778         } else {
779                     defaultOptions = new ButtonFactory[1];
780                     defaultOptions[0] = new ButtonFactory(
781                         UIManager.getString("OptionPane.okButtonText",l),
782                         getMnemonic("OptionPane.okButtonMnemonic", l),
783                         (Icon)DefaultLookup.get(optionPane, this,
784                                           "OptionPane.okIcon"));
785                 }
786                 return defaultOptions;
787                 
788         }
789         return suppliedOptions;
790     }
791     return null;
792     }
793
794     private int getMnemonic(String JavaDoc key, Locale JavaDoc l) {
795         String JavaDoc value = (String JavaDoc)UIManager.get(key, l);
796
797         if (value == null) {
798             return 0;
799         }
800         try {
801             return Integer.parseInt(value);
802         }
803         catch (NumberFormatException JavaDoc nfe) { }
804         return 0;
805     }
806
807     /**
808      * Returns true, basic L&F wants all the buttons to have the same
809      * width.
810      */

811     protected boolean getSizeButtonsToSameWidth() {
812     return true;
813     }
814
815     /**
816      * Returns the initial index into the buttons to select. The index
817      * is calculated from the initial value from the JOptionPane and
818      * options of the JOptionPane or 0.
819      */

820     protected int getInitialValueIndex() {
821     if (optionPane != null) {
822         Object JavaDoc iv = optionPane.getInitialValue();
823         Object JavaDoc[] options = optionPane.getOptions();
824
825         i