Print this page
4719 Common patchset for jdk1.7 support preparation
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/krb5/kadmin/gui/dchanger/DurationHelper.java
+++ new/usr/src/cmd/krb5/kadmin/gui/dchanger/DurationHelper.java
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22 /*
23 23 * ident "%Z%%M% %I% %E% SMI"
24 24 *
25 25 * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
26 26 * All rights reserved.
27 27 */
28 28
29 29 import java.awt.*;
30 30 import java.awt.event.*;
31 31 import java.text.NumberFormat;
32 32 import java.util.ResourceBundle;
33 33 import java.util.MissingResourceException;
34 34
35 35 /**
36 36 * This creates a modal dialog box that lets the user enter a duration of
37 37 * time in seconds/minutes/hours/days/weeks/months/years.
38 38 */
39 39 public class DurationHelper extends Dialog {
40 40
41 41 private boolean save;
42 42
43 43 private Frame parent;
44 44
45 45 private Choice unit;
46 46 private TextField value;
47 47 private Label total;
48 48
49 49 private Button ok;
50 50 private Button cancel;
51 51 private Button help;
52 52 private Button compute;
53 53
54 54 private HelpDialog hd = null;
55 55
56 56 // For I18N
57 57 private static ResourceBundle rb =
58 58 ResourceBundle.getBundle("GuiResource" /* NOI18N */);
59 59 private static ResourceBundle hrb =
60 60 ResourceBundle.getBundle("HelpData" /* NOI18N */);
61 61
62 62 private static String[] units = { getString("Seconds"),
63 63 getString("Minutes"),
64 64 getString("Hours"),
65 65 getString("Days"),
66 66 getString("Weeks"),
67 67 getString("Months"),
68 68 getString("Years") };
69 69 private static int[] unitMultipliers = {1, 60, 60*60, 60*60*24,
70 70 60*60*24*7, 60*60*24*30,
71 71 60*60*24*365 };
72 72 private static NumberFormat nf = NumberFormat.getInstance();
73 73 private static Toolkit toolkit = Toolkit.getDefaultToolkit();
74 74
75 75 /**
76 76 * Constructor for DurationHelper.
77 77 * @param parent the parent Frame to whom input will be blocked
78 78 * while this dialog box is begin shown(modal behaviour).
79 79 */
80 80 public DurationHelper(Frame parent, Color background, Color foreground) {
81 81 super(parent, getString("SEAM Duration Helper"), true);
82 82
83 83 this.parent = parent;
84 84
85 85 setLayout(new GridBagLayout());
86 86 addLabels();
87 87 addFields(background, foreground);
88 88 addButtons();
89 89 setSize(350, 150);
90 90 setResizable(false);
91 91 addWindowListener(new DHWindowListener());
92 92 }
93 93
94 94 /**
95 95 * Adds all the labels.
96 96 */
97 97 private void addLabels() {
98 98 GridBagConstraints gbc = new GridBagConstraints();
99 99 gbc.weightx = gbc.weighty = 1;
100 100 add(new Label(getString("Unit")), gbc);
101 101 add(new Label(getString("Value")), gbc);
102 102
103 103 gbc.gridx = 3;
104 104 gbc.gridy = 0;
105 105 add(new Label(getString("Seconds")), gbc);
106 106 }
107 107
108 108 /**
109 109 * Initializes the strings for the units.
110 110 */
111 111 private void initUnits() {
112 112 unit = new Choice();
113 113 for (int i = 0; i < units.length; i++)
114 114 unit.add(units[i]);
115 115 unit.select(getString("Hours"));
116 116 unit.addItemListener(new ItemListener() {
117 117 public void itemStateChanged(ItemEvent e) {
118 118 DurationHelper.this.checkErrorAndSetTotal();
119 119 }
120 120 });
121 121 }
122 122
123 123 /**
124 124 * Adds all the fields
125 125 */
126 126 private void addFields(Color background, Color foreground) {
127 127 GridBagConstraints gbc = new GridBagConstraints();
128 128 gbc.weightx = gbc.weighty = 1;
129 129 initUnits();
130 130 value = new TextField();
131 131 value.setBackground(background);
132 132 value.setForeground(foreground);
133 133 value.setColumns(10);
134 134
135 135 // TBD: make total large enough to hold the largest int
136 136 total = new Label(" " /* NO18N */,
137 137 Label.RIGHT);
138 138 gbc.gridx = 0;
139 139 gbc.gridy = 1;
140 140 add(unit, gbc);
141 141 gbc.gridx = 1;
142 142 add(value, gbc);
143 143 gbc.gridx = 3;
144 144 add(total, gbc);
145 145
146 146 value.addActionListener(new ActionListener() {
147 147 public void actionPerformed(ActionEvent e) {
148 148 DurationHelper.this.durationHelperClose(true);
149 149 }
150 150 });
151 151 }
152 152
153 153 /**
154 154 * Adds all the buttons.
155 155 */
156 156 private void addButtons() {
157 157
158 158 GridBagConstraints gbc = new GridBagConstraints();
159 159 gbc.weightx = gbc.weighty = 1;
160 160
161 161 gbc.gridwidth = GridBagConstraints.REMAINDER;
162 162 gbc.fill = GridBagConstraints.BOTH;
163 163 gbc.gridx = 0;
164 164 gbc.gridy = 2;
165 165 gbc.insets = new Insets(0, 10, 0, 10);
166 166 add(new LineSeparator(), gbc);
167 167 gbc.insets = new Insets(0, 0, 0, 0);
168 168
169 169 Panel p = new Panel();
170 170 p.setLayout(new GridBagLayout());
171 171 ok = new Button(getString("OK"));
172 172 cancel = new Button(getString("Cancel"));
173 173 help = new Button(getString("Help"));
174 174 gbc = new GridBagConstraints();
175 175 gbc.weightx = gbc.weighty = 1;
176 176 p.add(ok, gbc);
177 177 p.add(cancel, gbc);
178 178 p.add(help, gbc);
179 179
180 180 ActionListener bl = new ButtonListener();
181 181 ok.addActionListener(bl);
182 182 cancel.addActionListener(bl);
183 183 help.addActionListener(bl);
184 184
185 185 gbc.gridy = 3;
186 186 gbc.gridwidth = GridBagConstraints.REMAINDER;
187 187 gbc.fill = GridBagConstraints.HORIZONTAL;
188 188 add(p, gbc);
189 189
190 190 gbc = new GridBagConstraints();
191 191 gbc.gridx = 2;
192 192 gbc.gridy = 1;
193 193 compute = new Button(getString("="));
194 194 add(compute, gbc);
195 195 compute.addActionListener(bl);
196 196
197 197 }
198 198
199 199 /**
200 200 * Updates the label called total.
201 201 * @return false if the text entry in the value
202 202 * field is not parseable, true otherwise.
203 203 */
204 204 private boolean checkErrorAndSetTotal() {
205 205 try {
206 206 String noSpaces = value.getText().trim();
207 207 value.setText(noSpaces);
208 208 Long l = Long.valueOf(noSpaces);
209 209 total.setText(nf.format(l.longValue() *
210 210 unitMultipliers[unit.getSelectedIndex()]));
211 211 } catch (NumberFormatException e) {
212 212 value.requestFocus();
213 213 value.selectAll();
214 214 toolkit.beep();
215 215 return false;
216 216 }
217 217
218 218 return true;
219 219 }
220 220
221 221 /**
222 222 * Hides the duration helper.
223 223 * @param save true if the user wants to save the current value in
224 224 * the dialog box, false if it is to be discarded. This is decided
225 225 * based on whether the user clicked on the "Ok" button or the
226 226 * "Cancel" button. Choosing the window close menu is equivalent to
227 227 * clicking on "Cancel."
228 228 */
229 229 private void durationHelperClose(boolean save) {
230 230 if (save == true) {
231 231 if (!checkErrorAndSetTotal())
232 232 return;
233 233 }
234 234 this.save = save;
235 235 setVisible(false);
236 236 }
237 237
238 238 /**
239 239 * Determine whether or not the user wanted to save the value in
240 240 * this Dialog box. The user indicates this by clicking on the Ok
241 241 * button to save it and on the Cancel button to discard it. Using the
242 242 * window close menu responds the same way as cancel.
243 243 * @return true if the user wanted to use this value,
244 244 * false if it is to be discarded.
245 245 */
246 246 public boolean isSaved() {
247 247 return save;
248 248 }
249 249
250 250 /**
251 251 * The string representation of the contents of this dialog box.
252 252 * @return a String with the total number of seconds entered.
253 253 */
254 254 public String toString() {
255 255 return total.getText();
256 256 }
257 257
258 258 // * **********************************************
259 259 // I N N E R C L A S S E S F O L L O W
260 260 // * **********************************************
261 261
262 262 /**
263 263 * Listener for closing the dialog box through the window close
264 264 * menu.
265 265 */
266 266 private class DHWindowListener extends WindowAdapter {
267 267 public void windowClosing(WindowEvent e) {
268 268 durationHelperClose(false);
269 269 }
270 270 }
271 271
272 272 /**
273 273 * Listener for all the buttons.
274 274 * The listener is shared for the sake
↓ open down ↓ |
274 lines elided |
↑ open up ↑ |
275 275 * of reducing the number of overall listeners.
276 276 */
277 277 private class ButtonListener implements ActionListener {
278 278 public void actionPerformed(ActionEvent e) {
279 279 if (e.getSource() == ok) {
280 280 DurationHelper.this.durationHelperClose(true);
281 281 } else if (e.getSource() == cancel) {
282 282 DurationHelper.this.durationHelperClose(false);
283 283 } else if (e.getSource() == help) {
284 284 if (hd != null)
285 - hd.show();
285 + hd.setVisible(true);
286 286 else {
287 287 hd = new HelpDialog(DurationHelper. this.parent,
288 288 getString("Help for entering time duration"),
289 289 false, 5, 45);
290 290 hd.setVisible(true);
291 291 hd.setText(getString(hrb, "DurationHelperHelp"));
292 292 }
293 293 } else if (e.getSource() == compute) {
294 294 checkErrorAndSetTotal();
295 295 }
296 296 }
297 297 }
298 298
299 299 /**
300 300 * Call rb.getString(), but catch exception
301 301 * and return English
302 302 * key so that small spelling errors don't cripple the GUI
303 303 *
304 304 */
305 305 private static final String getString(String key) {
306 306 return (getString(rb, key));
307 307 }
308 308
309 309 private static final String getString(ResourceBundle rb, String key) {
310 310 try {
311 311 String res = rb.getString(key);
312 312 return res;
313 313 } catch (MissingResourceException e) {
314 314 System.out.println("Missing resource "+key+", using English.");
315 315 return key;
316 316 }
317 317 }
318 318
319 319 /*
320 320 * A main method to test this class.
321 321 */
322 322 /*
323 323 public static void main(String args[]) {
324 324 Frame f = new Frame("Test DurationHelper");
325 325 f.setVisible(true); // for help dialog to use this as parent
326 326 DurationHelper dh = new DurationHelper(f, Color.white, Color.black);
327 327 dh.setVisible(true);
328 328 System.out.println("Save is " + dh.save);
329 329 }
330 330 */
331 331 }
↓ open down ↓ |
36 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX