summaryrefslogtreecommitdiff
path: root/usr/src/cmd/krb5/kadmin/gui/dchanger/DurationHelper.java
blob: 45d3da80f6072fea442b26d049ae04eba1d00fda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * ident	"%Z%%M%	%I%	%E% SMI"
 *
 * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
 * All rights reserved.
 */

    import java.awt.*;
    import java.awt.event.*;
    import java.text.NumberFormat;
    import java.util.ResourceBundle;
    import java.util.MissingResourceException;

    /**
     * This creates a modal dialog box that lets the user enter a duration of
     * time in seconds/minutes/hours/days/weeks/months/years.
     */
    public class DurationHelper extends Dialog {

	private boolean save;

	private Frame parent;

	private Choice unit;
	private TextField value;
	private Label  total;

	private Button ok;
	private Button cancel;
	private Button help;
	private Button compute;

        private HelpDialog hd = null;

	// For I18N
	    private static ResourceBundle rb =
	    ResourceBundle.getBundle("GuiResource" /* NOI18N */);
            private static ResourceBundle hrb =
	    ResourceBundle.getBundle("HelpData" /* NOI18N */);

	private static String[] units = { getString("Seconds"),
	            			getString("Minutes"),
		    			getString("Hours"),
		    			getString("Days"),
		    			getString("Weeks"),
		    			getString("Months"),
		    			getString("Years")	};
	private static int[] unitMultipliers = {1, 60, 60*60, 60*60*24,
                                                60*60*24*7, 60*60*24*30,
                                                60*60*24*365	};
        private static NumberFormat nf = NumberFormat.getInstance();
	private static Toolkit toolkit = Toolkit.getDefaultToolkit();

        /**
         * Constructor for DurationHelper.
         * @param parent the parent Frame to whom input will be blocked
         * while this dialog box is begin shown(modal behaviour).
         */
    public DurationHelper(Frame parent,  Color background, Color foreground) {
		super(parent, getString("SEAM Duration Helper"), true);

		this.parent = parent;

		setLayout(new GridBagLayout());
		addLabels();
		addFields(background, foreground);
		addButtons();
		setSize(350, 150);
		setResizable(false);
		addWindowListener(new DHWindowListener());
    }

    /**
     * Adds all the labels.
     */
    private void addLabels() {
        GridBagConstraints gbc = new GridBagConstraints();
	gbc.weightx = gbc.weighty = 1;
	add(new Label(getString("Unit")), gbc);
	add(new Label(getString("Value")), gbc);

	gbc.gridx = 3;
	gbc.gridy = 0;
	add(new Label(getString("Seconds")), gbc);
    }

    /**
     * Initializes the strings for the units.
     */
    private void initUnits() {
        unit = new Choice();
	for (int i = 0; i < units.length; i++)
	    unit.add(units[i]);
	unit.select(getString("Hours"));
	unit.addItemListener(new ItemListener() {
		public void itemStateChanged(ItemEvent e) {
			DurationHelper.this.checkErrorAndSetTotal();
		}
	});
    }

    /**
     * Adds all the fields
     */
    private void addFields(Color background, Color foreground) {
        GridBagConstraints gbc = new GridBagConstraints();
	gbc.weightx =  gbc.weighty = 1;
	initUnits();
	value = new TextField();
	value.setBackground(background);
	value.setForeground(foreground);
	value.setColumns(10);

        // TBD: make total large enough to hold the largest int
        total = new Label("             " /* NO18N */,
                            Label.RIGHT);
	gbc.gridx = 0;
	gbc.gridy = 1;
	add(unit, gbc);
	gbc.gridx = 1;
	add(value, gbc);
	gbc.gridx = 3;
	add(total, gbc);

	value.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			DurationHelper.this.durationHelperClose(true);
		}
	});
    }

    /**
     * Adds all the buttons.
     */
    private void addButtons() {

        GridBagConstraints gbc = new GridBagConstraints();
	gbc.weightx =  gbc.weighty = 1;

	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gbc.fill = GridBagConstraints.BOTH;
	gbc.gridx = 0;
	gbc.gridy = 2;
	gbc.insets = new Insets(0, 10, 0, 10);
	add(new LineSeparator(), gbc);
	gbc.insets = new Insets(0, 0, 0, 0);

	Panel p = new Panel();
	p.setLayout(new GridBagLayout());
	ok = new Button(getString("OK"));
	cancel =  new Button(getString("Cancel"));
	help = new Button(getString("Help"));
	gbc = new GridBagConstraints();
        gbc.weightx =  gbc.weighty = 1;
	p.add(ok, gbc);
	p.add(cancel, gbc);
	p.add(help, gbc);

	ActionListener bl = new ButtonListener();
	ok.addActionListener(bl);
	cancel.addActionListener(bl);
	help.addActionListener(bl);

	gbc.gridy = 3;
	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gbc.fill = GridBagConstraints.HORIZONTAL;
	add(p, gbc);

	gbc = new GridBagConstraints();
	gbc.gridx = 2;
	gbc.gridy = 1;
	compute = new Button(getString("="));
	add(compute, gbc);
	compute.addActionListener(bl);

    }

    /**
     * Updates the label called total.
     * @return false if the text entry in the value
     * field is not parseable, true otherwise.
     */
    private boolean checkErrorAndSetTotal() {
        try {
            String noSpaces = value.getText().trim();
	    value.setText(noSpaces);
	    Long l = Long.valueOf(noSpaces);
	    total.setText(nf.format(l.longValue() *
                unitMultipliers[unit.getSelectedIndex()]));
        } catch (NumberFormatException e) {
          value.requestFocus();
          value.selectAll();
          toolkit.beep();
	  return false;
        }

	return true;
    }

    /**
     * Hides the duration helper.
     * @param save true if the user wants to save the current value in
     * the dialog box, false if it is to be discarded. This is decided
     * based on whether the user clicked on the "Ok" button or the
     * "Cancel" button. Choosing the window close menu is equivalent to
     *  clicking on "Cancel."
     */
    private void durationHelperClose(boolean save) {
        if (save == true) {
	    if (!checkErrorAndSetTotal())
		return;
	}
	this.save = save;
	setVisible(false);
    }

    /**
     * Determine whether or not the user wanted to save the value in
     * this Dialog box. The user indicates this by clicking on the Ok
     * button to save it and on the Cancel button to discard it. Using the
     * window close menu responds the same way as cancel. 
     * @return true if the user wanted to use this value,
     * false if it is to be discarded.
     */
    public boolean isSaved() {
	return save;
    }

    /**
     * The string representation of the contents of this dialog box.
     * @return a String with the total number of seconds entered.
     */
    public String toString() {
	return total.getText();
    }

    // * **********************************************
    // 	 I N N E R    C L A S S E S   F O L L O W
    // * **********************************************

    /**
     * Listener for closing the dialog box through the window close
     * menu.
     */
    private class DHWindowListener extends WindowAdapter {
	public  void windowClosing(WindowEvent e) {
		durationHelperClose(false);
	}
    }

    /**
     * Listener for all the buttons.
     * The listener is shared for the sake
     * of reducing the number of overall listeners.
     */
    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == ok) {
                DurationHelper.this.durationHelperClose(true);
	    } else if (e.getSource() == cancel) {
                DurationHelper.this.durationHelperClose(false);
	    } else if (e.getSource() == help) {
                if (hd != null)
                    hd.show();
                else {
                    hd = new HelpDialog(DurationHelper. this.parent,
                        getString("Help for entering time duration"),
                                    false, 5, 45);
                    hd.setVisible(true);
                    hd.setText(getString(hrb, "DurationHelperHelp"));
                }
            } else if (e.getSource() == compute) {
                checkErrorAndSetTotal();
            }
        }
    }

    /**
     * Call rb.getString(), but catch exception
     * and return English
     * key so that small spelling errors don't cripple the GUI
     *
     */
    private static final String getString(String key) {
	return (getString(rb, key));
    }

    private static final String getString(ResourceBundle rb, String key) {
        try {
	    String res = rb.getString(key);
            return res;
	} catch (MissingResourceException e) {
		System.out.println("Missing resource "+key+", using English.");
		return key;
	}
    }

    /*
     * A main method to test this class.
     */
    /*
    public static void main(String args[]) {
        Frame f = new Frame("Test DurationHelper");
        f.setVisible(true); // for help dialog to use this as parent
        DurationHelper dh = new DurationHelper(f, Color.white, Color.black);
        dh.setVisible(true);
        System.out.println("Save is " + dh.save);
    }
	  */
}