KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > component > ComponentSupport


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.component;
14
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Locale JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.servlet.http.HttpSession JavaDoc;
21 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
22 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
23
24 import org.apache.log4j.Logger;
25
26 import com.tonbeller.wcf.bookmarks.Bookmarkable;
27 import com.tonbeller.wcf.controller.Controller;
28 import com.tonbeller.wcf.controller.Dispatcher;
29 import com.tonbeller.wcf.controller.DispatcherSupport;
30 import com.tonbeller.wcf.controller.RequestContext;
31
32 /**
33  * default implementation of a component
34  * @author av
35  */

36 public abstract class ComponentSupport
37   implements Component, Form, HttpSessionBindingListener JavaDoc, Visible, RoleExprHolder, Bookmarkable {
38   private static Logger logger = Logger.getLogger(ComponentSupport.class);
39   private String JavaDoc id;
40   private String JavaDoc roleExpr;
41
42   private Dispatcher dispatcher = new DispatcherSupport();
43   private Form form = new FormSupport();
44
45   private Locale JavaDoc locale;
46   private boolean visible = true;
47
48   // needed for deregistering
49
private Controller controller;
50   private Component parent;
51   
52   private boolean autoValidate;
53   
54   /**
55    * creates a component
56    * @param id the id for the session attribute of this component
57    */

58   public ComponentSupport(String JavaDoc id, Component parent) {
59     this.id = id;
60     this.parent = parent;
61   }
62
63   /**
64    * returns a <code>Dispatcher</code> for this component. The dispatcher is local
65    * to this component (i.e. the component may <code>clear()</code> it). The dispatcher
66    * is registered with the Controller and functional.
67    */

68   public Dispatcher getDispatcher() {
69     return dispatcher;
70   }
71
72
73   /**
74    * called once when the component is created. Sets the locale of this component.
75    * Called by the doEndTag of the ComponentTag, i.e. nested tags have been initialized.
76    * @see #destroy
77    * @see #valueBound
78    * @see ComponentTag#doEndTag
79    */

80   public void initialize(RequestContext context) throws Exception JavaDoc {
81     logger.info(id);
82     locale = context.getLocale();
83     controller = Controller.instance(context.getSession());
84     controller.addRequestListener(this);
85   }
86   
87   /**
88    * called on session timeout or when the component is not used any more
89    * @see #initialize
90    * @see #valueUnbound
91    */

92   public void destroy(HttpSession JavaDoc session) throws Exception JavaDoc {
93     logger.info(id);
94     dispatcher.clear();
95     if (controller == null)
96       throw new IllegalStateException JavaDoc("not initialized");
97     controller.removeRequestListener(this);
98     controller = null;
99   }
100
101   public void request(RequestContext context) throws Exception JavaDoc {
102     if (autoValidate)
103       validate(context);
104     dispatcher.request(context);
105   }
106   
107   /**
108    * empty
109    */

110   public void valueBound(HttpSessionBindingEvent JavaDoc e) {
111   }
112
113   /**
114    * calls destroy on the component
115    */

116   public void valueUnbound(HttpSessionBindingEvent JavaDoc ev) {
117     try {
118       destroy(ev.getSession());
119     } catch (Exception JavaDoc ex) {
120       ex.printStackTrace();
121       logger.error(id, ex);
122     }
123   }
124
125   /**
126    * @return the id of this component (name of the session attribute).
127    */

128   public String JavaDoc getId() {
129     return id;
130   }
131
132   /**
133    * true, if this component should be rendered
134    * @return boolean
135    */

136   public boolean isVisible() {
137     return visible;
138   }
139
140   /**
141    * true, if this component should be rendered
142    * @param b
143    */

144   public void setVisible(boolean b) {
145     visible = b;
146   }
147
148   /**
149    * returns the <code>Form</code> for this component. The <code>Form</code> is local to
150    * this component.
151    *
152    * @return Form
153    * @deprecated Component implements Form directly
154    */

155   public Form getForm() {
156     return form;
157   }
158
159   /**
160    * @param string
161    */

162   public void setId(String JavaDoc string) {
163     id = string;
164   }
165
166   /**
167    * @return
168    */

169   public Locale JavaDoc getLocale() {
170     return locale;
171   }
172
173   /**
174    * @return
175    */

176   public String JavaDoc getRoleExpr() {
177     return roleExpr;
178   }
179
180   /**
181    * @param roleExpr
182    */

183   public void setRoleExpr(String JavaDoc roleExpr) {
184     this.roleExpr = roleExpr;
185   }
186
187   /**
188    * returns a Map. Derived classe may use this map to store their properties.
189    * To avoid conflicts the name of an entry (key) should equal the property name (getter/setter).
190    * @return a Map that contains the visible boolean
191    */

192   public Object JavaDoc getBookmarkState(int levelOfDetail) {
193     Map JavaDoc map = new HashMap JavaDoc();
194     map.put("visible", new Boolean JavaDoc(isVisible()));
195     return map;
196   }
197
198   /**
199    * restores the visible attribute from the map
200    * @param state a map
201    */

202   public void setBookmarkState(Object JavaDoc state) {
203     if (!(state instanceof Map JavaDoc))
204       return;
205     Map JavaDoc map = (Map JavaDoc) state;
206     Boolean JavaDoc b = (Boolean JavaDoc) map.get("visible");
207     if (b != null)
208       setVisible(b.booleanValue());
209   }
210
211   /**
212    * @param listener
213    */

214   public void addFormListener(FormListener listener) {
215     form.addFormListener(listener);
216   }
217   /**
218    * @param listener
219    */

220   public void removeFormListener(FormListener listener) {
221     form.removeFormListener(listener);
222   }
223   /**
224    * @param context
225    */

226   public void revert(RequestContext context) {
227     form.revert(context);
228   }
229   /**
230    * @param context
231    * @return
232    */

233   public boolean validate(RequestContext context) {
234     return form.validate(context);
235   }
236   /**
237    * @return Returns the parent.
238    */

239   public Component getParent() {
240     return parent;
241   }
242   /**
243    * sets the parent. Must be called before initialize()!!
244    * @param parent The parent to set.
245    */

246   public void setParent(Component parent) {
247     this.parent = parent;
248   }
249
250   /**
251    * forwards to another page
252    *
253    * @param uri if uri starts with "/" it will be interpreted
254    * as context relative path (i.e. the context will be prepended),
255    * otherwise it will be interpreted as relative to the current
256    * page.
257    */

258   public void setNextView(String JavaDoc uri) {
259     controller.setNextView(uri);
260   }
261   
262   protected String JavaDoc getNextView() {
263     return controller.getNextView();
264   }
265   
266   /**
267    * @return Returns the autoValidate.
268    */

269   public boolean isAutoValidate() {
270     return autoValidate;
271   }
272   
273   /**
274    * @param autoValidate The autoValidate to set.
275    */

276   public void setAutoValidate(boolean autoValidate) {
277     this.autoValidate = autoValidate;
278   }
279   
280   public boolean isListeningTo(Map JavaDoc httpParams) {
281     List JavaDoc list = dispatcher.findMatchingListeners(httpParams);
282     return !list.isEmpty();
283   }
284 }
285
Popular Tags