KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > renderer > BreadcrumbRenderer


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.common.renderer;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26
27 import org.alfresco.web.ui.common.Utils;
28 import org.alfresco.web.ui.common.component.IBreadcrumbHandler;
29 import org.alfresco.web.ui.common.component.UIBreadcrumb;
30
31 /**
32  * Renderer class for the UIBreadcrumb component
33  *
34  * @author Kevin Roast
35  */

36 public class BreadcrumbRenderer extends BaseRenderer
37 {
38    // ------------------------------------------------------------------------------
39
// Renderer implementation
40

41    /**
42     * @see javax.faces.render.Renderer#decode(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
43     */

44    public void decode(FacesContext context, UIComponent component)
45    {
46       Map JavaDoc requestMap = context.getExternalContext().getRequestParameterMap();
47       String JavaDoc fieldId = getHiddenFieldName(context, component);
48       String JavaDoc value = (String JavaDoc)requestMap.get(fieldId);
49       if (value != null && value.length() != 0)
50       {
51          // create a breadcrumb specific action event if we were clicked
52
int selectedIndex = Integer.parseInt(value);
53          UIBreadcrumb.BreadcrumbEvent event = new UIBreadcrumb.BreadcrumbEvent(component, selectedIndex);
54          component.queueEvent(event);
55       }
56    }
57
58    /**
59     * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
60     */

61    public void encodeBegin(FacesContext context, UIComponent component) throws IOException JavaDoc
62    {
63       // always check for this flag - as per the spec
64
if (component.isRendered() == true)
65       {
66          Writer JavaDoc out = context.getResponseWriter();
67          
68          UIBreadcrumb breadcrumb = (UIBreadcrumb)component;
69          // get the List of IBreadcrumbHandler elements from the component
70
List JavaDoc<IBreadcrumbHandler> elements = (List JavaDoc)breadcrumb.getValue();
71          
72          boolean first = true;
73          for (int index=0; index<elements.size(); index++)
74          {
75             IBreadcrumbHandler element = elements.get(index);
76             
77             // handle not optionally hiding the root part
78
if (index != 0 || breadcrumb.getShowRoot() == true)
79             {
80                out.write( renderBreadcrumb(context, breadcrumb, element.toString(), index, first) );
81                first = false;
82             }
83          }
84       }
85    }
86    
87    
88    // ------------------------------------------------------------------------------
89
// Private helpers
90

91    /**
92     * Render a single breadcrumb element as a link on the page
93     *
94     * @param context FacesContext
95     * @param bc The current UIBreadcrumb component
96     * @param element Text for the breadcrumb element
97     * @param index The index of the element into the original breadcrumb path
98     * @param first True is this is the root element
99     *
100     * @return HTML for this breadcrumb element
101     */

102    private String JavaDoc renderBreadcrumb(FacesContext context, UIBreadcrumb bc, String JavaDoc element, int index, boolean first)
103    {
104       // render breadcrumb link element
105
StringBuilder JavaDoc buf = new StringBuilder JavaDoc(200);
106       
107       // output separator
108
if (first == false)
109       {
110          buf.append(' ')
111             .append(Utils.encode(bc.getSeparator()))
112             .append(' ');
113       }
114       
115       // generate JavaScript to set a hidden form field and submit
116
// a form which request attributes that we can decode
117
buf.append("<a HREF='#' onclick=\"");
118       buf.append(Utils.generateFormSubmit(context, bc, getHiddenFieldName(context, bc), Integer.toString(index)));
119       buf.append('"');
120       
121       if (bc.getAttributes().get("style") != null)
122       {
123          buf.append(" style=\"")
124             .append(bc.getAttributes().get("style"))
125             .append('"');
126       }
127       if (bc.getAttributes().get("styleClass") != null)
128       {
129          buf.append(" class=")
130             .append(bc.getAttributes().get("styleClass"));
131       }
132       if (bc.getAttributes().get("tooltip") != null)
133       {
134          buf.append(" title=\"")
135          .append(bc.getAttributes().get("tooltip"))
136          .append('"');
137       }
138       buf.append('>');
139       
140       // output path element text
141
// TODO: optionally crop text length with ellipses - use title attribute for all
142
buf.append(Utils.encode(element));
143       
144       // close tag
145
buf.append("</a>");
146       
147       return buf.toString();
148    }
149    
150    /**
151     * Get the hidden field name for this breadcrumb.
152     * Assume there will not be many breadcrumbs on a page - therefore a hidden field
153     * for each is not a significant issue.
154     *
155     * @return hidden field name
156     */

157    private static String JavaDoc getHiddenFieldName(FacesContext context, UIComponent component)
158    {
159       return component.getClientId(context);
160    }
161 }
162
Popular Tags