1 16 package org.apache.myfaces.custom.navmenu.jscookmenu; 17 18 import org.apache.myfaces.component.html.util.AddResource; 19 import org.apache.myfaces.custom.navmenu.NavigationMenuItem; 20 import org.apache.myfaces.custom.navmenu.NavigationMenuUtils; 21 import org.apache.myfaces.custom.navmenu.UINavigationMenuItem; 22 import org.apache.myfaces.el.SimpleActionMethodBinding; 23 import org.apache.myfaces.renderkit.RendererUtils; 24 import org.apache.myfaces.renderkit.html.HtmlRenderer; 25 import org.apache.myfaces.renderkit.html.util.DummyFormResponseWriter; 26 import org.apache.myfaces.renderkit.html.util.DummyFormUtils; 27 import org.apache.myfaces.renderkit.html.util.JavascriptUtils; 28 29 import javax.faces.component.UIComponent; 30 import javax.faces.context.FacesContext; 31 import javax.faces.context.ResponseWriter; 32 import javax.faces.el.MethodBinding; 33 import javax.faces.el.ValueBinding; 34 import javax.faces.event.ActionEvent; 35 import javax.faces.webapp.UIComponentTag; 36 import java.io.IOException ; 37 import java.util.List ; 38 import java.util.Map ; 39 40 90 public class HtmlJSCookMenuRenderer 91 extends HtmlRenderer 92 { 93 95 private static final String JSCOOK_ACTION_PARAM = "jscook_action"; 96 97 public void decode(FacesContext context, UIComponent component) 98 { 99 RendererUtils.checkParamValidity(context, component, HtmlCommandJSCookMenu.class); 100 101 Map parameter = context.getExternalContext().getRequestParameterMap(); 102 String actionParam = (String )parameter.get(JSCOOK_ACTION_PARAM); 103 if (actionParam != null && !actionParam.trim().equals("") && 104 !actionParam.trim().equals("null")) 105 { 106 String compId = component.getId(); 107 int idx = actionParam.indexOf(':'); 108 if (idx == -1) { 109 return; 110 } 111 String actionId = actionParam.substring(0, idx); 112 if (! compId.equals(actionId)) { 113 return; 114 } 115 actionParam = actionParam.substring(idx + 1); 116 actionParam = decodeValueBinding(actionParam, context); 117 MethodBinding mb; 118 if (UIComponentTag.isValueReference(actionParam)) 119 { 120 mb = context.getApplication().createMethodBinding(actionParam, null); 121 } 122 else 123 { 124 mb = new SimpleActionMethodBinding(actionParam); 125 } 126 ((HtmlCommandJSCookMenu)component).setAction(mb); 127 128 component.queueEvent(new ActionEvent(component)); 129 } 130 } 131 132 private String decodeValueBinding(String actionParam, FacesContext context) 133 { 134 int idx = actionParam.indexOf(";#{"); 135 if (idx == -1) { 136 return actionParam; 137 } 138 139 String newActionParam = actionParam.substring(0, idx); 140 String vbParam = actionParam.substring(idx + 1); 141 142 idx = vbParam.indexOf('='); 143 if (idx == -1) { 144 return newActionParam; 145 } 146 String vbExpressionString = vbParam.substring(0, idx); 147 String vbValue = vbParam.substring(idx + 1); 148 149 ValueBinding vb = 150 context.getApplication().createValueBinding(vbExpressionString); 151 vb.setValue(context, vbValue); 152 153 return newActionParam; 154 } 155 156 public boolean getRendersChildren() 157 { 158 return true; 159 } 160 161 public void encodeChildren(FacesContext context, UIComponent component) throws IOException 162 { 163 RendererUtils.checkParamValidity(context, component, HtmlCommandJSCookMenu.class); 164 165 List list = NavigationMenuUtils.getNavigationMenuItemList(component); 166 if (list.size() > 0) 167 { 168 List uiNavMenuItemList = component.getChildren(); 169 DummyFormResponseWriter dummyFormResponseWriter = DummyFormUtils.getDummyFormResponseWriter(context); 170 dummyFormResponseWriter.addDummyFormParameter(JSCOOK_ACTION_PARAM); 171 dummyFormResponseWriter.setWriteDummyForm(true); 172 173 String myId = component.getId(); 174 175 ResponseWriter writer = context.getResponseWriter(); 176 177 writer.write("\n<script type=\"text/javascript\"><!--\n" + 178 "var myMenu =\n["); 179 encodeNavigationMenuItems(context, writer, 180 (NavigationMenuItem[]) list.toArray(new NavigationMenuItem[list.size()]), 181 uiNavMenuItemList, 182 myId); 183 184 writer.write("];\n" + 185 "--></script>\n"); 186 } 187 } 188 189 private void encodeNavigationMenuItems(FacesContext context, 190 ResponseWriter writer, 191 NavigationMenuItem[] items, 192 List uiNavMenuItemList, 193 String menuId) 194 throws IOException 195 { 196 for (int i = 0; i < items.length; i++) 197 { 198 NavigationMenuItem item = (NavigationMenuItem)items[i]; 199 Object tempObj = null; 200 UINavigationMenuItem uiNavMenuItem = null; 201 try { 202 tempObj = uiNavMenuItemList.get(i); 203 } catch (IndexOutOfBoundsException e) { 204 } 205 if (tempObj != null) { 206 if (tempObj instanceof UINavigationMenuItem) { 207 uiNavMenuItem = (UINavigationMenuItem) tempObj; 208 } 209 } 210 211 if (! item.isRendered()) { 212 continue; 213 } 214 215 if (i > 0) 216 { 217 writer.write(",\n"); 218 } 219 220 if (item.isSplit()) 221 { 222 writer.write("_cmSplit,"); 223 } 224 225 writer.write("["); 226 if (item.getIcon() != null) 227 { 228 String iconSrc = context.getApplication().getViewHandler().getResourceURL(context, item.getIcon()); 229 writer.write("'<img SRC=\""); 230 writer.write(context.getExternalContext().encodeResourceURL(iconSrc)); 231 writer.write("\"/>'"); 232 } 233 else 234 { 235 writer.write("''"); 236 } 237 writer.write(", '"); 238 writer.write(JavascriptUtils.encodeString(item.getLabel())); 239 writer.write("', "); 240 if (item.getAction() != null && ! item.isDisabled()) 241 { 242 writer.write("'"); 243 writer.write(menuId); 244 writer.write(':'); 245 writer.write(item.getAction()); 246 if (uiNavMenuItem != null) { 247 encodeValueBinding(writer, uiNavMenuItem, item); 248 } 249 writer.write("'"); 250 } 251 else 252 { 253 writer.write("null"); 254 } 255 writer.write(", '#', null"); 256 257 if (item.isRendered() && ! item.isDisabled()) { 258 NavigationMenuItem[] menuItems = item.getNavigationMenuItems(); 260 if (menuItems != null && menuItems.length > 0) 261 { 262 writer.write(","); 263 if (uiNavMenuItem != null) 264 { 265 encodeNavigationMenuItems(context, writer, menuItems, 266 uiNavMenuItem.getChildren(), menuId); 267 } 268 } 269 }; 270 writer.write("]"); 271 } 272 } 273 274 private void encodeValueBinding(ResponseWriter writer, UINavigationMenuItem uiNavMenuItem, 275 NavigationMenuItem item) throws IOException 276 { 277 ValueBinding vb = uiNavMenuItem.getValueBinding("NavMenuItemValue"); 278 if (vb == null) { 279 return; 280 } 281 String vbExpression = vb.getExpressionString(); 282 if (vbExpression == null) { 283 return; 284 } 285 Object tempObj = item.getValue(); 286 if (tempObj == null) { 287 return; 288 } 289 290 writer.write(";"); 291 writer.write(vbExpression); 292 writer.write("="); 293 writer.write(tempObj.toString()); 294 } 295 296 public void encodeEnd(FacesContext context, UIComponent component) throws IOException 297 { 298 RendererUtils.checkParamValidity(context, component, HtmlCommandJSCookMenu.class); 299 HtmlCommandJSCookMenu menu = (HtmlCommandJSCookMenu)component; 300 301 AddResource.addJavaScriptToHeader(NavigationMenuItem.class, "jscookmenu/JSCookMenu.js", context); 302 303 AddResource.addJavaScriptToHeader(NavigationMenuItem.class, "jscookmenu/ThemeOffice/theme.js", context); 304 AddResource.addStyleSheet(NavigationMenuItem.class, "jscookmenu/ThemeOffice/theme.css", context); 305 306 AddResource.addJavaScriptToHeader(NavigationMenuItem.class, "jscookmenu/ThemeMiniBlack/theme.js", context); 307 AddResource.addStyleSheet(NavigationMenuItem.class, "jscookmenu/ThemeMiniBlack/theme.css", context); 308 309 AddResource.addJavaScriptToHeader(NavigationMenuItem.class, "jscookmenu/ThemeIE/theme.js", context); 310 AddResource.addStyleSheet(NavigationMenuItem.class, "jscookmenu/ThemeIE/theme.css", context); 311 312 AddResource.addJavaScriptToHeader(NavigationMenuItem.class, "jscookmenu/ThemePanel/theme.js", context); 313 AddResource.addStyleSheet(NavigationMenuItem.class, "jscookmenu/ThemePanel/theme.css", context); 314 315 ResponseWriter writer = context.getResponseWriter(); 316 317 String menuId = component.getClientId(context).replaceAll(":","_") + "_menu"; 318 319 while(menuId.startsWith("_")) 320 { 321 menuId = menuId.substring(1); 322 } 323 324 writer.write("<div id=\"" + menuId + "\"></div>\n" + 325 "<script type=\"text/javascript\"><!--\n" + 326 "\tcmDraw ('" + menuId + "', myMenu, '" + menu.getLayout() + "', cm" + menu.getTheme() + ", '" + menu.getTheme() + "');\n" + 327 "--></script>\n"); 328 } 329 330 } 331 | Popular Tags |