import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;

public class JIBDateGUI extends    JFrame
                        implements ActionListener,
                                   WindowListener
{
  boolean bToggleFlag = true;

  DateFormat dfLocal,
             dfSelected;

  java.sql.Date jsqlDate = new java.sql.Date( 
     System.currentTimeMillis() );

  Font fLucida;

  int iFormat, 
      iSelIndex = 0;

  JButton    jbOK  = new JButton( "OK" ),
             jbToggle = new JButton( 
               "Toggle Display Names" );
  JComboBox  jcb = null;
  JPanel     jpNorth  = new JPanel(),
             jpCenter = new JPanel(),
             jpSouth  = new JPanel();
  JTextField jtI = new JTextField( 10 ),
             jtD = new JTextField( 10 ),
             jtP = new JTextField( 10 );

  Locale     lDefault = Locale.getDefault();
  Locale[]   alSupported;
  
  String[]   asDNames,
             asLDNames;


  public JIBDateGUI()
  {
    this( DateFormat.SHORT );
  } // end default constructor


  public JIBDateGUI( int argiFormat )
  {
    String s1 = null;

    setTitle( "JIBDateGUI" );
    addWindowListener( this );
        
    Font fJCB = jbToggle.getFont();
    fLucida = new Font("Lucida Sans", 
                        fJCB.getStyle(),
                        fJCB.getSize() );

    iFormat = argiFormat;
    dfLocal = DateFormat.getDateInstance( 
     iFormat );
    
    alSupported = Locale.getAvailableLocales();
    asDNames = new String[ alSupported.length ];
    asLDNames = new String[ alSupported.length ];
    for( int i = 0; i < alSupported.length; i++ )
    {
      asDNames[i] =
         alSupported[i].getDisplayName();

      s1 =
         alSupported[i].getDisplayName( alSupported[i] );
      if( fLucida.canDisplay( s1.charAt( 0 ) ) )
      { asLDNames[i] = s1; }
      else
      { asLDNames[i] = s1 + " - font can't display."; }

      if( iSelIndex == 0 && 
          lDefault.equals( alSupported[i] ) )
      { iSelIndex = i; }
    } // end for
    
    toggleDisplayNames();
    jtI.setText( dfLocal.format( jsqlDate ) );
    // cause ActionPerformed event
    jcb.setSelectedIndex( iSelIndex );

    jbOK.addActionListener( this );
    jbToggle.addActionListener( this );

    jtD.setEditable( false );
    jtD.setFont( fLucida );
    jtP.setEditable( false );
    jtP.setFont( fLucida );

    dfLocal.setLenient( false );

    jpNorth.add(new JLabel("Input a Date:"));
    jpNorth.add(jtI);
    jpNorth.add(jbOK);
    JLabel jlTemp = new JLabel("Default = " + 
            lDefault.getDisplayName() );
    jlTemp.setFont( fLucida );
    jpNorth.add( jlTemp );
    jpCenter.add(new JLabel("Display:"));
    jpCenter.add(jtD);
    jpCenter.add(new JLabel("Parsed ISO:"));
    jpCenter.add(jtP);

    Container cp = getContentPane();
    cp.add( jpNorth, BorderLayout.NORTH );
    cp.add( jpCenter, BorderLayout.CENTER );
    cp.add( jpSouth, BorderLayout.SOUTH );
    pack();

    show();

  }  // end constructor


  public void toggleDisplayNames()
  {
    boolean bjcbExist = false;

    if( jcb != null )
    {
      jpSouth.remove( jcb );
      jpSouth.remove( jbToggle );
      iSelIndex = jcb.getSelectedIndex();
      bjcbExist = true;
    }

    if( bToggleFlag )
    { jcb = new JComboBox( asDNames ); }
    else
    { jcb = new JComboBox( asLDNames ); }
    
    bToggleFlag = !bToggleFlag;

    if( bjcbExist )
    { jcb.setSelectedIndex( iSelIndex ); }
    jcb.setFont( fLucida );
    jcb.addActionListener( this );
    jpSouth.add( jcb );
    jpSouth.add( jbToggle );

  }  // end toggleDisplayNames
  

  // ActionListener Implementation
  public void actionPerformed(ActionEvent ae)
  {
    Object oSource = ae.getSource();

    if( oSource == jbToggle )
    {
      toggleDisplayNames();
      pack();
      return;
    }

    if( oSource == jcb )
    {
      dfSelected = DateFormat.getDateInstance( 
         iFormat, 
         alSupported[ jcb.getSelectedIndex() ] ); 
    }  // end if jcb, continue on
    
    jtD.setText( "" );
    jtP.setText( "" );
    
    try
    {
      java.util.Date d = dfLocal.parse( 
         jtI.getText() );
      jtI.setText( dfLocal.format( d ) );
      jtI.setCaretPosition(0);
      jtD.setText( dfSelected.format( d ) );
      jtD.setCaretPosition(0);
      d = dfSelected.parse( jtD.getText() );
      // get new java.sql.Date
      jsqlDate = new java.sql.Date( d.getTime() );
      
      jtP.setText( jsqlDate.toString() );
    }
    catch( ParseException pe ) 
    { 
      JOptionPane.showMessageDialog( this,
      pe.getMessage(), "", JOptionPane.ERROR_MESSAGE);
    }

  }  // End actionPerformed


// Window Listener Implementation
  public void windowOpened(WindowEvent we) {}
  public void windowClosing(WindowEvent we)
  {
    dispose();
    System.exit(0);
  }
  public void windowClosed(WindowEvent we) {}
  public void windowIconified(WindowEvent we) {}
  public void windowDeiconified(WindowEvent we) {}
  public void windowActivated(WindowEvent we) {}
  public void windowDeactivated(WindowEvent we) {}
// End Window Listener Implementation


  public static void main(String[] args)
  {
    int i = DateFormat.SHORT;
    String s = null;
    if( args.length == 1 )
    {
      if( args[0].equals( "full" ) )
      { i = DateFormat.FULL; }
      else
      if( args[0].equals( "long" ) )
      { i = DateFormat.LONG; }
      else
      if( args[0].equals( "medium" ) )
      { i = DateFormat.MEDIUM; }
    }

    new JIBDateGUI( i );

  }  // end main

}  // end class JIBDateGUI

