KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > repo > component > shelf > UIShelf


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.repo.component.shelf;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.faces.component.NamingContainer;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.context.ResponseWriter;
27 import javax.faces.el.MethodBinding;
28 import javax.faces.el.ValueBinding;
29 import javax.faces.event.AbortProcessingException;
30 import javax.faces.event.ActionEvent;
31 import javax.faces.event.FacesEvent;
32
33 import org.alfresco.web.ui.common.PanelGenerator;
34 import org.alfresco.web.ui.common.Utils;
35 import org.alfresco.web.ui.common.WebResources;
36 import org.alfresco.web.ui.common.component.SelfRenderingComponent;
37
38 /**
39  * @author Kevin Roast
40  */

41 public class UIShelf extends SelfRenderingComponent
42 {
43    // ------------------------------------------------------------------------------
44
// Component Impl
45

46    /**
47     * @see javax.faces.component.UIComponent#getFamily()
48     */

49    public String JavaDoc getFamily()
50    {
51       return "org.alfresco.faces.Shelf";
52    }
53    
54    /**
55     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
56     */

57    public void restoreState(FacesContext context, Object JavaDoc state)
58    {
59       Object JavaDoc values[] = (Object JavaDoc[])state;
60       // standard component attributes are restored by the super class
61
super.restoreState(context, values[0]);
62       this.groupPanel = (String JavaDoc)values[1];
63       this.groupBgcolor = (String JavaDoc)values[2];
64       this.selectedGroupPanel = (String JavaDoc)values[3];
65       this.selectedGroupBgcolor = (String JavaDoc)values[4];
66       this.innerGroupPanel = (String JavaDoc)values[5];
67       this.innerGroupBgcolor = (String JavaDoc)values[6];
68       this.groupExpandedActionListener = (MethodBinding)values[7];
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[8];
77       // standard component attributes are saved by the super class
78
values[0] = super.saveState(context);
79       values[1] = this.groupPanel;
80       values[2] = this.groupBgcolor;
81       values[3] = this.selectedGroupPanel;
82       values[4] = this.selectedGroupBgcolor;
83       values[5] = this.innerGroupPanel;
84       values[6] = this.innerGroupBgcolor;
85       values[7] = this.groupExpandedActionListener;
86       return values;
87    }
88    
89    /**
90     * @param binding The MethodBinding to call when the Group expand action is performed by the user
91     */

92    public void setGroupExpandedActionListener(MethodBinding binding)
93    {
94       this.groupExpandedActionListener = binding;
95    }
96    
97    /**
98     * @return The MethodBinding to call for the Group expand action
99     */

100    public MethodBinding getGroupExpandedActionListener()
101    {
102       return this.groupExpandedActionListener;
103    }
104    
105    /**
106     * @see javax.faces.component.UIComponentBase#decode(javax.faces.context.FacesContext)
107     */

108    public void decode(FacesContext context)
109    {
110       Map JavaDoc requestMap = context.getExternalContext().getRequestParameterMap();
111       String JavaDoc fieldId = getHiddenFieldName();
112       String JavaDoc value = (String JavaDoc)requestMap.get(fieldId);
113       
114       // we encoded the value to start with our Id
115
if (value != null && value.length() != 0)
116       {
117          int sepIndex = value.indexOf(NamingContainer.SEPARATOR_CHAR);
118          int groupIndex = Integer.parseInt( value.substring(0, sepIndex) );
119          boolean expanded = Boolean.parseBoolean( value.substring(sepIndex + 1) );
120          
121          // fire an event here to indicate the change that occured
122
ShelfEvent event = new ShelfEvent(this, groupIndex, expanded);
123          this.queueEvent(event);
124       }
125    }
126    
127    /**
128     * @see javax.faces.component.UIComponentBase#broadcast(javax.faces.event.FacesEvent)
129     */

130    public void broadcast(FacesEvent event) throws AbortProcessingException
131    {
132       if (event instanceof ShelfEvent)
133       {
134          ShelfEvent shelfEvent = (ShelfEvent)event;
135          
136          // set the new expanded state of the appropriate shelf item
137
int index = 0;
138          for (Iterator JavaDoc i=this.getChildren().iterator(); i.hasNext(); index++)
139          {
140             UIComponent child = (UIComponent)i.next();
141             if (index == shelfEvent.Index && child instanceof UIShelfGroup)
142             {
143                // found correct child - set the new state
144
((UIShelfGroup)child).setExpanded(shelfEvent.Expanded);
145                break;
146             }
147          }
148          
149          // if an action event is registered to be notified then fire that next
150
if (getGroupExpandedActionListener() != null)
151          {
152             Utils.processActionMethod(getFacesContext(), getGroupExpandedActionListener(), shelfEvent);
153          }
154       }
155       else
156       {
157          super.broadcast(event);
158       }
159    }
160
161    /**
162     * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
163     */

164    public void encodeBegin(FacesContext context) throws IOException JavaDoc
165    {
166       if (isRendered() == false)
167       {
168          return;
169       }
170       
171       ResponseWriter out = context.getResponseWriter();
172       
173       // TODO: allow config of spacing between ShelfGroup components
174
out.write("<table border=0 cellspacing=2 cellpadding=0 width=100%>");
175    }
176    
177    /**
178     * @see javax.faces.component.UIComponentBase#encodeChildren(javax.faces.context.FacesContext)
179     */

180    public void encodeChildren(FacesContext context) throws IOException JavaDoc
181    {
182       if (isRendered() == false)
183       {
184          return;
185       }
186       
187       ResponseWriter out = context.getResponseWriter();
188       
189       // output each shelf group in turn
190
int index = 0;
191       for (Iterator JavaDoc i=this.getChildren().iterator(); i.hasNext(); index++)
192       {
193          UIComponent child = (UIComponent)i.next();
194          if (child instanceof UIShelfGroup)
195          {
196             UIShelfGroup group = (UIShelfGroup)child;
197             if (group.isRendered() == true)
198             {
199                // output the surrounding structure then call the component to render itself and children
200
boolean isExpanded = group.isExpanded(); // TODO: get this from Shelf or ShelfGroup?
201
out.write("<tr><td>");
202                
203                String JavaDoc contextPath = context.getExternalContext().getRequestContextPath();
204                
205                // output appropriate panel start section and bgcolor
206
String JavaDoc groupPanel;
207                String JavaDoc groupBgcolor;
208                if (isExpanded == false)
209                {
210                   groupPanel = getGroupPanel();
211                   groupBgcolor = getGroupBgcolor();
212                }
213                else
214                {
215                   groupPanel = getSelectedGroupPanel();
216                   groupBgcolor = getSelectedGroupBgcolor();
217                }
218                if (groupBgcolor == null)
219                {
220                   groupBgcolor = PanelGenerator.BGCOLOR_WHITE;
221                }
222                if (groupPanel != null)
223                {
224                   PanelGenerator.generatePanelStart(out, contextPath, groupPanel, groupBgcolor);
225                }
226                
227                // output appropriate expanded icon state
228
out.write("<div style='padding-top:2px;padding-bottom:4px'><nobr>");
229                out.write("<a HREF='#' onclick=\"");
230                // encode value as the index of the ShelfGroup clicked and the new state
231
String JavaDoc value = Integer.toString(index) + NamingContainer.SEPARATOR_CHAR + Boolean.toString(!isExpanded);
232                out.write(Utils.generateFormSubmit(context, this, getHiddenFieldName(), value));
233                out.write("\">");
234                if (isExpanded == true)
235                {
236                   out.write(Utils.buildImageTag(context, WebResources.IMAGE_EXPANDED, 11, 11, ""));
237                }
238                else
239                {
240                   out.write(Utils.buildImageTag(context, WebResources.IMAGE_COLLAPSED, 11, 11, ""));
241                }
242                out.write("</a>&nbsp;");
243                
244                // output title label text
245
String JavaDoc label = group.getLabel();
246                out.write("<span");
247                outputAttribute(out, group.getAttributes().get("style"), "style");
248                outputAttribute(out, group.getAttributes().get("styleClass"), "class");
249                out.write('>');
250                out.write(Utils.encode(label));
251                out.write("</span>");
252                out.write("</nobr></div>");
253                
254                if (isExpanded == true)
255                {
256                   // if this is the expanded group, output the inner panel
257
String JavaDoc innerGroupPanel = getInnerGroupPanel();
258                   String JavaDoc innerGroupBgcolor = getInnerGroupBgcolor();
259                   if (innerGroupBgcolor == null)
260                   {
261                      innerGroupBgcolor = PanelGenerator.BGCOLOR_WHITE;
262                   }
263                   if (innerGroupPanel != null)
264                   {
265                      PanelGenerator.generatePanelStart(out, contextPath, innerGroupPanel, innerGroupBgcolor);
266                   }
267                   
268                   // allow child components to render themselves
269
Utils.encodeRecursive(context, group);
270                   
271                   if (innerGroupPanel != null)
272                   {
273                      PanelGenerator.generatePanelEnd(out, contextPath, innerGroupPanel);
274                   }
275                }
276                
277                // output panel and group end elements
278
PanelGenerator.generatePanelEnd(out, contextPath, groupPanel);
279                out.write("</td></tr>");
280             }
281          }
282       }
283    }
284    
285    /**
286     * @see javax.faces.component.UIComponentBase#encodeEnd(javax.faces.context.FacesContext)
287     */

288    public void encodeEnd(FacesContext context) throws IOException JavaDoc
289    {
290       if (isRendered() == false)
291       {
292          return;
293       }
294       
295       ResponseWriter out = context.getResponseWriter();
296       
297       out.write("</table>");
298    }
299
300    /**
301     * @see javax.faces.component.UIComponentBase#getRendersChildren()
302     */

303    public boolean getRendersChildren()
304    {
305       return true;
306    }
307
308    
309    // ------------------------------------------------------------------------------
310
// Strongly typed component property accessors
311

312    /**
313     * @return Returns the group panel name.
314     */

315    public String JavaDoc getGroupPanel()
316    {
317       ValueBinding vb = getValueBinding("groupPanel");
318       if (vb != null)
319       {
320          this.groupPanel = (String JavaDoc)vb.getValue(getFacesContext());
321       }
322       
323       return this.groupPanel;
324    }
325    
326    /**
327     * @param groupPanel The group panel name to set.
328     */

329    public void setGroupPanel(String JavaDoc groupPanel)
330    {
331       this.groupPanel = groupPanel;
332    }
333    
334    /**
335     * @return Returns the group background colour.
336     */

337    public String JavaDoc getGroupBgcolor()
338    {
339       ValueBinding vb = getValueBinding("groupBgcolor");
340       if (vb != null)
341       {
342          this.groupBgcolor = (String JavaDoc)vb.getValue(getFacesContext());
343       }
344       
345       return this.groupBgcolor;
346    }
347    
348    /**
349     * @param groupBgcolor The group background colour to set.
350     */

351    public void setGroupBgcolor(String JavaDoc groupBgcolor)
352    {
353       this.groupBgcolor = groupBgcolor;
354    }
355    
356    /**
357     * @return Returns the selected group panel name.
358     */

359    public String JavaDoc getSelectedGroupPanel()
360    {
361       ValueBinding vb = getValueBinding("selectedGroupPanel");
362       if (vb != null)
363       {
364          this.selectedGroupPanel = (String JavaDoc)vb.getValue(getFacesContext());
365       }
366       
367       return this.selectedGroupPanel;
368    }
369    
370    /**
371     * @param selectedGroupPanel The selected group panel name to set.
372     */

373    public void setSelectedGroupPanel(String JavaDoc selectedGroupPanel)
374    {
375       this.selectedGroupPanel = selectedGroupPanel;
376    }
377    
378    /**
379     * @return Returns the selected group background colour.
380     */

381    public String JavaDoc getSelectedGroupBgcolor()
382    {
383       ValueBinding vb = getValueBinding("selectedGroupBgcolor");
384       if (vb != null)
385       {
386          this.selectedGroupBgcolor = (String JavaDoc)vb.getValue(getFacesContext());
387       }
388       
389       return this.selectedGroupBgcolor;
390    }
391    
392    /**
393     * @param selectedGroupBgcolor The selected group background colour to set.
394     */

395    public void setSelectedGroupBgcolor(String JavaDoc selectedGroupBgcolor)
396    {
397       this.selectedGroupBgcolor = selectedGroupBgcolor;
398    }
399    
400    /**
401     * @return Returns the inner group panel name.
402     */

403    public String JavaDoc getInnerGroupPanel()
404    {
405       ValueBinding vb = getValueBinding("innerGroupPanel");
406       if (vb != null)
407       {
408          this.innerGroupPanel = (String JavaDoc)vb.getValue(getFacesContext());
409       }
410       
411       return this.innerGroupPanel;
412    }
413    
414    /**
415     * @param innerGroupPanel The inner group panel name to set.
416     */

417    public void setInnerGroupPanel(String JavaDoc innerGroupPanel)
418    {
419       this.innerGroupPanel = innerGroupPanel;
420    }
421    
422    /**
423     * @return Returns the inner group background colour.
424     */

425    public String JavaDoc getInnerGroupBgcolor()
426    {
427       ValueBinding vb = getValueBinding("innerGroupBgcolor");
428       if (vb != null)
429       {
430          this.innerGroupBgcolor = (String JavaDoc)vb.getValue(getFacesContext());
431       }
432       
433       return this.innerGroupBgcolor;
434    }
435    
436    /**
437     * @param innerGroupBgcolor The inner group background colour to set.
438     */

439    public void setInnerGroupBgcolor(String JavaDoc innerGroupBgcolor)
440    {
441       this.innerGroupBgcolor = innerGroupBgcolor;
442    }
443    
444    
445    // ------------------------------------------------------------------------------
446
// Private helpers
447

448    /**
449     * We use a hidden field name on the assumption that very few shelf instances will
450     * be present on a single page.
451     *
452     * @return hidden field name
453     */

454    private String JavaDoc getHiddenFieldName()
455    {
456       return getClientId(getFacesContext());
457    }
458    
459    
460    // ------------------------------------------------------------------------------
461
// Inner classes
462

463    /**
464     * Class representing the an action event relevant to the Shelf.
465     */

466    public static class ShelfEvent extends ActionEvent
467    {
468       public ShelfEvent(UIComponent component, int index, boolean expanded)
469       {
470          super(component);
471          Expanded = expanded;
472          Index = index;
473       }
474       
475       public boolean Expanded;
476       public int Index;
477    }
478    
479    
480    // ------------------------------------------------------------------------------
481
// Private data
482

483    /** component properties */
484    private String JavaDoc groupPanel;
485    private String JavaDoc groupBgcolor;
486    private String JavaDoc selectedGroupPanel;
487    private String JavaDoc selectedGroupBgcolor;
488    private String JavaDoc innerGroupPanel;
489    private String JavaDoc innerGroupBgcolor;
490    private MethodBinding groupExpandedActionListener;
491 }
492
Popular Tags