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