View Javadoc

1   package net.sf.tacos.components;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.apache.tapestry.AbstractComponent;
7   import org.apache.tapestry.IActionListener;
8   import org.apache.tapestry.IDirect;
9   import org.apache.tapestry.IMarkupWriter;
10  import org.apache.tapestry.IRequestCycle;
11  import org.apache.tapestry.IScript;
12  import org.apache.tapestry.Tapestry;
13  import org.apache.tapestry.TapestryUtils;
14  import org.apache.tapestry.engine.DirectServiceParameter;
15  import org.apache.tapestry.engine.IEngineService;
16  import org.apache.tapestry.engine.ILink;
17  import org.apache.tapestry.listener.ListenerInvoker;
18  
19  /**
20   * A component for creating a javascript function whose body
21   * is a call of an url definied by the direct service. This component is similar to
22   * <a href="http://tapestry.apache.org/tapestry4.1/components/link/directlink.html">DirectLink</a>
23   * component but allows you to call listener methods from javascript code without to click on any link or button.
24   * <p>The required parameters are: a listener method and the name of the function to generate.</p>
25   * <pre>
26   *&lt;span jwcid="@tacos:ClientLink" listener="listener:onClick" function="myFunction"/&gt;
27   *</pre>
28   *<p>The generated function can be used like this:</p>
29   *<pre>
30   *function test(){
31   *	...
32   *	myFunction();
33   *}
34   *</pre>
35   * 
36   * @author Igor Drobiazko
37   *
38   */
39  public abstract class ClientLink extends AbstractComponent implements IDirect{
40  	/**
41  	 * {@inheritDoc}
42  	 */
43  	protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {		
44          renderBody(writer, cycle);
45          
46          if (!cycle.isRewinding()) {
47              Map params = new HashMap();
48              params.put("url", getLink().getURL());        
49              params.put("function", getFunction());
50              getScript().execute(this, cycle, TapestryUtils.getPageRenderSupport(cycle, this), params);
51          }
52  	}
53  	
54  	/**
55  	 * {@inheritDoc}
56  	 */
57  	public void trigger(IRequestCycle cycle) {
58          IActionListener listener = getListener();
59          if (listener == null)
60              throw Tapestry.createRequiredParameterException(this, "listener");
61          
62          getListenerInvoker().invokeListener(listener, this, cycle);
63  	}
64  	
65  	/**Returns a link*/
66  	private ILink getLink(){
67  		DirectServiceParameter dsp = new DirectServiceParameter(this);
68  		return getEngine().getLink(isStateful(), dsp);
69  	}
70  
71      /** The required listener parameter. */
72      public abstract IActionListener getListener();
73      /** Name of the javascript function to generate. */
74      public abstract String getFunction();
75      
76      /** Injected script. */
77      public abstract IScript getScript();
78      /** Injected listener invoker. */
79      public abstract ListenerInvoker getListenerInvoker();
80      /** Injected engine service . */
81      public abstract IEngineService getEngine();
82  }