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.internal.renderkit.renderer;
21  
22  import org.apache.myfaces.tobago.internal.component.AbstractUICollapsiblePanel;
23  import org.apache.myfaces.tobago.internal.util.StringUtils;
24  import org.apache.myfaces.tobago.renderkit.RendererBase;
25  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
26  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
27  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
28  import org.apache.myfaces.tobago.util.ComponentUtils;
29  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
30  
31  import javax.faces.context.FacesContext;
32  import java.io.IOException;
33  import java.util.Map;
34  
35  public class CollapsiblePanelRendererBase<T extends AbstractUICollapsiblePanel> extends RendererBase<T> {
36  
37    private static final String SUFFIX_COLLAPSE = "collapse";
38  
39    @Override
40    public void decodeInternal(final FacesContext facesContext, final T component) {
41      super.decodeInternal(facesContext, component);
42  
43      final String clientId = component.getClientId(facesContext);
44      final String hiddenId = clientId + ComponentUtils.SUB_SEPARATOR + SUFFIX_COLLAPSE;
45  
46      final Map<String, String> requestParameterMap = facesContext.getExternalContext().getRequestParameterMap();
47      if (requestParameterMap.containsKey(hiddenId)) {
48        final String newValue = requestParameterMap.get(hiddenId);
49        if (StringUtils.isNotBlank(newValue)) {
50          component.setSubmittedCollapsed(Boolean.valueOf(newValue));
51        }
52      }
53    }
54  
55    protected void encodeHidden(final TobagoResponseWriter writer, final String clientId, final boolean collapsed)
56        throws IOException {
57      final String hiddenId = clientId + ComponentUtils.SUB_SEPARATOR + SUFFIX_COLLAPSE;
58      writer.startElement(HtmlElements.INPUT);
59      writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN);
60      writer.writeNameAttribute(hiddenId);
61      writer.writeIdAttribute(hiddenId);
62      writer.writeAttribute(HtmlAttributes.VALUE, Boolean.toString(collapsed), false);
63      writer.endElement(HtmlElements.INPUT);
64    }
65  
66    @Override
67    public boolean getRendersChildren() {
68      return true;
69    }
70  
71    @Override
72    public void encodeChildrenInternal(final FacesContext facesContext, final T component) throws IOException {
73      if (component.isNormalLifecycle()) {
74        super.encodeChildrenInternal(facesContext, component);
75      }
76    }
77  }