001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Jean-Marc Orliaguet, Chalmers
018 *
019 * $Id$
020 */
021
022package org.nuxeo.theme.jsf.component;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import javax.faces.component.UIOutput;
031import javax.faces.context.FacesContext;
032import javax.faces.context.ResponseWriter;
033
034import org.nuxeo.theme.html.Utils;
035
036public class UITabs extends UIOutput {
037
038    private String identifier;
039
040    private String styleClass;
041
042    private String controlledBy;
043
044    @Override
045    public void encodeAll(final FacesContext context) throws IOException {
046        final ResponseWriter writer = context.getResponseWriter();
047
048        final Map<String, Object> attributes = getAttributes();
049        identifier = (String) attributes.get("identifier");
050        styleClass = (String) attributes.get("styleClass");
051        controlledBy = (String) attributes.get("controlledBy");
052
053        // view
054        final Map<String, Object> view = new HashMap<String, Object>();
055        view.put("id", identifier);
056        final Map<String, Object> widget = new HashMap<String, Object>();
057        widget.put("type", "tabs");
058        if (styleClass != null) {
059            widget.put("styleClass", styleClass);
060        }
061        if (null != controlledBy) {
062            view.put("controllers", controlledBy.split(","));
063        }
064
065        final List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
066        for (Object child : getChildren()) {
067            if (child instanceof UITab) {
068                UITab tab = (UITab) child;
069                Map<String, Object> tabAttributes = tab.getAttributes();
070                Map<String, Object> itemMap = new HashMap<String, Object>();
071                itemMap.put("label", tabAttributes.get("label"));
072
073                String link = (String) tabAttributes.get("link");
074                if (link != null) {
075                    itemMap.put("link", link);
076                }
077
078                String switchTo = (String) tabAttributes.get("switchTo");
079                if (null != switchTo) {
080                    itemMap.put("switchTo", switchTo);
081                }
082
083                items.add(itemMap);
084            }
085        }
086        widget.put("items", items);
087        view.put("widget", widget);
088
089        writer.startElement("ins", this);
090        writer.writeAttribute("class", "view", null);
091        writer.write(Utils.toJson(view));
092        writer.endElement("ins");
093    }
094
095    public String getControlledBy() {
096        return controlledBy;
097    }
098
099    public void setControlledBy(final String controlledBy) {
100        this.controlledBy = controlledBy;
101    }
102
103    public String getIdentifier() {
104        return identifier;
105    }
106
107    public void setIdentifier(final String identifier) {
108        this.identifier = identifier;
109    }
110
111    public String getStyleClass() {
112        return styleClass;
113    }
114
115    public void setStyleClass(final String styleClass) {
116        this.styleClass = styleClass;
117    }
118
119}