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