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.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.myfaces.tobago.component.UITreeSelect;
25  import org.apache.myfaces.tobago.internal.component.AbstractUIData;
26  import org.apache.myfaces.tobago.internal.component.AbstractUITreeListbox;
27  import org.apache.myfaces.tobago.internal.component.AbstractUITreeNode;
28  import org.apache.myfaces.tobago.internal.util.StringUtils;
29  import org.apache.myfaces.tobago.model.Selectable;
30  import org.apache.myfaces.tobago.renderkit.RendererBase;
31  import org.apache.myfaces.tobago.renderkit.css.Classes;
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.util.ComponentUtils;
37  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
38  
39  import javax.faces.component.UIComponent;
40  import javax.faces.component.UINamingContainer;
41  import javax.faces.context.FacesContext;
42  import java.io.IOException;
43  
44  public class TreeSelectRenderer extends RendererBase {
45  
46    private static final Log LOG = LogFactory.getLog(TreeSelectRenderer.class);
47  
48    public void decode(FacesContext facesContext, UIComponent component) {
49  
50      final UITreeSelect select = (UITreeSelect) component;
51      final AbstractUITreeNode node = ComponentUtils.findAncestor(select, AbstractUITreeNode.class);
52      final AbstractUIData data = ComponentUtils.findAncestor(node, AbstractUIData.class);
53  
54      if (ComponentUtils.isOutputOnly(select)) {
55        return;
56      }
57  
58      final String clientId = select.getClientId(facesContext);
59      final String name;
60      if (data.getSelectableAsEnum().isSingle()) {
61        name = getClientIdWithoutRowIndex(data, clientId);
62      } else {
63        name = clientId;
64      }
65  
66      final String parameter = (String) facesContext.getExternalContext().getRequestParameterMap().get(name);
67  
68      if (LOG.isDebugEnabled()) {
69        LOG.debug("parameter = '" + parameter + "'");
70      }
71  
72      final boolean selected = clientId.equals(parameter);
73      if (!select.isValueStoredInState()) {
74        select.setSubmittedValue(selected ? "true" : "false");
75      }
76    }
77  
78    @Override
79    public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
80  
81      final UITreeSelect select = (UITreeSelect) component;
82      final AbstractUITreeNode node = ComponentUtils.findAncestor(select, AbstractUITreeNode.class);
83      final AbstractUIData data = ComponentUtils.findAncestor(node, AbstractUIData.class);
84  
85      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
86  
87      if (data instanceof AbstractUITreeListbox) {
88        writer.write(StringUtils.defaultString(select.getLabel()));
89        return;
90      }
91  
92      final String id = select.getClientId(facesContext);
93      final String currentValue = getCurrentValue(facesContext, select);
94      final boolean checked;
95      if (select.isValueStoredInState()) {
96        checked = data.getSelectedState().isSelected(node.getPath());
97      } else {
98        checked = "true".equals(currentValue);
99      }
100 
101     final boolean folder = data.isFolder();
102     final Selectable selectable = data.getSelectableAsEnum();
103 
104 
105     writer.startElement(HtmlElements.SPAN, null);
106     writer.writeClassAttribute(Classes.create(select));
107     HtmlRendererUtils.writeDataAttributes(facesContext, writer, select);
108 
109     if (select.isShowCheckbox()
110         && selectable != Selectable.NONE
111         && (!selectable.isLeafOnly() || !folder)) {
112       writer.startElement(HtmlElements.INPUT, null);
113       if (selectable.isSingle()) {
114         writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.RADIO, false);
115         writer.writeNameAttribute(getClientIdWithoutRowIndex(data, id));
116       } else {
117         writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.CHECKBOX, false);
118         writer.writeNameAttribute(id);
119       }
120       writer.writeAttribute(HtmlAttributes.VALUE, id, false);
121       writer.writeIdAttribute(id);
122       writer.writeAttribute(HtmlAttributes.CHECKED, checked);
123       writer.endElement(HtmlElements.INPUT);
124     }
125 
126     // label
127     final String label = select.getLabel();
128     if (StringUtils.isNotEmpty(label)) {
129       writer.startElement(HtmlElements.LABEL, null);
130       writer.writeClassAttribute(Classes.create(select, "label"));
131       HtmlRendererUtils.renderTip(select, writer);
132       writer.writeAttribute(HtmlAttributes.FOR, id, false);
133       writer.writeText(label);
134       writer.endElement(HtmlElements.LABEL);
135     }
136 
137     writer.endElement(HtmlElements.SPAN);
138   }
139 
140   private String getClientIdWithoutRowIndex(AbstractUIData data, String id) {
141     final char separatorChar = UINamingContainer.getSeparatorChar(FacesContext.getCurrentInstance());
142     return id.replace("" + separatorChar + data.getRowIndex() + separatorChar, "" + separatorChar);
143   }
144 
145   @Override
146   public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
147   }
148 }