1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * ident	"%Z%%M%	%I%	%E% SMI"
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate  * All rights reserved.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate import java.awt.*;
30*0Sstevel@tonic-gate import java.awt.event.*;
31*0Sstevel@tonic-gate import java.text.*;
32*0Sstevel@tonic-gate import java.util.*;
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /**
35*0Sstevel@tonic-gate  * This class creates a dialog box that helps the user enter date and
36*0Sstevel@tonic-gate  * time with mouse clicks.  The dialog box need only be created
37*0Sstevel@tonic-gate  * once. The Ok and Cancel buttons merely call setVisible with an
38*0Sstevel@tonic-gate  *  argument of false.
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate // The layout will consist of 3 panels: topPanel contains the
42*0Sstevel@tonic-gate // different labels and fields. middlePanel contains the buttons
43*0Sstevel@tonic-gate // midnight and now. bottomPanel contains the buttons ok, cancel and
44*0Sstevel@tonic-gate // help. The last two panels are separated by a LineSeparator.
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate public class DateTimeDialog extends Dialog {
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate     private boolean save;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate     private Frame parent;
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate     private DCPanel dateDCPanel;
53*0Sstevel@tonic-gate     private DCPanel yearDCPanel;
54*0Sstevel@tonic-gate     private DCPanel hourDCPanel;
55*0Sstevel@tonic-gate     private DCPanel minuteDCPanel;
56*0Sstevel@tonic-gate     private DCPanel secondDCPanel;
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate     private Choice month;
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate     private DCCircularTextField date;
61*0Sstevel@tonic-gate     private DCCircularTextField hour;
62*0Sstevel@tonic-gate     private DCCircularTextField second;
63*0Sstevel@tonic-gate     private DCCircularTextField minute;
64*0Sstevel@tonic-gate     private DCTextField year;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate     private Button ok;
67*0Sstevel@tonic-gate     private Button cancel;
68*0Sstevel@tonic-gate     private Button help;
69*0Sstevel@tonic-gate     private Button now;
70*0Sstevel@tonic-gate     private Button midnight;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate     private HelpDialog hd = null;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate     private Panel topPanel;
75*0Sstevel@tonic-gate     private Panel middlePanel;
76*0Sstevel@tonic-gate     private Panel bottomPanel;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate     private GregorianCalendar calendar = null;
79*0Sstevel@tonic-gate     private static int MONTH_LEN[] = {31, 28, 31, 30, 31, 30, 31,
80*0Sstevel@tonic-gate                                       31, 30, 31, 30, 31};
81*0Sstevel@tonic-gate     private static DateFormat df =
82*0Sstevel@tonic-gate     DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
83*0Sstevel@tonic-gate 				 DateFormat.MEDIUM);
84*0Sstevel@tonic-gate     private static Toolkit toolkit = Toolkit.getDefaultToolkit();
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate   // For I18N
87*0Sstevel@tonic-gate     private static ResourceBundle rb =
88*0Sstevel@tonic-gate     ResourceBundle.getBundle("GuiResource" /* NOI18N */);
89*0Sstevel@tonic-gate     private static ResourceBundle hrb =
90*0Sstevel@tonic-gate     ResourceBundle.getBundle("HelpData" /* NOI18N */);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate     /**
93*0Sstevel@tonic-gate      * Constructor that lays out the componeents and sets the different
94*0Sstevel@tonic-gate      * event handlers.
95*0Sstevel@tonic-gate      */
DateTimeDialog(Frame parent, Color background, Color foreground)96*0Sstevel@tonic-gate     public DateTimeDialog(Frame parent, Color background, Color foreground) {
97*0Sstevel@tonic-gate     super(parent, getString("SEAM Date/Time Helper"), true);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate     this.parent = parent;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate     setLayout(new GridBagLayout());
102*0Sstevel@tonic-gate     addLabels();
103*0Sstevel@tonic-gate     addFields(background, foreground);
104*0Sstevel@tonic-gate     addDCPanels();
105*0Sstevel@tonic-gate     addButtons();
106*0Sstevel@tonic-gate     addFocusListeners();
107*0Sstevel@tonic-gate     setCurrentTime();
108*0Sstevel@tonic-gate     setSize(250, 300);
109*0Sstevel@tonic-gate     setResizable(false);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate     addWindowListener(new DCWindowListener());
112*0Sstevel@tonic-gate     //      initializeFocusOnTextField();
113*0Sstevel@tonic-gate     }
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate     /**
116*0Sstevel@tonic-gate      * Adds the labels only
117*0Sstevel@tonic-gate      */
addLabels()118*0Sstevel@tonic-gate     private void addLabels() {
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate     GridBagConstraints gbc = new GridBagConstraints();
121*0Sstevel@tonic-gate     gbc.weighty = 1;
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate     topPanel = new Panel();
124*0Sstevel@tonic-gate     topPanel.setLayout(new GridBagLayout());
125*0Sstevel@tonic-gate     gbc.gridwidth = GridBagConstraints.REMAINDER;
126*0Sstevel@tonic-gate     gbc.fill = GridBagConstraints.BOTH;
127*0Sstevel@tonic-gate     gbc.anchor = GridBagConstraints.CENTER;
128*0Sstevel@tonic-gate     gbc.gridx = 0;
129*0Sstevel@tonic-gate     gbc.gridy = 0;
130*0Sstevel@tonic-gate     add(topPanel, gbc);
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate     gbc.fill = GridBagConstraints.NONE;
133*0Sstevel@tonic-gate     gbc.anchor = GridBagConstraints.EAST;
134*0Sstevel@tonic-gate     gbc.gridx = 0;
135*0Sstevel@tonic-gate     gbc.gridwidth = 1;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate     gbc.gridy = 0;
138*0Sstevel@tonic-gate     topPanel.add(new Label(getString("Month")), gbc);
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate     gbc.gridy = 1;
141*0Sstevel@tonic-gate     topPanel.add(new Label(getString("Date")), gbc);
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate     gbc.gridy = 2;
144*0Sstevel@tonic-gate     topPanel.add(new Label(getString("Year")), gbc);
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate     gbc.gridy = 3;
147*0Sstevel@tonic-gate     topPanel.add(new Label(getString("Hour")), gbc);
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate     gbc.gridy = 4;
150*0Sstevel@tonic-gate     topPanel.add(new Label(getString("Minute")), gbc);
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate     gbc.gridy = 5;
153*0Sstevel@tonic-gate     topPanel.add(new Label(getString("Second")), gbc);
154*0Sstevel@tonic-gate     }
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate     /**
157*0Sstevel@tonic-gate      * Adds the fields that will store the month, year, date, hour,
158*0Sstevel@tonic-gate      * minute and second.
159*0Sstevel@tonic-gate      */
addFields(Color background, Color foreground)160*0Sstevel@tonic-gate     private void addFields(Color background, Color foreground) {
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate     GridBagConstraints gbc = new GridBagConstraints();
163*0Sstevel@tonic-gate     gbc.weighty = 1;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate     month = new Choice();
166*0Sstevel@tonic-gate     initializeMonth();
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate     date = new DCCircularTextField("1", 2);
169*0Sstevel@tonic-gate     date.setMinimum(1);
170*0Sstevel@tonic-gate     date.setBackground(background);
171*0Sstevel@tonic-gate     date.setForeground(foreground);
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate     hour = new DCCircularTextField("00", 2);
174*0Sstevel@tonic-gate     hour.setMaximum(23);
175*0Sstevel@tonic-gate     hour.setBackground(background);
176*0Sstevel@tonic-gate     hour.setForeground(foreground);
177*0Sstevel@tonic-gate     minute = new DCCircularTextField("00", 2);
178*0Sstevel@tonic-gate     minute.setBackground(background);
179*0Sstevel@tonic-gate     minute.setForeground(foreground);
180*0Sstevel@tonic-gate     second = new DCCircularTextField("00", 2);
181*0Sstevel@tonic-gate     second.setBackground(background);
182*0Sstevel@tonic-gate     second.setForeground(foreground);
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate     year  = new DCTextField("2000", 4);
185*0Sstevel@tonic-gate     year.setBackground(background);
186*0Sstevel@tonic-gate     year.setForeground(foreground);
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate     Panel tempPanel = new Panel();
189*0Sstevel@tonic-gate     tempPanel.add(month);
190*0Sstevel@tonic-gate     gbc.gridwidth = GridBagConstraints.REMAINDER;
191*0Sstevel@tonic-gate     gbc.fill = GridBagConstraints.HORIZONTAL;
192*0Sstevel@tonic-gate     gbc.anchor = GridBagConstraints.WEST;
193*0Sstevel@tonic-gate     gbc.gridx = 1;
194*0Sstevel@tonic-gate     gbc.gridy = 0;
195*0Sstevel@tonic-gate     topPanel.add(tempPanel, gbc);
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate     // Remaining fields are in topPanel
199*0Sstevel@tonic-gate     gbc.gridwidth = 1;
200*0Sstevel@tonic-gate     gbc.fill = GridBagConstraints.NONE;
201*0Sstevel@tonic-gate     gbc.gridx = 1;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate     gbc.gridy = 1;
204*0Sstevel@tonic-gate     topPanel.add(date, gbc);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate     gbc.gridy = 2;
207*0Sstevel@tonic-gate     topPanel.add(year, gbc);
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate     gbc.gridy = 3;
210*0Sstevel@tonic-gate     topPanel.add(hour, gbc);
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate     gbc.gridy = 4;
213*0Sstevel@tonic-gate     topPanel.add(minute, gbc);
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate     gbc.gridy = 5;
216*0Sstevel@tonic-gate     topPanel.add(second, gbc);
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate     }
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate   // Adds the panels with the +/- buttons for each DCField
addDCPanels()221*0Sstevel@tonic-gate     private void addDCPanels() {
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate     GridBagConstraints gbc = new GridBagConstraints();
224*0Sstevel@tonic-gate     gbc.weighty = 1;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate     gbc.gridx = 2;
227*0Sstevel@tonic-gate     gbc.gridwidth = GridBagConstraints.REMAINDER;
228*0Sstevel@tonic-gate     gbc.gridheight = 1;
229*0Sstevel@tonic-gate     gbc.fill = GridBagConstraints.NONE;
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate     dateDCPanel = new DCPanel();
232*0Sstevel@tonic-gate     yearDCPanel = new DCPanel();
233*0Sstevel@tonic-gate     hourDCPanel = new DCPanel();
234*0Sstevel@tonic-gate     minuteDCPanel = new DCPanel();
235*0Sstevel@tonic-gate     secondDCPanel = new DCPanel();
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate     gbc.gridy = 1;
238*0Sstevel@tonic-gate     topPanel.add(dateDCPanel, gbc);
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate     gbc.gridy = GridBagConstraints.RELATIVE;
241*0Sstevel@tonic-gate     topPanel.add(yearDCPanel, gbc);
242*0Sstevel@tonic-gate     topPanel.add(hourDCPanel, gbc);
243*0Sstevel@tonic-gate     topPanel.add(minuteDCPanel, gbc);
244*0Sstevel@tonic-gate     topPanel.add(secondDCPanel, gbc);
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate     dateDCPanel.setListener(date);
247*0Sstevel@tonic-gate     yearDCPanel.setListener(year);
248*0Sstevel@tonic-gate     hourDCPanel.setListener(hour);
249*0Sstevel@tonic-gate     minuteDCPanel.setListener(minute);
250*0Sstevel@tonic-gate     secondDCPanel.setListener(second);
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate     }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate     /**
256*0Sstevel@tonic-gate      * Sets the strings in the month pull-down menu. Also adds a listener
257*0Sstevel@tonic-gate      * that will modify the maximum date allowed depending on the month.
258*0Sstevel@tonic-gate      */
initializeMonth()259*0Sstevel@tonic-gate     private void initializeMonth() {
260*0Sstevel@tonic-gate     DateFormatSymbols dfSymbols = new DateFormatSymbols();
261*0Sstevel@tonic-gate     String[] monthStrings = dfSymbols.getMonths();
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate         month.removeAll();
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate         for (int i = 0; i < monthStrings.length; i++) {
266*0Sstevel@tonic-gate 	month.add(monthStrings[i]);
267*0Sstevel@tonic-gate         }
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate         month.addItemListener(new DCMonthChangeListener());
270*0Sstevel@tonic-gate     }
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate   // Adds all the buttons
addButtons()273*0Sstevel@tonic-gate     private void addButtons() {
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate         GridBagConstraints gbc = new GridBagConstraints();
276*0Sstevel@tonic-gate         gbc.weighty = 1;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate         middlePanel = new Panel();
280*0Sstevel@tonic-gate         now  = new Button(getString("Now"));
281*0Sstevel@tonic-gate         midnight	= new Button(getString("Midnight"));
282*0Sstevel@tonic-gate         middlePanel.add(midnight);
283*0Sstevel@tonic-gate         middlePanel.add(now);
284*0Sstevel@tonic-gate         gbc.fill = GridBagConstraints.HORIZONTAL;
285*0Sstevel@tonic-gate         gbc.gridwidth = GridBagConstraints.REMAINDER;
286*0Sstevel@tonic-gate         gbc.gridx = 0;
287*0Sstevel@tonic-gate         gbc.gridy = 1;
288*0Sstevel@tonic-gate         add(middlePanel, gbc);
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate         gbc.gridwidth = GridBagConstraints.REMAINDER;
291*0Sstevel@tonic-gate         gbc.gridheight = 1;
292*0Sstevel@tonic-gate         gbc.fill = GridBagConstraints.BOTH;
293*0Sstevel@tonic-gate         gbc.gridx = 0;
294*0Sstevel@tonic-gate         gbc.gridy = 2;
295*0Sstevel@tonic-gate         add(new LineSeparator(), gbc);
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate         bottomPanel = new Panel();
298*0Sstevel@tonic-gate         ok = new Button(getString("OK"));
299*0Sstevel@tonic-gate         cancel =	new Button(getString("Cancel"));
300*0Sstevel@tonic-gate         help = new Button(getString("Help"));
301*0Sstevel@tonic-gate         bottomPanel.add(ok);
302*0Sstevel@tonic-gate         bottomPanel.add(cancel);
303*0Sstevel@tonic-gate         bottomPanel.add(help);
304*0Sstevel@tonic-gate         gbc.fill = GridBagConstraints.HORIZONTAL;
305*0Sstevel@tonic-gate         gbc.gridwidth = GridBagConstraints.REMAINDER;
306*0Sstevel@tonic-gate         gbc.gridx = 0;
307*0Sstevel@tonic-gate         gbc.gridy = 3;
308*0Sstevel@tonic-gate         add(bottomPanel, gbc);
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate         DCButtonListener bl = new DCButtonListener();
311*0Sstevel@tonic-gate         ok.addActionListener(bl);
312*0Sstevel@tonic-gate         cancel.addActionListener(bl);
313*0Sstevel@tonic-gate         help.addActionListener(bl);
314*0Sstevel@tonic-gate         now.addActionListener(bl);
315*0Sstevel@tonic-gate         midnight.addActionListener(bl);
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate     }
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate     /**
320*0Sstevel@tonic-gate      * Adds a listener to all the text fields so that when they go out
321*0Sstevel@tonic-gate      * of focus (by tab or clicking), their values are checked for
322*0Sstevel@tonic-gate      * errors.
323*0Sstevel@tonic-gate      */
addFocusListeners()324*0Sstevel@tonic-gate     private void addFocusListeners() {
325*0Sstevel@tonic-gate     FocusListener fl = new DCFocusListener();
326*0Sstevel@tonic-gate     date.addFocusListener(fl);
327*0Sstevel@tonic-gate     year.addFocusListener(fl);
328*0Sstevel@tonic-gate     hour.addFocusListener(fl);
329*0Sstevel@tonic-gate     minute.addFocusListener(fl);
330*0Sstevel@tonic-gate     second.addFocusListener(fl);
331*0Sstevel@tonic-gate     }
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate     /**
334*0Sstevel@tonic-gate      * Closes (hides) the dialog box when the user is done
335*0Sstevel@tonic-gate      * @param save true if the box is being dismissed by clicking on
336*0Sstevel@tonic-gate      * "ok" and the user wants to retain the modified value, false
337*0Sstevel@tonic-gate      * otherwise.
338*0Sstevel@tonic-gate      */
dateTimeDialogClose(boolean save)339*0Sstevel@tonic-gate     private void dateTimeDialogClose(boolean save) {
340*0Sstevel@tonic-gate         if (save == true) {
341*0Sstevel@tonic-gate 	if (!updateFromGui())
342*0Sstevel@tonic-gate 	   return;
343*0Sstevel@tonic-gate     }
344*0Sstevel@tonic-gate     this.save = save;
345*0Sstevel@tonic-gate     setVisible(false);
346*0Sstevel@tonic-gate     }
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate     /**
349*0Sstevel@tonic-gate      * Checks to see is all text fields contain valid values.
350*0Sstevel@tonic-gate      * @return true if all are valid, false otherwise.
351*0Sstevel@tonic-gate      */
updateFromGui()352*0Sstevel@tonic-gate     private boolean updateFromGui() {
353*0Sstevel@tonic-gate         return (checkErrorAndSet(date) && checkErrorAndSet(year) &&
354*0Sstevel@tonic-gate 	        checkErrorAndSet(hour) && checkErrorAndSet(minute) &&
355*0Sstevel@tonic-gate 	        checkErrorAndSet(second));
356*0Sstevel@tonic-gate     }
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate     /**
359*0Sstevel@tonic-gate      * Checks the value stored as text in the field and sets its numeric
360*0Sstevel@tonic-gate      * value to that if it is legitimate.
361*0Sstevel@tonic-gate      * @return true if the value was legitimate and got set, false
362*0Sstevel@tonic-gate      * otherwise.
363*0Sstevel@tonic-gate      */
checkErrorAndSet(DCTextField tf)364*0Sstevel@tonic-gate     private boolean checkErrorAndSet(DCTextField tf) {
365*0Sstevel@tonic-gate         int i = 0;
366*0Sstevel@tonic-gate         boolean errorState = false;
367*0Sstevel@tonic-gate         try {
368*0Sstevel@tonic-gate 	i = new Integer(tf.getText().trim()).intValue();
369*0Sstevel@tonic-gate 	errorState = !tf.checkValue(i);
370*0Sstevel@tonic-gate         } catch (NumberFormatException e2) {
371*0Sstevel@tonic-gate 	errorState =  true;
372*0Sstevel@tonic-gate         }
373*0Sstevel@tonic-gate         if (errorState) {
374*0Sstevel@tonic-gate 	tf.selectAll();
375*0Sstevel@tonic-gate 	toolkit.beep();
376*0Sstevel@tonic-gate         }
377*0Sstevel@tonic-gate         else
378*0Sstevel@tonic-gate 	tf.setValue(i);
379*0Sstevel@tonic-gate         return !errorState;
380*0Sstevel@tonic-gate     }
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate     /**
383*0Sstevel@tonic-gate      * Checks if the user requested that the value in this
384*0Sstevel@tonic-gate      * DateTimeDialog be used e.g., by clicking on "Ok" instead of
385*0Sstevel@tonic-gate      * "Cancel."
386*0Sstevel@tonic-gate      * @return true if the user wants to save the value in the
387*0Sstevel@tonic-gate      * DateTimeDialog, false otherwise.
388*0Sstevel@tonic-gate      */
389*0Sstevel@tonic-gate 
isSaved()390*0Sstevel@tonic-gate     public boolean isSaved() {
391*0Sstevel@tonic-gate         return save;
392*0Sstevel@tonic-gate     }
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate     /**
395*0Sstevel@tonic-gate      * Sets the date and time in fields to the current date and time.
396*0Sstevel@tonic-gate      */
setCurrentTime()397*0Sstevel@tonic-gate     public void setCurrentTime() {
398*0Sstevel@tonic-gate         setDate(new Date());
399*0Sstevel@tonic-gate     }
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate     /**
402*0Sstevel@tonic-gate      * Sets the current date of the DateTimeDialog and updates the gui
403*0Sstevel@tonic-gate      *   components to reflect that.
404*0Sstevel@tonic-gate      * @param date the Date to set it to.
405*0Sstevel@tonic-gate      */
setDate(Date newDate)406*0Sstevel@tonic-gate     public void setDate(Date newDate) {
407*0Sstevel@tonic-gate         calendar = new GregorianCalendar();
408*0Sstevel@tonic-gate         calendar.setTime(newDate);
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate     // update gui components now
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate     year.setValue(calendar.get(Calendar.YEAR));
413*0Sstevel@tonic-gate     month.select(calendar.get(Calendar.MONTH));
414*0Sstevel@tonic-gate     date.setValue(calendar.get(Calendar.DATE));
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate     // Make sure the date is in the valid range for the given month
417*0Sstevel@tonic-gate     fixDateField();
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate     hour.setValue(calendar.get(Calendar.HOUR_OF_DAY));
420*0Sstevel@tonic-gate     minute.setValue(calendar.get(Calendar.MINUTE));
421*0Sstevel@tonic-gate     second.setValue(calendar.get(Calendar.SECOND));
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate     }
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate     /**
426*0Sstevel@tonic-gate      * Set the time fields to midnight, i.e., clears them.
427*0Sstevel@tonic-gate      */
setMidnight()428*0Sstevel@tonic-gate     private void setMidnight() {
429*0Sstevel@tonic-gate 	    hour.setValue(0);
430*0Sstevel@tonic-gate 	    minute.setValue(0);
431*0Sstevel@tonic-gate 	    second.setValue(0);
432*0Sstevel@tonic-gate     }
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate     /**
435*0Sstevel@tonic-gate      * Make sure the date does not exceed the maximum allowable value
436*0Sstevel@tonic-gate      * for the currently selected month.
437*0Sstevel@tonic-gate      */
fixDateField()438*0Sstevel@tonic-gate     private void fixDateField() {
439*0Sstevel@tonic-gate         int monthIndex = month.getSelectedIndex();
440*0Sstevel@tonic-gate         int max = MONTH_LEN[monthIndex];
441*0Sstevel@tonic-gate         date.setMaximum(calendar.isLeapYear(year.getValue()) &&
442*0Sstevel@tonic-gate 	        monthIndex == 1 ? max + 1 : max);
443*0Sstevel@tonic-gate     }
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate   // * **********************************************
446*0Sstevel@tonic-gate   // 	 I N N E R    C L A S S E S   F O L L O W
447*0Sstevel@tonic-gate   // ***********************************************
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate     /**
450*0Sstevel@tonic-gate      * Listener for closing the dialog box through the window close
451*0Sstevel@tonic-gate      * menu.
452*0Sstevel@tonic-gate      */
453*0Sstevel@tonic-gate     private class DCWindowListener extends WindowAdapter {
windowClosing(WindowEvent e)454*0Sstevel@tonic-gate     public  void windowClosing(WindowEvent e) {
455*0Sstevel@tonic-gate         dateTimeDialogClose(false);
456*0Sstevel@tonic-gate         }
457*0Sstevel@tonic-gate     }
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate     /**
460*0Sstevel@tonic-gate      * Listener for any change in the month selected through the
461*0Sstevel@tonic-gate      * pull down menu
462*0Sstevel@tonic-gate      */
463*0Sstevel@tonic-gate     private class DCMonthChangeListener implements ItemListener {
itemStateChanged(ItemEvent e)464*0Sstevel@tonic-gate     public void itemStateChanged(ItemEvent e) {
465*0Sstevel@tonic-gate         fixDateField();
466*0Sstevel@tonic-gate     }
467*0Sstevel@tonic-gate     }
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate     /**
470*0Sstevel@tonic-gate      * Listener for all the buttons. The listener is shared for the sake
471*0Sstevel@tonic-gate      * of reducing the number of overall listeners.
472*0Sstevel@tonic-gate      * TBD: I18N the help
473*0Sstevel@tonic-gate      */
474*0Sstevel@tonic-gate     private class DCButtonListener implements ActionListener {
actionPerformed(ActionEvent e)475*0Sstevel@tonic-gate     public void actionPerformed(ActionEvent e) {
476*0Sstevel@tonic-gate         if (e.getSource() == ok) {
477*0Sstevel@tonic-gate 	DateTimeDialog.this.dateTimeDialogClose(true);
478*0Sstevel@tonic-gate         }
479*0Sstevel@tonic-gate         else
480*0Sstevel@tonic-gate 	if (e.getSource() == cancel) {
481*0Sstevel@tonic-gate     	  DateTimeDialog.this.dateTimeDialogClose(false);
482*0Sstevel@tonic-gate 	}
483*0Sstevel@tonic-gate 	else
484*0Sstevel@tonic-gate 	  if (e.getSource() == now) {
485*0Sstevel@tonic-gate 	    DateTimeDialog.this.setCurrentTime();
486*0Sstevel@tonic-gate 	  }
487*0Sstevel@tonic-gate 	  else
488*0Sstevel@tonic-gate 	    if (e.getSource() == midnight) {
489*0Sstevel@tonic-gate 		DateTimeDialog.this.setMidnight();
490*0Sstevel@tonic-gate 	    }
491*0Sstevel@tonic-gate 	    else
492*0Sstevel@tonic-gate                if (e.getSource() == help) {
493*0Sstevel@tonic-gate 		if (hd != null)
494*0Sstevel@tonic-gate 		  hd.show();
495*0Sstevel@tonic-gate 		else {
496*0Sstevel@tonic-gate 		  hd = new
497*0Sstevel@tonic-gate 		    HelpDialog(DateTimeDialog.this.parent,
498*0Sstevel@tonic-gate 			getString("Help for Date and Time Dialog"), false);
499*0Sstevel@tonic-gate 		  hd.setVisible(true);
500*0Sstevel@tonic-gate 		  hd.setText(getString(hrb, "DateTimeDialogHelp"));
501*0Sstevel@tonic-gate 		   }
502*0Sstevel@tonic-gate 	        }
503*0Sstevel@tonic-gate         } // actionPerformed
504*0Sstevel@tonic-gate     }
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate     /**
507*0Sstevel@tonic-gate      * Listener for any change in focus with respect to the text
508*0Sstevel@tonic-gate      * fields. When a text field is going out of focus, it detemines if the
509*0Sstevel@tonic-gate      * text value in it is valid. If not, it returns focus to that text
510*0Sstevel@tonic-gate      * field.
511*0Sstevel@tonic-gate      */
512*0Sstevel@tonic-gate     private class DCFocusListener extends FocusAdapter {
513*0Sstevel@tonic-gate 
focusLost(FocusEvent e)514*0Sstevel@tonic-gate         public void focusLost(FocusEvent e) {
515*0Sstevel@tonic-gate 	if (!checkErrorAndSet((DCTextField)e.getSource()))
516*0Sstevel@tonic-gate 	  ((DCTextField)e.getSource()).requestFocus();
517*0Sstevel@tonic-gate         }
518*0Sstevel@tonic-gate     }
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate     /**
521*0Sstevel@tonic-gate      * The string representation of the dialog box.
522*0Sstevel@tonic-gate      * @return a String which contians the date and time in locale
523*0Sstevel@tonic-gate      * default format, but to MEDIUM length formatting style.
524*0Sstevel@tonic-gate      */
toString()525*0Sstevel@tonic-gate     public String toString() {
526*0Sstevel@tonic-gate         calendar = new GregorianCalendar(year.getValue(),
527*0Sstevel@tonic-gate 				       month.getSelectedIndex(),
528*0Sstevel@tonic-gate 				       date.getValue(),
529*0Sstevel@tonic-gate 				       hour.getValue(),
530*0Sstevel@tonic-gate 				       minute.getValue(),
531*0Sstevel@tonic-gate 				       second.getValue());
532*0Sstevel@tonic-gate         return df.format(calendar.getTime());
533*0Sstevel@tonic-gate     }
534*0Sstevel@tonic-gate 
535*0Sstevel@tonic-gate     /**
536*0Sstevel@tonic-gate      * Call rb.getString(), but catch exception and return English
537*0Sstevel@tonic-gate      * key so that small spelling errors don't cripple the GUI
538*0Sstevel@tonic-gate      *
539*0Sstevel@tonic-gate      */
getString(String key)540*0Sstevel@tonic-gate     private static final String getString(String key) {
541*0Sstevel@tonic-gate     return (getString(rb, key));
542*0Sstevel@tonic-gate     }
543*0Sstevel@tonic-gate 
getString(ResourceBundle rb, String key)544*0Sstevel@tonic-gate     private static final String getString(ResourceBundle rb, String key) {
545*0Sstevel@tonic-gate     try {
546*0Sstevel@tonic-gate 	String res = rb.getString(key);
547*0Sstevel@tonic-gate 	return res;
548*0Sstevel@tonic-gate     } catch (MissingResourceException e) {
549*0Sstevel@tonic-gate 	System.out.println("Missing resource "+key+", using English.");
550*0Sstevel@tonic-gate 	return key;
551*0Sstevel@tonic-gate         }
552*0Sstevel@tonic-gate     }
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate     /*
555*0Sstevel@tonic-gate     public static final void main(String args[]) {
556*0Sstevel@tonic-gate     Frame f = new Frame();
557*0Sstevel@tonic-gate     //  while (true){
558*0Sstevel@tonic-gate         DateTimeDialog d = new DateTimeDialog(f, Color.white, Color.black);
559*0Sstevel@tonic-gate         d.setVisible(true);
560*0Sstevel@tonic-gate         System.out.println(d.toString());
561*0Sstevel@tonic-gate       //    }
562*0Sstevel@tonic-gate     }
563*0Sstevel@tonic-gate     */
564*0Sstevel@tonic-gate }
565