001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.component;
021
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_INNER_HEIGHT;
025 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_INNER_WIDTH;
026 import static org.apache.myfaces.tobago.TobagoConstants.FACET_LAYOUT;
027 import org.apache.myfaces.tobago.renderkit.LayoutRenderer;
028 import org.apache.myfaces.tobago.util.LayoutUtil;
029
030 import javax.faces.component.UIComponent;
031 import javax.faces.component.UIComponentBase;
032 import javax.faces.context.FacesContext;
033 import java.io.IOException;
034
035 public abstract class UILayout extends UIComponentBase {
036
037 private static final Log LOG = LogFactory.getLog(UILayout.class);
038
039 public void layoutBegin(FacesContext facesContext, UIComponent component) {
040 // prepare component to render
041 prepareDimension(facesContext, component);
042
043 }
044
045
046 public static void prepareDimension(FacesContext facesContext, UIComponent component) {
047 // LOG.info("prepareDimension for " + component.getClientId(facesContext) + " is " + component.getRendererType());
048 setInnerWidth(facesContext, component);
049 setInnerHeight(facesContext, component);
050 }
051
052 private static void setInnerWidth(FacesContext facesContext, UIComponent component) {
053 Integer layoutWidth = LayoutUtil.getLayoutWidth(component);
054 if (layoutWidth != null) {
055 int space = layoutWidth.intValue();
056 int innerSpace = LayoutUtil.getInnerSpace(facesContext, component, space, true);
057 component.getAttributes().put(ATTR_INNER_WIDTH, Integer.valueOf(innerSpace));
058 }
059 }
060
061 private static void setInnerHeight(FacesContext facesContext, UIComponent component) {
062 Integer layoutHeight = LayoutUtil.getLayoutHeight(component);
063 if (layoutHeight != null) {
064 int space = layoutHeight.intValue();
065 int innerSpace = LayoutUtil.getInnerSpace(facesContext, component, space, false);
066 component.getAttributes().put(ATTR_INNER_HEIGHT, Integer.valueOf(innerSpace));
067 }
068 }
069
070
071 public void encodeChildrenOfComponent(FacesContext facesContext, UIComponent component) throws IOException {
072 ((LayoutRenderer) getRenderer(facesContext)).encodeChildrenOfComponent(facesContext, component);
073 }
074
075
076 public static UILayout getLayout(UIComponent component) {
077 UILayout layout = (UILayout) component.getFacet(FACET_LAYOUT);
078 if (layout == null) {
079 if (component instanceof LayoutProvider) {
080 layout = ((LayoutProvider) component).provideLayout();
081 } else {
082 layout = UIDefaultLayout.getInstance();
083 }
084 }
085 return layout;
086 }
087 }