home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / intl / datamgmt / demos-1.1 / DateTimeDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  29.1 KB  |  995 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /*
  18.  * @(#)DateTimeDemo.java    1.1 96/11/23
  19.  *
  20.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  21.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  22.  *
  23.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  24.  *
  25.  *   The original version of this source code and documentation is copyrighted
  26.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  27.  * materials are provided under terms of a License Agreement between Taligent
  28.  * and Sun. This technology is protected by multiple US and International
  29.  * patents. This notice and attribution to Taligent may not be removed.
  30.  *   Taligent is a registered trademark of Taligent, Inc.
  31.  *
  32.  * Permission to use, copy, modify, and distribute this software
  33.  * and its documentation for NON-COMMERCIAL purposes and without
  34.  * fee is hereby granted provided that this copyright notice
  35.  * appears in all copies. Please refer to the file "copyright.html"
  36.  * for further important copyright and licensing information.
  37.  *
  38.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  39.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  40.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  41.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  42.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  43.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  44.  *
  45.  */
  46.  
  47. import java.applet.Applet;
  48. import java.awt.*;
  49. import java.lang.*;
  50. import java.net.*;
  51. import java.io.*;
  52.  
  53. import java.util.*;
  54. import java.text.DateFormat;
  55. import java.text.SimpleDateFormat;
  56. import java.text.ParsePosition;
  57.  
  58. import java.awt.event.KeyAdapter;
  59. import java.awt.event.KeyEvent;
  60.  
  61. import java.awt.event.ItemListener;
  62. import java.awt.event.ItemEvent;
  63.  
  64. import java.awt.event.ActionListener;
  65. import java.awt.event.ActionEvent;
  66.  
  67. import java.awt.event.WindowAdapter;
  68. import java.awt.event.WindowEvent;
  69.  
  70.  
  71. /**
  72.  * DateTimeDemo demonstrates how Date/Time formatter works.
  73.  */
  74. public class DateTimeDemo extends DemoApplet
  75. {
  76.     /**
  77.      * The main function which defines the behavior of the DateTimeDemo
  78.      * applet when an applet is started.
  79.      */
  80.     public static void main(String argv[]) {
  81.         DemoApplet.showDemo(new DateTimeFrame(null));
  82.     }
  83.  
  84.     /**
  85.      * This creates a DateTimeFrame for the demo applet.
  86.      */
  87.     public Frame createDemoFrame(DemoApplet applet) {
  88.         return new DateTimeFrame(applet);
  89.     }
  90. }
  91.  
  92. /**
  93.  * A Frame is a top-level window with a title. The default layout for a frame
  94.  * is BorderLayout.  The DateTimeFrame class defines the window layout of
  95.  * DateTimeDemo.
  96.  */
  97. class DateTimeFrame extends Frame implements ItemListener, ActionListener
  98. {
  99.     private static final String creditString
  100.         = "";
  101.  
  102.     private static final int FIELD_COLUMNS = 45;
  103.  
  104.     private static final boolean DEBUG = false;
  105.  
  106.     private static final int millisPerHour = 60 * 60 * 1000;
  107.     private boolean isLocalized = false;
  108.     private boolean lenientMode = true;
  109.     private Locale curLocale = Locale.US;
  110.  
  111.     private SimpleDateFormat format;
  112.     private Locale[] locales;
  113.     protected DemoApplet applet;
  114.  
  115.     // Mapping tables for displaying Rep. Cities for given timezones.
  116.     private static final int kAdjCityIndex[]
  117.     // many-to-1 mapping:
  118.     // locale index --> rep city index
  119.     = { 1,
  120.         3,
  121.         2,
  122.         4,
  123.         0,
  124.         5,  // eg, Locale Index: 5 --> Rep. City index: 5
  125.     1,
  126.     0,
  127.         6};
  128.  
  129.     private static final int kZoneOffsets[]
  130.     // 1-to-1 maping:
  131.     // kZoneOffsets returns the zone offset for a given rep. city index.
  132.     = { 1*millisPerHour,
  133.        -8*millisPerHour,
  134.                       0,
  135.        -5*millisPerHour,
  136.        -7*millisPerHour,
  137.        -6*millisPerHour,
  138.         9*millisPerHour};
  139.  
  140.     private static final String kZoneIDs[]
  141.     // 1-1 maping:
  142.     // kZoneIDs returns the zone ID string for a given rep. city index.
  143.     = {"ECT",
  144.        "ECT",
  145.        "GMT",
  146.        "EST",
  147.        "PST",
  148.        "EST",
  149.        "JST"};
  150.  
  151.  
  152.     /**
  153.      * Constructs a new DateTimeFrame that is initially invisible.
  154.      */
  155.     public DateTimeFrame(DemoApplet applet)
  156.     {
  157.         super("Date/Time Formatting Demo");
  158.         this.applet = applet;
  159.         init();
  160.  
  161.         // set up event handling for text items
  162.         MyKeyAdapter keyListener = new MyKeyAdapter();
  163.         inputText.addKeyListener(keyListener);
  164.         patternText.addKeyListener(keyListener);
  165.         outputText.addKeyListener(keyListener);
  166.         millisText.addKeyListener(keyListener);
  167.  
  168.         // set up event handling for "item events"
  169.     getLocalized.addItemListener(this);
  170.     getLenientMode.addItemListener(this);
  171.  
  172.         // set up event handling for "action events"
  173.     up.addActionListener(this);
  174.     down.addActionListener(this);
  175.  
  176.         addWindowListener(new MyWindowAdapter());
  177.  
  178.         start();
  179.     }
  180.  
  181.  
  182.     /**
  183.      * Initializes the applet. You never need to call this directly, it
  184.      * is called automatically by the system once the applet is created.
  185.      */
  186.     public void init()
  187.     {
  188.         // Get G7 locales only for demo purpose. To get all the locales
  189.         // supported, switch to calling TimeFormat.getAvailableLocales().
  190.         // commented.  However, the mapping tables such as kAdjCityIndex
  191.         // must be expended as well.
  192.         locales = Utility.getG7Locales();
  193. //        locales = TimeFormat.getAvailableLocales();
  194.  
  195.         buildGUI();
  196.  
  197.         // Stick the names of the locales into the locale popup menu
  198.         Locale displayLocale = Locale.getDefault();
  199.         for (int i = 0; i < locales.length; i++) {
  200.             if (locales[i].getCountry().length() > 0) {
  201.                 localeMenu.addItem( locales[i].getDisplayName() );
  202.             }
  203.         }
  204.         localeMenu.select( Locale.US.getDisplayName());
  205.  
  206.         // For starters, use the default format for the selected locale
  207.         // in the menu
  208.         setFormatFromLocale(true);
  209.         formatText();
  210.     }
  211.  
  212.     //------------------------------------------------------------
  213.     // package private
  214.     //------------------------------------------------------------
  215.     void addWithFont(Container container, Component foo, Font font) {
  216.         if (font != null)
  217.             foo.setFont(font);
  218.         container.add(foo);
  219.     }
  220.  
  221.     /**
  222.      * Called to start the applet. You never need to call this method
  223.      * directly, it is called when the applet's document is visited.
  224.      */
  225.     public void start()
  226.     {
  227.         // do nothing
  228.     }
  229.  
  230.     /**
  231.      * This function is called when it is necessary to display a new
  232.      * format pattern. This happens when the state of the "Localized Pattern"
  233.      * CheckBox is changed.
  234.      */
  235.     public void handleNewFormat()
  236.     {
  237.         if( isLocalized ) {
  238.             Utility.setText(patternText, format.toLocalizedPattern() );
  239.         }
  240.         else {
  241.             Utility.setText(patternText, format.toPattern() );
  242.         }
  243.     }
  244.  
  245.     /**
  246.      * This function is called when users change the Calendar (time fields)
  247.      * lenient mode. When the state of the "Lenient Mode" CheckBox
  248.      * is changed, the time string in the "1.1 Date" text field will be
  249.      * re-parsed, and the parsing result will be displayed in the "1.0 Date"
  250.      * text field.
  251.      */
  252.     public void lenientModeChanged()
  253.     {
  254.         format.setLenient(lenientMode);
  255.  
  256.         parseText();
  257.     }
  258.  
  259.     //{{DECLARE_CONTROLS
  260.     Panel localePanel;
  261.     Panel formatPanel;
  262.     CheckboxGroup group1;
  263.     CheckboxGroup group2;
  264.     Label label1;
  265.     Label label2;
  266.     Label label3;
  267.     Label demo;
  268.     Label code;
  269.     Choice localeMenu;
  270.     Choice dateStyleMenu;
  271.     Choice timeStyleMenu;
  272.     Choice dateMenu;
  273.     Choice cityMenu;
  274.     Label dateLabel;
  275.     Label cityLabel;
  276.     TextField millisText;
  277.     Label millisLabel;
  278.     Button up;
  279.     Button down;
  280.     Label localeLabel;
  281.     Label dateStyleLabel;
  282.     Label timeStyleLabel;
  283.     TextField inputText;
  284.     TextField outputText;
  285.     Label formatLabel;
  286.     Label parseLabel;
  287.     //Button rightButton;
  288.     //Button leftButton;
  289.     TextField patternText;
  290.     Label label4;
  291.     Checkbox getDateInstance;
  292.     Checkbox getTimeInstance;
  293.     Checkbox getDateTimeInstance;
  294.     Checkbox getRoll;
  295.     Checkbox getAdd;
  296.     Checkbox getLocalized;
  297.     Checkbox getLenientMode;
  298.  
  299.     //}}
  300.  
  301.     public void buildGUI()
  302.     {
  303.         //{{INIT_CONTROLS
  304.  
  305.         setBackground(Utility.bgColor);
  306.         setLayout(new FlowLayout()); // shouldn't be necessary, but it is.
  307.  
  308. // TITLE
  309.         Panel titlePanel = new Panel();
  310.  
  311.  
  312.         label1=new Label("Date/Time Formatting Demo", Label.CENTER);
  313.         label1.setFont(Utility.titleFont);
  314.  
  315.         titlePanel.add(label1);
  316.  
  317. // CREDITS
  318.  
  319.         Panel creditPanel = new Panel();
  320.  
  321.         demo=new Label(creditString, Label.CENTER);
  322.         demo.setFont(Utility.creditFont);
  323.         creditPanel.add(demo);
  324.  
  325.         titlePanel.add(creditPanel);
  326.  
  327.         Utility.fixGrid(titlePanel,1);
  328.          add(titlePanel);
  329.  
  330.  
  331. // IO Panel
  332.         Panel topPanel = new Panel();
  333.         topPanel.setLayout(new FlowLayout());
  334.  
  335.         label3=new Label("1.1 Date", Label.RIGHT);
  336.         label3.setFont(Utility.labelFont);
  337.         topPanel.add(label3);
  338.  
  339.         outputText=new TextField(FIELD_COLUMNS);
  340.         outputText.setFont(Utility.editFont);
  341.         topPanel.add(outputText);
  342.  
  343.  
  344.         label2=new Label("1.0 Date", Label.RIGHT);
  345.         label2.setFont(Utility.labelFont);
  346.         topPanel.add(label2);
  347.  
  348. // intentional use of deprecated method Date.toGMTString
  349.         inputText=new TextField(new Date().toGMTString(),FIELD_COLUMNS);
  350.         inputText.setFont(Utility.editFont);
  351.         topPanel.add(inputText);
  352.  
  353.  
  354.         millisLabel=new Label("Millis", Label.RIGHT);
  355.         millisLabel.setFont(Utility.labelFont);
  356.         topPanel.add(millisLabel);
  357.  
  358.         millisText=new TextField(FIELD_COLUMNS);
  359.         millisText.setFont(Utility.editFont);
  360.         topPanel.add(millisText);
  361.  
  362.         label4=new Label("Pattern", Label.RIGHT);
  363.         label4.setFont(Utility.labelFont);
  364.         topPanel.add(label4);
  365.  
  366.         patternText=new TextField(FIELD_COLUMNS);
  367.         patternText.setFont(Utility.editFont);
  368.         topPanel.add(patternText);
  369.  
  370.         topPanel.add(new Label(" "));
  371.  
  372.         getLocalized=new Checkbox("Localized Pattern");
  373.         getLocalized.setFont(Utility.labelFont);
  374.  
  375.         getLenientMode=new Checkbox("Lenient Mode", null, lenientMode);
  376.         getLenientMode.setFont(Utility.labelFont);
  377.  
  378.         Panel checkBoxesPanel = new Panel();
  379.         checkBoxesPanel.setLayout(new GridLayout(1,2,40,0));
  380.         checkBoxesPanel.add(getLocalized);
  381.         checkBoxesPanel.add(getLenientMode);
  382.  
  383.         topPanel.add(checkBoxesPanel);
  384.  
  385.         Utility.fixGrid(topPanel,2);
  386.          add(topPanel);
  387.  
  388. // DATE
  389.  
  390.         Panel datePanel=new Panel();
  391.         datePanel.setLayout(new FlowLayout());
  392.  
  393.         group2= new CheckboxGroup();
  394.  
  395.         getRoll=new Checkbox("Roll",group2, true);
  396.         getAdd=new Checkbox("Add",group2, false);
  397.  
  398.         dateLabel=new Label("Date Fields");
  399.         dateLabel.setFont(Utility.labelFont);
  400.  
  401.         Panel upDown = new Panel();
  402.         upDown.setLayout(new GridLayout(2,1));
  403.  
  404.         // *** If the images are not found, we use the label.
  405.         up = new Button("^");
  406.         down = new Button("v");
  407.         up.setBackground(Utility.bgColor);
  408.         down.setBackground(Utility.bgColor);
  409.         upDown.add(up);
  410.         upDown.add(down);
  411.  
  412.  
  413.         Panel rollAddBoxes = new Panel();
  414.         rollAddBoxes.setLayout(new GridLayout(2,1));
  415.  
  416.         rollAddBoxes.add(getRoll);
  417.         rollAddBoxes.add(getAdd);
  418.  
  419.         Panel rollAddPanel = new Panel();
  420.         rollAddPanel.setLayout(new FlowLayout());
  421.         rollAddPanel.add(rollAddBoxes);
  422.         rollAddPanel.add(upDown);
  423.  
  424.         dateMenu= new Choice();
  425.         dateMenu.setBackground(Utility.choiceColor);
  426.         dateMenu.addItem( "Year");
  427.         dateMenu.addItem( "Month");
  428.         dateMenu.addItem( "Day of Month");
  429.         dateMenu.addItem( "Hour of Day");
  430.         dateMenu.addItem( "Minute");
  431.         dateMenu.addItem( "Second");
  432.         dateMenu.addItem( "Millisecond");
  433.  
  434.         Panel dateLM = new Panel();
  435.         dateLM.setLayout(new GridLayout(2,1));
  436.         dateLM.add(dateLabel);
  437.         dateLM.add(dateMenu);
  438.  
  439.         datePanel.add(dateLM);
  440.  
  441. // CITIES
  442.  
  443.         Panel citiesPanel=new Panel();
  444.         citiesPanel.setLayout(new FlowLayout());
  445.         Panel cityPanel=new Panel();
  446.         cityPanel.setLayout(new GridLayout(2,1));
  447.         cityMenu= new Choice();
  448.         cityMenu.setBackground(Utility.choiceColor);
  449.         cityMenu.addItem( "Paris");
  450.         cityMenu.addItem( "Frankfurt");
  451.         cityMenu.addItem( "London");
  452.         cityMenu.addItem( "Washington");
  453.         cityMenu.addItem( "Vancouver");
  454.         cityMenu.addItem( "Montreal");
  455.         cityMenu.addItem( "Tokyo");
  456.  
  457.         cityLabel=new Label("City");
  458.         cityLabel.setFont(Utility.labelFont);
  459.  
  460.         cityPanel.add(cityLabel);
  461.         cityPanel.add(cityMenu);
  462.         citiesPanel.add(cityPanel);
  463.  
  464.         Panel cityDatePanel = new Panel();
  465.         cityDatePanel.add(citiesPanel);
  466.         cityDatePanel.add(datePanel);
  467.         cityDatePanel.add(rollAddPanel);    // choices
  468.         Utility.fixGrid(cityDatePanel,1);
  469.          add(cityDatePanel);
  470.  
  471. // BORDER
  472.         // true means raised, false = depressed
  473.         BorderPanel borderPanel = new BorderPanel(BorderPanel.RAISED);
  474.         borderPanel.setBackground(Utility.bgColor);
  475.         borderPanel.setLayout(null);
  476.         borderPanel.setSize(8,150);
  477.          add(borderPanel);
  478.  
  479. // LOCALE
  480.  
  481.         // sets up localePanel
  482.         localePanel=new Panel();
  483.         localePanel.setLayout(new GridLayout(2,1));
  484.  
  485.         localeLabel=new Label("Locale");
  486.         localeLabel.setFont(Utility.labelFont);
  487.         localeMenu= new Choice();
  488.         localeMenu.setBackground(Utility.choiceColor);
  489.  
  490.         localePanel.add("loc",localeLabel);
  491.         localePanel.add(localeMenu);
  492.  
  493.         // sets up formatPanel
  494.         formatPanel=new Panel();
  495.  
  496.         group1= new CheckboxGroup();
  497.         getDateInstance=new Checkbox("Date Format",group1, false);
  498.         getTimeInstance=new Checkbox("Time Format",group1, false);
  499.         getDateTimeInstance=new Checkbox("Date and Time Format",group1, true);
  500.  
  501.         Panel formatButtons = new Panel();
  502.         formatButtons.setLayout(new GridLayout(3,1));
  503.  
  504.         formatButtons.add(getDateInstance);
  505.         formatButtons.add(getTimeInstance);
  506.         formatButtons.add(getDateTimeInstance);
  507.  
  508.         Panel dateStylePanel=new Panel();
  509.         dateStylePanel.setLayout(new GridLayout(2,1));
  510.         dateStyleLabel=new Label("Date Style");
  511.         dateStyleLabel.setFont(Utility.labelFont);
  512.         dateStyleMenu= new Choice();
  513.         dateStyleMenu.setBackground(Utility.choiceColor);
  514.         dateStyleMenu.addItem("Full");
  515.         dateStyleMenu.addItem("Long");
  516.         dateStyleMenu.addItem("Default");
  517.         dateStyleMenu.addItem("Short");
  518.         dateStyleMenu.select("Long");
  519.         dateStylePanel.add("loc",dateStyleLabel);
  520.         dateStylePanel.add(dateStyleMenu);
  521.  
  522.         Panel timeStylePanel=new Panel();
  523.         timeStylePanel.setLayout(new GridLayout(2,1));
  524.         timeStyleLabel=new Label("Time Style");
  525.         timeStyleLabel.setFont(Utility.labelFont);
  526.         timeStyleMenu= new Choice();
  527.         timeStyleMenu.setBackground(Utility.choiceColor);
  528.         timeStyleMenu.addItem("Full");
  529.         timeStyleMenu.addItem("Long");
  530.         timeStyleMenu.addItem("Default");
  531.         timeStyleMenu.addItem("Short");
  532.         timeStyleMenu.select("Long");
  533.         timeStylePanel.add("loc",timeStyleLabel);
  534.         timeStylePanel.add(timeStyleMenu);
  535.  
  536.         Panel dtStylePanel = new Panel();
  537.         dtStylePanel.setLayout(new GridLayout(1,2,20,0));
  538.         dtStylePanel.add(dateStylePanel);
  539.         dtStylePanel.add(timeStylePanel);
  540.  
  541.         formatPanel.add(formatButtons);
  542.         formatPanel.add(dtStylePanel);
  543.         Utility.fixGrid(formatPanel,1);
  544.  
  545.         Panel localesFormatPanel = new Panel();
  546.  
  547.         localesFormatPanel.add(localePanel);
  548.         localesFormatPanel.add(formatPanel);
  549.         Utility.fixGrid(localesFormatPanel,1);
  550.          add(localesFormatPanel);
  551.  
  552.         Panel copyrightPanel = new Panel();
  553.         addWithFont (copyrightPanel,new Label(Utility.copyright1, Label.LEFT),
  554.             Utility.creditFont);
  555.         addWithFont (copyrightPanel,new Label(Utility.copyright2, Label.LEFT),
  556.             Utility.creditFont);
  557.         Utility.fixGrid(copyrightPanel,1);
  558.          add(copyrightPanel);
  559.  
  560.         //}}
  561.  
  562.     }
  563.  
  564.     /**
  565.      * Handles "action events"
  566.      */
  567.     public void actionPerformed(ActionEvent evt)
  568.     {
  569.         if (evt.getSource() == up) {
  570.                 dateFieldChanged(true);
  571.         } else if (evt.getSource() == down) {
  572.                 dateFieldChanged(false);
  573.         }
  574.     }
  575.  
  576.  
  577.     /**
  578.      * Handles keyboard events for all text items
  579.      */
  580.     class MyKeyAdapter extends KeyAdapter {
  581.         public void keyReleased(KeyEvent evt) {
  582.             if (evt.getSource() == inputText)  {
  583.                 formatText();
  584.             } else if (evt.getSource() == patternText)  {
  585.                 patternTextChanged();
  586.             } else if (evt.getSource() == outputText)  {
  587.                 parseText();
  588.             } else if (evt.getSource() == millisText)  {
  589.                 millisChanged();
  590.             }
  591.         }
  592.     }
  593.  
  594.     /**
  595.      * Handles "item events"
  596.      */
  597.     public void itemStateChanged(ItemEvent evt)
  598.     {
  599.         if (evt.getSource() == getLocalized) {
  600.                 isLocalized = getLocalized.getState();
  601.                 handleNewFormat();
  602.         } else if (evt.getSource() == getLenientMode) {
  603.                 lenientMode = getLenientMode.getState();
  604.                 lenientModeChanged();
  605.     } else if (evt.getSource() == getRoll) {
  606.                 clickedGetRoll();
  607.         } else if (evt.getSource() == getAdd) {
  608.                 clickedGetAdd();
  609.         } else if (evt.getSource() == getDateInstance) {
  610.                 clickedGetDateFormat();
  611.         } else if (evt.getSource() == getTimeInstance) {
  612.                 clickedGetTimeFormat();
  613.         } else if (evt.getSource() == getDateTimeInstance) {
  614.                 clickedGetDateTimeFormat();
  615.         } else if (evt.getSource() == localeMenu) {
  616.                 selectedLocaleMenu();
  617.         } else if (evt.getSource() == dateStyleMenu) {
  618.                 selectedDateStyleMenu();
  619.         } else if (evt.getSource() == timeStyleMenu) {
  620.                 selectedTimeStyleMenu();
  621.         } else if (evt.getSource() == cityMenu) {
  622.                 cityChanged();
  623.         }
  624.     }
  625.  
  626.     /**
  627.      * Handles window events.
  628.      */
  629.     class MyWindowAdapter extends WindowAdapter {
  630.         public void windowClosing(WindowEvent e) {
  631.             setVisible(false);
  632.             dispose();
  633.  
  634.             if (applet != null) {
  635.                 applet.demoClosed();
  636.             } else
  637.                 System.exit(0);
  638.         }
  639.     }
  640.  
  641.     /**
  642.      * This function is called when users select a new time and/or date
  643.      * format pattern, or a new locale.
  644.      */
  645.     public void setFormatFromLocale(boolean localChanged) {
  646.         int localeIndex = localeMenu.getSelectedIndex();
  647.         int dateStyleIndex = dateStyleMenu.getSelectedIndex()
  648.                              + DateFormat.FULL;
  649.         int timeStyleIndex = timeStyleMenu.getSelectedIndex()
  650.                              + DateFormat.FULL;
  651.         if (localChanged)
  652.             // Find the locale corresponding to the selected menu item
  653.             curLocale = locales[localeIndex];
  654.  
  655.         if (getDateInstance.getState()) {
  656.             format =
  657.             (SimpleDateFormat) DateFormat.getDateInstance(dateStyleIndex,
  658.                                                         curLocale);
  659.         } else if (getTimeInstance.getState()) {
  660.             format =
  661.             (SimpleDateFormat) DateFormat.getTimeInstance(timeStyleIndex,
  662.                                                         curLocale);
  663.         } else {
  664.             format =
  665.             (SimpleDateFormat) DateFormat.getDateTimeInstance(dateStyleIndex,
  666.                                                             timeStyleIndex,
  667.                                                             curLocale);
  668.         }
  669.         if (!localChanged) {
  670.             //LAURA Make sure the time zone still matches the selected city
  671.             int index = cityMenu.getSelectedIndex();
  672.             TimeZone timeZone = TimeZone.getTimeZone(kZoneIDs[index]);
  673.             format.setTimeZone(timeZone);
  674.         }
  675.  
  676.         patternText.setText( format.toPattern() );
  677.         if (!localChanged)
  678.         {
  679.             // locale not changed, only pattern format is changed.
  680.             setMillisText();
  681.             millisFormat();
  682.         }
  683.         else // change to selecting a rep. city based on new locale selected
  684.         {
  685.             // Removed as per Mark's request on 6/3/97
  686.             // cityMenu.select(kAdjCityIndex[localeIndex]);
  687.             cityChanged();
  688.         }
  689.     }
  690.  
  691.     /**
  692.      * This function is called when users change the pattern text.
  693.      */
  694.     public void setFormatFromPattern() {
  695.         String timePattern = patternText.getText();
  696.         if( isLocalized ) {
  697.             format.applyLocalizedPattern(timePattern);
  698.         }
  699.         else {
  700.             format.applyPattern(timePattern);
  701.         }
  702.         millisFormat();
  703.         millisParse();
  704.     }
  705.  
  706.     private boolean add = false;
  707.  
  708.     /**
  709.      * This function is called when the "Roll" radio button is selected.
  710.      */
  711.     public  void clickedGetRoll() {
  712.         add=false;
  713.     }
  714.  
  715.     /**
  716.      * This function is called when the "Add" radio button is selected.
  717.      */
  718.     public void clickedGetAdd() {
  719.         add=true;
  720.     }
  721.  
  722.     /**
  723.      * This function is called when the "Date Format" radio button is selected.
  724.      */
  725.     public void clickedGetDateFormat() {
  726.         setFormatFromLocale(false);
  727.     }
  728.  
  729.     /**
  730.      * This function is called when the "Time Format" radio button is selected.
  731.      */
  732.     public void clickedGetTimeFormat() {
  733.         setFormatFromLocale(false);
  734.     }
  735.  
  736.     /**
  737.      * This function is called when the "Date and Time Format" radio button
  738.      * is selected.
  739.      */
  740.     public void clickedGetDateTimeFormat() {
  741.         setFormatFromLocale(false);
  742.     }
  743.  
  744.     /**
  745.      * This function is called when a new locale is selected.
  746.      */
  747.     public void selectedLocaleMenu() {
  748.         setFormatFromLocale(true);
  749.     }
  750.  
  751.     /**
  752.      * This function is called when a new Date (Format) Style is selected.
  753.      */
  754.     public void selectedDateStyleMenu() {
  755.         setFormatFromLocale(false);
  756.     }
  757.  
  758.     /**
  759.      * This function is called when a new Time (Format) Style is selected.
  760.      */
  761.     public void selectedTimeStyleMenu() {
  762.         setFormatFromLocale(false);
  763.     }
  764.  
  765.     /**
  766.      * Store the current time in milliseconds.
  767.      */
  768.     long time = System.currentTimeMillis();
  769.  
  770.     /**
  771.      * This function is called when it is necessary to parse the time
  772.      * string in the "1.0 Date" text field.
  773.      */
  774.     public void formatText() {
  775.         String leftString = inputText.getText();
  776.         if (leftString.length() == 0)
  777.         {
  778.             errorText("Error: no input to format!");
  779.             return;
  780.         }
  781.  
  782.         try {
  783. // intentional use of deprecated method Date.parse
  784.             time = Date.parse(leftString);
  785.         }
  786.         catch (Exception e) {
  787.             outputText.setText("ERROR");
  788.             errorText("Exception: Date.parse: "+leftString);
  789.             return;
  790.         }
  791.         setMillisText();
  792.         millisFormat();
  793.     }
  794.  
  795.     /**
  796.      * This function is called when it is necessary to parse the time
  797.      * string in the "1.1 Date" text field.
  798.      */
  799.     public void parseText() {
  800.         String rightString = outputText.getText();
  801.  
  802.         ParsePosition status = new ParsePosition(0);
  803.  
  804.         if (rightString.length() == 0)
  805.         {
  806.             errorText("Error: no input to parse!");
  807.             return;
  808.         }
  809.  
  810.         try {
  811.             time = format.parse(rightString, status).getTime();
  812.         }
  813.         catch (Exception e) {
  814.             inputText.setText("ERROR");
  815.             errorText("Exception: parse: "+rightString);
  816.             return;
  817.         }
  818.         setMillisText();
  819.         millisParse();
  820.  
  821.         int start = outputText.getSelectionStart();
  822.         int end = outputText.getSelectionEnd();
  823.  
  824.         millisFormat();
  825.  
  826.         outputText.select(start,end);
  827.     }
  828.  
  829.     /**
  830.      * This function is called when it is necessary to format the time
  831.      * in the "Millis" text field.
  832.      */
  833.     public void millisFormat() {
  834.         String out = "";
  835.         try {
  836.             out = format.format(new Date(time));
  837.         }
  838.         catch (Exception e) {
  839.             outputText.setText("ERROR");
  840.             errorText("Exception: format: "+time);
  841.             return;
  842.         }
  843.         outputText.setText( out );
  844.         errorText("Formatted...");
  845.     }
  846.  
  847.     /**
  848.      * This function is called when it is necessary to display the time
  849.      * value parsed using GMT string in the "1.0 Date" text field.
  850.      */
  851.     public void millisParse() {
  852.         String input = "";
  853.         try {
  854. // intentional use of deprecated method Date.toGMTString
  855.             input = new Date(time).toGMTString();
  856.             }
  857.         catch (Exception e) {
  858.             inputText.setText("ERROR");
  859.             errorText("Exception: in toGMTString: "+time);
  860.             return;
  861.         }
  862.         inputText.setText( input );
  863.         errorText("Parsed...");
  864.     }
  865.  
  866.     /**
  867.      * This function is called when the time value in the "Millis" text field
  868.      * is changed. The new time value will be formatted and displayed in both
  869.      * "1.1 Date" and "1.0 Date" text fields.
  870.      */
  871.     public void millisChanged() {
  872.         String millisString = millisText.getText();
  873.         try {
  874.             time = Long.parseLong(millisString);
  875.             }
  876.         catch (Exception e) {
  877.             errorText("Exception: Bad value for millis. Must be Long");
  878.             return;
  879.         }
  880.         millisFormat();
  881.         millisParse();
  882.         errorText("Millis changed...");
  883.     }
  884.  
  885.     /**
  886.      * This function is called when it is necessary to display the time
  887.      * value in milliseconds in the "Millis" text field.
  888.      */
  889.     public void setMillisText() {
  890.         millisText.setText(Long.toString(time));
  891.     }
  892.  
  893.     /**
  894.      * This function is called when users change the pattern text.
  895.      */
  896.     public void patternTextChanged() {
  897.         setFormatFromPattern();
  898.     }
  899.  
  900.     /**
  901.      * This function is called when users select a new representative city.
  902.      */
  903.     public void cityChanged() {
  904.         int index = cityMenu.getSelectedIndex();
  905.         /*
  906.         SimpleTimeZone timeZone = new SimpleTimeZone(kZoneOffsets[index],
  907.                                                      kZoneIDs[index]);
  908.         timeZone.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY,
  909.             2 * millisPerHour);
  910.         timeZone.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
  911.             2 * millisPerHour);
  912.             */
  913.         TimeZone timeZone = TimeZone.getTimeZone(kZoneIDs[index]);
  914.         format.setTimeZone(timeZone);
  915.  
  916.         millisFormat();
  917.         millisParse();
  918.     }
  919.  
  920.     private boolean addMode() {
  921.         return add;
  922.     }
  923.  
  924.     /**
  925.      * This function is called when users select a new time field
  926.      * to add or roll its value.
  927.      */
  928.     public void dateFieldChanged(boolean up) {
  929.         String d = dateMenu.getSelectedItem();
  930.         byte field = 0;
  931.  
  932.         if (d.equals("Year")) {
  933.             field = (byte) Calendar.YEAR;
  934.         } else
  935.         if (d.equals("Month")) {
  936.             field = (byte) Calendar.MONTH;
  937.         } else
  938.         if (d.equals("Day of Month")) {
  939.             field = (byte) Calendar.DATE;
  940.         } else
  941.         if (d.equals("Hour of Day")) {
  942.             field = (byte) Calendar.HOUR_OF_DAY;
  943.         } else
  944.         if (d.equals("Minute")) {
  945.             field = (byte) Calendar.MINUTE;
  946.         } else
  947.         if (d.equals("Second")) {
  948.             field = (byte) Calendar.SECOND;
  949.         } else
  950.         if (d.equals("Millisecond")) {
  951.             field = (byte) Calendar.MILLISECOND;
  952.         }
  953.  
  954.         format.getCalendar().setTime(new Date(time));
  955.     //        format.getCalendar().computeFields();
  956.  
  957.         if (up) {
  958.             if (addMode()) {
  959.                 format.getCalendar().add(field, 1);
  960.             } else {
  961.                 format.getCalendar().roll(field, true);
  962.             }
  963.         } else {
  964.             if (addMode()) {
  965.                 format.getCalendar().add(field, -1);
  966.             } else {
  967.                 format.getCalendar().roll(field, false);
  968.             }
  969.         }
  970.  
  971.         time = format.getCalendar().getTime().getTime();
  972.  
  973.         setMillisText();
  974.  
  975.         millisFormat();
  976.  
  977.         millisParse();
  978.  
  979.     }
  980.  
  981.     /**
  982.      * Print out the error message while debugging this program.
  983.      */
  984.     public void errorText(String s)
  985.     {
  986.         if (DEBUG)
  987.         {
  988.             System.out.println(s);
  989.         }
  990.     }
  991.  
  992. }
  993.  
  994.  
  995.