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(FacesContext facesContext, UIComponent component) {
42      if (component instanceof AbstractUIPage) {
43        AbstractUIPage page = (AbstractUIPage) component;
44  
45        decodeActionPosition(facesContext, page);
46        decodePageState(facesContext, page);
47      }
48    }
49  
50    private void decodeActionPosition(FacesContext facesContext, AbstractUIPage page) {
51      String actionIdName = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form-action";
52      String newActionId = (String) 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        String actionPositionName = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "action-position";
60        String actionPositionString = (String)
61            facesContext.getExternalContext().getRequestParameterMap().get(actionPositionName);
62        if (LOG.isDebugEnabled()) {
63          LOG.debug("actionPosition='" + actionPositionString + "'");
64        }
65        if (StringUtils.isNotEmpty(actionPositionString)) {
66          Box actionPosition = new Box(actionPositionString);
67          page.setActionPosition(actionPosition);
68        } else {
69          page.setActionPosition(null);
70        }
71      } catch (Exception e) {
72        LOG.warn("Can't analyse parameter for action-position", e);
73      }
74    }
75  
76    @SuppressWarnings("unchecked")
77    private void decodePageState(FacesContext facesContext, AbstractUIPage page) {
78      String name;
79      String value = null;
80      try {
81        name = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form-clientDimension";
82        value = facesContext.getExternalContext().getRequestParameterMap().get(name);
83        if (StringUtils.isNotBlank(value)) {
84          StringTokenizer tokenizer = new StringTokenizer(value, ";");
85          final Measure width = Measure.valueOf(tokenizer.nextToken());
86          final Measure height = Measure.valueOf(tokenizer.nextToken());
87          // XXX remove me later
88          PageState pageState = page.getPageState(facesContext);
89          if (pageState != null) {
90            pageState.setClientWidth(width.getPixel());
91            pageState.setClientHeight(height.getPixel());
92          }
93          final ClientProperties clientProperties = ClientProperties.getInstance(facesContext);
94          clientProperties.setPageWidth(width);
95          clientProperties.setPageHeight(height);
96        }
97      } catch (Exception e) {
98        LOG.error("Error in decoding state: value='" + value + "'", e);
99      }
100   }
101 }