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.RendererTypes;
23  import org.apache.myfaces.tobago.component.UITreeNode;
24  import org.apache.myfaces.tobago.component.UITreeSelect;
25  import org.apache.myfaces.tobago.internal.component.AbstractUITree;
26  import org.apache.myfaces.tobago.layout.Measure;
27  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
28  import org.apache.myfaces.tobago.renderkit.css.Classes;
29  import org.apache.myfaces.tobago.renderkit.css.Position;
30  import org.apache.myfaces.tobago.renderkit.css.Style;
31  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
33  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
34  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
35  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
36  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
37  import org.apache.myfaces.tobago.util.ComponentUtils;
38  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
39  
40  import javax.faces.component.UIComponent;
41  import javax.faces.context.FacesContext;
42  import java.io.IOException;
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  public class TreeListboxRenderer extends LayoutComponentRendererBase {
47  
48    public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
49      super.prepareRender(facesContext, component);
50      setRendererTypeForCommandsAndNodes(component);
51    }
52  
53    protected void setRendererTypeForCommandsAndNodes(UIComponent component) {
54      for (UIComponent child : component.getChildren()) {
55        if (child instanceof UITreeNode) {
56          child.setRendererType(RendererTypes.TREE_LISTBOX_NODE);
57        }
58        setRendererTypeForCommandsAndNodes(child);
59      }
60    }
61  
62    @Override
63    public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
64      // will be rendered in encodeEnd()
65    }
66  
67    @Override
68    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
69  
70      final AbstractUITree tree = (AbstractUITree) component;
71      final String clientId = tree.getClientId(facesContext);
72      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
73      final Style style = new Style(facesContext, tree);
74      final Style scrollDivStyle = new Style();
75  
76      writer.startElement(HtmlElements.DIV, tree);
77      scrollDivStyle.setWidth(Measure.valueOf(6 * 160)); // todo: depth * width of a select
78      scrollDivStyle.setHeight(style.getHeight() // todo: what, when there is no scrollbar?
79          .subtract(15)); // todo: scrollbar height
80      scrollDivStyle.setPosition(Position.ABSOLUTE);
81      writer.writeStyleAttribute(scrollDivStyle);
82  
83      writer.startElement(HtmlElements.DIV, tree);
84      writer.writeClassAttribute(Classes.create(tree));
85      HtmlRendererUtils.writeDataAttributes(facesContext, writer, tree);
86      writer.writeStyleAttribute(style);
87  
88      writer.startElement(HtmlElements.INPUT, tree);
89      writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, false);
90      writer.writeNameAttribute(clientId);
91      writer.writeIdAttribute(clientId);
92      writer.writeAttribute(HtmlAttributes.VALUE, ";", false);
93      writer.endElement(HtmlElements.INPUT);
94  
95      writer.startElement(HtmlElements.INPUT, tree);
96      writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, false);
97      writer.writeNameAttribute(clientId + ComponentUtils.SUB_SEPARATOR + AbstractUITree.SUFFIX_MARKED);
98      writer.writeIdAttribute(clientId + ComponentUtils.SUB_SEPARATOR + AbstractUITree.SUFFIX_MARKED);
99      writer.writeAttribute(HtmlAttributes.VALUE, "", false);
100     writer.endElement(HtmlElements.INPUT);
101 
102     if (tree.getSelectableAsEnum().isSupportedByTreeListbox()) {
103       writer.startElement(HtmlElements.INPUT, tree);
104       writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, false);
105       writer.writeNameAttribute(clientId + AbstractUITree.SELECT_STATE);
106       writer.writeIdAttribute(clientId + AbstractUITree.SELECT_STATE);
107       writer.writeAttribute(HtmlAttributes.VALUE, ";", false);
108       writer.writeAttribute(DataAttributes.SELECTION_MODE, tree.getSelectableAsEnum().name(), false);
109       writer.endElement(HtmlElements.INPUT);
110     }
111 
112     List<Integer> thisLevel = new ArrayList<Integer>();
113     thisLevel.add(0);
114     List<Integer> nextLevel = new ArrayList<Integer>();
115     int depth = tree.getTreeDataModel().getDepth() != -1
116         ? tree.getTreeDataModel().getDepth()
117         : 7;  // XXX not a fix value!!!
118     // todo: use (TreeListbox ?)Layout
119     Measure currentWidth = tree.getCurrentWidth();
120     Measure width = currentWidth.divide(depth);
121     for (int level = 0; level < depth; level++) {
122 
123       writer.startElement(HtmlElements.DIV, null);
124       writer.writeClassAttribute(Classes.create(tree, "level"));
125       Style levelStyle = new Style();
126       levelStyle.setLeft(width.multiply(level));
127       levelStyle.setWidth(width);
128       writer.writeStyleAttribute(levelStyle);
129       // at the start of each div there is an empty and disabled select tag to show empty area.
130       // this is not needed for the 1st level.
131       if (level > 0) {
132         writer.startElement(HtmlElements.SELECT, null);
133         writer.writeAttribute(HtmlAttributes.DISABLED, true);
134         writer.writeAttribute(HtmlAttributes.SIZE, 9); // must be > 1, but the real size comes from the layout
135         writer.writeClassAttribute(Classes.create(tree, "select"));
136         writer.endElement(HtmlElements.SELECT);
137       }
138 
139       for(Integer rowIndex : thisLevel) {
140         encodeSelectBox(facesContext, tree, writer, rowIndex, nextLevel);
141       }
142 
143       thisLevel.clear();
144       List<Integer> swap = thisLevel;
145       thisLevel = nextLevel;
146       nextLevel = swap;
147 
148       writer.endElement(HtmlElements.DIV);
149     }
150 
151     writer.endElement(HtmlElements.DIV);
152     writer.endElement(HtmlElements.DIV);
153 
154     tree.setRowIndex(-1);
155   }
156 
157   private void encodeSelectBox(
158       FacesContext facesContext, AbstractUITree tree, TobagoResponseWriter writer,
159       int parentRowIndex, List<Integer> foldersRowIndices)
160       throws IOException {
161 
162     tree.setRowIndex(parentRowIndex);
163 
164     final UITreeNode node = ComponentUtils.findDescendant(tree, UITreeNode.class);
165     final String parentId = node.getClientId(facesContext);
166 
167     writer.startElement(HtmlElements.SELECT, tree);
168     writer.writeClassAttribute(Classes.create(tree, "select"));
169     if (parentId != null) {
170       writer.writeAttribute(DataAttributes.TREE_PARENT, parentId, false);
171     }
172 
173     writer.writeAttribute(HtmlAttributes.SIZE, 9); // must be > 1, but the real size comes from the layout
174 //    writer.writeAttribute(HtmlAttributes.MULTIPLE, siblingMode);
175 
176     final UITreeSelect label = ComponentUtils.findDescendant(tree, UITreeSelect.class);
177     final Object labelValue = label.getLabel();
178     if (labelValue != null) {
179       writer.startElement(HtmlElements.OPTGROUP, tree);
180       writer.writeAttribute(HtmlAttributes.LABEL, labelValue.toString(), true);
181       writer.endElement(HtmlElements.OPTGROUP);
182     }
183 
184     final List<Integer> rowIndices = tree.getRowIndicesOfChildren();
185 
186     for (Integer rowIndex : rowIndices) {
187       tree.setRowIndex(rowIndex);
188       if (!tree.isRowAvailable()) {
189         break;
190       }
191 
192       for (UIComponent child : tree.getChildren()) {
193         RenderUtils.prepareRendererAll(facesContext, child);
194         RenderUtils.encode(facesContext, child);
195       }
196 
197       if (tree.isFolder()) {
198         foldersRowIndices.add(rowIndex);
199       }
200     }
201 
202     writer.endElement(HtmlElements.SELECT);
203   }
204 }