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