/*----------------------------------------------------------------------------
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.openapi.util.propertyMap.PropertyMap;
import  com.togethersoft.openapi.util.propertyMap.PropertyMapListener;
import  com.togethersoft.openapi.util.propertyMap.Property;

import  com.togethersoft.openapi.sci.*;
import  com.togethersoft.openapi.sci.pattern.SciPatternProperty;

import  java.util.Enumeration;

public abstract class Page {

        private static final char   DOT = '.';
        private static final String DOT_STRING = ".";

        public static final String THIS               = "";

        public static final String FALSE              = "false";
        public static final String TRUE               = "true";
        public static final String ALL                = "all";
        public static final String NONE               = "none";
        public static final String TOP                = "top";
        public static final String BOTTOM             = "bottom";

        public static final String WIZARD             = "wizard";
        public static final String VIEW_UI            = ".view.UI";
        public static final String EXPAND_LEVEL       = ".view.expandLevel";
        public static final String ITEM               = ".item.";
        public static final String NAME               = ".name";
        public static final String CONDITION          = ".condition";
        public static final String ENABLED            = ".enabled";
        public static final String MULTIPLE           = ".multiple";

        public static final String TREE_TABLE_NODEUI  = "treeTableNodeUI";
        public static final String CARD_NODEUI        = "cardNodeUI";
        public static final String STACK_NODEUI       = "stackNodeUI";

        public static final String NULL_FIELD         = "NullField";
        public static final String STRING_FIELD       = "StringField";
        public static final String EXTENDED_STRING_FIELD = "ExtendedStringField";
        public static final String BOOLEAN_FIELD      = "BooleanField";
        public static final String FILENAME_FIELD     = "FileNameField";
        public static final String INT_FIELD          = "IntField";


        protected Page(PropertyMap properties, String pagePrefix) {
                this.prefix = pagePrefix;
                this.properties = properties;

                init();
                }

        protected abstract void init();

        protected void setUIPropertyValue(String key, Object value) {
                String usedKey = key;
                if (!usedKey.equals(THIS)) {
                        if (usedKey.startsWith(DOT_STRING)) {
                                usedKey = usedKey.substring(DOT_STRING.length());
                                }

                        usedKey = getPrefix() + DOT + usedKey;
                        }
                else {
                        usedKey = getPrefix();
                        }

                getProperties().setPropertyValue(usedKey, value);
                }

        protected void setPropertyValue(String key, Object value) {
                getProperties().setPropertyValue(key, value);
                }

        protected void addPropertyValue(String key, Object value) {
                getProperties().addProperty(key, value);
                }

        protected Object getPropertyValue(String key) {
                return getProperties().getPropertyValue(key);
                }

        protected Enumeration propertyValues(final String key) {
                return new Enumeration() {
                        public boolean hasMoreElements() {
                                return this.properties.hasMoreElements();
                                }

                        public Object nextElement() {
                                return ((Property) this.properties.nextElement()).getValue();
                                }

                        private Enumeration properties = getProperties().properties(key);
                        };
                }

        protected String getStringPropertyValue(String key) {
                Object value = getPropertyValue(key);
                if (value instanceof String) {
                        return (String) value;
                        }
                if (value != null) {
                        return value.toString();
                        }

                return null;
                }

        protected void addPropertyMapListener(String key, PropertyMapListener listener) {
                getProperties().addPropertyMapListener(key, listener);
                }

        protected PropertyMap getProperties() {
                return this.properties;
                }

        protected String getPrefix() {
                return this.prefix;
                }

        protected SciPackage getContainingPackage() {
                SciContainer container = (SciContainer) getProperties().getPropertyValue(SciPatternProperty.CONTAINER);
                if (container instanceof SciPackage) {
                        return (SciPackage) container;
                        }

                return null;
                }

        private PropertyMap properties;
        private String     prefix;
        }


