KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > common > component > UIBreadcrumb


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.component;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import javax.faces.component.UICommand;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.el.ValueBinding;
27 import javax.faces.event.AbortProcessingException;
28 import javax.faces.event.ActionEvent;
29 import javax.faces.event.FacesEvent;
30
31 /**
32  * @author kevinr
33  */

34 public class UIBreadcrumb extends UICommand
35 {
36    // ------------------------------------------------------------------------------
37
// Construction
38

39    /**
40     * Default Constructor
41     */

42    public UIBreadcrumb()
43    {
44       setRendererType("org.alfresco.faces.BreadcrumbRenderer");
45    }
46
47
48    // ------------------------------------------------------------------------------
49
// Component implementation
50

51    /**
52     * @see javax.faces.component.UIComponent#getFamily()
53     */

54    public String JavaDoc getFamily()
55    {
56       return "org.alfresco.faces.Controls";
57    }
58    
59    /**
60     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
61     */

62    public void restoreState(FacesContext context, Object JavaDoc state)
63    {
64       Object JavaDoc values[] = (Object JavaDoc[])state;
65       // standard component attributes are restored by the super class
66
super.restoreState(context, values[0]);
67       this.separator = (String JavaDoc)values[1];
68       this.showRoot = (Boolean JavaDoc)values[2];
69    }
70    
71    /**
72     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
73     */

74    public Object JavaDoc saveState(FacesContext context)
75    {
76       Object JavaDoc values[] = new Object JavaDoc[3];
77       // standard component attributes are saved by the super class
78
values[0] = super.saveState(context);
79       values[1] = this.separator;
80       values[2] = this.showRoot;
81       return (values);
82    }
83    
84    /**
85     * @see javax.faces.component.UICommand#broadcast(javax.faces.event.FacesEvent)
86     */

87    public void broadcast(FacesEvent event) throws AbortProcessingException
88    {
89       if (event instanceof BreadcrumbEvent)
90       {
91          setSelectedPathIndex( ((BreadcrumbEvent)event).SelectedIndex );
92       }
93       
94       // default ActionEvent processing for a UICommand
95
super.broadcast(event);
96    }
97
98    /**
99     * Set the selected path index. This modifies the current path value.
100     *
101     * @return last selected path index
102     */

103    public void setSelectedPathIndex(int index)
104    {
105       // getValue() will return a List of IBreadcrumbHandler (see impl below)
106
List JavaDoc<IBreadcrumbHandler> elements = (List JavaDoc)getValue();
107       
108       if (elements.size() >= index)
109       {
110          // copy path elements up to the selected index to a new List
111
List JavaDoc<IBreadcrumbHandler> path = new ArrayList JavaDoc<IBreadcrumbHandler>(index + 1);
112          path.addAll(elements.subList(0, index + 1));
113          
114          // set the new List as our new path value
115
setValue(path);
116          
117          // call the app logic for the element handler and perform any required navigation
118
String JavaDoc outcome = path.get(index).navigationOutcome(this);
119          if (outcome != null)
120          {
121             String JavaDoc viewId = getFacesContext().getViewRoot().getViewId();
122             getFacesContext().getApplication().getNavigationHandler().handleNavigation(
123                   getFacesContext(), viewId, outcome);
124          }
125       }
126    }
127    
128    /**
129     * Override getValue() to deal with converting a String path into a valid List of IBreadcrumbHandler
130     */

131    public Object JavaDoc getValue()
132    {
133       List JavaDoc<IBreadcrumbHandler> elements = null;
134       
135       Object JavaDoc value = super.getValue();
136       if (value instanceof String JavaDoc)
137       {
138          elements = new ArrayList JavaDoc(8);
139          // found a String based path - convert to List of IBreadcrumbHandler instances
140
StringTokenizer JavaDoc t = new StringTokenizer JavaDoc((String JavaDoc)value, SEPARATOR);
141          while (t.hasMoreTokens() == true)
142          {
143             IBreadcrumbHandler handler = new DefaultPathHandler(t.nextToken());
144             elements.add(handler);
145          }
146          
147          // save result so we don't need to repeat the conversion
148
setValue(elements);
149       }
150       else if (value instanceof List JavaDoc)
151       {
152          elements = (List JavaDoc)value;
153       }
154       else if (value != null)
155       {
156          throw new IllegalArgumentException JavaDoc("UIBreadcrumb value must be a String path or List of IBreadcrumbHandler!");
157       }
158       else
159       {
160          elements = new ArrayList JavaDoc(8);
161       }
162       
163       return elements;
164    }
165    
166    /**
167     * Append a handler object to the current breadcrumb structure
168     *
169     * @param handler The IBreadcrumbHandler to append
170     */

171    public void appendHandler(IBreadcrumbHandler handler)
172    {
173       if (handler == null)
174       {
175          throw new NullPointerException JavaDoc("IBreadcrumbHandler instance cannot be null!");
176       }
177       
178       List JavaDoc elements = (List JavaDoc)getValue();
179       elements.add(handler);
180    }
181    
182    
183    // ------------------------------------------------------------------------------
184
// Strongly typed component property accessors
185

186    /**
187     * Get the separator string to output between each breadcrumb element
188     *
189     * @return separator string
190     */

191    public String JavaDoc getSeparator()
192    {
193       ValueBinding vb = getValueBinding("separator");
194       if (vb != null)
195       {
196          this.separator = (String JavaDoc)vb.getValue(getFacesContext());
197       }
198       
199       return this.separator;
200    }
201    
202    /**
203     * Set separator
204     *
205     * @param separator the separator string to output between each breadcrumb element
206     */

207    public void setSeparator(String JavaDoc separator)
208    {
209       this.separator = separator;
210    }
211    
212    /**
213     * Get whether to show the root of the path
214     *
215     * @return true to show the root of the path, false to hide it
216     */

217    public boolean getShowRoot()
218    {
219       ValueBinding vb = getValueBinding("showRoot");
220       if (vb != null)
221       {
222          this.showRoot = (Boolean JavaDoc)vb.getValue(getFacesContext());
223       }
224       
225       if (this.showRoot != null)
226       {
227          return this.showRoot.booleanValue();
228       }
229       else
230       {
231          // return default
232
return true;
233       }
234    }
235    
236    /**
237     * Set whether to show the root of the path
238     *
239     * @param showRoot Whether to show the root of the path
240     */

241    public void setShowRoot(boolean showRoot)
242    {
243       this.showRoot = Boolean.valueOf(showRoot);
244    }
245    
246    
247    // ------------------------------------------------------------------------------
248
// Inner classes
249

250    /**
251     * Class representing the clicking of a breadcrumb element.
252     */

253    public static class BreadcrumbEvent extends ActionEvent
254    {
255       public BreadcrumbEvent(UIComponent component, int selectedIndex)
256       {
257          super(component);
258          SelectedIndex = selectedIndex;
259       }
260       
261       public int SelectedIndex = 0;
262    }
263    
264    /**
265     * Class representing a handler for the default String path based breadcrumb
266     */

267    private static class DefaultPathHandler implements IBreadcrumbHandler
268    {
269       /**
270        * Constructor
271        *
272        * @param label The element display label
273        */

274       public DefaultPathHandler(String JavaDoc label)
275       {
276          this.label = label;
277       }
278       
279       /**
280        * Return the element display label
281        */

282       public String JavaDoc toString()
283       {
284          return this.label;
285       }
286       
287       /**
288        * @see org.alfresco.web.ui.common.component.IBreadcrumbHandler#navigationOutcome(org.alfresco.web.ui.common.component.UIBreadcrumb)
289        */

290       public String JavaDoc navigationOutcome(UIBreadcrumb breadcrumb)
291       {
292          // no outcome for the default handler - return to current page
293
return null;
294       }
295       
296       private String JavaDoc label;
297    }
298    
299    
300    // ------------------------------------------------------------------------------
301
// Private data
302

303    /** visible separator value */
304    private String JavaDoc separator = null;
305    
306    /** true to show the root of the breadcrumb path, false otherwise */
307    private Boolean JavaDoc showRoot = null;
308    
309    /** the separator for a breadcrumb path value */
310    public final static String JavaDoc SEPARATOR = "/";
311 }
312
Popular Tags