View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.renderkit.html.scarborough.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.UITreeNode;
23  import org.apache.myfaces.tobago.internal.component.AbstractUIData;
24  import org.apache.myfaces.tobago.internal.component.AbstractUITree;
25  import org.apache.myfaces.tobago.model.ExpandedState;
26  import org.apache.myfaces.tobago.model.SelectedState;
27  import org.apache.myfaces.tobago.model.Selectable;
28  import org.apache.myfaces.tobago.model.TreePath;
29  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
30  import org.apache.myfaces.tobago.renderkit.css.Classes;
31  import org.apache.myfaces.tobago.renderkit.css.Style;
32  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
33  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
34  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
35  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
36  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
37  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
38  import org.apache.myfaces.tobago.util.ComponentUtils;
39  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  import javax.faces.component.UIComponent;
44  import javax.faces.context.FacesContext;
45  import java.io.IOException;
46  
47  public class TreeRenderer extends LayoutComponentRendererBase {
48  
49    private static final Logger LOG = LoggerFactory.getLogger(TreeRenderer.class);
50  
51    @Override
52    public void decode(FacesContext facesContext, UIComponent component) {
53      RenderUtils.decodeScrollPosition(facesContext, component);
54      final AbstractUITree tree = (AbstractUITree) component;
55      RenderUtils.decodedStateOfTreeData(facesContext, tree);
56    }
57  
58    @Override
59    public boolean getRendersChildren() {
60      return true;
61    }
62  
63    @Override
64    public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
65      // will be rendered in encodeEnd()
66    }
67  
68    @Override
69    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
70  
71      AbstractUITree tree = (AbstractUITree) component;
72  
73      String clientId = tree.getClientId(facesContext);
74      UIComponent root = ComponentUtils.findDescendant(tree, UITreeNode.class);
75      if (root == null) {
76        LOG.error("Can't find the tree root. This may occur while updating a tree from Tobago 1.0 to 1.5. "
77            + "Please refer the documentation to see how to use tree tags.");
78        return;
79      }
80  
81      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
82  
83      writer.startElement(HtmlElements.DIV, tree);
84      writer.writeClassAttribute(Classes.create(tree));
85      Style style = new Style(facesContext, tree);
86      writer.writeStyleAttribute(style);
87      writer.writeIdAttribute(clientId);
88      HtmlRendererUtils.writeDataAttributes(facesContext, writer, tree);
89      writer.writeAttribute("data-tobago-scroll-panel", "true", true);
90  
91      final Selectable selectable = tree.getSelectableAsEnum();
92      if (selectable.isSupportedByTree()) {
93        writer.writeAttribute(DataAttributes.SELECTABLE, selectable.getValue(), false);
94      }
95  
96      final SelectedState selectedState = tree.getSelectedState();
97      final StringBuilder selectedValue = new StringBuilder(",");
98  
99      final ExpandedState expandedState = tree.getExpandedState();
100     final StringBuilder expandedValue = new StringBuilder(",");
101 
102     final int last = tree.isRowsUnlimited() ? Integer.MAX_VALUE : tree.getFirst() + tree.getRows();
103     for (int rowIndex = tree.getFirst(); rowIndex < last; rowIndex++) {
104       tree.setRowIndex(rowIndex);
105       if (!tree.isRowAvailable()) {
106         break;
107       }
108 
109       final TreePath path = tree.getPath();
110 
111       if (selectedState.isSelected(path)) {
112         selectedValue.append(rowIndex);
113         selectedValue.append(",");
114       }
115 
116       if (tree.isFolder() && expandedState.isExpanded(path)) {
117         expandedValue.append(rowIndex);
118         expandedValue.append(",");
119       }
120 
121       for (UIComponent child : tree.getChildren()) {
122         RenderUtils.prepareRendererAll(facesContext, child);
123         RenderUtils.encode(facesContext, child);
124       }
125     }
126     tree.setRowIndex(-1);
127 
128     writer.startElement(HtmlElements.INPUT, tree);
129     writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, false);
130     final String selectedId = clientId + ComponentUtils.SUB_SEPARATOR + AbstractUITree.SUFFIX_SELECTED;
131     writer.writeNameAttribute(selectedId);
132     writer.writeIdAttribute(selectedId);
133     writer.writeClassAttribute(Classes.create(tree, AbstractUITree.SUFFIX_SELECTED));
134     writer.writeAttribute(HtmlAttributes.VALUE, selectedValue.toString(), false);
135     writer.endElement(HtmlElements.INPUT);
136 
137     writer.startElement(HtmlElements.INPUT, tree);
138     writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, false);
139     final String expandedId = clientId + ComponentUtils.SUB_SEPARATOR + AbstractUIData.SUFFIX_EXPANDED;
140     writer.writeNameAttribute(expandedId);
141     writer.writeIdAttribute(expandedId);
142     writer.writeClassAttribute(Classes.create(tree, AbstractUIData.SUFFIX_EXPANDED));
143     writer.writeAttribute(HtmlAttributes.VALUE, expandedValue.toString(), false);
144     writer.endElement(HtmlElements.INPUT);
145 
146     RenderUtils.writeScrollPosition(facesContext, writer, tree);
147 
148     writer.endElement(HtmlElements.DIV);
149   }
150 
151   @Override
152   public boolean getPrepareRendersChildren() {
153     return true;
154   }
155 }