xref: /titanic_44/usr/src/cmd/krb5/kadmin/gui/dchanger/DateTimeDialog.java (revision 4d0eb50e691de4c20b1dd9976ad6839fede8a42d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1999-2000 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate import java.awt.*;
287c478bd9Sstevel@tonic-gate import java.awt.event.*;
297c478bd9Sstevel@tonic-gate import java.text.*;
307c478bd9Sstevel@tonic-gate import java.util.*;
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /**
337c478bd9Sstevel@tonic-gate  * This class creates a dialog box that helps the user enter date and
347c478bd9Sstevel@tonic-gate  * time with mouse clicks.  The dialog box need only be created
357c478bd9Sstevel@tonic-gate  * once. The Ok and Cancel buttons merely call setVisible with an
367c478bd9Sstevel@tonic-gate  *  argument of false.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate // The layout will consist of 3 panels: topPanel contains the
407c478bd9Sstevel@tonic-gate // different labels and fields. middlePanel contains the buttons
417c478bd9Sstevel@tonic-gate // midnight and now. bottomPanel contains the buttons ok, cancel and
427c478bd9Sstevel@tonic-gate // help. The last two panels are separated by a LineSeparator.
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate public class DateTimeDialog extends Dialog {
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate     private boolean save;
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate     private Frame parent;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate     private DCPanel dateDCPanel;
517c478bd9Sstevel@tonic-gate     private DCPanel yearDCPanel;
527c478bd9Sstevel@tonic-gate     private DCPanel hourDCPanel;
537c478bd9Sstevel@tonic-gate     private DCPanel minuteDCPanel;
547c478bd9Sstevel@tonic-gate     private DCPanel secondDCPanel;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate     private Choice month;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate     private DCCircularTextField date;
597c478bd9Sstevel@tonic-gate     private DCCircularTextField hour;
607c478bd9Sstevel@tonic-gate     private DCCircularTextField second;
617c478bd9Sstevel@tonic-gate     private DCCircularTextField minute;
627c478bd9Sstevel@tonic-gate     private DCTextField year;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate     private Button ok;
657c478bd9Sstevel@tonic-gate     private Button cancel;
667c478bd9Sstevel@tonic-gate     private Button help;
677c478bd9Sstevel@tonic-gate     private Button now;
687c478bd9Sstevel@tonic-gate     private Button midnight;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate     private HelpDialog hd = null;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate     private Panel topPanel;
737c478bd9Sstevel@tonic-gate     private Panel middlePanel;
747c478bd9Sstevel@tonic-gate     private Panel bottomPanel;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate     private GregorianCalendar calendar = null;
777c478bd9Sstevel@tonic-gate     private static int MONTH_LEN[] = {31, 28, 31, 30, 31, 30, 31,
787c478bd9Sstevel@tonic-gate 				    31, 30, 31, 30, 31};
797c478bd9Sstevel@tonic-gate     private static DateFormat df =
807c478bd9Sstevel@tonic-gate     DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
817c478bd9Sstevel@tonic-gate 				 DateFormat.MEDIUM);
827c478bd9Sstevel@tonic-gate     private static Toolkit toolkit = Toolkit.getDefaultToolkit();
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate   // For I18N
857c478bd9Sstevel@tonic-gate     private static ResourceBundle rb =
867c478bd9Sstevel@tonic-gate     ResourceBundle.getBundle("GuiResource" /* NOI18N */);
877c478bd9Sstevel@tonic-gate     private static ResourceBundle hrb =
887c478bd9Sstevel@tonic-gate     ResourceBundle.getBundle("HelpData" /* NOI18N */);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate     /**
917c478bd9Sstevel@tonic-gate      * Constructor that lays out the componeents and sets the different
927c478bd9Sstevel@tonic-gate      * event handlers.
937c478bd9Sstevel@tonic-gate      */
DateTimeDialog(Frame parent, Color background, Color foreground)947c478bd9Sstevel@tonic-gate     public DateTimeDialog(Frame parent, Color background, Color foreground) {
957c478bd9Sstevel@tonic-gate     super(parent, getString("SEAM Date/Time Helper"), true);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate     this.parent = parent;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate     setLayout(new GridBagLayout());
1007c478bd9Sstevel@tonic-gate     addLabels();
1017c478bd9Sstevel@tonic-gate     addFields(background, foreground);
1027c478bd9Sstevel@tonic-gate     addDCPanels();
1037c478bd9Sstevel@tonic-gate     addButtons();
1047c478bd9Sstevel@tonic-gate     addFocusListeners();
1057c478bd9Sstevel@tonic-gate     setCurrentTime();
1067c478bd9Sstevel@tonic-gate     setSize(250, 300);
1077c478bd9Sstevel@tonic-gate     setResizable(false);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate     addWindowListener(new DCWindowListener());
1107c478bd9Sstevel@tonic-gate     //	    initializeFocusOnTextField();
1117c478bd9Sstevel@tonic-gate     }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate     /**
1147c478bd9Sstevel@tonic-gate      * Adds the labels only
1157c478bd9Sstevel@tonic-gate      */
addLabels()1167c478bd9Sstevel@tonic-gate     private void addLabels() {
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate     GridBagConstraints gbc = new GridBagConstraints();
1197c478bd9Sstevel@tonic-gate     gbc.weighty = 1;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate     topPanel = new Panel();
1227c478bd9Sstevel@tonic-gate     topPanel.setLayout(new GridBagLayout());
1237c478bd9Sstevel@tonic-gate     gbc.gridwidth = GridBagConstraints.REMAINDER;
1247c478bd9Sstevel@tonic-gate     gbc.fill = GridBagConstraints.BOTH;
1257c478bd9Sstevel@tonic-gate     gbc.anchor = GridBagConstraints.CENTER;
1267c478bd9Sstevel@tonic-gate     gbc.gridx = 0;
1277c478bd9Sstevel@tonic-gate     gbc.gridy = 0;
1287c478bd9Sstevel@tonic-gate     add(topPanel, gbc);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate     gbc.fill = GridBagConstraints.NONE;
1317c478bd9Sstevel@tonic-gate     gbc.anchor = GridBagConstraints.EAST;
1327c478bd9Sstevel@tonic-gate     gbc.gridx = 0;
1337c478bd9Sstevel@tonic-gate     gbc.gridwidth = 1;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate     gbc.gridy = 0;
1367c478bd9Sstevel@tonic-gate     topPanel.add(new Label(getString("Month")), gbc);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate     gbc.gridy = 1;
1397c478bd9Sstevel@tonic-gate     topPanel.add(new Label(getString("Date")), gbc);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate     gbc.gridy = 2;
1427c478bd9Sstevel@tonic-gate     topPanel.add(new Label(getString("Year")), gbc);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate     gbc.gridy = 3;
1457c478bd9Sstevel@tonic-gate     topPanel.add(new Label(getString("Hour")), gbc);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate     gbc.gridy = 4;
1487c478bd9Sstevel@tonic-gate     topPanel.add(new Label(getString("Minute")), gbc);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate     gbc.gridy = 5;
1517c478bd9Sstevel@tonic-gate     topPanel.add(new Label(getString("Second")), gbc);
1527c478bd9Sstevel@tonic-gate     }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate     /**
1557c478bd9Sstevel@tonic-gate      * Adds the fields that will store the month, year, date, hour,
1567c478bd9Sstevel@tonic-gate      * minute and second.
1577c478bd9Sstevel@tonic-gate      */
addFields(Color background, Color foreground)1587c478bd9Sstevel@tonic-gate     private void addFields(Color background, Color foreground) {
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate     GridBagConstraints gbc = new GridBagConstraints();
1617c478bd9Sstevel@tonic-gate     gbc.weighty = 1;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate     month = new Choice();
1647c478bd9Sstevel@tonic-gate     initializeMonth();
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate     date = new DCCircularTextField("1", 2);
1677c478bd9Sstevel@tonic-gate     date.setMinimum(1);
1687c478bd9Sstevel@tonic-gate     date.setBackground(background);
1697c478bd9Sstevel@tonic-gate     date.setForeground(foreground);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate     hour = new DCCircularTextField("00", 2);
1727c478bd9Sstevel@tonic-gate     hour.setMaximum(23);
1737c478bd9Sstevel@tonic-gate     hour.setBackground(background);
1747c478bd9Sstevel@tonic-gate     hour.setForeground(foreground);
1757c478bd9Sstevel@tonic-gate     minute = new DCCircularTextField("00", 2);
1767c478bd9Sstevel@tonic-gate     minute.setBackground(background);
1777c478bd9Sstevel@tonic-gate     minute.setForeground(foreground);
1787c478bd9Sstevel@tonic-gate     second = new DCCircularTextField("00", 2);
1797c478bd9Sstevel@tonic-gate     second.setBackground(background);
1807c478bd9Sstevel@tonic-gate     second.setForeground(foreground);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate     year  = new DCTextField("2000", 4);
1837c478bd9Sstevel@tonic-gate     year.setBackground(background);
1847c478bd9Sstevel@tonic-gate     year.setForeground(foreground);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate     Panel tempPanel = new Panel();
1877c478bd9Sstevel@tonic-gate     tempPanel.add(month);
1887c478bd9Sstevel@tonic-gate     gbc.gridwidth = GridBagConstraints.REMAINDER;
1897c478bd9Sstevel@tonic-gate     gbc.fill = GridBagConstraints.HORIZONTAL;
1907c478bd9Sstevel@tonic-gate     gbc.anchor = GridBagConstraints.WEST;
1917c478bd9Sstevel@tonic-gate     gbc.gridx = 1;
1927c478bd9Sstevel@tonic-gate     gbc.gridy = 0;
1937c478bd9Sstevel@tonic-gate     topPanel.add(tempPanel, gbc);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate     // Remaining fields are in topPanel
1977c478bd9Sstevel@tonic-gate     gbc.gridwidth = 1;
1987c478bd9Sstevel@tonic-gate     gbc.fill = GridBagConstraints.NONE;
1997c478bd9Sstevel@tonic-gate     gbc.gridx = 1;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate     gbc.gridy = 1;
2027c478bd9Sstevel@tonic-gate     topPanel.add(date, gbc);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate     gbc.gridy = 2;
2057c478bd9Sstevel@tonic-gate     topPanel.add(year, gbc);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate     gbc.gridy = 3;
2087c478bd9Sstevel@tonic-gate     topPanel.add(hour, gbc);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate     gbc.gridy = 4;
2117c478bd9Sstevel@tonic-gate     topPanel.add(minute, gbc);
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate     gbc.gridy = 5;
2147c478bd9Sstevel@tonic-gate     topPanel.add(second, gbc);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate     }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate   // Adds the panels with the +/- buttons for each DCField
addDCPanels()2197c478bd9Sstevel@tonic-gate     private void addDCPanels() {
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate     GridBagConstraints gbc = new GridBagConstraints();
2227c478bd9Sstevel@tonic-gate     gbc.weighty = 1;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate     gbc.gridx = 2;
2257c478bd9Sstevel@tonic-gate     gbc.gridwidth = GridBagConstraints.REMAINDER;
2267c478bd9Sstevel@tonic-gate     gbc.gridheight = 1;
2277c478bd9Sstevel@tonic-gate     gbc.fill = GridBagConstraints.NONE;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate     dateDCPanel = new DCPanel();
2307c478bd9Sstevel@tonic-gate     yearDCPanel = new DCPanel();
2317c478bd9Sstevel@tonic-gate     hourDCPanel = new DCPanel();
2327c478bd9Sstevel@tonic-gate     minuteDCPanel = new DCPanel();
2337c478bd9Sstevel@tonic-gate     secondDCPanel = new DCPanel();
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate     gbc.gridy = 1;
2367c478bd9Sstevel@tonic-gate     topPanel.add(dateDCPanel, gbc);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate     gbc.gridy = GridBagConstraints.RELATIVE;
2397c478bd9Sstevel@tonic-gate     topPanel.add(yearDCPanel, gbc);
2407c478bd9Sstevel@tonic-gate     topPanel.add(hourDCPanel, gbc);
2417c478bd9Sstevel@tonic-gate     topPanel.add(minuteDCPanel, gbc);
2427c478bd9Sstevel@tonic-gate     topPanel.add(secondDCPanel, gbc);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate     dateDCPanel.setListener(date);
2457c478bd9Sstevel@tonic-gate     yearDCPanel.setListener(year);
2467c478bd9Sstevel@tonic-gate     hourDCPanel.setListener(hour);
2477c478bd9Sstevel@tonic-gate     minuteDCPanel.setListener(minute);
2487c478bd9Sstevel@tonic-gate     secondDCPanel.setListener(second);
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate     }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate     /**
2547c478bd9Sstevel@tonic-gate      * Sets the strings in the month pull-down menu. Also adds a listener
2557c478bd9Sstevel@tonic-gate      * that will modify the maximum date allowed depending on the month.
2567c478bd9Sstevel@tonic-gate      */
initializeMonth()2577c478bd9Sstevel@tonic-gate     private void initializeMonth() {
2587c478bd9Sstevel@tonic-gate     DateFormatSymbols dfSymbols = new DateFormatSymbols();
2597c478bd9Sstevel@tonic-gate     String[] monthStrings = dfSymbols.getMonths();
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	month.removeAll();
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	for (int i = 0; i < monthStrings.length; i++) {
2647c478bd9Sstevel@tonic-gate 	month.add(monthStrings[i]);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	month.addItemListener(new DCMonthChangeListener());
2687c478bd9Sstevel@tonic-gate     }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate   // Adds all the buttons
addButtons()2717c478bd9Sstevel@tonic-gate     private void addButtons() {
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	GridBagConstraints gbc = new GridBagConstraints();
2747c478bd9Sstevel@tonic-gate 	gbc.weighty = 1;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	middlePanel = new Panel();
2787c478bd9Sstevel@tonic-gate 	now  = new Button(getString("Now"));
2797c478bd9Sstevel@tonic-gate 	midnight	= new Button(getString("Midnight"));
2807c478bd9Sstevel@tonic-gate 	middlePanel.add(midnight);
2817c478bd9Sstevel@tonic-gate 	middlePanel.add(now);
2827c478bd9Sstevel@tonic-gate 	gbc.fill = GridBagConstraints.HORIZONTAL;
2837c478bd9Sstevel@tonic-gate 	gbc.gridwidth = GridBagConstraints.REMAINDER;
2847c478bd9Sstevel@tonic-gate 	gbc.gridx = 0;
2857c478bd9Sstevel@tonic-gate 	gbc.gridy = 1;
2867c478bd9Sstevel@tonic-gate 	add(middlePanel, gbc);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	gbc.gridwidth = GridBagConstraints.REMAINDER;
2897c478bd9Sstevel@tonic-gate 	gbc.gridheight = 1;
2907c478bd9Sstevel@tonic-gate 	gbc.fill = GridBagConstraints.BOTH;
2917c478bd9Sstevel@tonic-gate 	gbc.gridx = 0;
2927c478bd9Sstevel@tonic-gate 	gbc.gridy = 2;
2937c478bd9Sstevel@tonic-gate 	add(new LineSeparator(), gbc);
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	bottomPanel = new Panel();
2967c478bd9Sstevel@tonic-gate 	ok = new Button(getString("OK"));
2977c478bd9Sstevel@tonic-gate 	cancel =	new Button(getString("Cancel"));
2987c478bd9Sstevel@tonic-gate 	help = new Button(getString("Help"));
2997c478bd9Sstevel@tonic-gate 	bottomPanel.add(ok);
3007c478bd9Sstevel@tonic-gate 	bottomPanel.add(cancel);
3017c478bd9Sstevel@tonic-gate 	bottomPanel.add(help);
3027c478bd9Sstevel@tonic-gate 	gbc.fill = GridBagConstraints.HORIZONTAL;
3037c478bd9Sstevel@tonic-gate 	gbc.gridwidth = GridBagConstraints.REMAINDER;
3047c478bd9Sstevel@tonic-gate 	gbc.gridx = 0;
3057c478bd9Sstevel@tonic-gate 	gbc.gridy = 3;
3067c478bd9Sstevel@tonic-gate 	add(bottomPanel, gbc);
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	DCButtonListener bl = new DCButtonListener();
3097c478bd9Sstevel@tonic-gate 	ok.addActionListener(bl);
3107c478bd9Sstevel@tonic-gate 	cancel.addActionListener(bl);
3117c478bd9Sstevel@tonic-gate 	help.addActionListener(bl);
3127c478bd9Sstevel@tonic-gate 	now.addActionListener(bl);
3137c478bd9Sstevel@tonic-gate 	midnight.addActionListener(bl);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate     }
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate     /**
3187c478bd9Sstevel@tonic-gate      * Adds a listener to all the text fields so that when they go out
3197c478bd9Sstevel@tonic-gate      * of focus (by tab or clicking), their values are checked for
3207c478bd9Sstevel@tonic-gate      * errors.
3217c478bd9Sstevel@tonic-gate      */
addFocusListeners()3227c478bd9Sstevel@tonic-gate     private void addFocusListeners() {
3237c478bd9Sstevel@tonic-gate     FocusListener fl = new DCFocusListener();
3247c478bd9Sstevel@tonic-gate     date.addFocusListener(fl);
3257c478bd9Sstevel@tonic-gate     year.addFocusListener(fl);
3267c478bd9Sstevel@tonic-gate     hour.addFocusListener(fl);
3277c478bd9Sstevel@tonic-gate     minute.addFocusListener(fl);
3287c478bd9Sstevel@tonic-gate     second.addFocusListener(fl);
3297c478bd9Sstevel@tonic-gate     }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate     /**
3327c478bd9Sstevel@tonic-gate      * Closes (hides) the dialog box when the user is done
3337c478bd9Sstevel@tonic-gate      * @param save true if the box is being dismissed by clicking on
3347c478bd9Sstevel@tonic-gate      * "ok" and the user wants to retain the modified value, false
3357c478bd9Sstevel@tonic-gate      * otherwise.
3367c478bd9Sstevel@tonic-gate      */
dateTimeDialogClose(boolean save)3377c478bd9Sstevel@tonic-gate     private void dateTimeDialogClose(boolean save) {
3387c478bd9Sstevel@tonic-gate 	if (save == true) {
3397c478bd9Sstevel@tonic-gate 	if (!updateFromGui())
3407c478bd9Sstevel@tonic-gate 	   return;
3417c478bd9Sstevel@tonic-gate     }
3427c478bd9Sstevel@tonic-gate     this.save = save;
3437c478bd9Sstevel@tonic-gate     setVisible(false);
3447c478bd9Sstevel@tonic-gate     }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate     /**
3477c478bd9Sstevel@tonic-gate      * Checks to see is all text fields contain valid values.
3487c478bd9Sstevel@tonic-gate      * @return true if all are valid, false otherwise.
3497c478bd9Sstevel@tonic-gate      */
updateFromGui()3507c478bd9Sstevel@tonic-gate     private boolean updateFromGui() {
3517c478bd9Sstevel@tonic-gate 	return (checkErrorAndSet(date) && checkErrorAndSet(year) &&
3527c478bd9Sstevel@tonic-gate 		checkErrorAndSet(hour) && checkErrorAndSet(minute) &&
3537c478bd9Sstevel@tonic-gate 		checkErrorAndSet(second));
3547c478bd9Sstevel@tonic-gate     }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate     /**
3577c478bd9Sstevel@tonic-gate      * Checks the value stored as text in the field and sets its numeric
3587c478bd9Sstevel@tonic-gate      * value to that if it is legitimate.
3597c478bd9Sstevel@tonic-gate      * @return true if the value was legitimate and got set, false
3607c478bd9Sstevel@tonic-gate      * otherwise.
3617c478bd9Sstevel@tonic-gate      */
checkErrorAndSet(DCTextField tf)3627c478bd9Sstevel@tonic-gate     private boolean checkErrorAndSet(DCTextField tf) {
3637c478bd9Sstevel@tonic-gate 	int i = 0;
3647c478bd9Sstevel@tonic-gate 	boolean errorState = false;
3657c478bd9Sstevel@tonic-gate 	try {
3667c478bd9Sstevel@tonic-gate 	i = new Integer(tf.getText().trim()).intValue();
3677c478bd9Sstevel@tonic-gate 	errorState = !tf.checkValue(i);
3687c478bd9Sstevel@tonic-gate 	} catch (NumberFormatException e2) {
3697c478bd9Sstevel@tonic-gate 	errorState =  true;
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 	if (errorState) {
3727c478bd9Sstevel@tonic-gate 	tf.selectAll();
3737c478bd9Sstevel@tonic-gate 	toolkit.beep();
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 	else
3767c478bd9Sstevel@tonic-gate 	tf.setValue(i);
3777c478bd9Sstevel@tonic-gate 	return !errorState;
3787c478bd9Sstevel@tonic-gate     }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate     /**
3817c478bd9Sstevel@tonic-gate      * Checks if the user requested that the value in this
3827c478bd9Sstevel@tonic-gate      * DateTimeDialog be used e.g., by clicking on "Ok" instead of
3837c478bd9Sstevel@tonic-gate      * "Cancel."
3847c478bd9Sstevel@tonic-gate      * @return true if the user wants to save the value in the
3857c478bd9Sstevel@tonic-gate      * DateTimeDialog, false otherwise.
3867c478bd9Sstevel@tonic-gate      */
3877c478bd9Sstevel@tonic-gate 
isSaved()3887c478bd9Sstevel@tonic-gate     public boolean isSaved() {
3897c478bd9Sstevel@tonic-gate 	return save;
3907c478bd9Sstevel@tonic-gate     }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate     /**
3937c478bd9Sstevel@tonic-gate      * Sets the date and time in fields to the current date and time.
3947c478bd9Sstevel@tonic-gate      */
setCurrentTime()3957c478bd9Sstevel@tonic-gate     public void setCurrentTime() {
3967c478bd9Sstevel@tonic-gate 	setDate(new Date());
3977c478bd9Sstevel@tonic-gate     }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate     /**
4007c478bd9Sstevel@tonic-gate      * Sets the current date of the DateTimeDialog and updates the gui
4017c478bd9Sstevel@tonic-gate      *	 components to reflect that.
4027c478bd9Sstevel@tonic-gate      * @param date the Date to set it to.
4037c478bd9Sstevel@tonic-gate      */
setDate(Date newDate)4047c478bd9Sstevel@tonic-gate     public void setDate(Date newDate) {
4057c478bd9Sstevel@tonic-gate 	calendar = new GregorianCalendar();
4067c478bd9Sstevel@tonic-gate 	calendar.setTime(newDate);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate     // update gui components now
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate     year.setValue(calendar.get(Calendar.YEAR));
4117c478bd9Sstevel@tonic-gate     month.select(calendar.get(Calendar.MONTH));
4127c478bd9Sstevel@tonic-gate     date.setValue(calendar.get(Calendar.DATE));
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate     // Make sure the date is in the valid range for the given month
4157c478bd9Sstevel@tonic-gate     fixDateField();
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate     hour.setValue(calendar.get(Calendar.HOUR_OF_DAY));
4187c478bd9Sstevel@tonic-gate     minute.setValue(calendar.get(Calendar.MINUTE));
4197c478bd9Sstevel@tonic-gate     second.setValue(calendar.get(Calendar.SECOND));
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate     }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate     /**
4247c478bd9Sstevel@tonic-gate      * Set the time fields to midnight, i.e., clears them.
4257c478bd9Sstevel@tonic-gate      */
setMidnight()4267c478bd9Sstevel@tonic-gate     private void setMidnight() {
4277c478bd9Sstevel@tonic-gate 	    hour.setValue(0);
4287c478bd9Sstevel@tonic-gate 	    minute.setValue(0);
4297c478bd9Sstevel@tonic-gate 	    second.setValue(0);
4307c478bd9Sstevel@tonic-gate     }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate     /**
4337c478bd9Sstevel@tonic-gate      * Make sure the date does not exceed the maximum allowable value
4347c478bd9Sstevel@tonic-gate      * for the currently selected month.
4357c478bd9Sstevel@tonic-gate      */
fixDateField()4367c478bd9Sstevel@tonic-gate     private void fixDateField() {
4377c478bd9Sstevel@tonic-gate 	int monthIndex = month.getSelectedIndex();
4387c478bd9Sstevel@tonic-gate 	int max = MONTH_LEN[monthIndex];
4397c478bd9Sstevel@tonic-gate 	date.setMaximum(calendar.isLeapYear(year.getValue()) &&
4407c478bd9Sstevel@tonic-gate 		monthIndex == 1 ? max + 1 : max);
4417c478bd9Sstevel@tonic-gate     }
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate   // * **********************************************
4447c478bd9Sstevel@tonic-gate   //	 I N N E R    C L A S S E S   F O L L O W
4457c478bd9Sstevel@tonic-gate   // ***********************************************
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate     /**
4487c478bd9Sstevel@tonic-gate      * Listener for closing the dialog box through the window close
4497c478bd9Sstevel@tonic-gate      * menu.
4507c478bd9Sstevel@tonic-gate      */
4517c478bd9Sstevel@tonic-gate     private class DCWindowListener extends WindowAdapter {
windowClosing(WindowEvent e)4527c478bd9Sstevel@tonic-gate     public  void windowClosing(WindowEvent e) {
4537c478bd9Sstevel@tonic-gate 	dateTimeDialogClose(false);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate     }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate     /**
4587c478bd9Sstevel@tonic-gate      * Listener for any change in the month selected through the
4597c478bd9Sstevel@tonic-gate      * pull down menu
4607c478bd9Sstevel@tonic-gate      */
4617c478bd9Sstevel@tonic-gate     private class DCMonthChangeListener implements ItemListener {
itemStateChanged(ItemEvent e)4627c478bd9Sstevel@tonic-gate     public void itemStateChanged(ItemEvent e) {
4637c478bd9Sstevel@tonic-gate 	fixDateField();
4647c478bd9Sstevel@tonic-gate     }
4657c478bd9Sstevel@tonic-gate     }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate     /**
4687c478bd9Sstevel@tonic-gate      * Listener for all the buttons. The listener is shared for the sake
4697c478bd9Sstevel@tonic-gate      * of reducing the number of overall listeners.
4707c478bd9Sstevel@tonic-gate      * TBD: I18N the help
4717c478bd9Sstevel@tonic-gate      */
4727c478bd9Sstevel@tonic-gate     private class DCButtonListener implements ActionListener {
actionPerformed(ActionEvent e)4737c478bd9Sstevel@tonic-gate     public void actionPerformed(ActionEvent e) {
4747c478bd9Sstevel@tonic-gate 	if (e.getSource() == ok) {
4757c478bd9Sstevel@tonic-gate 	DateTimeDialog.this.dateTimeDialogClose(true);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 	else
4787c478bd9Sstevel@tonic-gate 	if (e.getSource() == cancel) {
4797c478bd9Sstevel@tonic-gate 	  DateTimeDialog.this.dateTimeDialogClose(false);
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 	else
4827c478bd9Sstevel@tonic-gate 	  if (e.getSource() == now) {
4837c478bd9Sstevel@tonic-gate 	    DateTimeDialog.this.setCurrentTime();
4847c478bd9Sstevel@tonic-gate 	  }
4857c478bd9Sstevel@tonic-gate 	  else
4867c478bd9Sstevel@tonic-gate 	    if (e.getSource() == midnight) {
4877c478bd9Sstevel@tonic-gate 		DateTimeDialog.this.setMidnight();
4887c478bd9Sstevel@tonic-gate 	    }
4897c478bd9Sstevel@tonic-gate 	    else
4907c478bd9Sstevel@tonic-gate 		if (e.getSource() == help) {
4917c478bd9Sstevel@tonic-gate 		    if (hd != null)
492*4d0eb50eSRichard PALO 			hd.setVisible(true);
4937c478bd9Sstevel@tonic-gate 		else {
4947c478bd9Sstevel@tonic-gate 		    hd = new
4957c478bd9Sstevel@tonic-gate 		    HelpDialog(DateTimeDialog.this.parent,
4967c478bd9Sstevel@tonic-gate 			getString("Help for Date and Time Dialog"), false);
4977c478bd9Sstevel@tonic-gate 		    hd.setVisible(true);
4987c478bd9Sstevel@tonic-gate 		    hd.setText(getString(hrb, "DateTimeDialogHelp"));
4997c478bd9Sstevel@tonic-gate 		}
5007c478bd9Sstevel@tonic-gate 	    }
5017c478bd9Sstevel@tonic-gate 	} // actionPerformed
5027c478bd9Sstevel@tonic-gate     }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate     /**
5057c478bd9Sstevel@tonic-gate      * Listener for any change in focus with respect to the text
5067c478bd9Sstevel@tonic-gate      * fields. When a text field is going out of focus, it detemines if the
5077c478bd9Sstevel@tonic-gate      * text value in it is valid. If not, it returns focus to that text
5087c478bd9Sstevel@tonic-gate      * field.
5097c478bd9Sstevel@tonic-gate      */
5107c478bd9Sstevel@tonic-gate     private class DCFocusListener extends FocusAdapter {
5117c478bd9Sstevel@tonic-gate 
focusLost(FocusEvent e)5127c478bd9Sstevel@tonic-gate 	public void focusLost(FocusEvent e) {
5137c478bd9Sstevel@tonic-gate 	if (!checkErrorAndSet((DCTextField)e.getSource()))
5147c478bd9Sstevel@tonic-gate 	  ((DCTextField)e.getSource()).requestFocus();
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate     }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate     /**
5197c478bd9Sstevel@tonic-gate      * The string representation of the dialog box.
5207c478bd9Sstevel@tonic-gate      * @return a String which contians the date and time in locale
5217c478bd9Sstevel@tonic-gate      * default format, but to MEDIUM length formatting style.
5227c478bd9Sstevel@tonic-gate      */
toString()5237c478bd9Sstevel@tonic-gate     public String toString() {
5247c478bd9Sstevel@tonic-gate 	calendar = new GregorianCalendar(year.getValue(),
5257c478bd9Sstevel@tonic-gate 					month.getSelectedIndex(),
5267c478bd9Sstevel@tonic-gate 					date.getValue(),
5277c478bd9Sstevel@tonic-gate 					hour.getValue(),
5287c478bd9Sstevel@tonic-gate 					minute.getValue(),
5297c478bd9Sstevel@tonic-gate 					second.getValue());
5307c478bd9Sstevel@tonic-gate 	return df.format(calendar.getTime());
5317c478bd9Sstevel@tonic-gate     }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate     /**
5347c478bd9Sstevel@tonic-gate      * Call rb.getString(), but catch exception and return English
5357c478bd9Sstevel@tonic-gate      * key so that small spelling errors don't cripple the GUI
5367c478bd9Sstevel@tonic-gate      *
5377c478bd9Sstevel@tonic-gate      */
getString(String key)5387c478bd9Sstevel@tonic-gate     private static final String getString(String key) {
5397c478bd9Sstevel@tonic-gate     return (getString(rb, key));
5407c478bd9Sstevel@tonic-gate     }
5417c478bd9Sstevel@tonic-gate 
getString(ResourceBundle rb, String key)5427c478bd9Sstevel@tonic-gate     private static final String getString(ResourceBundle rb, String key) {
5437c478bd9Sstevel@tonic-gate     try {
5447c478bd9Sstevel@tonic-gate 	String res = rb.getString(key);
5457c478bd9Sstevel@tonic-gate 	return res;
5467c478bd9Sstevel@tonic-gate     } catch (MissingResourceException e) {
5477c478bd9Sstevel@tonic-gate 	System.out.println("Missing resource "+key+", using English.");
5487c478bd9Sstevel@tonic-gate 	return key;
5497c478bd9Sstevel@tonic-gate 	}
5507c478bd9Sstevel@tonic-gate     }
5517c478bd9Sstevel@tonic-gate 
552*4d0eb50eSRichard PALO     /* BEGIN JSTYLED */
5537c478bd9Sstevel@tonic-gate     /*
5547c478bd9Sstevel@tonic-gate     public static final void main(String args[]) {
5557c478bd9Sstevel@tonic-gate     Frame f = new Frame();
5567c478bd9Sstevel@tonic-gate     //	while (true){
5577c478bd9Sstevel@tonic-gate 	DateTimeDialog d = new DateTimeDialog(f, Color.white, Color.black);
5587c478bd9Sstevel@tonic-gate 	d.setVisible(true);
5597c478bd9Sstevel@tonic-gate 	System.out.println(d.toString());
5607c478bd9Sstevel@tonic-gate       //    }
5617c478bd9Sstevel@tonic-gate     }
5627c478bd9Sstevel@tonic-gate     */
563*4d0eb50eSRichard PALO     /* END JSTYLED */
5647c478bd9Sstevel@tonic-gate }
565