001    package org.apache.myfaces.tobago.renderkit.html.scarborough.standard.tag;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.component.UITreeData;
021    import org.apache.myfaces.tobago.component.UITreeNode;
022    import org.apache.myfaces.tobago.internal.component.AbstractUITree;
023    import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
024    import org.apache.myfaces.tobago.renderkit.css.Classes;
025    import org.apache.myfaces.tobago.renderkit.css.Style;
026    import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
027    import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
028    import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
029    import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
030    import org.apache.myfaces.tobago.util.ComponentUtils;
031    import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
032    import org.slf4j.Logger;
033    import org.slf4j.LoggerFactory;
034    
035    import javax.faces.component.UIComponent;
036    import javax.faces.context.FacesContext;
037    import java.io.IOException;
038    import java.util.Arrays;
039    
040    public class TreeRenderer extends LayoutComponentRendererBase {
041    
042      private static final Logger LOG = LoggerFactory.getLogger(TreeRenderer.class);
043    
044      @Override
045      public void decode(FacesContext facesContext, UIComponent component) {
046        if (ComponentUtils.isOutputOnly(component)) {
047          return;
048        }
049    
050        AbstractUITree tree = (AbstractUITree) component;
051        tree.setValid(true);
052      }
053    
054      @Override
055      public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
056        // will be rendered in encodeEnd()
057      }
058    
059      @Override
060      public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
061    
062        AbstractUITree tree = (AbstractUITree) component;
063    
064        String clientId = tree.getClientId(facesContext);
065        UIComponent root = tree.getRoot();
066        if (root == null) {
067          LOG.error("Can't find the tree root. This may occur while updating a tree from Tobago 1.0 to 1.5. "
068              + "Please refer the documentation to see how to use tree tags.");
069          return;
070        }
071    
072        TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
073    
074        writer.startElement(HtmlElements.DIV, tree);
075        writer.writeClassAttribute(Classes.create(tree));
076        Style style = new Style(facesContext, tree);
077        writer.writeStyleAttribute(style);
078    
079        writer.startElement(HtmlElements.INPUT, tree);
080        writer.writeAttribute(HtmlAttributes.TYPE, "hidden", false);
081        writer.writeNameAttribute(clientId);
082        writer.writeIdAttribute(clientId);
083        writer.writeAttribute(HtmlAttributes.VALUE, ";", false);
084        writer.endElement(HtmlElements.INPUT);
085    
086        writer.startElement(HtmlElements.INPUT, tree);
087        writer.writeAttribute(HtmlAttributes.TYPE, "hidden", false);
088        writer.writeNameAttribute(clientId + AbstractUITree.MARKED);
089        writer.writeIdAttribute(clientId + AbstractUITree.MARKED);
090        writer.writeAttribute(HtmlAttributes.VALUE, "", false);
091        writer.endElement(HtmlElements.INPUT);
092    
093    /*
094        if (tree.getSelectableAsEnum().isSupportedByTree()) {
095          writer.startElement(HtmlElements.INPUT, tree);
096          writer.writeAttribute(HtmlAttributes.TYPE, "hidden", false);
097          writer.writeNameAttribute(clientId + AbstractUITree.SELECT_STATE);
098          writer.writeIdAttribute(clientId + AbstractUITree.SELECT_STATE);
099          writer.writeAttribute(HtmlAttributes.VALUE, ";", false);
100          writer.endElement(HtmlElements.INPUT);
101        }
102    */
103    
104        RenderUtils.encode(facesContext, root, Arrays.asList(UITreeNode.class, UITreeData.class));
105    
106        writer.endElement(HtmlElements.DIV);
107      }
108    }