KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > swing > ConfirmCheckBoxDialog


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util.swing;
35
36 import edu.rice.cs.drjava.ui.MainFrame;
37 import javax.swing.*;
38
39 /**
40  * Simple class wrapping JOptionPane to have a checkbox underneath the message.
41  * @version $Id: ConfirmCheckBoxDialog.java 4076 2007-01-19 23:01:04Z dlsmith $
42  */

43 public class ConfirmCheckBoxDialog {
44   private JDialog _dialog;
45   private JOptionPane _optionPane;
46   private JCheckBox _checkBox;
47
48   /** Instantiates a new confirm dialog with default checkbox text.
49    * @param parent the parent frame
50    * @param title the title of the dialog
51    * @param message the stuff to display in the body of the dialog. For a simple message, it should be a String; it can
52    * also be an Object[] including Strings and Components to display in the body of the dialog.
53    */

54   public ConfirmCheckBoxDialog(JFrame parent, String JavaDoc title, Object JavaDoc message) {
55     this(parent, title, message, "Do not show this message again");
56   }
57
58   /** Instantiates a new confirm dialog with Yes/No as the options.
59    * @param parent the parent frame
60    * @param title the title of the dialog
61    * @param message the stuff to display in the body of the dialog. For a simple message, it should be a String; it can
62    * also be an Object[] including Strings and Components to display in the body of the dialog.
63    * @param checkBoxText the text to display with the checkbox
64    */

65   public ConfirmCheckBoxDialog(JFrame parent, String JavaDoc title, Object JavaDoc message, String JavaDoc checkBoxText) {
66     this(parent, title, message, checkBoxText, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); }
67
68  /** Instantiates a new confirm dialog with Yes/No as the options.
69    * @param parent The parent frame
70    * @param title The title of the dialog
71    * @param message The stuff to display in the body of the dialog. For a simple message, it should be a String; it can
72    * also be an Object[] including Strings and Components to display in the body of the dialog.
73    * @param checkBoxText The text to display with the checkbox
74    * @param messageType The JOptionPane message type
75    * @param optionType The JOptionPane option type
76    */

77   public ConfirmCheckBoxDialog(JFrame parent, String JavaDoc title, Object JavaDoc message, String JavaDoc checkBoxText, int messageType,
78                                int optionType) {
79     _optionPane = new JOptionPane(message, messageType, optionType);
80     JPanel checkBoxPane = new JPanel();
81     checkBoxPane.add(_initCheckBox(checkBoxText));
82     _optionPane.add(checkBoxPane, 1);
83     _dialog = _optionPane.createDialog(parent, title);
84   }
85
86   /** Initializes the JCheckBox to have the given text. */
87   private JCheckBox _initCheckBox(String JavaDoc text) {
88     _checkBox = new JCheckBox(text);
89     return _checkBox;
90   }
91
92   /** Shows the dialog.
93    * @return the JOptionPane result of showing the dialog.
94    */

95   public int show() {
96     MainFrame.setPopupLoc(_dialog, _dialog.getOwner());
97     _dialog.setVisible(true);
98
99     Object JavaDoc val = _optionPane.getValue();
100     if (val == null || !(val instanceof Integer JavaDoc)) {
101       return JOptionPane.CLOSED_OPTION;
102     }
103     return ((Integer JavaDoc)val).intValue();
104   }
105
106   /**
107    * Gets the selected value of the check box. Should not be called until
108    * the dialog is shown.
109    * @return the value of the checkbox
110    */

111   public boolean getCheckBoxValue() {
112     return _checkBox.isSelected();
113   }
114 }
Popular Tags