001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 * $Id$
013 */
014
015package org.nuxeo.theme.jsf.component;
016
017import java.io.IOException;
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import javax.faces.component.UIOutput;
024import javax.faces.context.FacesContext;
025import javax.faces.context.ResponseWriter;
026
027import org.nuxeo.theme.html.Utils;
028
029public class UITabs extends UIOutput {
030
031    private String identifier;
032
033    private String styleClass;
034
035    private String controlledBy;
036
037    @Override
038    public void encodeAll(final FacesContext context) throws IOException {
039        final ResponseWriter writer = context.getResponseWriter();
040
041        final Map<String, Object> attributes = getAttributes();
042        identifier = (String) attributes.get("identifier");
043        styleClass = (String) attributes.get("styleClass");
044        controlledBy = (String) attributes.get("controlledBy");
045
046        // view
047        final Map<String, Object> view = new HashMap<String, Object>();
048        view.put("id", identifier);
049        final Map<String, Object> widget = new HashMap<String, Object>();
050        widget.put("type", "tabs");
051        if (styleClass != null) {
052            widget.put("styleClass", styleClass);
053        }
054        if (null != controlledBy) {
055            view.put("controllers", controlledBy.split(","));
056        }
057
058        final List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
059        for (Object child : getChildren()) {
060            if (child instanceof UITab) {
061                UITab tab = (UITab) child;
062                Map<String, Object> tabAttributes = tab.getAttributes();
063                Map<String, Object> itemMap = new HashMap<String, Object>();
064                itemMap.put("label", tabAttributes.get("label"));
065
066                String link = (String) tabAttributes.get("link");
067                if (link != null) {
068                    itemMap.put("link", link);
069                }
070
071                String switchTo = (String) tabAttributes.get("switchTo");
072                if (null != switchTo) {
073                    itemMap.put("switchTo", switchTo);
074                }
075
076                items.add(itemMap);
077            }
078        }
079        widget.put("items", items);
080        view.put("widget", widget);
081
082        writer.startElement("ins", this);
083        writer.writeAttribute("class", "view", null);
084        writer.write(Utils.toJson(view));
085        writer.endElement("ins");
086    }
087
088    public String getControlledBy() {
089        return controlledBy;
090    }
091
092    public void setControlledBy(final String controlledBy) {
093        this.controlledBy = controlledBy;
094    }
095
096    public String getIdentifier() {
097        return identifier;
098    }
099
100    public void setIdentifier(final String identifier) {
101        this.identifier = identifier;
102    }
103
104    public String getStyleClass() {
105        return styleClass;
106    }
107
108    public void setStyleClass(final String styleClass) {
109        this.styleClass = styleClass;
110    }
111
112}