/*----------------------------------------------------------------------------
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;

import  com.togethersoft.openapi.ide.IdeLocalDiagnostic;

import  java.util.Enumeration;
import  java.util.Vector;

public class ConstraintsPage extends Page {

        public static final String PAGE_NUMBER = "__visibleWizardPage__";

        public ConstraintsPage(PropertyMap properties, String pagePrefix, int pageNumber) {
                super(properties, pagePrefix);
                this.pageNumber = new Integer(pageNumber);
                }

        public void init() {

                /*
                 * Create Page UI.
                 */

                setUIPropertyValue(THIS,    NULL_FIELD);
                setUIPropertyValue(VIEW_UI, TREE_TABLE_NODEUI);
                setUIPropertyValue(NAME,    "Vocabulary properties values");

                setUIPropertyValue(ITEM + ClientProperty.VOCABULARY_NAME,    STRING_FIELD);


                /*
                 * Set up UI behaviour.
                 */

                /*
                 * add listener to the page number !!!
                 */

                addPropertyMapListener(PAGE_NUMBER, new PropertyMapListener() {
                        public void propertiesChanged(PropertyMapEvent propertyMapEvent) {
                                IdeLocalDiagnostic.trace("page changed : " + getPropertyValue(PAGE_NUMBER));
                                if (getPropertyValue(PAGE_NUMBER).equals(ConstraintsPage.this.pageNumber)) {
                                        removeConstraints();
                                        addConstraints();
                                        }
                                }
                        });

                addPropertyMapListener(ClientProperty.SERVICE_DESCRIPTION_TYPE, new PropertyMapListener() {
                        public void propertiesChanged(PropertyMapEvent propertyMapEvent) {
                                if (ClientConstant.CUSTOM_DESCRIPTION.equals(getPropertyValue(ClientProperty.SERVICE_DESCRIPTION_TYPE))) {
                                        setUIPropertyValue(THIS + CONDITION, TRUE);
                                        }
                                else {
                                        setUIPropertyValue(THIS + CONDITION, FALSE);
                                        }
                                }
                        });

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

        private void removeConstraints() {
                for(int i = 0; i < this.constraints.size(); i++) {
                        String constraintName = (String) this.constraints.elementAt(i);
                        setUIPropertyValue(ITEM + "$$" + constraintName, null);
                        setPropertyValue("$$" + constraintName, null);
                        }

                this.constraints.removeAllElements();
                }

        private void addConstraints() {
                for(Enumeration constraints = propertyValues(ClientProperty.CUSTOM_CONSTRAINT); constraints.hasMoreElements();) {
                        String constraintName = (String) constraints.nextElement();
                        this.constraints.addElement(constraintName);
                        }

                for(int i = 0; i < this.constraints.size(); i++) {
                        String constraintName = (String) this.constraints.elementAt(i);
                        setUIPropertyValue(ITEM + "$$" + constraintName, STRING_FIELD);
                        setUIPropertyValue(ITEM + "$$" + constraintName + NAME, constraintName);
                        setPropertyValue("$$" + constraintName, constraintName.toLowerCase());
                        }
                }

        private Integer pageNumber = null;
        private Vector constraints = new Vector();
        }


