/Users/lyon/j4p/src/gui/dialogs/ModalJDialog.java
|
1 package gui.dialogs;
2
3 import gui.run.RunButton;
4
5 import javax.swing.*;
6 import java.awt.*;
7 import java.awt.event.WindowAdapter;
8 import java.awt.event.WindowEvent;
9
10 /**
11 * This class implements a standard data entry
12 * dialog with "Ok" and "Cancel" buttons.
13 * Subclasses can override the isDataValid(),
14 * okButtonPressed(), and cancelButtonPressed()
15 * methods to perform implementation specific
16 * processing. <P> By default, the dialog is
17 * modal, and has a JPanel with a BorderLayout for
18 * its content pane.
19 *
20 * @author David Fraser
21 * @author Michael Harris
22 */
23 public class ModalJDialog extends JDialog {
24 // Constants
25
26 /**
27 * The spacing between components in pixels
28 */
29 private static final int COMPONENT_SPACING = 10;
30
31 // Attributes
32
33 /**
34 * Flag indicating if the "Cancel" button was
35 * pressed to close dialog
36 */
37 private boolean myIsDialogCancelled = true;
38
39 /**
40 * The content pane for holding user
41 * components
42 */
43 private Container myUserContentPane;
44
45 // Methods
46
47 /**
48 * This method is the default constructor.
49 */
50 public ModalJDialog() {
51 init();
52 }
53
54 /**
55 * This method creates a StandardDialog with
56 * the given parent frame and title.
57 *
58 * @param parent The parent frame for the
59 * dialog.
60 * @param title The title to display in the
61 * dialog.
62 */
63 public ModalJDialog(Frame parent,
64 String title) {
65 super(parent, title);
66
67 init();
68 }
69
70 /**
71 * This method creates a StandardDialog with
72 * the given parent dialog and title.
73 *
74 * @param parent The parent dialog for the
75 * dialog.
76 * @param title The title to display in the
77 * dialog.
78 */
79 public ModalJDialog(Dialog parent,
80 String title) {
81 super(parent, title);
82
83 init();
84 }
85
86 /**
87 * This method sets up the default attributes
88 * of the dialog and the content pane.
89 */
90 private void init() {
91 setModal(true);
92 setDefaultCloseOperation(
93 DO_NOTHING_ON_CLOSE);
94
95 // Setup the internal content pane to
96 // hold the user content pane
97 // and the standard button panel
98
99
100 JPanel internalContentPane = new JPanel();
101
102 internalContentPane.setLayout(new BorderLayout(
103 COMPONENT_SPACING,
104 COMPONENT_SPACING));
105
106 internalContentPane.setBorder(BorderFactory.createEmptyBorder(
107 COMPONENT_SPACING,
108 COMPONENT_SPACING,
109 COMPONENT_SPACING,
110 COMPONENT_SPACING));
111
112 // Create the standard button panel with "Ok" and "Cancel"
113
114
115
116 JPanel buttonPanel = new JPanel();
117
118 buttonPanel.setBorder(BorderFactory.createEmptyBorder(
119 0, 0, 0, 0));
120
121 buttonPanel.add(new RunButton("ok") {
122 public void run() {
123 okPressed();
124 }
125 });
126
127 internalContentPane.add(buttonPanel,
128 BorderLayout.SOUTH);
129
130 // Initialise the user content pane with a JPanel
131
132 setContentPane( new JPanel(new BorderLayout()));
133
134 super.setContentPane(internalContentPane);
135
136 // Finally, add a listener for the window close button.
137 // Process this event the same as the "Cancel" button.
138
139 WindowAdapter windowAdapter = new WindowAdapter() {
140 public void windowClosing(
141 WindowEvent windowEvent) {
142 myIsDialogCancelled = true;
143 dispose();
144 }
145 };
146
147 addWindowListener(windowAdapter);
148 }
149
150 public void okPressed() {
151 setVisible(false);
152 dispose();
153 }
154
155 /**
156 * This method gets the content pane for
157 * adding components. Components should not be
158 * added directly to the dialog.
159 *
160 * @return the content pane for the dialog.
161 */
162 public Container getContentPane() {
163 return myUserContentPane;
164 }
165
166 /**
167 * This method sets the content pane for
168 * adding components. Components should not be
169 * added directly to the dialog.
170 *
171 * @param contentPane The content pane for the
172 * dialog.
173 */
174 public void setContentPane(
175 Container contentPane) {
176 myUserContentPane = contentPane;
177
178 super.getContentPane().add(
179 myUserContentPane,
180 BorderLayout.CENTER);
181 }
182
183 /**
184 * This method returns <code>true</code> if
185 * the User cancelled the dialog otherwise
186 * <code>false</code>. The dialog is cancelled
187 * if the "Cancel" button is pressed or the
188 * "Close" window button is pressed, or the
189 * "Escape" key is pressed. In other words, if
190 * the User has caused the dialog to close by
191 * any method other than by pressing the "Ok"
192 * button, this method will return
193 * <code>true</code>.
194 */
195 public boolean hasUserCancelled() {
196 return myIsDialogCancelled;
197 }
198
199 /**
200 * This method is used to validate the current
201 * dialog box. This method provides a default
202 * response of <code>true</code>. This method
203 * should be implemented by each dialog that
204 * extends this class.
205 *
206 * @return a boolean indicating if the data is
207 * valid. <code>true</code> indicates
208 * that all of the fields were
209 * validated correctly and <code>false</code>
210 * indicates the validation failed
211 */
212 protected boolean isValidData() {
213 return true;
214 }
215 }
216