KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > form > ButtonHandler


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.form;
14
15 import javax.servlet.http.HttpSession JavaDoc;
16
17 import org.apache.log4j.Logger;
18 import org.w3c.dom.Element JavaDoc;
19
20 import com.tonbeller.wcf.controller.RequestContext;
21 import com.tonbeller.wcf.controller.RequestListener;
22 import com.tonbeller.wcf.scroller.Scroller;
23 import com.tonbeller.wcf.ui.Button;
24
25 /**
26  * Default ButtonHandler.
27  *
28  * TODO should be unified with ActionReference Code!
29  *
30  * Supports the following attributes
31  * <dl>
32  * <dt><b>action="revert"</b><br><dd>
33  * calls revert() on the <code>Form</code>, i.e. formats
34  * the data from the bean for presentation.
35  *
36  * <dt><b>action="validate"</b><br><dd>
37  * calls validate() on the <code>Form</code>, i.e. parses
38  * user input and writes the values to the bean properties
39  *
40  * <dt><b>forward="/some/page.jsp"</b><br><dd>
41  * if validation succeeds forwards to another page. Paramers make sense here.
42  *
43  * <dt><b>hide="true"</b><br><dd>
44  * if validation succeeds hides the component
45  *
46  * <dt><b>successAttr="name-of-request-attribute"</b><br><dd>
47  * if validation succeeds a request attribute with value <code>true</code>
48  * is created.
49  *
50  * <dt><b>scroller="true"</b><br><dd>
51  * keeps the current browser window scroll position after the
52  * request is finished
53  * </dl>
54  * <p>
55  *
56  * @author av
57  */

58
59 public class ButtonHandler implements NodeHandler, RequestListener {
60
61   private XmlComponent comp;
62   private Element JavaDoc element;
63   private boolean pressed = false;
64   private boolean success = true;
65
66   public static final String JavaDoc NO_ACTION = "";
67   public static final String JavaDoc VALIDATE_ACTION = "validate";
68   public static final String JavaDoc REVERT_ACTION = "revert";
69   private static Logger logger = Logger.getLogger(ButtonHandler.class);
70
71
72   public void initialize(RequestContext context, XmlComponent comp, Element JavaDoc element) {
73     this.comp = comp;
74     this.element = element;
75     comp.getDispatcher().addRequestListener(Button.getId(element), null, this);
76   }
77   
78   public void destroy(HttpSession JavaDoc session) {
79   }
80   
81   /**
82    * calls revert/validate on the form and sets the next view
83    */

84   public void request(RequestContext context) throws Exception JavaDoc {
85     if(element.getAttribute("scroller")!=null)
86       Scroller.enableScroller(context);
87       
88     pressed = true;
89     success = true;
90     String JavaDoc action = element.getAttribute("action");
91     if (action.equals("revert"))
92       comp.revert(context);
93     else if (action.equals("validate"))
94       success = comp.validate(context);
95     if (success) {
96
97       boolean hide = "true".equals(element.getAttribute("hide"));
98       if (hide)
99         comp.setVisible(false);
100
101       String JavaDoc forward = element.getAttribute("forward");
102       if (forward != null && forward.length() > 0)
103         comp.setNextView(forward);
104
105       String JavaDoc successAttr = element.getAttribute("successAttr");
106       if (successAttr.length() > 0)
107         context.getRequest().setAttribute(successAttr, "true");
108     }
109   }
110
111   /** does nothing */
112   public void render(RequestContext context) throws Exception JavaDoc {
113   }
114
115 }
116
Popular Tags