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      public static void prepareDimension(FacesContext facesContext, UIComponent component) {
045    //    LOG.info("prepareDimension for " + component.getClientId(facesContext) + " is " + component.getRendererType());
046        setInnerWidth(facesContext, component);
047        setInnerHeight(facesContext, component);
048      }
049    
050      private static void setInnerWidth(FacesContext facesContext, UIComponent component) {
051        Integer layoutWidth = LayoutUtil.getLayoutWidth(component);
052        if (layoutWidth != null) {
053          int space = layoutWidth.intValue();
054          int innerSpace = LayoutUtil.getInnerSpace(facesContext, component, space, true);
055          component.getAttributes().put(ATTR_INNER_WIDTH, Integer.valueOf(innerSpace));
056        }
057      }
058    
059      private static void setInnerHeight(FacesContext facesContext, UIComponent component) {
060        Integer layoutHeight = LayoutUtil.getLayoutHeight(component);
061        if (layoutHeight != null) {
062          int space = layoutHeight.intValue();
063          int innerSpace = LayoutUtil.getInnerSpace(facesContext, component, space, false);
064          component.getAttributes().put(ATTR_INNER_HEIGHT, Integer.valueOf(innerSpace));
065        }
066      }
067    
068    
069      public void encodeChildrenOfComponent(FacesContext facesContext, UIComponent component) throws IOException {
070        ((LayoutRenderer) getRenderer(facesContext)).encodeChildrenOfComponent(facesContext, component);
071      }
072    
073    
074      public static UILayout getLayout(UIComponent component) {
075        UILayout layout = (UILayout) component.getFacet(FACET_LAYOUT);
076        if (layout == null) {
077          if (component instanceof LayoutProvider) {
078            layout = ((LayoutProvider) component).provideLayout();
079          } else {
080            layout = UIDefaultLayout.getInstance();
081          }
082        }
083        return layout;
084      }
085    }