001    package net.sf.tacos.components.dojo;
002    
003    import java.util.Collection;
004    import java.util.HashMap;
005    import java.util.Map;
006    
007    import org.apache.tapestry.IActionListener;
008    import org.apache.tapestry.IDirect;
009    import org.apache.tapestry.IMarkupWriter;
010    import org.apache.tapestry.IRequestCycle;
011    import org.apache.tapestry.IScript;
012    import org.apache.tapestry.Tapestry;
013    import org.apache.tapestry.TapestryUtils;
014    import org.apache.tapestry.dojo.AbstractWidget;
015    import org.apache.tapestry.engine.DirectServiceParameter;
016    import org.apache.tapestry.engine.IEngineService;
017    import org.apache.tapestry.engine.ILink;
018    import org.apache.tapestry.listener.ListenerInvoker;
019    
020    public abstract class DropTarget extends AbstractWidget implements IDirect{
021            /**
022             * Name of the {@link IRequestCycle} parameter whose value is the
023             * identifier of the dropped draggable source
024             * @see {@link IRequestCycle#getParameter(String)}
025             */
026            public static final String DRAG_SOURCE_PARAMETER = "dragSourceId";
027            
028            /**
029             * {@inheritDoc}
030             */
031            public void renderWidget(IMarkupWriter writer, IRequestCycle cycle) {
032            writer.begin(getTemplateTagName());
033            renderIdAttribute(writer, cycle);
034            renderInformalParameters(writer, cycle);
035            renderBody(writer, cycle);
036            writer.end();
037            
038            if (!cycle.isRewinding()) {
039                Map params = new HashMap();
040                params.put("component", this);
041                params.put("dragSource", getDragSource());
042                params.put("dragSourceElement", getDragSourceElement());
043                params.put("dragSourceTag", getDragSourceTag());
044                params.put("acceptedTypes", getAcceptedTypes());
045                params.put("insertDragSource", new Boolean(isInsertDragSource()));
046                params.put("onDrop", getOnDrop());
047                if (getListener()!=null)
048                    params.put("url", getLink().getURL());        
049    
050                getScript().execute(this, cycle, TapestryUtils.getPageRenderSupport(cycle, this), params);
051            }
052            }
053            
054            /**
055             * {@inheritDoc}
056             */
057            public void trigger(IRequestCycle cycle) {
058            IActionListener listener = getListener();
059            if (listener == null)
060                throw Tapestry.createRequiredParameterException(this, "listener");
061            
062            getListenerInvoker().invokeListener(listener, this, cycle);
063            }
064            
065            /**Returns a link*/
066            private ILink getLink(){
067                    DirectServiceParameter dsp = new DirectServiceParameter(this);
068                    return getEngine().getLink(isStateful(), dsp);
069            }
070            
071        /** Reference to js function - called when a drop event is generated.*/
072        public abstract String getOnDrop();        
073        /** List item source parameter */
074        public abstract Object getDragSource();
075        /** Drag source element */
076        public abstract String getDragSourceElement();
077        /** Drag source tag */
078        public abstract String getDragSourceTag();
079        /** Type of tags target accepts */
080        public abstract Collection getAcceptedTypes();
081        /** Whether or not to allow insertion of source element */
082        public abstract boolean isInsertDragSource();
083        /** Injected listener parameter, may be null. */
084        public abstract IActionListener getListener();
085        /** Injected script. */
086        public abstract IScript getScript();
087        /** Injected listener invoker. */
088        public abstract ListenerInvoker getListenerInvoker();
089        /** Injected engine service . */
090        public abstract IEngineService getEngine();
091    
092    }