001    package net.sf.tacos.components;
002    
003    import java.util.HashMap;
004    import java.util.Map;
005    
006    import org.apache.tapestry.IMarkupWriter;
007    import org.apache.tapestry.IRequestCycle;
008    import org.apache.tapestry.IScript;
009    import org.apache.tapestry.TapestryUtils;
010    import org.apache.tapestry.AbstractComponent;
011    import org.apache.tapestry.IActionListener;
012    import org.apache.tapestry.IForm;
013    import org.apache.tapestry.IComponent;
014    import org.apache.tapestry.pageload.IComponentVisitor;
015    import org.apache.tapestry.internal.event.EventBoundListener;
016    import org.apache.tapestry.internal.event.IComponentEventInvoker;
017    import org.apache.tapestry.internal.Component;
018    import org.apache.tapestry.form.IFormComponent;
019    
020    /**
021     * A component that acts like the EventListener annotation.
022     *
023     * @author Andreas Andreou
024     */
025    public abstract class EventListener extends AbstractComponent {
026    
027    
028        /**
029         * Whether or not to perform form validation.
030         * Default is false.
031         *
032         * @return Whether or not to validate the form.
033         */
034        public abstract boolean isValidateForm();
035    
036        /**
037         * Will either submit the form normally or asynchronously. Default is asyncrhonous.
038         *
039         * @return True if form should be submitted asynchronously, false otherwise.
040         */
041        public abstract boolean isAsync();
042    
043        /**
044         * The list of events that should cause this listener to invoke. Ie
045         * <code>events = {"onClick", "onOptionSelect"}</code> etc..
046         */
047        public abstract String[] getEvents();
048    
049        /**
050         * The unique {@link org.apache.tapestry.IComponent} ids of the targeted sources that this listener will be
051         * listening to events on.
052         */
053        public abstract String[] getTargets();
054    
055        /** The required listener parameter. */
056        public abstract IActionListener getListener();
057    
058        /** The required listener parameter. */
059        public abstract AbstractComponent getComponent();
060    
061        /** Injected. */
062        public abstract IScript getScript();
063    
064        public abstract IComponentEventInvoker getEventInvoker();
065        public abstract IComponentVisitor getEventConnectionVisitor();
066    
067        /**
068         * {@inheritDoc}
069         */
070            protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {
071            if (cycle.isRewinding())
072                return;
073            AbstractComponent comp = getComponent();
074            comp.setHasEvents(true);
075            String peekId = comp.peekClientId();
076            System.out.println(peekId);
077            getContainer().getSpecification().addEventListener(
078                    peekId,//comp.getId(),
079                    getEvents(),
080                    getListener().getMethodName(),
081                    getForm().getClientId(),
082                    isValidateForm(),
083                    isAsync(),
084                    false,
085                    true);
086    
087            getEventConnectionVisitor().visitComponent(getContainer());
088            /*String idPath = comp.getExtendedId();
089    
090            getContainer().getSpecification().rewireComponentId(compId, idPath);
091            getEventInvoker().addEventListener();*/
092    
093            /*Map parms = new HashMap();
094            parms.put("component", this);
095    
096            getScript().execute(this, cycle, TapestryUtils.getPageRenderSupport(cycle, this), parms);*/
097    
098            }
099    
100        /**
101         * Finds the form that encloses this component.
102         * @return
103         */
104        public IForm getForm() {
105            return TapestryUtils.getForm(getPage().getRequestCycle(), this);
106        }    
107    
108    }