File Source: StandardDialog.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.StandardDialog__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.ui_swing.components;
24
25 import com.dmdirc.ui.CoreUIUtils;
26
27 import java.awt.Dialog;
28 import java.awt.Dimension;
29 import java.awt.Frame;
30 import java.awt.Window;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.awt.event.InputEvent;
34 import java.awt.event.KeyEvent;
35
36 import javax.swing.JButton;
37 import javax.swing.JComponent;
38 import javax.swing.JDialog;
39 import javax.swing.JRootPane;
40 import javax.swing.KeyStroke;
41
42 /**
43 * Provides common methods for dialogs.
44 */
/*
P/P * Method: JButton access$100(StandardDialog)
*
* Preconditions:
* x0 != null
* init'ed(x0.okButton)
*
* Postconditions:
* return_value == x0.okButton
* init'ed(return_value)
*/
45 public class StandardDialog extends JDialog {
46
47 /**
48 * A version number for this class. It should be changed whenever the class
49 * structure is changed (or anything else that would prevent serialized
50 * objects being unserialized with the new class).
51 */
52 private static final long serialVersionUID = 1;
53 /**
54 * The OK button for this frame.
55 */
56 private JButton okButton;
57 /**
58 * The cancel button for this frame.
59 */
60 private JButton cancelButton;
61
62 /**
63 * Creates a new instance of StandardDialog.
64 * @param owner The frame that owns this dialog
65 * @param modal Whether to display modally or not
66 */
67 public StandardDialog(final Frame owner, final boolean modal) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.StandardDialog(Frame, bool)
*
* Test Vectors:
* owner: Addr_Set{null}, Inverse{null}
*/
68 super(owner, modal);
69
70 if (owner != null) {
71 setIconImages(owner.getIconImages());
72 }
73 }
74
75 /**
76 * Creates a new instance of StandardDialog.
77 * @param owner The frame that owns this dialog
78 * @param modal Whether to display modally or not
79 */
80 public StandardDialog(final Window owner, final ModalityType modal) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.StandardDialog(Window, Dialog$ModalityType)
*
* Test Vectors:
* owner: Addr_Set{null}, Inverse{null}
*/
81 super(owner, modal);
82
83 if (owner != null) {
84 setIconImages(owner.getIconImages());
85 }
86 }
87
88 /**
89 * Creates a new instance of StandardDialog.
90 * @param owner The frame that owns this dialog
91 * @param modal Whether to display modally or not
92 */
93 public StandardDialog(final Dialog owner, final boolean modal) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.StandardDialog(Dialog, bool)
*
* Test Vectors:
* owner: Addr_Set{null}, Inverse{null}
*/
94 super(owner, modal);
95
96 if (owner != null) {
97 setIconImages(owner.getIconImages());
98 }
99 }
100
101 /**
102 * Centers and displays this dialog.
103 */
104 public void display() {
/*
P/P * Method: void display()
*/
105 CoreUIUtils.centreWindow(this);
106 setVisible(true);
107 }
108
109 /**
110 * Sets the specified button up as the OK button.
111 * @param button The target button
112 */
113 protected void setOkButton(final JButton button) {
/*
P/P * Method: void setOkButton(JButton)
*
* Preconditions:
* button != null
*
* Postconditions:
* this.okButton == button
* this.okButton != null
*/
114 okButton = button;
115 button.setText("OK");
116 button.setDefaultCapable(false);
117 }
118
119 /**
120 * Sets the specified button up as the Cancel button.
121 * @param button The target button
122 */
123 protected void setCancelButton(final JButton button) {
/*
P/P * Method: void setCancelButton(JButton)
*
* Preconditions:
* button != null
*
* Postconditions:
* this.cancelButton == button
* this.cancelButton != null
*/
124 cancelButton = button;
125 button.setText("Cancel");
126 button.setDefaultCapable(false);
127 }
128
129 /**
130 * Gets the left hand button for a dialog.
131 * @return left JButton
132 */
133 protected final JButton getLeftButton() {
/*
P/P * Method: JButton getLeftButton()
*
* Preconditions:
* (soft) init'ed(this.cancelButton)
* (soft) init'ed(this.okButton)
*
* Presumptions:
* java.lang.System:getProperty(...)@134 != null
*
* Postconditions:
* return_value == One-of{this.okButton, this.cancelButton}
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:startsWith(...)@134: {0}, {1}
*/
134 if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
135 return getOkButton();
136 } else {
137 return getCancelButton();
138 }
139 }
140
141 /**
142 * Gets the right hand button for a dialog.
143 * @return right JButton
144 */
145 protected final JButton getRightButton() {
/*
P/P * Method: JButton getRightButton()
*
* Preconditions:
* (soft) init'ed(this.cancelButton)
* (soft) init'ed(this.okButton)
*
* Presumptions:
* java.lang.System:getProperty(...)@146 != null
*
* Postconditions:
* return_value == One-of{this.cancelButton, this.okButton}
* init'ed(return_value)
*
* Test Vectors:
* java.lang.String:startsWith(...)@146: {0}, {1}
*/
146 if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
147 return getCancelButton();
148 } else {
149 return getOkButton();
150 }
151 }
152
153 /**
154 * Orders the OK and Cancel buttons in an appropriate order for the current
155 * operating system.
156 * @param leftButton The left-most button
157 * @param rightButton The right-most button
158 */
159 protected final void orderButtons(final JButton leftButton,
160 final JButton rightButton) {
/*
P/P * Method: void orderButtons(JButton, JButton)
*
* Preconditions:
* leftButton != null
* rightButton != null
*
* Presumptions:
* java.lang.System:getProperty(...)@161 != null
*
* Postconditions:
* this.cancelButton == One-of{rightButton, leftButton}
* this.cancelButton != null
* this.okButton == One-of{leftButton, rightButton}
* this.okButton != null
*
* Test Vectors:
* java.lang.String:startsWith(...)@161: {0}, {1}
*/
161 if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
162 // Windows - put the OK button on the left
163 setOkButton(leftButton);
164 setCancelButton(rightButton);
165 } else {
166 // Everything else - adhere to usability guidelines and put it on
167 // the right.
168 setOkButton(rightButton);
169 setCancelButton(leftButton);
170 }
171 leftButton.setPreferredSize(new Dimension(100, 25));
172 rightButton.setPreferredSize(new Dimension(100, 25));
173 leftButton.setMinimumSize(new Dimension(100, 25));
174 rightButton.setMinimumSize(new Dimension(100, 25));
175 }
176
177 /**
178 * Creates the root pane of this dialog. We hook in two keylisteners
179 * to send enter/escape events to our buttons.
180 * @return The new root pane
181 */
182 @Override
183 protected final JRootPane createRootPane() {
/*
P/P * Method: JRootPane createRootPane()
*
* Postconditions:
* return_value == &new JRootPane(createRootPane#3)
* new JRootPane(createRootPane#3) num objects == 1
*/
184 final ActionListener escapeListener = new ActionListener() {
185
186 /** {@inheritDoc} */
187 @Override
188 public void actionPerformed(final ActionEvent actionEvent) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* init'ed(this.cancelButton)
*/
189 executeAction(StandardDialog.this.cancelButton);
190 }
191 };
192
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.StandardDialog$2(StandardDialog)
*/
193 final ActionListener enterListener = new ActionListener() {
194
195 /** {@inheritDoc} */
196 @Override
197 public void actionPerformed(final ActionEvent actionEvent) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* (soft) init'ed(this.okButton)
*/
198 if (StandardDialog.this.getFocusOwner() instanceof JButton) {
199 executeAction((JButton) StandardDialog.this.getFocusOwner());
200 } else {
201 executeAction(StandardDialog.this.okButton);
202 }
203 }
204 };
205
206 final KeyStroke escape = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
207 final KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,
208 InputEvent.CTRL_DOWN_MASK);
209
210 final JRootPane customRootPane = new JRootPane();
211
212 customRootPane.registerKeyboardAction(escapeListener, escape,
213 JComponent.WHEN_IN_FOCUSED_WINDOW);
214 customRootPane.registerKeyboardAction(enterListener, enter,
215 JComponent.WHEN_IN_FOCUSED_WINDOW);
216
217 return customRootPane;
218 }
219
220 /**
221 * Retrieves the OK button for this form.
222 * @return The form's OK button
223 */
224 public final JButton getOkButton() {
/*
P/P * Method: JButton getOkButton()
*
* Preconditions:
* init'ed(this.okButton)
*
* Postconditions:
* return_value == this.okButton
* init'ed(return_value)
*/
225 return okButton;
226 }
227
228 /**
229 * Retrieves the Cancel button for this form.
230 * @return The form's cancel button
231 */
232 public final JButton getCancelButton() {
/*
P/P * Method: JButton getCancelButton()
*
* Preconditions:
* init'ed(this.cancelButton)
*
* Postconditions:
* return_value == this.cancelButton
* init'ed(return_value)
*/
233 return cancelButton;
234 }
235
236 /**
237 * Simulates the user clicking on the specified target button.
238 * @param target The button to use
239 */
240 protected final void executeAction(final JButton target) {
/*
P/P * Method: void executeAction(JButton)
*
* Test Vectors:
* target: Addr_Set{null}, Inverse{null}
*/
241 if (target != null) {
242 target.doClick();
243 }
244 }
245 }
SofCheck Inspector Build Version : 2.17854
| StandardDialog.java |
2009-Jun-25 01:54:24 |
| StandardDialog.class |
2009-Sep-02 17:04:14 |
| StandardDialog$1.class |
2009-Sep-02 17:04:14 |
| StandardDialog$2.class |
2009-Sep-02 17:04:14 |