KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > menubar > MenuItemRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.component.menubar;
35
36 import com.icesoft.faces.component.CSS_DEFAULT;
37 import com.icesoft.faces.component.InvalidComponentTypeException;
38 import com.icesoft.faces.component.ext.HtmlCommandLink;
39 import com.icesoft.faces.component.ext.HtmlGraphicImage;
40 import com.icesoft.faces.component.ext.HtmlOutputText;
41 import com.icesoft.faces.component.ext.HtmlPanelGroup;
42 import com.icesoft.faces.component.ext.taglib.Util;
43 import com.icesoft.faces.context.DOMContext;
44 import com.icesoft.faces.renderkit.dom_html_basic.HTML;
45 import com.icesoft.faces.util.CoreUtils;
46 import com.icesoft.faces.util.DOMUtils;
47 import org.w3c.dom.Element JavaDoc;
48 import org.w3c.dom.Node JavaDoc;
49
50 import javax.faces.component.NamingContainer;
51 import javax.faces.component.UIComponent;
52 import javax.faces.component.UIParameter;
53 import javax.faces.component.UIViewRoot;
54 import javax.faces.context.FacesContext;
55 import javax.faces.el.MethodBinding;
56 import javax.faces.event.ActionEvent;
57 import javax.faces.event.ActionListener;
58 import java.beans.Beans JavaDoc;
59 import java.io.IOException JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.Map JavaDoc;
62
63
64 public class MenuItemRenderer extends MenuItemRendererBase {
65
66     private static final String JavaDoc HIDDEN_FIELD_NAME = "cl";
67
68
69     private static String JavaDoc SUB = "_sub";
70     private static String JavaDoc KEYWORD_NULL = "null";
71     private static String JavaDoc KEYWORD_THIS = "this";
72     private final String JavaDoc DEFAULT_IMAGEDIR = "/xmlhttp/css/xp/css-images/";
73     private static final String JavaDoc SUBMENU_IMAGE = "submenu.gif";
74     private static String JavaDoc LINK_SUFFIX = "link";
75
76     public void decode(FacesContext facesContext, UIComponent uiComponent) {
77
78         validateParameters(facesContext, uiComponent, null);
79         if (isStatic(uiComponent)) {
80             return;
81         }
82         String JavaDoc componentId = uiComponent.getClientId(facesContext);
83         Map JavaDoc requestParameterMap =
84                 facesContext.getExternalContext().getRequestParameterMap();
85         String JavaDoc hiddenFieldName = deriveCommonHiddenFieldName(facesContext,
86                                                              (MenuItem) uiComponent);
87         String JavaDoc hiddenFieldNameInRequestMap =
88                 (String JavaDoc) requestParameterMap.get(hiddenFieldName);
89
90         if (hiddenFieldNameInRequestMap == null
91             || hiddenFieldNameInRequestMap.equals("")) {
92             // this command link did not invoke the submit
93
return;
94         }
95
96         // debugging
97
//examineRequest(facesContext, uiComponent, requestParameterMap, hiddenFieldName, hiddenFieldNameInRequestMap);
98
String JavaDoc commandLinkClientId = componentId + ":" + LINK_SUFFIX;
99         if (hiddenFieldNameInRequestMap.equals(commandLinkClientId)) {
100             ActionEvent actionEvent = new ActionEvent(uiComponent);
101             uiComponent.queueEvent(actionEvent);
102         }
103     }
104
105
106     // this component renders its children, so, this method will be called once
107
// for each top-level menu node. From there, this component will manage
108
// the rendering of all children components. The idea is to end up with
109
// a fairly flat structure. There will exist a master div that contains the
110
// entire menu. Inside that div there will exist a first-order child div for
111
// each top level menu item and there will exist a first-order child div for
112
// each submenu. Inside the submenu div there will exist a div to hold each
113
// menu item in the submenu. Note that there is no nesting of submenu items
114
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
115             throws IOException JavaDoc {
116
117         if (!(uiComponent.getParent() instanceof MenuBar) &&
118             !(uiComponent.getParent() instanceof MenuItems)) {
119             throw new InvalidComponentTypeException(
120                     "MenuBar expected as parent of top-level MenuItem");
121         }
122         // If static model declaration (in the jsp) is employed then the
123
// immediate parent will be the Menu component
124
// Else if the model declaration is in the bean class then there
125
// is a MenuItems between the MenuItem and the Menu component
126
MenuBar menuComponent = null;
127         if (uiComponent.getParent() instanceof MenuBar) {
128             menuComponent = (MenuBar) uiComponent.getParent();
129         } else if (uiComponent.getParent().getParent() instanceof MenuBar) {
130             menuComponent = (MenuBar) uiComponent.getParent().getParent();
131         } else {
132             throw new InvalidComponentTypeException("Expecting MenuBar");
133         }
134
135         // is vertical ?
136
boolean vertical = menuComponent.getOrientation().equalsIgnoreCase(
137                 MenuBar.ORIENTATION_VERTICAL);
138
139         validateParameters(facesContext, uiComponent, MenuItemBase.class);
140
141         // first render
142
DOMContext domContext =
143                 DOMContext.attachDOMContext(facesContext, uiComponent);
144         String JavaDoc clientId = uiComponent.getClientId(facesContext);
145         if (!domContext.isInitialized()) {
146             Element JavaDoc topLevelDiv = domContext.createRootElement(HTML.DIV_ELEM);
147             topLevelDiv.setAttribute(HTML.ID_ATTR, clientId);
148         }
149         Element JavaDoc topLevelDiv = (Element JavaDoc) domContext.getRootNode();
150         topLevelDiv.setAttribute(HTML.NAME_ATTR, "TOP_LEVEL");
151
152         String JavaDoc rootItemSubClass = CSS_DEFAULT.MENU_BAR_ITEM_STYLE;
153         if (vertical) {
154             rootItemSubClass = CSS_DEFAULT.MENU_BAR_VERTICAL_SUFFIX_STYLE +
155                                 rootItemSubClass;
156         }
157         topLevelDiv.setAttribute(HTML.CLASS_ATTR,
158                 ((MenuItem) uiComponent).
159                 getUserDefinedStyleClass(menuComponent.getItemStyleClass(),
160                         rootItemSubClass));
161
162         if (uiComponent.getChildCount() > 0) {
163             String JavaDoc displayEvent = HTML.ONMOUSEOVER_ATTR;
164             if (vertical) {
165                 topLevelDiv.setAttribute(displayEvent,
166                                          "Ice.Menu.hideOrphanedMenusNotRelatedTo(this);" +
167                                          expand("this", clientId + "_sub",
168                                                 KEYWORD_THIS));
169             } else {
170                 topLevelDiv.setAttribute(displayEvent,
171                                          "Ice.Menu.hideOrphanedMenusNotRelatedTo(this);" +
172                                          expand("this", clientId + "_sub",
173                                                 KEYWORD_NULL));
174             }
175         } else {
176             topLevelDiv.setAttribute(HTML.ONMOUSEOVER_ATTR,
177                                      "Ice.Menu.hideOrphanedMenusNotRelatedTo(this);");
178         }
179
180         DOMContext.removeChildren(topLevelDiv);
181         Element JavaDoc masterDiv = (Element JavaDoc) topLevelDiv.getParentNode();
182
183         renderAnchor(facesContext, domContext, 0, (MenuItem) uiComponent,
184                      topLevelDiv, menuComponent, vertical);
185
186         if ((uiComponent.getChildCount() > 0) &&
187             (((MenuItem) uiComponent).isChildrenMenuItem())) {
188             renderChildrenRecursive(facesContext, menuComponent, uiComponent,
189                                     vertical, masterDiv);
190
191         }
192     }
193
194     private String JavaDoc expand(String JavaDoc supermenu, String JavaDoc submenu, String JavaDoc submenuDiv) {
195         // delimit ids to force resolution from ids to elements
196
if (!(supermenu.equalsIgnoreCase(KEYWORD_NULL)) &&
197             !(supermenu.equalsIgnoreCase(KEYWORD_THIS))) {
198             supermenu = "$('" + supermenu + "')";
199         }
200         if (!(submenu.equalsIgnoreCase(KEYWORD_NULL)) &&
201             !(submenu.equalsIgnoreCase(KEYWORD_THIS))) {
202             submenu = "$('" + submenu + "')";
203         }
204         if (!(submenuDiv.equalsIgnoreCase(KEYWORD_NULL)) &&
205             !(submenuDiv.equalsIgnoreCase(KEYWORD_THIS))) {
206             submenuDiv = "$('" + submenuDiv + "')";
207         }
208         return "Ice.Menu.show(" + supermenu + "," + submenu + "," + submenuDiv +
209                ");";
210     }
211
212     protected static String JavaDoc deriveCommonHiddenFieldName(
213             FacesContext facesContext,
214             UIComponent uiComponent) {
215
216         if (Beans.isDesignTime()){
217             return "";
218         }
219         UIComponent parentNamingContainer = findForm(uiComponent);
220         String JavaDoc parentClientId = parentNamingContainer.getClientId(facesContext);
221         String JavaDoc hiddenFieldName = parentClientId
222                                  + NamingContainer.SEPARATOR_CHAR
223                                  + UIViewRoot.UNIQUE_ID_PREFIX
224                                  + HIDDEN_FIELD_NAME;
225         return hiddenFieldName;
226     }
227
228     // this method is used to render topLevel horizontal menu items
229
// horizontal menu items do not have icons or subMenu indicators
230
private Element JavaDoc makeTopLevelAnchor(FacesContext facesContext,
231                                        MenuItem menuItem,
232                                        MenuBar menuBar) {
233
234         DOMContext domContext =
235                 DOMContext.getDOMContext(facesContext, menuItem);
236         Element JavaDoc anchor = domContext.createElement(HTML.ANCHOR_ELEM);
237         if (!menuItem.isDisabled()) {
238             anchor.setAttribute(HTML.HREF_ATTR, menuItem.getLink());
239             if (menuItem.getTarget() != null) {
240                 anchor.setAttribute(HTML.TARGET_ATTR, menuItem.getTarget());
241             }
242         }
243         // create div
244
Element JavaDoc div = domContext.createElement(HTML.DIV_ELEM);
245
246         anchor.appendChild(div);
247
248         // only render icons if noIcons is false
249
if ((!menuBar.getNoIcons().equalsIgnoreCase("true")) &&
250             (getIcon(menuItem) != null)) {
251             // do not render icon if it is the default blank image
252
// this only applies to horizontal top level menu items
253
if (!getIcon(menuItem).endsWith(MenuItem.DEFAULT_ICON)) {
254                 Element JavaDoc iconImg = domContext.createElement(HTML.IMG_ELEM);
255                 iconImg.setAttribute(HTML.SRC_ATTR, getIcon(menuItem));
256                 iconImg.setAttribute(HTML.STYLE_ATTR, "border:none;");
257                 iconImg.setAttribute(HTML.CLASS_ATTR, menuItem.
258                         getUserDefinedStyleClass(menuBar.getItemImageStyleClass(),
259                                 CSS_DEFAULT.MENU_BAR_ITEM_STYLE+
260                                 CSS_DEFAULT.MENU_ITEM_IMAGE_STYLE));
261                 div.appendChild(iconImg);
262             }
263         }
264
265         // create a span for text
266
Element JavaDoc span = domContext.createElement(HTML.SPAN_ELEM);
267         if (!menuItem.isDisabled()) {
268             anchor.setAttribute(HTML.STYLE_CLASS_ATTR, "iceLink");
269         } else {
270             anchor.setAttribute(HTML.STYLE_CLASS_ATTR, "iceLink-dis");
271         }
272         span.setAttribute(HTML.CLASS_ATTR, menuItem.
273                 getUserDefinedStyleClass(menuBar.getItemLabelStyleClass(),
274                         CSS_DEFAULT.MENU_BAR_ITEM_LABEL_STYLE));
275         div.appendChild(span);
276         // create text
277
Node JavaDoc text = domContext.createTextNode(DOMUtils.escapeAnsi(menuItem.getValue().toString()));
278         span.appendChild(text);
279
280         return anchor;
281     }
282
283     // this method is used to render top level vertical menu items
284
private Element JavaDoc makeTopLevelVerticalAnchor(FacesContext facesContext,
285                                                MenuItem menuItem,
286                                                MenuBar menuBar) {
287
288         DOMContext domContext =
289                 DOMContext.getDOMContext(facesContext, menuItem);
290         Element JavaDoc anchor = domContext.createElement(HTML.ANCHOR_ELEM);
291         if (!menuItem.isDisabled()){
292             anchor.setAttribute(HTML.HREF_ATTR, menuItem.getLink());
293             if (menuItem.getTarget() != null) {
294                 anchor.setAttribute(HTML.TARGET_ATTR, menuItem.getTarget());
295             }
296         }
297         // create div
298
Element JavaDoc div = domContext.createElement(HTML.DIV_ELEM);
299
300         anchor.appendChild(div);
301
302         if (menuItem.getChildCount() > 0) {
303             Element JavaDoc subImg = domContext.createElement(HTML.IMG_ELEM);
304             subImg.setAttribute(HTML.SRC_ATTR, getSubMenuImage(menuBar));
305             subImg.setAttribute(HTML.STYLE_ATTR, "border:none;");
306             subImg.setAttribute(HTML.CLASS_ATTR,
307                                 menuBar.getSubMenuIndicatorStyleClass());
308             div.appendChild(subImg);
309         }
310
311         // only render icons if noIcons is false
312
if ((!menuBar.getNoIcons().equalsIgnoreCase("true")) &&
313             (getIcon(menuItem) != null)) {
314             Element JavaDoc iconImg = domContext.createElement(HTML.IMG_ELEM);
315             iconImg.setAttribute(HTML.SRC_ATTR, getIcon(menuItem));
316             iconImg.setAttribute(HTML.STYLE_ATTR, "border:none;");
317             iconImg.setAttribute(HTML.CLASS_ATTR, menuItem.
318                     getUserDefinedStyleClass(menuBar.getItemImageStyleClass(),
319                             CSS_DEFAULT.MENU_BAR_VERTICAL_SUFFIX_STYLE+
320                             CSS_DEFAULT.MENU_BAR_ITEM_STYLE+
321                             CSS_DEFAULT.MENU_ITEM_IMAGE_STYLE));
322             div.appendChild(iconImg);
323         }
324
325         // create a span for text
326
Element JavaDoc span = domContext.createElement(HTML.SPAN_ELEM);
327         if (!menuItem.isDisabled()) {
328             anchor.setAttribute(HTML.STYLE_CLASS_ATTR,"iceLink");
329         } else {
330             anchor.setAttribute(HTML.STYLE_CLASS_ATTR,"iceLink-dis");
331         }
332         span.setAttribute(HTML.CLASS_ATTR, menuItem.
333                 getUserDefinedStyleClass(menuBar.getItemLabelStyleClass(),
334                         CSS_DEFAULT.MENU_BAR_VERTICAL_SUFFIX_STYLE+
335                         CSS_DEFAULT.MENU_BAR_ITEM_LABEL_STYLE));
336
337         div.appendChild(span);
338         // create text
339
Node JavaDoc text = domContext.createTextNode(DOMUtils.escapeAnsi(menuItem.getValue().toString()));
340         span.appendChild(text);
341
342         return anchor;
343     }
344
345     private Element JavaDoc makeAnchor(FacesContext facesContext, DOMContext domContext,
346                                MenuItem menuItem, MenuBar menuBar) {
347
348         Element JavaDoc anchor = domContext.createElement(HTML.ANCHOR_ELEM);
349         if (!menuItem.isDisabled()) {
350             anchor.setAttribute(HTML.HREF_ATTR, menuItem.getLink());
351             if (menuItem.getTarget() != null) {
352                 anchor.setAttribute(HTML.TARGET_ATTR, menuItem.getTarget());
353             }
354         }
355         // create div
356
Element JavaDoc div = domContext.createElement(HTML.DIV_ELEM);
357
358         anchor.appendChild(div);
359
360         if (menuItem.getChildCount() > 0) {
361             Element JavaDoc subImg = domContext.createElement(HTML.IMG_ELEM);
362             subImg.setAttribute(HTML.SRC_ATTR, getSubMenuImage(menuBar));
363             subImg.setAttribute(HTML.STYLE_ATTR, "border:none;");
364             subImg.setAttribute(HTML.CLASS_ATTR,
365                                 menuBar.getSubMenuIndicatorStyleClass());
366             div.appendChild(subImg);
367         }
368
369         // only render icons if noIcons is false
370
if ((!menuBar.getNoIcons().equalsIgnoreCase("true")) &&
371             (getIcon(menuItem) != null)) {
372             Element JavaDoc iconImg = domContext.createElement(HTML.IMG_ELEM);
373             iconImg.setAttribute(HTML.SRC_ATTR, getIcon(menuItem));
374             iconImg.setAttribute(HTML.STYLE_ATTR, "border:none;");
375             iconImg.setAttribute(HTML.CLASS_ATTR, menuItem.getImageStyleClass());
376             div.appendChild(iconImg);
377         }
378
379         // create a span for text
380
Element JavaDoc span = domContext.createElement(HTML.SPAN_ELEM);
381         if (!menuItem.isDisabled()) {
382             anchor.setAttribute(HTML.STYLE_CLASS_ATTR,"iceLink");
383         } else {
384             anchor.setAttribute(HTML.STYLE_CLASS_ATTR,"iceLink-dis");
385         }
386         span.setAttribute(HTML.CLASS_ATTR, menuItem.getLabelStyleClass());
387
388         div.appendChild(span);
389         // create text
390
Node JavaDoc text = domContext.createTextNode(DOMUtils.escapeAnsi(menuItem.getValue().toString()));
391         span.appendChild(text);
392
393         return anchor;
394     }
395
396     private void renderChildrenRecursive(FacesContext facesContext,
397                                          MenuBar menuComponent,
398                                          UIComponent uiComponent,
399                                          boolean vertical, Element JavaDoc masterDiv) {
400         DOMContext domContext =
401                 DOMContext.getDOMContext(facesContext, uiComponent);
402         // create the div that will hold all the sub menu items
403
Element JavaDoc submenuDiv = domContext.createElement(HTML.DIV_ELEM);
404         submenuDiv.setAttribute(HTML.NAME_ATTR, "SUBMENU");
405         String JavaDoc subMenuDivId = uiComponent.getClientId(facesContext) + SUB;
406         submenuDiv.setAttribute(HTML.ID_ATTR, subMenuDivId);
407
408 // if (vertical) {
409
// submenuDiv.setAttribute(HTML.CLASS_ATTR,
410
// CSS_DEFAULT.SUBMENU_VERTICAL_STYLE);
411
// } else {
412
// submenuDiv.setAttribute(HTML.CLASS_ATTR, CSS_DEFAULT.SUBMENU_STYLE);
413
// }
414
submenuDiv.setAttribute(HTML.CLASS_ATTR, menuComponent.getSubMenuStyleClass());
415         submenuDiv.setAttribute(HTML.STYLE_ATTR, "display:none");
416         masterDiv.appendChild(submenuDiv);
417         // check if this menuItem is disabled, if it is lets disable the children
418
// render each menuItem in this submenu
419
boolean disabled =((MenuItem)uiComponent).isDisabled();
420         for (int childIndex = 0; childIndex < uiComponent.getChildCount();
421              childIndex++) {
422             UIComponent nextSubMenuItem =
423                     (UIComponent) uiComponent.getChildren().get(childIndex);
424             if (!(nextSubMenuItem instanceof UIParameter)) {
425                 Element JavaDoc subMenuItemDiv =
426                         domContext.createElement(HTML.DIV_ELEM);
427                 submenuDiv.appendChild(subMenuItemDiv);
428                 if (nextSubMenuItem instanceof MenuItemSeparator) {
429                     renderSeparatorDiv(domContext, subMenuItemDiv, (MenuItemSeparator)nextSubMenuItem);
430                     continue;
431                 }
432                 subMenuItemDiv.setAttribute(HTML.CLASS_ATTR,
433                         ((MenuItem) nextSubMenuItem).getStyleClass());
434                 subMenuItemDiv.setAttribute(HTML.NAME_ATTR, "ITEM");
435                 String JavaDoc subMenuItemClientId =
436                         nextSubMenuItem.getClientId(facesContext);
437                 subMenuItemDiv.setAttribute(HTML.ID_ATTR, subMenuItemClientId);
438                 if (nextSubMenuItem.getChildCount() > 0 &&
439                     ((MenuItem) nextSubMenuItem).isChildrenMenuItem()) {
440                     subMenuItemDiv.setAttribute(HTML.ONMOUSEOVER_ATTR,
441                                                 "Ice.Menu.hideOrphanedMenusNotRelatedTo(this);" +
442                                                 expand(subMenuDivId,
443                                                        subMenuItemClientId +
444                                                        SUB, KEYWORD_THIS));
445                 } else {
446                     subMenuItemDiv.setAttribute(HTML.ONMOUSEOVER_ATTR,
447                                                 "Ice.Menu.hideOrphanedMenusNotRelatedTo(this);");
448
449                 }
450                 subMenuItemDiv.setAttribute(HTML.ID_ATTR,
451                                             nextSubMenuItem.getClientId(
452                                                     facesContext));
453                 // if parent is disabled apply the disabled attribute value of the parent menuItem to this submenuItem
454
if (disabled) {
455                     ((MenuItem) nextSubMenuItem).setDisabled(disabled);
456                 }
457                 // add a command link if we need one
458
if (nextSubMenuItem instanceof MenuItem) {
459                     renderAnchor(facesContext, domContext, childIndex,
460                                  (MenuItem) nextSubMenuItem, subMenuItemDiv,
461                                  menuComponent, vertical);
462                 }
463             }
464         }
465
466         // recurse
467
// check if parent is disabled , if it is the child items should also be disabled.
468
// we should not render child MenuItems of a disabled menuItem
469

470         for (int childIndex = 0; childIndex < uiComponent.getChildCount();
471              childIndex++) {
472             UIComponent nextSubMenuItem =
473                     (UIComponent) uiComponent.getChildren().get(childIndex);
474             if (nextSubMenuItem.getChildCount() > 0){
475                 renderChildrenRecursive(facesContext, menuComponent,
476                                         nextSubMenuItem, vertical, masterDiv);
477             }
478         }
479     }
480
481     /**
482      * @param facesContext
483      * @param domContext
484      * @param childIndex
485      * @param nextSubMenuItem
486      * @param subMenuItemDiv
487      */

488     private void renderAnchor(FacesContext facesContext, DOMContext domContext,
489                               int childIndex, MenuItem nextSubMenuItem,
490                               Element JavaDoc subMenuItemDiv,
491                               MenuBar menuComponent, boolean vertical) {
492
493         // check if the nextSubMenuItem isRendered
494
if (!nextSubMenuItem.isRendered()) {
495             return;
496         }
497
498         // check if this is a Top Level Menu or MenuItems
499
if ((nextSubMenuItem.getParent() instanceof MenuBar) ||
500             ((nextSubMenuItem.getParent() instanceof MenuItems)
501              && (nextSubMenuItem.getParent().getParent() instanceof MenuBar))) {
502             // handle action/actionListeners if attached to top level menuItems
503
if (nextSubMenuItem.hasActionOrActionListener()) {
504                 HtmlCommandLink link = new HtmlCommandLink();
505                 if (nextSubMenuItem.isDisabled()) {
506                     link.setDisabled(true);
507                 } else { // only add action and actionlisteners on enabled menuItems
508
MethodBinding action = nextSubMenuItem.getAction();
509                     if (action != null) {
510                         link.setAction(action);
511                     }
512                     MethodBinding actionListener = nextSubMenuItem.getActionListener();
513                     if (actionListener != null) {
514                         link.setActionListener(actionListener);
515                     }
516                     ActionListener[] actionListeners = nextSubMenuItem.getActionListeners();
517                     if (actionListeners != null) {
518                         for(int i = 0; i < actionListeners.length; i++) {
519                             link.removeActionListener(actionListeners[i]);
520                             link.addActionListener(actionListeners[i]);
521                         }
522                     }
523                 }
524                 link.setValue(nextSubMenuItem.getValue());
525                 link.setParent(nextSubMenuItem);
526                 link.setId(LINK_SUFFIX);
527                 //link.setStyleClass("");
528
Node JavaDoc lastCursorParent = domContext.getCursorParent();
529                 domContext.setCursorParent(subMenuItemDiv);
530                 if (vertical) {
531                     addChildren(link, nextSubMenuItem, menuComponent);
532                 } else {
533                     addTopLevelChildren(link, nextSubMenuItem, menuComponent);
534                 }
535
536                 ((MenuItem) nextSubMenuItem).addParameter(link);
537                 try {
538                     link.encodeBegin(facesContext);
539                     link.encodeChildren(facesContext);
540                     link.encodeEnd(facesContext);
541
542                 } catch (IOException JavaDoc e) {
543                     e.printStackTrace();
544                 }
545                 domContext.setCursorParent(lastCursorParent);
546             } else {
547                 // anchor
548
Element JavaDoc anchor = null;
549                 if (vertical) {
550                     anchor = makeTopLevelVerticalAnchor(facesContext,
551                                                         nextSubMenuItem,
552                                                         menuComponent);
553                 } else {
554                     anchor = makeTopLevelAnchor(facesContext,
555                                                 nextSubMenuItem,
556                                                 menuComponent);
557                 }
558                 subMenuItemDiv.appendChild(anchor);
559             }
560         } else if (nextSubMenuItem.hasActionOrActionListener()) {
561             HtmlCommandLink link = new HtmlCommandLink();
562             if (nextSubMenuItem.isDisabled()){
563                 link.setDisabled(true);
564             } else { // only set action and actionListeners on enabled menuItems
565
MethodBinding action = nextSubMenuItem.getAction();
566                 if (action != null) {
567                     link.setAction(action);
568                 }
569                 MethodBinding actionListener = nextSubMenuItem.getActionListener();
570                 if (actionListener != null) {
571                     link.setActionListener(actionListener);
572                 }
573                 ActionListener[] actionListeners = nextSubMenuItem.getActionListeners();
574                 if (actionListeners != null) {
575                     for(int i = 0; i < actionListeners.length; i++) {
576                         link.removeActionListener(actionListeners[i]);
577                         link.addActionListener(actionListeners[i]);
578                     }
579                 }
580             }
581             link.setValue(nextSubMenuItem.getValue());
582             link.setParent(nextSubMenuItem);
583             link.setId(LINK_SUFFIX);
584
585             Node JavaDoc lastCursorParent = domContext.getCursorParent();
586             domContext.setCursorParent(subMenuItemDiv);
587             addChildren(link, nextSubMenuItem, menuComponent);
588             ((MenuItem) nextSubMenuItem).addParameter(link);
589             try {
590                 link.encodeBegin(facesContext);
591                 link.encodeChildren(facesContext);
592                 link.encodeEnd(facesContext);
593
594             } catch (IOException JavaDoc e) {
595                 e.printStackTrace();
596             }
597             domContext.setCursorParent(lastCursorParent);
598
599         } else {
600             // anchor
601
Element JavaDoc anchor = makeAnchor(facesContext, domContext,
602                                         nextSubMenuItem, menuComponent);
603             subMenuItemDiv.appendChild(anchor);
604         }
605     }
606
607     // this method is used to add icon and label
608
// to top level horizontal menu items
609
private void addTopLevelChildren(HtmlCommandLink link,
610                                      MenuItem nextSubMenuItem,
611                                      MenuBar menuComponent) {
612         HtmlPanelGroup div = new HtmlPanelGroup();
613
614         if ((!menuComponent.getNoIcons().equalsIgnoreCase("true"))
615             && (getIcon(nextSubMenuItem) != null)) {
616             HtmlGraphicImage image = new HtmlGraphicImage();
617             image.setUrl(getIcon(nextSubMenuItem));
618             image.setStyle("border:none;");
619             image.setStyleClass(nextSubMenuItem.getImageStyleClass());
620             div.getChildren().add(image);
621         }
622
623         HtmlOutputText outputText = new HtmlOutputText();
624         outputText.setValue(link.getValue());
625 // if (!nextSubMenuItem.isDisabled()) {
626
// outputText.setStyleClass("iceSubMenuRowLabel");
627
// } else {
628
// outputText.setStyleClass("iceSubMenuRowLabel-dis");
629
// }
630
outputText.setStyleClass(nextSubMenuItem.getLabelStyleClass());
631         link.setValue("");
632         div.getChildren().add(outputText);
633
634         link.getChildren().add(div);
635     }
636
637     // this method is used to add icon, label and indicator
638
// to top level vertical menu items
639
void addChildren(HtmlCommandLink link, MenuItem nextSubMenuItem,
640                      MenuBar menuComponent) {
641         HtmlPanelGroup div = new HtmlPanelGroup();
642         if (nextSubMenuItem.getChildCount() > 0 &&
643             nextSubMenuItem.isChildrenMenuItem()) {
644             HtmlGraphicImage image = new HtmlGraphicImage();
645             image.setUrl(getSubMenuImage(menuComponent));
646             image.setStyle("border:none;");
647             image.setStyleClass(menuComponent.getSubMenuIndicatorStyleClass());
648             div.getChildren().add(image);
649         }
650
651         if ((!menuComponent.getNoIcons().equalsIgnoreCase("true"))
652             && (getIcon(nextSubMenuItem) != null)) {
653             HtmlGraphicImage image = new HtmlGraphicImage();
654             image.setUrl(getIcon(nextSubMenuItem));
655             image.setStyle("border:none;");
656             image.setStyleClass(nextSubMenuItem.getImageStyleClass());
657             div.getChildren().add(image);
658         }
659
660         HtmlOutputText outputText = new HtmlOutputText();
661         outputText.setValue(link.getValue());
662 // if (!nextSubMenuItem.isDisabled()) {
663
// outputText.setStyleClass("iceSubMenuRowLabel");
664
// } else {
665
// outputText.setStyleClass("iceSubMenuRowLabel-dis");
666
// }
667
outputText.setStyleClass(nextSubMenuItem.getLabelStyleClass());
668         link.setValue("");
669         div.getChildren().add(outputText);
670
671         link.getChildren().add(div);
672     }
673
674     private void renderSeparatorDiv(DOMContext domContext, Element JavaDoc parent,
675             MenuItemSeparator menuItemSeparator) {
676         Element JavaDoc hr = domContext.createElement("hr");
677         parent.setAttribute(HTML.CLASS_ATTR, menuItemSeparator.getStyleClass());
678         parent.appendChild(hr);
679     }
680
681     /**
682      * @return SubMenuImage url
683      */

684     private String JavaDoc getSubMenuImage(MenuBar menuComponent) {
685         String JavaDoc customPath = null;
686         if ((customPath = menuComponent.getImageDir()) != null) {
687             return customPath + SUBMENU_IMAGE;
688         }
689         return CoreUtils.resolveResourceURL(FacesContext.getCurrentInstance(),
690                DEFAULT_IMAGEDIR + SUBMENU_IMAGE);
691     }
692
693     protected String JavaDoc getTextValue(UIComponent component) {
694         if (component instanceof MenuItem) {
695             return ((MenuItem) component).getValue().toString();
696         }
697         return null;
698     }
699
700     protected String JavaDoc getIcon(UIComponent component) {
701         if (component instanceof MenuItem) {
702             return ((MenuItem) component).getIcon();
703         }
704         return null;
705     }
706
707     /* (non-Javadoc)
708      * @see com.icesoft.faces.component.menubar.MenuItemRendererBase
709      * #encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
710      */

711     public void encodeChildren(FacesContext context, UIComponent component)
712             throws IOException JavaDoc {
713     }
714
715     /**
716      * This method is used for debugging.
717      *
718      * @param facesContext
719      * @param uiComponent
720      * @param requestParameterMap
721      * @param hiddenFieldName
722      */

723     private void examineRequest(FacesContext facesContext,
724                                 UIComponent uiComponent,
725                                 Map JavaDoc requestParameterMap, String JavaDoc hiddenFieldName,
726                                 String JavaDoc hiddenValue) {
727         Iterator JavaDoc entries = requestParameterMap.entrySet().iterator();
728         System.out.println("decoding " + ((MenuItem) uiComponent).getValue());
729         System.out.println("request map");
730         while (entries.hasNext()) {
731             Map.Entry JavaDoc next = (Map.Entry JavaDoc) entries.next();
732             if (!next.getKey().toString().equals("rand")) {
733                 System.out.println("[" + next.getKey().toString() + "=" +
734                                    next.getValue() + "]");
735             }
736         }
737         System.out
738                 .println("looking for hidden field [" + hiddenFieldName + "]");
739         System.out.println(
740                 "client id = [" + uiComponent.getClientId(facesContext));
741         System.out.println(
742                 "################################################ QUEUEING for hidden field [" +
743                 hiddenValue + "]");
744     }
745
746     /* (non-Javadoc)
747      * @see com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer
748      * #encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
749      */

750     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
751             throws IOException JavaDoc {
752         DOMContext domContext =
753                 DOMContext.getDOMContext(facesContext, uiComponent);
754         super.encodeEnd(facesContext, uiComponent);
755         domContext.streamWrite(facesContext, uiComponent);
756     }
757
758
759 }
760
Popular Tags