001    package org.apache.myfaces.tobago.component;
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.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_INNER_HEIGHT;
023    import static org.apache.myfaces.tobago.TobagoConstants.ATTR_INNER_WIDTH;
024    import static org.apache.myfaces.tobago.TobagoConstants.FACET_LAYOUT;
025    import org.apache.myfaces.tobago.renderkit.LayoutRenderer;
026    import org.apache.myfaces.tobago.util.LayoutUtil;
027    
028    import javax.faces.component.UIComponent;
029    import javax.faces.component.UIComponentBase;
030    import javax.faces.context.FacesContext;
031    import java.io.IOException;
032    
033    public abstract class UILayout extends UIComponentBase {
034    
035      private static final Log LOG = LogFactory.getLog(UILayout.class);
036    
037      public void layoutBegin(FacesContext facesContext, UIComponent component) {
038        // prepare component to render
039        prepareDimension(facesContext, component);
040    
041      }
042    
043    
044    
045      public static void prepareDimension(FacesContext facesContext, UIComponent component) {
046    //    LOG.info("prepareDimension for " + component.getClientId(facesContext) + " is " + component.getRendererType());
047        setInnerWidth(facesContext, component);
048        setInnerHeight(facesContext, component);
049      }
050    
051      private static void setInnerWidth(FacesContext facesContext, UIComponent component) {
052        Integer  layoutWidth = LayoutUtil.getLayoutWidth(component);
053        if (layoutWidth != null) {
054          int space = layoutWidth.intValue();
055          int innerSpace = LayoutUtil.getInnerSpace(facesContext, component, space, true);
056          component.getAttributes().put(ATTR_INNER_WIDTH, Integer.valueOf(innerSpace));
057        }
058      }
059    
060      private static void setInnerHeight(FacesContext facesContext, UIComponent component) {
061        Integer  layoutHeight = LayoutUtil.getLayoutHeight(component);
062        if (layoutHeight != null) {
063          int space = layoutHeight.intValue();
064          int innerSpace = LayoutUtil.getInnerSpace(facesContext, component, space, false);
065          component.getAttributes().put(ATTR_INNER_HEIGHT, Integer.valueOf(innerSpace));
066        }
067      }
068    
069    
070      public void encodeChildrenOfComponent(FacesContext facesContext, UIComponent component) throws IOException {
071        ((LayoutRenderer) getRenderer(facesContext)).encodeChildrenOfComponent(facesContext, component);
072      }
073    
074    
075      public static UILayout getLayout(UIComponent component) {
076        UILayout layout = (UILayout) component.getFacet(FACET_LAYOUT);
077        if (layout == null) {
078          if (component instanceof LayoutProvider) {
079            layout = ((LayoutProvider) component).provideLayout();
080          } else {
081            layout = UIDefaultLayout.getInstance();
082          }
083        }
084        return layout;
085      }
086    }