/*----------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
----------------------------------------------------------------------------*/
package com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Service_Provider.ui;

import com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Service_Provider.ClientProperty;
import com.togethersoft.modules.patterns.JAVA.CLASS.HP_E_Speak.Service_Provider.ClientConstant;

import com.togethersoft.openapi.util.propertyMap.PropertyMap;
import com.togethersoft.openapi.util.propertyMap.PropertyMapEvent;
import com.togethersoft.openapi.util.propertyMap.PropertyMapListener;

public class ConnectionPage extends Page {

        private static final String CONNECTION_TYPE_FIELD =
        "ChoiceField( {"+
        " values := {\"" + ClientConstant.DEFAULT_CONNECTION + "\", \""+ ClientConstant.PARAMETRIZED_CONNECTION + "\", \""+ ClientConstant.HARDCODED_CONNECTION + "\"},"+
        " names  := {\"   to the local ES Core\", \"   using properties file\", \"   using hardcoded values\"}"+
        " } )";

        public ConnectionPage(PropertyMap properties, String pagePrefix) {
                super(properties, pagePrefix);
                }

        public void init() {

                /*
                 * Create Page UI.
                 */
                setUIPropertyValue(THIS,         NULL_FIELD);
                setUIPropertyValue(VIEW_UI,      TREE_TABLE_NODEUI);
                setUIPropertyValue(EXPAND_LEVEL, "2");

                setUIPropertyValue(ITEM + ClientProperty.CONNECTION_TYPE, CONNECTION_TYPE_FIELD);

                setUIPropertyValue(ITEM + ClientProperty.CONNECTION_TYPE + ITEM + ClientProperty.URL, STRING_FIELD);
                setUIPropertyValue(ITEM + ClientProperty.CONNECTION_TYPE + ITEM + ClientProperty.URL + ENABLED, FALSE);
                setUIPropertyValue(ITEM + ClientProperty.CONNECTION_TYPE + ITEM + ClientProperty.URL + ITEM + ClientProperty.PROTOCOL,   STRING_FIELD);
                setUIPropertyValue(ITEM + ClientProperty.CONNECTION_TYPE + ITEM + ClientProperty.URL + ITEM + ClientProperty.HOSTNAME,   STRING_FIELD);
                setUIPropertyValue(ITEM + ClientProperty.CONNECTION_TYPE + ITEM + ClientProperty.URL + ITEM + ClientProperty.PORTNUMBER, INT_FIELD);

                addPropertyMapListener(ClientProperty.CONNECTION_TYPE, new PropertyMapListener() {
                        public void propertiesChanged(PropertyMapEvent propertyMapEvent) {
                                Object connectionType = getProperties().getPropertyValue(ClientProperty.CONNECTION_TYPE);

                                if (ClientConstant.HARDCODED_CONNECTION.equals(connectionType)) {
                                        setUIPropertyValue(ITEM + ClientProperty.CONNECTION_TYPE + ITEM + ClientProperty.URL + CONDITION, TRUE);
                                        }
                                else {
                                        setUIPropertyValue(ITEM + ClientProperty.CONNECTION_TYPE + ITEM + ClientProperty.URL + CONDITION, FALSE);
                                        }
                                }
                        });

                addPropertyMapListener(ClientProperty.PROTOCOL, new PropertyMapListener() {
                        public void propertiesChanged(PropertyMapEvent propertyMapEvent) {
                                updateURL();
                                }
                        });

                addPropertyMapListener(ClientProperty.HOSTNAME, new PropertyMapListener() {
                        public void propertiesChanged(PropertyMapEvent propertyMapEvent) {
                                updateURL();
                                }
                        });

                addPropertyMapListener(ClientProperty.PORTNUMBER, new PropertyMapListener() {
                        public void propertiesChanged(PropertyMapEvent propertyMapEvent) {
                                updateURL();
                                }
                        });

                setPropertyValue(ClientProperty.CONNECTION_TYPE, ClientConstant.DEFAULT_CONNECTION);

                setPropertyValue(ClientProperty.PORTNUMBER, "12346");
                setPropertyValue(ClientProperty.HOSTNAME,   "localhost");
                setPropertyValue(ClientProperty.PROTOCOL,   "TCP");
                }

        private void updateURL() {
                if (getPropertyValue(ClientProperty.PROTOCOL)   == null) return;
                if (getPropertyValue(ClientProperty.HOSTNAME)   == null) return;
                if (getPropertyValue(ClientProperty.PORTNUMBER) == null) return;

                try {
                        Integer.parseInt(getStringPropertyValue(ClientProperty.PORTNUMBER));
                        }
                catch(NumberFormatException nfException) {
                        return;
                        }

                String url = getPropertyValue(ClientProperty.PROTOCOL) + ":" + getPropertyValue(ClientProperty.HOSTNAME) + ":" + getPropertyValue(ClientProperty.PORTNUMBER);
                setPropertyValue(ClientProperty.URL, url);
                }
        }


