/*
 * A dialog box to allow the user to enter call forwarding
 * information, namely the user name and the host address  
 * the call should be forwarded to.
 *
 * Author: Janet H. Park
 * Date: August, 1998
 *
 */

import java.awt.*;

public class RegisterDialog extends Dialog
{
	Simphony main;

	public RegisterDialog(Simphony parent, boolean modal)
	{
		super(parent, modal);
		main = parent;

		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		GridBagLayout gridBagLayout;
		gridBagLayout = new GridBagLayout();
		setLayout(gridBagLayout);
		setVisible(false);
		setSize(insets().left + insets().right + 382,insets().top + insets().bottom + 157);
		panel1 = new java.awt.Panel();
		panel1.setLayout(null);
		panel1.setBounds(insets().left + 10,insets().top + 10,362,137);
		panel1.setForeground(new Color(-16777125));
		panel1.setBackground(new Color(-3545857));
		GridBagConstraints gbc;
		gbc = new GridBagConstraints();
		gbc.weightx = 100.0;
		gbc.weighty = 100.0;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(10,10,10,10);
		((GridBagLayout)getLayout()).setConstraints(panel1, gbc);
		add(panel1);
		buttonDone = new java.awt.Button();
		buttonDone.setActionCommand("button");
		buttonDone.setLabel("Done");
		buttonDone.setBounds(206,98,72,24);
		buttonDone.setBackground(new Color(-5984570));
		panel1.add(buttonDone);
		labelUid = new java.awt.Label("user ID:",Label.RIGHT);
		labelUid.setBounds(38,14,84,24);
		labelUid.setFont(new Font("Dialog", Font.PLAIN, 12));
		panel1.add(labelUid);
		textFieldUid = new java.awt.TextField();
		textFieldUid.setBounds(146,14,206,28);
		panel1.add(textFieldUid);
		textFieldAddr = new java.awt.TextField();
		textFieldAddr.setBounds(146,50,205,28);
		textFieldAddr.setBackground(new Color(-3545857));
		panel1.add(textFieldAddr);
		labelAddr = new java.awt.Label("redirect calls to:",Label.RIGHT);
		labelAddr.setBounds(2,38,118,36);
		panel1.add(labelAddr);
		buttonClear = new java.awt.Button();
		buttonClear.setActionCommand("button");
		buttonClear.setLabel("Clear");
		buttonClear.setBounds(98,98,72,24);
		buttonClear.setBackground(new Color(-5984569));
		panel1.add(buttonClear);
		setTitle("Register Call Forwarding");
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymAction lSymAction = new SymAction();
		buttonClear.addActionListener(lSymAction);
		buttonDone.addActionListener(lSymAction);
		//}}
	}
	
	public void addNotify()
	{
  	    // Record the size of the window prior to calling parents addNotify.
	    Dimension d = getSize();

		super.addNotify();

		if (fComponentsAdjusted)
			return;

		// Adjust components according to the insets
		setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
		Component components[] = getComponents();
		for (int i = 0; i < components.length; i++)
		{
			Point p = components[i].getLocation();
			p.translate(insets().left, insets().top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
	}

    // Used for addNotify check.
	boolean fComponentsAdjusted = false;


	public RegisterDialog (Simphony parent, String title, boolean modal)
	{
		this(parent, modal);
		setTitle(title);
	}

	public synchronized void show()
	{
		Rectangle bounds = getParent().bounds();
		Rectangle abounds = bounds();

		move(bounds.x + (bounds.width - abounds.width)/ 2,
			 bounds.y + (bounds.height - abounds.height)/2);

		super.show();
	}

	//{{DECLARE_CONTROLS
	java.awt.Panel panel1;
	java.awt.Button buttonDone;
	java.awt.Label labelUid;
	java.awt.TextField textFieldUid;
	java.awt.TextField textFieldAddr;
	java.awt.Label labelAddr;
	java.awt.Button buttonClear;
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == RegisterDialog.this)
				Dialog1_WindowClosing(event);
		}
	}
	
	void Dialog1_WindowClosing(java.awt.event.WindowEvent event)
	{
		hide();
	}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == buttonClear)
				buttonClear_Action(event);
			else if (object == buttonDone)
				buttonDone_Action(event);
		}
	}

	void buttonClear_Action(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
			 
		//{{CONNECTION
		// Clear the text for TextField
		textFieldUid.setText("");
		//}}
			 
		//{{CONNECTION
		// Clear the text for TextField
		textFieldAddr.setText("");
		//}}
	}

	void buttonDone_Action(java.awt.event.ActionEvent event)
	{
		String uid = textFieldUid.getText().trim();
		String fwdAddr = textFieldAddr.getText().trim();
	
		if ((uid.length() != 0) && (fwdAddr.length() != 0)) {
			main.setCallFwd(uid, fwdAddr);
		} else
			main.setCallFwdOff();
			 
		//{{CONNECTION
		// Hide the Dialog
		setVisible(false);
		//}}
	}
}
