KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > tags > menu > NavigationBarTag


1 package org.roller.presentation.tags.menu;
2
3 import java.io.StringWriter JavaDoc;
4
5 import javax.servlet.http.HttpServletRequest JavaDoc;
6 import javax.servlet.http.HttpServletResponse JavaDoc;
7 import javax.servlet.jsp.JspException JavaDoc;
8 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.apache.velocity.Template;
13 import org.apache.velocity.VelocityContext;
14 import org.apache.velocity.app.Velocity;
15 import org.roller.RollerException;
16 import org.roller.presentation.RollerContext;
17 import org.roller.presentation.RollerRequest;
18 import org.roller.presentation.velocity.ContextLoader;
19 import org.roller.presentation.velocity.PageModel;
20
21 /**
22  * Draws the most complete possible Roller navigation bar based on request
23  * parameters userName, folderId and authenticated user (if there is one).
24  *
25  * By supplying a "view" attribute, you can replace the default display
26  * with a custom implementation of the Navigation Bar. Implement by
27  * creating a new VM file and placing it in /WEB-INF/classes.
28  *
29  * @jsp.tag name="NavigationBar"
30  */

31 public class NavigationBarTag extends MenuTag
32 {
33     private static Log mLogger =
34         LogFactory.getFactory().getInstance(RollerRequest.class);
35         
36     private boolean mVertical = false;
37     private String JavaDoc mDelimiter = "|";
38
39     /** @jsp.attribute */
40     public boolean getVertical()
41     {
42         return mVertical;
43     }
44
45     public void setVertical(boolean v)
46     {
47         mVertical = v;
48     }
49
50     /** @jsp.attribute */
51     public String JavaDoc getDelimiter()
52     {
53         return mDelimiter;
54     }
55
56     public void setDelimiter(String JavaDoc v)
57     {
58         mDelimiter = v;
59     }
60
61    /**
62     * Replace the 'standard' NavigationBar display with a custom vm file.
63     *
64     * @jsp.attribute required="false"
65     */

66     public String JavaDoc getView() { return super.getView(); }
67     public void setView( String JavaDoc v )
68     {
69         super.setView(v);
70     }
71
72     /** Name of the model to be used.
73       * Must correspond to name of XML file in WEB-INF directory.
74       * @jsp.attribute required="false"
75       */

76     public String JavaDoc getModel() { return super.getModel(); }
77     public void setModel( String JavaDoc v ) { super.setModel(v); }
78     
79     //-------------------------------------------------------------
80
public String JavaDoc view(boolean isVertical)
81     {
82         mVertical = isVertical;
83
84         return emit();
85     }
86     
87     public void prepareContext( VelocityContext ctx )
88     {
89         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc)pageContext.getRequest();
90         HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc)pageContext.getResponse();
91
92         RollerRequest rreq = RollerRequest.getRollerRequest(req);
93         rreq.setPageContext(pageContext);
94         RollerContext rollerCtx = RollerContext.getRollerContext(req);
95         try
96         {
97             ContextLoader.setupContext( ctx, rreq, res );
98             PageModel pageModel = (PageModel)ctx.get("pageModel");
99             ctx.put("model", pageModel);
100             ctx.put("pages", pageModel.getPages());
101             ctx.put("req", req);
102             ctx.put("res", res);
103             ctx.put("vertical", Boolean.valueOf(getVertical()));
104             ctx.put("delimiter", getDelimiter());
105             ctx.put("editorui", Boolean.TRUE);
106         }
107         catch (Exception JavaDoc e)
108         {
109             // superclass says I can't throw an exception
110
mLogger.error(e);
111         }
112     }
113
114     //-------------------------------------------------------------
115

116     /**
117      * Evaluate any tags inside us. This will also allow us to have child tags
118      * send us messages.
119      * @return
120      * @throws JspException
121      */

122     public int doStartTag(java.io.PrintWriter JavaDoc pw)
123         throws JspException JavaDoc
124     {
125         return TagSupport.EVAL_BODY_INCLUDE;
126     }
127
128     /**
129      * @return
130      * @throws JspException
131      */

132     public int doEndTag(java.io.PrintWriter JavaDoc pw) throws JspException JavaDoc
133     {
134         try
135         {
136             // a special view VM has been defined
137
if (getView() != null)
138             {
139                 Template template = Velocity.getTemplate(
140                     getVelocityClasspathResource( getTemplateClasspath() ) );
141                 VelocityContext context = getVelocityContext();
142                 prepareContext( context );
143                 template.merge(context, pw);
144                 return EVAL_PAGE;
145             }
146             else
147             {
148                 //setView("/navbar.vm");
149
//String myResource= getVelocityClasspathResource(getTemplateClasspath());
150

151                 String JavaDoc myResource= getVelocityClasspathResource("/navbar.vm");
152
153                 VelocityContext myVelocityContext = getVelocityContext();
154
155                 // ask concrete class to prepare context
156
prepareContext( myVelocityContext );
157                 if (myVelocityContext.get("pageHelper") == null)
158                     throw new RollerException("Failure initializing ContextLoader.");
159
160                 StringWriter JavaDoc myStringWriter = new StringWriter JavaDoc();
161                 
162                 String JavaDoc[] vars = {"vertical", "delimiter" };
163                 Velocity.invokeVelocimacro("showNavBar", "NavigationBar", vars,
164                     myVelocityContext, myStringWriter);
165
166                 pw.println(myStringWriter);
167
168                 return EVAL_PAGE;
169
170             }
171         }
172         catch (Exception JavaDoc e)
173         {
174             mLogger.error("EditorNavigationBarTag exception",e);
175             throw new JspException JavaDoc(e);
176         }
177     }
178 }
Popular Tags