001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component.menubar;
035:
036: import com.icesoft.faces.component.InvalidComponentTypeException;
037: import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
038:
039: import javax.faces.component.UIComponent;
040: import javax.faces.context.FacesContext;
041: import javax.faces.el.MethodBinding;
042: import javax.faces.event.ActionListener;
043: import java.io.IOException;
044: import java.util.List;
045:
046: public class MenuItemsRenderer extends DomBasicRenderer {
047:
048: public boolean getRendersChildren() {
049: return true;
050: }
051:
052: public void encodeChildren(FacesContext context,
053: UIComponent component) throws IOException {
054: MenuItems menuItems = (MenuItems) component;
055: List children = (List) menuItems.getValue();
056: // extract the actionListener and action methodBindings from the MenuItems
057: // then attach them to the child MenuItem objects
058: ActionListener[] als = menuItems.getActionListeners();
059: MethodBinding almb = menuItems.getActionListener();
060: MethodBinding amb = menuItems.getAction();
061: setParentsRecursive(component, children, als, almb, amb);
062: renderRecursive(context, children);
063: }
064:
065: private void renderRecursive(FacesContext context, List children)
066: throws IOException {
067: for (int i = 0; i < children.size(); i++) {
068: UIComponent nextChildMenuNode = (UIComponent) children
069: .get(i);
070: encodeParentAndChildren(context, nextChildMenuNode);
071: }
072: }
073:
074: private void setParentsRecursive(UIComponent parent, List children,
075: ActionListener[] als, MethodBinding almb, MethodBinding amb) {
076: for (int i = 0; i < children.size(); i++) {
077: UIComponent nextChild = null;
078: if (!((nextChild = (UIComponent) children.get(i)) instanceof MenuItemBase)) {
079: throw new InvalidComponentTypeException(
080: "MenuItemsRenderer expects MenuItemBase children");
081: }
082: nextChild.setParent(parent);
083:
084: // here's where we attach the action and actionlistener methodBindings to the MenuItem
085: if (nextChild instanceof MenuItemBase) {
086: MenuItemBase nextChildMenuItemBase = (MenuItemBase) nextChild;
087: if (null != als) {
088: for (int j = 0; j < als.length; j++) {
089: nextChildMenuItemBase
090: .removeActionListener(als[j]);
091: nextChildMenuItemBase.addActionListener(als[j]);
092: }
093: }
094: if (null != almb) {
095: nextChildMenuItemBase.setActionListener(almb);
096: }
097: if (null != amb) {
098: nextChildMenuItemBase.setAction(amb);
099: }
100: }
101:
102: List grandChildren = nextChild.getChildren();
103: setParentsRecursive(nextChild, grandChildren, als, almb,
104: amb);
105: }
106: }
107: }
|