1   /*
2    * Copyright (c) 2003 Sun Microsystems, Inc. All  Rights Reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    * 
8    * -Redistributions of source code must retain the above copyright
9    *  notice, this list of conditions and the following disclaimer.
10   * 
11   * -Redistribution in binary form must reproduct the above copyright
12   *  notice, this list of conditions and the following disclaimer in
13   *  the documentation and/or other materials provided with the distribution.
14   * 
15   * Neither the name of Sun Microsystems, Inc. or the names of contributors
16   * may be used to endorse or promote products derived from this software
17   * without specific prior written permission.
18   * 
19   * This software is provided "AS IS," without a warranty of any kind. ALL
20   * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
21   * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
22   * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
23   * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
24   * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
25   * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
26   * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
27   * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
28   * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
29   * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30   * 
31   * You acknowledge that Software is not designed, licensed or intended for
32   * use in the design, construction, operation or maintenance of any nuclear
33   * facility.
34   */
35  
36  /*
37   * @(#)OptionPaneDemo.java  1.7 03/01/23
38   */
39  
40  
41  import javax.swing.*;
42  import javax.swing.event.*;
43  import javax.swing.text.*;
44  import javax.swing.border.*;
45  import javax.swing.colorchooser.*;
46  import javax.swing.filechooser.*;
47  import javax.accessibility.*;
48  
49  import java.awt.*;
50  import java.awt.event.*;
51  import java.beans.*;
52  import java.util.*;
53  import java.io.*;
54  import java.applet.*;
55  import java.net.*;
56  
57  /**
58   * JOptionPaneDemo
59   *
60   * @version 1.7 01/23/03
61   * @author Jeff Dinkins
62   */
63  public class OptionPaneDemo extends DemoModule {
64  
65      /**
66       * main method allows us to run as a standalone demo.
67       */
68      public static void main(String[] args) {
69      OptionPaneDemo demo = new OptionPaneDemo(null);
70      demo.mainImpl();
71      }
72  
73      /**
74       * OptionPaneDemo Constructor
75       */
76      public OptionPaneDemo(SwingSet2 swingset) {
77      // Set the title for this demo, and an icon used to represent this
78      // demo inside the SwingSet2 app.
79      super(swingset, "OptionPaneDemo", "toolbar/JOptionPane.gif");
80  
81      JPanel demo = getDemoPanel();
82  
83      demo.setLayout(new BoxLayout(demo, BoxLayout.X_AXIS));
84  
85      JPanel bp = new JPanel() {
86          public Dimension getMaximumSize() {
87          return new Dimension(getPreferredSize().width, super.getMaximumSize().height);
88          }
89      };
90      bp.setLayout(new BoxLayout(bp, BoxLayout.Y_AXIS));
91  
92      bp.add(Box.createRigidArea(VGAP30));
93      bp.add(Box.createRigidArea(VGAP30));
94  
95      bp.add(createInputDialogButton());      bp.add(Box.createRigidArea(VGAP15));
96      bp.add(createWarningDialogButton());    bp.add(Box.createRigidArea(VGAP15));
97      bp.add(createMessageDialogButton());    bp.add(Box.createRigidArea(VGAP15));
98      bp.add(createComponentDialogButton());  bp.add(Box.createRigidArea(VGAP15));
99      bp.add(createConfirmDialogButton());    bp.add(Box.createVerticalGlue());
100 
101     demo.add(Box.createHorizontalGlue());
102     demo.add(bp);
103     demo.add(Box.createHorizontalGlue());
104     }
105 
106     public JButton createWarningDialogButton() {
107     Action a = new AbstractAction(getString("OptionPaneDemo.warningbutton")) {
108         public void actionPerformed(ActionEvent e) {
109         JOptionPane.showMessageDialog(
110             getDemoPanel(),
111             getString("OptionPaneDemo.warningtext"),
112             getString("OptionPaneDemo.warningtitle"),
113             JOptionPane.WARNING_MESSAGE
114         );
115         }
116     };
117     return createButton(a);
118     }
119 
120     public JButton createMessageDialogButton() {
121     Action a = new AbstractAction(getString("OptionPaneDemo.messagebutton")) {
122         URL img = getClass().getResource("/resources/images/optionpane/bottle.gif");
123         String imagesrc = "<img src=\"" + img + "\" width=\"284\" height=\"100\">";
124         String message = getString("OptionPaneDemo.messagetext");
125         public void actionPerformed(ActionEvent e) {
126         JOptionPane.showMessageDialog(
127             getDemoPanel(),
128             "<html>" + imagesrc + "<br><center>" + message + "</center><br></html>"
129         );
130         }
131     };
132     return createButton(a);
133     }
134 
135     public JButton createConfirmDialogButton() {
136     Action a = new AbstractAction(getString("OptionPaneDemo.confirmbutton")) {
137         public void actionPerformed(ActionEvent e) {
138                 int result = JOptionPane.showConfirmDialog(getDemoPanel(), getString("OptionPaneDemo.confirmquestion"));
139                 if(result == JOptionPane.YES_OPTION) {
140             JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmyes"));
141         } else if(result == JOptionPane.NO_OPTION) {
142                     JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.confirmno"));
143         }
144         }
145     };
146     return createButton(a);
147     }
148 
149     public JButton createInputDialogButton() {
150     Action a = new AbstractAction(getString("OptionPaneDemo.inputbutton")) {
151         public void actionPerformed(ActionEvent e) {
152                 String result = JOptionPane.showInputDialog(getDemoPanel(), getString("OptionPaneDemo.inputquestion"));
153         JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.inputresponse"));
154         }
155     };
156     return createButton(a);
157     }
158 
159     public JButton createComponentDialogButton() {
160     Action a = new AbstractAction(getString("OptionPaneDemo.componentbutton")) {
161         public void actionPerformed(ActionEvent e) {
162         // In a ComponentDialog, you can show as many message components and
163         // as many options as you want:
164 
165         // Messages
166                 Object[]      message = new Object[4];
167                 message[0] = getString("OptionPaneDemo.componentmessage");
168                 message[1] = new JTextField(getString("OptionPaneDemo.componenttextfield"));
169 
170                 JComboBox cb = new JComboBox();
171                 cb.addItem(getString("OptionPaneDemo.component_cb1"));
172                 cb.addItem(getString("OptionPaneDemo.component_cb2"));
173                 cb.addItem(getString("OptionPaneDemo.component_cb3"));
174                 message[2] = cb;
175 
176                 message[3] = getString("OptionPaneDemo.componentmessage2");
177 
178         // Options
179                 String[] options = {
180             getString("OptionPaneDemo.component_op1"),
181             getString("OptionPaneDemo.component_op2"),
182             getString("OptionPaneDemo.component_op3"),
183             getString("OptionPaneDemo.component_op4"),
184             getString("OptionPaneDemo.component_op5")
185         };
186                 int result = JOptionPane.showOptionDialog(
187             getDemoPanel(),                             // the parent that the dialog blocks
188             message,                                    // the dialog message array
189             getString("OptionPaneDemo.componenttitle"), // the title of the dialog window
190             JOptionPane.DEFAULT_OPTION,                 // option type
191             JOptionPane.INFORMATION_MESSAGE,            // message type
192             null,                                       // optional icon, use null to use the default icon
193             options,                                    // options string array, will be made into buttons
194             options[3]                                  // option that should be made into a default button
195         );
196         switch(result) {
197            case 0: // yes
198              JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r1"));
199              break;
200            case 1: // no
201              JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r2"));
202              break;
203            case 2: // maybe
204              JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r3"));
205              break;
206            case 3: // probably
207              JOptionPane.showMessageDialog(getDemoPanel(), getString("OptionPaneDemo.component_r4"));
208              break;
209            default:
210              break;
211         }
212 
213         }
214     };
215     return createButton(a);
216     }
217         
218     public JButton createButton(Action a) {
219     JButton b = new JButton() {
220         public Dimension getMaximumSize() {
221         int width = Short.MAX_VALUE;
222         int height = super.getMaximumSize().height;
223         return new Dimension(width, height);
224         }
225     };
226     // setting the following client property informs the button to show
227     // the action text as it's name. The default is to not show the
228     // action text.
229     b.putClientProperty("displayActionText", Boolean.TRUE);
230     b.setAction(a);
231     // b.setAlignmentX(JButton.CENTER_ALIGNMENT);
232     return b;
233     }
234 
235 }
236