View Javadoc

1   package net.sf.tacos.components.scriptaculous;
2   
3   import java.util.Collection;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.apache.tapestry.AbstractComponent;
8   import org.apache.tapestry.IActionListener;
9   import org.apache.tapestry.IDirect;
10  import org.apache.tapestry.IMarkupWriter;
11  import org.apache.tapestry.IRequestCycle;
12  import org.apache.tapestry.IScript;
13  import org.apache.tapestry.Tapestry;
14  import org.apache.tapestry.TapestryUtils;
15  import org.apache.tapestry.engine.DirectServiceParameter;
16  import org.apache.tapestry.engine.IEngineService;
17  import org.apache.tapestry.engine.ILink;
18  import org.apache.tapestry.listener.ListenerInvoker;
19  
20  /**
21   * Implementation of the <a href="http://wiki.script.aculo.us/scriptaculous/show/Droppables">Droppables</a>.
22   * <p>
23   * This component makes a html element droppable. Droppables are elements which allow Draggables to be dropped on.
24   * When a draggable is released over a droppable, a listener method may be triggered to inform your application
25   * about the dropped element. 
26   * </p>
27   * 
28   * <p>Example:
29   * <pre>&lt;div jwcid="@tacos:Droppable" listener="listener:onDrop" hoverClass="literal:hoverCss" /&gt;</pre>
30   * </p>
31   * 
32   * @author Igor Drobiazko
33   * @since 4.1
34   * @see Draggable
35   */
36  public abstract class Droppable extends AbstractComponent implements IDirect{
37  	/**
38  	 * Name of the {@link IRequestCycle} parameter whose value is the
39  	 * identifier of the dropped draggable
40  	 * @see {@link IRequestCycle#getParameter(String)}
41  	 */
42  	public static final String DRAGGABLE_PARAMETER = "draggable";
43  	
44  	/**
45  	 * {@inheritDoc}
46  	 */
47  	protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {
48          writer.begin(getTemplateTagName());
49          renderIdAttribute(writer, cycle);
50          renderInformalParameters(writer, cycle);
51          renderBody(writer, cycle);
52          writer.end();
53          
54          Map params = new HashMap();
55          params.put("component", this);
56          params.put("accept", getAccept());
57          params.put("hoverClass", getHoverClass());
58          if(getListener()!=null)
59          	params.put("url", getLink().getURL());
60          
61          getScript().execute(this, cycle, TapestryUtils.getPageRenderSupport(cycle, this), params);
62  	}
63  	
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	public void trigger(IRequestCycle cycle) {
68          IActionListener listener = getListener();
69          if (listener == null)
70              throw Tapestry.createRequiredParameterException(this, "listener");
71          
72          getListenerInvoker().invokeListener(listener, this, cycle);
73  	}
74  	
75  	/**Returns a link*/
76  	private ILink getLink(){
77  		return getEngine().getLink(isStateful(), new DirectServiceParameter(this));
78  	}
79  
80      /** Injected listener parameter, may be null. */
81      public abstract IActionListener getListener();
82      /** Injected script. */
83      public abstract IScript getScript();
84      /** Injected listener invoker. */
85      public abstract ListenerInvoker getListenerInvoker();
86      /** Injected engine service . */
87      public abstract IEngineService getEngine();
88  	/**
89  	 * Returns {@link String[]} or {@link Collection} of strings describing CSS classes.
90  	 * The Droppable will only accept {@link Draggable} that have one or more of these CSS classes.
91  	 */
92      public abstract Collection getAccept();
93      /**
94       * Returns the CSS class this Droppable will have
95       * when an accepted {@link Draggable} is hovered over it.
96       */
97      public abstract String getHoverClass();
98  }