KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.HttpSession JavaDoc;
16 import javax.servlet.jsp.JspException JavaDoc;
17 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
18
19 import org.apache.log4j.Logger;
20
21 import com.tonbeller.wcf.bookmarks.BookmarkManager;
22 import com.tonbeller.wcf.bookmarks.Bookmarkable;
23 import com.tonbeller.wcf.controller.RequestContext;
24
25 /**
26  * initializes the component during doEndTag()
27  *
28  * @author av
29  */

30 public abstract class ComponentTag extends TagSupport JavaDoc {
31   boolean objectCreated;
32   boolean visible = true;
33   boolean validate = false;
34   String JavaDoc role;
35
36   private static Logger logger = Logger.getLogger(ComponentTag.class);
37
38   /**
39    * creates the component. Called whenever the object does not exist in the
40    * session
41    * @return the object that will be placed into the session
42    */

43   protected abstract Component createComponent(RequestContext context) throws Exception JavaDoc;
44
45   /**
46    * Creates the component if it does not exsist in the session. If the
47    * component is created the body of the tag is evaluated, else its skipped.
48    */

49   public int doStartTag() throws JspException JavaDoc {
50     logger.info(id);
51     try {
52       objectCreated = false;
53       HttpSession JavaDoc session = pageContext.getSession();
54       if (session.getAttribute(getId()) == null) {
55         if (logger.isInfoEnabled())
56           logger.info("creating " + id);
57         Component comp = createComponent(RequestContext.instance());
58         session.setAttribute(getId(), comp);
59         objectCreated = true;
60         return EVAL_BODY_INCLUDE;
61       }
62       return SKIP_BODY;
63     } catch (Exception JavaDoc e) {
64       logger.error("trouble creating " + getId(), e);
65       throw new JspException JavaDoc(e);
66     }
67   }
68
69   /**
70    * returns the component
71    */

72   public Component getComponent() {
73     return (Component) pageContext.getSession().getAttribute(getId());
74   }
75
76   /**
77    * returns true if the previous call to doStartTag has created the object, so further
78    * initialization can be done.
79    */

80   public boolean isObjectCreated() {
81     return objectCreated;
82   }
83
84
85   public int doEndTag() throws JspException JavaDoc {
86     logger.info(id);
87     if (objectCreated) {
88       try {
89         Component comp = getComponent();
90         comp.initialize(RequestContext.instance());
91         comp.setVisible(visible);
92         if (comp instanceof ComponentSupport)
93           ((ComponentSupport)comp).setAutoValidate(validate);
94         if (comp instanceof RoleExprHolder)
95            ((RoleExprHolder) comp).setRoleExpr(role);
96         if (comp instanceof Bookmarkable)
97           BookmarkManager.instance(pageContext.getSession()).restoreAttributeState(id);
98       } catch (Exception JavaDoc e) {
99         logger.error("trouble initializing " + getId(), e);
100         throw new JspException JavaDoc(e.toString(), e);
101       }
102     }
103     return super.doEndTag();
104   }
105
106   /**
107    * @param b
108    */

109   public void setVisible(boolean b) {
110     visible = b;
111   }
112
113   /**
114    * @param role
115    */

116   public void setRole(String JavaDoc role) {
117     this.role = role;
118   }
119
120   /**
121    * @param validate The validate to set.
122    */

123   public void setValidate(boolean validate) {
124     this.validate = validate;
125   }
126 }
127
Popular Tags