001    package org.apache.myfaces.tobago.renderkit;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.apache.myfaces.tobago.layout.Measure;
022    import org.apache.myfaces.tobago.model.PageState;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    import org.apache.myfaces.tobago.internal.component.AbstractUIPage;
026    import org.apache.myfaces.tobago.layout.Box;
027    import org.apache.myfaces.tobago.util.ComponentUtils;
028    
029    import javax.faces.component.UIComponent;
030    import javax.faces.context.FacesContext;
031    import java.util.StringTokenizer;
032    
033    public class PageRendererBase extends LayoutComponentRendererBase {
034    
035      private static final Logger LOG = LoggerFactory.getLogger(PageRendererBase.class);
036    
037      public void decode(FacesContext facesContext, UIComponent component) {
038        if (component instanceof AbstractUIPage) {
039          AbstractUIPage page = (AbstractUIPage) component;
040    
041          decodeActionPosition(facesContext, page);
042          decodePageState(facesContext, page);
043        }
044      }
045    
046      private void decodeActionPosition(FacesContext facesContext, AbstractUIPage page) {
047        String actionIdName = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form-action";
048        String newActionId = (String) facesContext.getExternalContext().getRequestParameterMap().get(actionIdName);
049        if (LOG.isDebugEnabled()) {
050          LOG.debug("action = " + newActionId);
051        }
052        page.setActionId(newActionId);
053    
054        try {
055          String actionPositionName = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "action-position";
056          String actionPositionString = (String)
057              facesContext.getExternalContext().getRequestParameterMap().get(actionPositionName);
058          if (LOG.isDebugEnabled()) {
059            LOG.debug("actionPosition='" + actionPositionString + "'");
060          }
061          if (StringUtils.isNotEmpty(actionPositionString)) {
062            Box actionPosition = new Box(actionPositionString);
063            page.setActionPosition(actionPosition);
064          } else {
065            page.setActionPosition(null);
066          }
067        } catch (Exception e) {
068          LOG.warn("Can't analyse parameter for action-position", e);
069        }
070      }
071    
072      @SuppressWarnings("unchecked")
073      private void decodePageState(FacesContext facesContext, AbstractUIPage page) {
074        String name;
075        String value = null;
076        try {
077          name = page.getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "form-clientDimension";
078          value = (String) facesContext.getExternalContext().getRequestParameterMap().get(name);
079          if (value != null) {
080            StringTokenizer tokenizer = new StringTokenizer(value, ";");
081            Measure width = Measure.parse(tokenizer.nextToken());
082            Measure height = Measure.parse(tokenizer.nextToken());
083            // XXX remove me later
084            PageState pageState = page.getPageState(facesContext);
085            if (pageState != null) {
086              pageState.setClientWidth(width.getPixel());
087              pageState.setClientHeight(height.getPixel());
088            }
089            facesContext.getExternalContext().getRequestMap().put("tobago-page-clientDimension-width", width);
090            facesContext.getExternalContext().getRequestMap().put("tobago-page-clientDimension-height", height);
091          }
092        } catch (Exception e) {
093          LOG.error("Error in decoding state: value='" + value + "'", e);
094        }
095      }
096    }