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;
21  
22  import org.apache.myfaces.tobago.context.ClientProperties;
23  import org.apache.myfaces.tobago.internal.component.AbstractUIPage;
24  import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
25  import org.apache.myfaces.tobago.internal.util.StringUtils;
26  import org.apache.myfaces.tobago.layout.Box;
27  import org.apache.myfaces.tobago.layout.Measure;
28  import org.apache.myfaces.tobago.model.PageState;
29  import org.apache.myfaces.tobago.util.ComponentUtils;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import javax.faces.component.UIComponent;
34  import javax.faces.context.FacesContext;
35  import java.util.StringTokenizer;
36  
37  public class PageRendererBase extends LayoutComponentRendererBase {
38  
39    private static final Logger LOG = LoggerFactory.getLogger(PageRendererBase.class);
40  
41    public void decode(final FacesContext facesContext, final UIComponent component) {
42      if (component instanceof AbstractUIPage) {
43        final AbstractUIPage page = (AbstractUIPage) component;
44  
45        decodeActionPosition(facesContext, page);
46        decodePageState(facesContext, page);
47      }
48    }
49  
50    private void decodeActionPosition(final FacesContext facesContext, final AbstractUIPage page) {
51      final String actionIdName = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form-action";
52      final String newActionId = facesContext.getExternalContext().getRequestParameterMap().get(actionIdName);
53      if (LOG.isDebugEnabled()) {
54        LOG.debug("action = " + newActionId);
55      }
56      page.setActionId(newActionId);
57      FacesContextUtils.setActionId(facesContext, newActionId);
58      try {
59        final String actionPositionName
60            = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "action-position";
61        final String actionPositionString
62            = facesContext.getExternalContext().getRequestParameterMap().get(actionPositionName);
63        if (LOG.isDebugEnabled()) {
64          LOG.debug("actionPosition='" + actionPositionString + "'");
65        }
66        if (StringUtils.isNotEmpty(actionPositionString)) {
67          final Box actionPosition = new Box(actionPositionString);
68          page.setActionPosition(actionPosition);
69        } else {
70          page.setActionPosition(null);
71        }
72      } catch (final Exception e) {
73        LOG.warn("Can't analyse parameter for action-position", e);
74      }
75    }
76  
77    @SuppressWarnings("unchecked")
78    private void decodePageState(final FacesContext facesContext, final AbstractUIPage page) {
79      final String name;
80      String value = null;
81      try {
82        name = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form-clientDimension";
83        value = facesContext.getExternalContext().getRequestParameterMap().get(name);
84        if (StringUtils.isNotBlank(value)) {
85          final StringTokenizer tokenizer = new StringTokenizer(value, ";");
86          final Measure width = Measure.valueOf(tokenizer.nextToken());
87          final Measure height = Measure.valueOf(tokenizer.nextToken());
88          // XXX remove me later
89          final PageState pageState = page.getPageState(facesContext);
90          if (pageState != null) {
91            pageState.setClientWidth(width.getPixel());
92            pageState.setClientHeight(height.getPixel());
93          }
94          final ClientProperties clientProperties = ClientProperties.getInstance(facesContext);
95          clientProperties.setPageWidth(width);
96          clientProperties.setPageHeight(height);
97        }
98      } catch (final Exception e) {
99        LOG.error("Error in decoding state: value='" + value + "'", e);
100     }
101   }
102 }