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

import  com.togethersoft.openapi.sci.SciObject;
import  com.togethersoft.openapi.util.propertyMap.PropertyMap;

import  java.util.Enumeration;
import  java.util.Vector;
import com.togethersoft.together.component.ide.license.IdeLicenseAccess;
import com.togethersoft.together.component.ide.license.IdeLicenseFeature;
/**
 * @author TogetherSoft
 */
public class CompositeSciTemplate extends AbstractSciTemplate {

        public CompositeSciTemplate(PropertyMap properties) {
                super(properties);
                this.templates = new Vector();
                }

        public boolean prepare(SciObject target) {
                if (!IdeLicenseAccess.getBooleanFeature(IdeLicenseFeature.HP_ESPEAK)) {
                 setErrorMessage("Cannot apply this pattern in this edition.");
                 return false;
                }
                for(Enumeration templates = templates(); templates.hasMoreElements();) {
                        SciTemplate template = (SciTemplate) templates.nextElement();
                        if (!template.prepare(target))
                                return false;
                        }
                return true;
                }

        public SciObject generate(SciObject target) {
                if (!canGenerate()) {
                        return null;
                        }

                SciObject result = target;

                for(Enumeration templates = templates(); templates.hasMoreElements();) {
                        SciTemplate template = (SciTemplate) templates.nextElement();
                        result = template.generate(result);
                        }

                return result;
                }

        public boolean canGenerate() {
                for(Enumeration templates = templates(); templates.hasMoreElements();) {
                        SciTemplate template = (SciTemplate) templates.nextElement();
                        if (!template.canGenerate()) {
                                return false;
                                }
                        }

                return true;
                }

        public Object addTemplate(SciTemplate template) {
                int id = -1;

                if (template != null) {
                        id = this.templates.size();
                        this.templates.addElement(template);
                        }

                return new Integer(id);
                }

        public SciTemplate removeTemplate(Object id) {
                SciTemplate template = null;

                if (getIntValue(id) >= 0) {
                        template = (SciTemplate) this.templates.elementAt(getIntValue(id));
                        this.templates.removeElementAt(getIntValue(id));
                        }

                return template;
                }

        public Enumeration templates() {
                return this.templates.elements();
                }

        protected int getIntValue(Object id) {
                if (id instanceof Integer) {
                        return ((Integer) id).intValue();
                        }

                return -1;
                }

        private Vector templates;
        }


