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

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

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

import  java.util.Enumeration;
/**
 * @author TogetherSoft
 */
public class ConstraintsPage extends Page {

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

        public void init() {

                /*
                 * Create Page UI.
                 */

                setUIPropertyValue(THIS,    NULL_FIELD);
                setUIPropertyValue(VIEW_UI, TREE_TABLE_NODEUI);
                setUIPropertyValue(NAME,    "Service constraints values");


                /*
                 * Set up UI behaviour.
                 */

                addPropertyMapListener(ClientProperty.SERVICE_QUERY_TYPE, new PropertyMapListener() {
                        public void propertiesChanged(PropertyMapEvent propertyMapEvent) {
                                Object queryType = getProperties().getPropertyValue(ClientProperty.SERVICE_QUERY_TYPE);
                                if (ClientConstant.CUSTOM_QUERY.equals(queryType)) {
                                        setUIPropertyValue(THIS + CONDITION, TRUE);
                                        }
                                else {
                                        setUIPropertyValue(THIS + CONDITION, FALSE);
                                        }
                                }
                        });

                addPropertyMapListener(ClientProperty.CUSTOM_CONSTRAINT, new PropertyMapListener() {
                        public void propertiesChanged(PropertyMapEvent propertyMapEvent) {

                                if (previousFields != null) {
                                        while(this.previousFields.hasMoreElements()) {
                                                String item = (String) this.previousFields.nextElement();
                                                setUIPropertyValue(ITEM + item + CONDITION, FALSE);
                                                }
                                        }

                                this.previousFields = propertyValues(ClientProperty.CUSTOM_CONSTRAINT);

                                for(Enumeration constraints = propertyValues(ClientProperty.CUSTOM_CONSTRAINT); constraints.hasMoreElements();) {
                                        String item = (String) constraints.nextElement();
                                        setUIPropertyValue(ITEM + item, STRING_FIELD);
                                        setUIPropertyValue(ITEM + item + CONDITION, TRUE);
                                        setPropertyValue(item, item);
                                        }
                                }

                        private Enumeration previousFields = null;
                        });

                /*
                 * Set initial values.
                 */
                setUIPropertyValue(THIS + CONDITION, FALSE);
                }
        }


