| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>3.2. Control Listener</title><link rel="stylesheet" href="css/stylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.75.0"><link rel="home" href="index.html" title="Apache Click"><link rel="up" href="ch03.html" title="Chapter 3. Controls"><link rel="prev" href="ch03.html" title="Chapter 3. Controls"><link rel="next" href="ch03s03.html" title="3.3. Control Classes"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3.2. Control Listener</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch03.html">Prev</a> </td><th width="60%" align="center">Chapter 3. Controls</th><td width="20%" align="right"> <a accesskey="n" href="ch03s03.html">Next</a></td></tr></table><hr></div><div class="sect1" title="3.2. Control Listener"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="control-listener"></a>3.2. Control Listener</h2></div></div></div><p>Click Controls provide an action listener mechanism similar to a |
| <code class="classname">java.awt.ActionListener</code>. |
| </p><p>Click supports two styles of action listeners. The first is using the |
| <a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/ActionListener.html" target="_blank">ActionListener</a> |
| interface and |
| <a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/control/AbstractControl.html#setActionListener(org.apache.click.ActionListener)" target="_blank">setActionListener(ActionListener)</a> |
| method which provides compile time safety. |
| </p><p>The second is to register the action listener via the |
| <a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/Control.html#setListener(java.lang.Object, java.lang.String)" target="_blank">setListener(Object, String)</a> |
| method where you specify the call back method via its name. This second style |
| uses less lines of code, but has no compile time safety. |
| </p><p>Examples of these two action listener styles are provided below: |
| </p><pre class="programlisting"><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">class</span> ActionDemo <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">extends</span> BorderPage { |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="com">// Uses listener style 1</span> |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> ActionLink link = <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">new</span> ActionLink(); |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="com">// Uses listener style 2</span> |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> ActionButton button = <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">new</span> ActionButton(); |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> ActionDemo() { |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="com">// Verbose but provides compile time safety</span> |
| link.setActionListener(<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">new</span> ActionListener() { |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">boolean</span> onAction(Control source) { |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">return</span> onLinkClick(source); |
| } |
| }); |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="com">// Succinct but typos will cause runtime errors</span> |
| button.setListener(<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">this</span>, <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="str">"onButtonClick"</span>); |
| } |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="com">// Event Handlers ---------------------------------------------------------</span> |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">boolean</span> onLinkClick(Control source) { |
| .. |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">return</span> true; |
| } |
| |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">boolean</span> onButtonClick() { |
| .. |
| <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">return</span> true; |
| } |
| }</pre><p>All call back listener methods must return a boolean value. If they |
| return true the further processing of other controls and page methods should |
| continue. Otherwise if they return false, then any further processing should |
| be aborted. By returning false you can effectively exit at this point and |
| redirect or forward to another page. This execution logic is illustrated in |
| the <a class="link" href="ch02s02.html#activity-diagram" title="Figure 2.3. Page Execution Activity Diagram">Page Execution Activity Diagram</a>. |
| </p><p>Being able to stop further processing and do something else can be very |
| handy. For example your Pages onRender() method may perform an expensive database |
| operation. By returning false in an event handler you can skip this step and |
| render the template or forward to the next page. |
| </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch03.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch03.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch03s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 3. Controls </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 3.3. Control Classes</td></tr></table></div></body></html> |