package serial;

/*
	A basic extension of the java.awt.Frame class
 */

import java.awt.*;

public class DTMFKeyFrame extends Frame
{
    private SerialEvObserver obs=null; 
	
	public DTMFKeyFrame(SerialEvObserver obs){
	    this();
	    this.obs=obs;
	}
	
    public void addSerialEvObserver(SerialEvObserver obs){
	    this.obs=obs;
    };
	
	
	public DTMFKeyFrame()
	{
		// 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
		setLayout(null);
		setVisible(false);
		setSize(insets().left + insets().right + 188,insets().top + insets().bottom + 190);
		setBackground(new Color(12632256));
		button1 = new java.awt.Button();
		button1.setActionCommand("button");
		button1.setLabel("1");
		button1.setBounds(insets().left + 10,insets().top + 38,30,34);
		button1.setFont(new Font("Dialog", Font.BOLD, 20));
		button1.setBackground(new Color(12632256));
		add(button1);
		button2 = new java.awt.Button();
		button2.setActionCommand("button");
		button2.setLabel("2");
		button2.setBounds(insets().left + 46,insets().top + 38,30,34);
		button2.setFont(new Font("Dialog", Font.BOLD, 20));
		button2.setBackground(new Color(12632256));
		add(button2);
		button3 = new java.awt.Button();
		button3.setActionCommand("button");
		button3.setLabel("3");
		button3.setBounds(insets().left + 82,insets().top + 38,30,34);
		button3.setFont(new Font("Dialog", Font.BOLD, 20));
		button3.setBackground(new Color(12632256));
		add(button3);
		button4 = new java.awt.Button();
		button4.setActionCommand("button");
		button4.setLabel("4");
		button4.setBounds(insets().left + 10,insets().top + 74,30,34);
		button4.setFont(new Font("Dialog", Font.BOLD, 20));
		button4.setBackground(new Color(12632256));
		add(button4);
		button5 = new java.awt.Button();
		button5.setActionCommand("button");
		button5.setLabel("5");
		button5.setBounds(insets().left + 46,insets().top + 74,30,34);
		button5.setFont(new Font("Dialog", Font.BOLD, 20));
		button5.setBackground(new Color(12632256));
		add(button5);
		button6 = new java.awt.Button();
		button6.setActionCommand("button");
		button6.setLabel("6");
		button6.setBounds(insets().left + 82,insets().top + 74,30,34);
		button6.setFont(new Font("Dialog", Font.BOLD, 20));
		button6.setBackground(new Color(12632256));
		add(button6);
		button7 = new java.awt.Button();
		button7.setActionCommand("button");
		button7.setLabel("7");
		button7.setBounds(insets().left + 10,insets().top + 110,30,34);
		button7.setFont(new Font("Dialog", Font.BOLD, 20));
		button7.setBackground(new Color(12632256));
		add(button7);
		button8 = new java.awt.Button();
		button8.setActionCommand("button");
		button8.setLabel("8");
		button8.setBounds(insets().left + 46,insets().top + 110,30,34);
		button8.setFont(new Font("Dialog", Font.BOLD, 20));
		button8.setBackground(new Color(12632256));
		add(button8);
		button9 = new java.awt.Button();
		button9.setActionCommand("button");
		button9.setLabel("9");
		button9.setBounds(insets().left + 82,insets().top + 110,30,34);
		button9.setFont(new Font("Dialog", Font.BOLD, 20));
		button9.setBackground(new Color(12632256));
		add(button9);
		button10 = new java.awt.Button();
		button10.setActionCommand("button");
		button10.setLabel("*");
		button10.setBounds(insets().left + 10,insets().top + 146,30,34);
		button10.setFont(new Font("Dialog", Font.BOLD, 20));
		button10.setBackground(new Color(12632256));
		add(button10);
		button11 = new java.awt.Button();
		button11.setActionCommand("button");
		button11.setLabel("0");
		button11.setBounds(insets().left + 46,insets().top + 146,30,34);
		button11.setFont(new Font("Dialog", Font.BOLD, 20));
		button11.setBackground(new Color(12632256));
		add(button11);
		button12 = new java.awt.Button();
		button12.setActionCommand("button");
		button12.setLabel("#");
		button12.setBounds(insets().left + 82,insets().top + 146,30,34);
		button12.setFont(new Font("Dialog", Font.BOLD, 20));
		button12.setBackground(new Color(12632256));
		add(button12);
		button13 = new java.awt.Button();
		button13.setActionCommand("button");
		button13.setLabel("Ring");
		button13.setBounds(insets().left + 118,insets().top + 38,61,34);
		button13.setFont(new Font("Dialog", Font.BOLD, 20));
		button13.setBackground(new Color(12632256));
		add(button13);
		button14 = new java.awt.Button();
		button14.setActionCommand("button");
		button14.setLabel("Bye");
		button14.setBounds(insets().left + 118,insets().top + 74,60,34);
		button14.setFont(new Font("Dialog", Font.BOLD, 20));
		button14.setBackground(new Color(12632256));
		add(button14);
		button15 = new java.awt.Button();
		button15.setActionCommand("button");
		button15.setLabel("F1");
		button15.setBounds(insets().left + 118,insets().top + 110,61,34);
		button15.setFont(new Font("Dialog", Font.BOLD, 20));
		button15.setBackground(new Color(12632256));
		add(button15);
		button16 = new java.awt.Button();
		button16.setActionCommand("button");
		button16.setLabel("F2");
		button16.setBounds(insets().left + 118,insets().top + 146,60,34);
		button16.setFont(new Font("Dialog", Font.BOLD, 20));
		button16.setBackground(new Color(12632256));
		add(button16);
		label1 = new java.awt.Label("DTMF key pad",Label.CENTER);
		label1.setBounds(insets().left + 18,insets().top + 10,148,16);
		label1.setFont(new Font("Dialog", Font.BOLD, 12));
		add(label1);
		setTitle("Untitled");
		//}}

		//{{INIT_MENUS
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymAction lSymAction = new SymAction();
		button1.addActionListener(lSymAction);
		button2.addActionListener(lSymAction);
		button3.addActionListener(lSymAction);
		button4.addActionListener(lSymAction);
		button5.addActionListener(lSymAction);
		button6.addActionListener(lSymAction);
		button7.addActionListener(lSymAction);
		button8.addActionListener(lSymAction);
		button9.addActionListener(lSymAction);
		button10.addActionListener(lSymAction);
		button11.addActionListener(lSymAction);
		button12.addActionListener(lSymAction);
		button13.addActionListener(lSymAction);
		button14.addActionListener(lSymAction);
		button15.addActionListener(lSymAction);
		button16.addActionListener(lSymAction);
		//}}
	}

	public DTMFKeyFrame(String title)
	{
		this();
		setTitle(title);
	}

	public synchronized void show()
	{
		move(50, 50);
		super.show();
	}

	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;

	//{{DECLARE_CONTROLS
	java.awt.Button button1;
	java.awt.Button button2;
	java.awt.Button button3;
	java.awt.Button button4;
	java.awt.Button button5;
	java.awt.Button button6;
	java.awt.Button button7;
	java.awt.Button button8;
	java.awt.Button button9;
	java.awt.Button button10;
	java.awt.Button button11;
	java.awt.Button button12;
	java.awt.Button button13;
	java.awt.Button button14;
	java.awt.Button button15;
	java.awt.Button button16;
	java.awt.Label label1;
	//}}

	//{{DECLARE_MENUS
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == DTMFKeyFrame.this)
				Frame1_WindowClosing(event);
		}
	}
	
	void Frame1_WindowClosing(java.awt.event.WindowEvent event)
	{
		hide();		 // hide the Frame
	}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			sendChar(((Button)object).getLabel());
		}
	}

	
	public void sendChar(String key) {
		System.out.println(">>>>>>Received String "+key);
		
		int b;
		StringBuffer DTMFLine = new StringBuffer();
		
		if (key.length()==1) b=key.charAt(0); else b=0;
		System.out.print((char)b+":"+b);
	    
	    if ((b>='0')&(b<='9')|(b=='*')) { // DTMF TONE
	        SerialEv ev = new SerialDTMFEv((char)b);
	        if (obs!=null) obs.notify(ev);
	        DTMFLine.append((char)b);
	    }
		    
        if (b=='#') { // end selection DTMF TONE
           if (obs!=null) obs.notify(new SerialDTMFLineEv(DTMFLine.toString()));
           DTMFLine = new StringBuffer();
        }
		    
		if (key.length() > 1) { // we have a command
	        if (key.equals("Ring")) key = "RING ";
	        if (key.equals("Bye")) key = "NO ACTIVITY ";
	        SerialEv ev = new SerialLineEv(key);
	        if (obs!=null) obs.notify(ev);
		}
}  // sendChar
	
}
