import helloWorld.*;

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import org.omg.CORBA.*;

public class Applet
    extends java.applet.Applet
    implements ActionListener {

    private ShortHolder minute;
    private ShortHolder hour;

    private GoodDay goodDay;
    private String text;
    private String locality;
    private Button helloWorldButton;
    private TextField textField;

    public void init() {

        minute = new ShortHolder();
        hour = new ShortHolder();

        helloWorldButton = new Button("Invoke remote method");
        helloWorldButton.setFont(new Font("Helvetica",
            Font.BOLD, 20));
        helloWorldButton.setActionCommand("invoke");
        helloWorldButton.addActionListener( (ActionListener) this );

        textField = new TextField();
        textField.setEditable(false);
	textField.setFont(new Font("Helvetica", Font.BOLD, 14));
        
        setLayout( new GridLayout(2,1));
        add( helloWorldButton );
        add( textField );

        try {
            // initialiaze the ORB (using this applet)
            ORB orb = ORB.init( this, null );

            // bind to GoodDay
            goodDay = GoodDayHelper.narrow( orb.string_to_object( readIOR() ));
        }

        // catch  exceptions
        catch(SystemException ex) {
            System.err.println(ex);
        }
    }

      public void actionPerformed( ActionEvent e ) {

         if( e.getActionCommand().equals("invoke") ) {

            // invoke the operation
            try {
                locality = new String( goodDay.hello( hour, minute ) );
            }
            // catch  exceptions
            catch(SystemException ex) {
                System.err.println(ex);
            }
            if( minute.value < 10 )
                text = new String("The local time in " + locality +
                    " is " + hour.value + ":0" + minute.value + "." );
            else
                text = new String("The local time in " + locality +
                    " is " + hour.value + ":" + minute.value + "." );
            textField.setText( text );
        }
    }

    private String readIOR()
    {
	try
	{
	    System.out.println("Trying to read from " +getCodeBase().toString()+"ior");
	    URL iorURL = new URL( getCodeBase().toString()+"ior");
	    String line;
	    BufferedReader in = new BufferedReader(new InputStreamReader(iorURL.openStream()) ); 
	    line = in.readLine();	    	
	    in.close();
	    return line;
	}
	catch( Exception ex )
	{
	    System.err.println(ex);
	}
	return null;
    }

}

