// Cisco Systems Confidential
// Copyright (c) 1999 by Cisco Systems, Inc.
// All rights reserved.

package com.cisco.ipphone.sdk.wfapi;

import java.awt.*;
//import java.beans.*;
import java.beans.PropertyChangeListener;
import symantec.itools.awt.TabPanel;
import symantec.itools.awt.ComboBox;
import java.awt.event.*;

import com.cisco.wfapi.*;
import com.cisco.wfapi.step.*;

import com.cisco.wfapi.expression.*;
import com.cisco.wfapi.util.MessageBox;
//import java.beans.*;
import java.beans.PropertyVetoException;

public class StepPhonePushCustomizer extends java.awt.Container
	     implements java.beans.Customizer,
					java.awt.event.WindowListener,
					ExtendedCustomizerUI
{
	private static final String helpTag = "PhonePush";

	private StepPhonePush theBean;

        //{{DECLARE_CONTROLS
        symantec.itools.awt.TabPanel Basic = new symantec.itools.awt.TabPanel();
        java.awt.Panel pushInfo = new java.awt.Panel();
        java.awt.Label xmlData = new java.awt.Label();
        java.awt.Label target = new java.awt.Label();
        java.awt.Label targetType = new java.awt.Label();
        symantec.itools.awt.ComboBox targetTypeCombo = new symantec.itools.awt.ComboBox();
        com.cisco.wfapi.expression.ExpressionPanel expressionPanelXmlData = new com.cisco.wfapi.expression.ExpressionPanel();
        com.cisco.wfapi.expression.ExpressionPanel expressionPanelTarget = new com.cisco.wfapi.expression.ExpressionPanel();
        //}}

	public StepPhonePushCustomizer()
	{
		//{{INIT_CONTROLS
		Insets ins = getInsets();
		try {
			{
				String[] tempString = new String[1];
				tempString[0] = "PUSH information";
				Basic.setPanelLabels(tempString);
			}
		}
		catch(java.beans.PropertyVetoException e) { }
		add(Basic);
		Basic.setBounds(12,0,528,468);
		pushInfo.setLayout(null);
		Basic.add(pushInfo);
		pushInfo.setBounds(12,33,504,424);

                xmlData.setText("XML Data:");
                pushInfo.add(xmlData);
                xmlData.setBounds(25,30,100,20);
		expressionPanelXmlData.setLayout(null);
		pushInfo.add(expressionPanelXmlData);
		expressionPanelXmlData.setBounds(150,30,250,50);

                targetType.setText("Target Type:");
                pushInfo.add(targetType);
                targetType.setBounds(25,130,100,20);
                try {
                  targetTypeCombo.addItem("DeviceName");
                  targetTypeCombo.addItem("Extension");
                  targetTypeCombo.select(StepPhonePush.TYPE_DEVICENAME);
                  targetTypeCombo.setBounds(150,130,250,100);
                }
                catch (PropertyVetoException ex) {
                }
                pushInfo.add(targetTypeCombo);

                target.setText("Target:");
                pushInfo.add(target);
                target.setBounds(25,230,100,20);
		expressionPanelTarget.setLayout(null);
		pushInfo.add(expressionPanelTarget);
		expressionPanelTarget.setBounds(150,230,250,50);

		try {
			Basic.setCurrentPanelNdx(0);
		}
		catch(java.beans.PropertyVetoException e) { }
		//}}

		//{{REGISTER_LISTENERS
		//}}
	}

    public void addPropertyChangeListener(PropertyChangeListener listener)
    {
        // This method is derived from interface java.beans.Customizer
        // to do: code goes here
    }

    public void removePropertyChangeListener(PropertyChangeListener listener)
    {
        // This method is derived from interface java.beans.Customizer
        // to do: code goes here
    }

    public void setObject(Object bean)
    {
        // This method is derived from interface java.beans.Customizer
        theBean = (StepPhonePush)bean;

        WFContextDefinition context = getContext();
        if(context == null) return;

		expressionPanelXmlData.setContext(context);
		Expression expr = new Expression();
		expr.setFormula(theBean.getXmlData());
		expressionPanelXmlData.setNewExpr(expr);

		expressionPanelTarget.setContext(context);
		expr = new Expression();
		expr.setFormula(theBean.getTarget());
		expressionPanelTarget.setNewExpr(expr);

                try {
                  targetTypeCombo.select(theBean.getTargetType());
                }
                catch (PropertyVetoException ex) {
                }

		try {
            	expressionPanelXmlData.setExpectedReturnType(Class.forName("java.lang.String"));
            	expressionPanelTarget.setExpectedReturnType(Class.forName("java.lang.String"));
		} catch (ClassNotFoundException e) {
        }
    }

    WFContextDefinition getContext()
    {
        WFWorkflow container = theBean.getContainer();
        if(container == null) return null;
        WFContextDefinition context = container.getContextDefinition();
        if(context == null) return null;
        return context;
    }

	// Must implement this for proper display of
	// the customizer
    public Dimension getPreferredSize()
    {
        Rectangle myRect = Basic.getBounds();
        return new Dimension(myRect.getSize());
    }

	// This is a place holder for integrating context sensitive help
	// The mechanism is not complete for this version of API
	public String getHelpTag()
	{
		// This method is derived from interface ExtendedCustomizerUI
	    return helpTag;
	}

	public boolean canClose()
	{
        // This method is derived from interface ExtendedCustomizerUI
	    if (!expressionPanelXmlData.valid())
		{
			MessageBox.display(this, "Invalid Field", "Invalid XML Data");
			return false;
		}

		theBean.setXmlData(expressionPanelXmlData.getFormula());

	    if (!expressionPanelTarget.valid())
		{
			MessageBox.display(this, "Invalid Field", "Invalid Target");
			return false;
		}

		theBean.setTarget(expressionPanelTarget.getFormula());
                theBean.setTargetType(targetTypeCombo.getSelectedIndex());

	    return true;
	}

	public void windowOpened(java.awt.event.WindowEvent e)
	{
		expressionPanelXmlData.setVisible(true);
		expressionPanelXmlData.setFocus();
	}

	public void windowClosed(WindowEvent e) {}
	public void windowClosing(WindowEvent e) {}
	public void windowActivated(WindowEvent e){}
	public void windowDeactivated(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}

    public void addNotify() {
        super.addNotify();
        Component cmp = getParent();
        if(cmp instanceof Dialog) {
            ((Dialog)cmp).addWindowListener(this);
        }
    }
}