KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
20
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.context.ResponseWriter;
24 import javax.faces.el.ValueBinding;
25
26 import org.alfresco.web.ui.common.Utils;
27
28 /**
29  * @author Kevin Roast
30  */

31 public class UIMenu extends SelfRenderingComponent
32 {
33    // ------------------------------------------------------------------------------
34
// Component Impl
35

36    /**
37     * @see javax.faces.component.UIComponent#getFamily()
38     */

39    public String JavaDoc getFamily()
40    {
41       return "org.alfresco.faces.Controls";
42    }
43
44    /**
45     * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
46     */

47    public void encodeBegin(FacesContext context) throws IOException JavaDoc
48    {
49       if (isRendered() == false)
50       {
51          return;
52       }
53       
54       ResponseWriter out = context.getResponseWriter();
55       
56       // output a textual label with an optional icon to show the menu
57
String JavaDoc menuId = getNextMenuId(this, context);
58       out.write("<a HREF='#' onclick=\"javascript:_toggleMenu(event, '");
59       out.write(menuId);
60       out.write("');return false;\"");
61       outputAttribute(out, getAttributes().get("style"), "style");
62       outputAttribute(out, getAttributes().get("styleClass"), "class");
63       outputAttribute(out, getTooltip(), "title");
64       out.write('>');
65       
66       // output label text
67
String JavaDoc label = getLabel();
68       if (label != null)
69       {
70          out.write(Utils.encode(label));
71       }
72       
73       // output image
74
if (getAttributes().get("image") != null)
75       {
76          out.write(Utils.buildImageTag(context, (String JavaDoc)getAttributes().get("image"), null, "absmiddle"));
77       }
78       
79       out.write("</a>");
80       
81       // output the hidden DIV section to contain the menu item table
82
out.write("<br><div id='");
83       out.write(menuId);
84       out.write("' style=\"position:absolute;display:none;padding-left:2px;\">");
85       out.write("<table border=0 cellpadding=0");
86       outputAttribute(out, getAttributes().get("itemSpacing"), "cellspacing");
87       outputAttribute(out, getAttributes().get("menuStyle"), "style");
88       outputAttribute(out, getAttributes().get("menuStyleClass"), "class");
89       out.write(">");
90    }
91
92    /**
93     * @see javax.faces.component.UIComponentBase#encodeEnd(javax.faces.context.FacesContext)
94     */

95    public void encodeEnd(FacesContext context) throws IOException JavaDoc
96    {
97       if (isRendered() == false)
98       {
99          return;
100       }
101       
102       ResponseWriter out = context.getResponseWriter();
103       
104       // end the menu table and the hidden DIV section
105
out.write("</table></div>");
106    }
107    
108    /**
109     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
110     */

111    public void restoreState(FacesContext context, Object JavaDoc state)
112    {
113       Object JavaDoc values[] = (Object JavaDoc[])state;
114       // standard component attributes are restored by the super class
115
super.restoreState(context, values[0]);
116       this.label = (String JavaDoc)values[1];
117       this.tooltip = (String JavaDoc)values[2];
118    }
119    
120    /**
121     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
122     */

123    public Object JavaDoc saveState(FacesContext context)
124    {
125       Object JavaDoc values[] = new Object JavaDoc[3];
126       // standard component attributes are saved by the super class
127
values[0] = super.saveState(context);
128       values[1] = this.label;
129       values[2] = this.tooltip;
130       return values;
131    }
132    
133    
134    // ------------------------------------------------------------------------------
135
// Strongly typed component property accessors
136

137    /**
138     * @return Returns the label.
139     */

140    public String JavaDoc getLabel()
141    {
142       ValueBinding vb = getValueBinding("label");
143       if (vb != null)
144       {
145          this.label = (String JavaDoc)vb.getValue(getFacesContext());
146       }
147       return this.label;
148    }
149
150    /**
151     * @param label The label to set.
152     */

153    public void setLabel(String JavaDoc label)
154    {
155       this.label = label;
156    }
157
158    /**
159     * @return Returns the tooltip.
160     */

161    public String JavaDoc getTooltip()
162    {
163       ValueBinding vb = getValueBinding("tooltip");
164       if (vb != null)
165       {
166          this.tooltip = (String JavaDoc)vb.getValue(getFacesContext());
167       }
168       return this.tooltip;
169    }
170
171    /**
172     * @param tooltip The tooltip to set.
173     */

174    public void setTooltip(String JavaDoc tooltip)
175    {
176       this.tooltip = tooltip;
177    }
178    
179    
180    // ------------------------------------------------------------------------------
181
// Helpers
182

183    /**
184     * Return the next usable menu DIV id in a sequence
185     *
186     * @param context FacesContext
187     *
188     * @return next menu ID
189     */

190    public static String JavaDoc getNextMenuId(UIComponent component, FacesContext context)
191    {
192       Integer JavaDoc val = (Integer JavaDoc)context.getExternalContext().getRequestMap().get(MENU_ID_KEY);
193       if (val == null)
194       {
195          val = Integer.valueOf(0);
196       }
197       
198       // build next id in sequence
199
String JavaDoc id = component.getClientId(context) + '_' + val.toString();
200       
201       // save incremented value in the request ready for next menu component instance
202
val = Integer.valueOf( val.intValue() + 1 );
203       context.getExternalContext().getRequestMap().put(MENU_ID_KEY, val);
204       
205       return id;
206    }
207    
208    
209    // ------------------------------------------------------------------------------
210
// Private members
211

212    private final static String JavaDoc MENU_ID_KEY = "__awc_menu_id";
213    
214    private String JavaDoc label;
215    
216    private String JavaDoc tooltip;
217 }
218
Popular Tags