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