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.internal.component;
21  
22  import org.apache.myfaces.tobago.component.ComponentTypes;
23  import org.apache.myfaces.tobago.component.Facets;
24  import org.apache.myfaces.tobago.component.OnComponentPopulated;
25  import org.apache.myfaces.tobago.component.RendererTypes;
26  import org.apache.myfaces.tobago.internal.layout.LayoutUtils;
27  import org.apache.myfaces.tobago.layout.LayoutComponent;
28  import org.apache.myfaces.tobago.layout.LayoutContainer;
29  import org.apache.myfaces.tobago.layout.LayoutManager;
30  import org.apache.myfaces.tobago.util.ComponentUtils;
31  import org.apache.myfaces.tobago.util.CreateComponentUtils;
32  
33  import javax.faces.component.UIComponent;
34  import javax.faces.context.FacesContext;
35  import java.io.IOException;
36  import java.util.List;
37  
38  public abstract class AbstractUIPanel extends AbstractUIPanelBase
39      implements OnComponentPopulated, LayoutContainer, LayoutComponent {
40  
41    @Override
42    public void encodeBegin(final FacesContext facesContext) throws IOException {
43  
44      super.encodeBegin(facesContext);
45      ((AbstractUILayoutBase) getLayoutManager()).encodeBegin(facesContext);
46    }
47  
48    @Override
49    public void encodeChildren(final FacesContext facesContext) throws IOException {
50  
51      ((AbstractUILayoutBase) getLayoutManager()).encodeChildren(facesContext);
52    }
53  
54    @Override
55    public void encodeEnd(final FacesContext facesContext) throws IOException {
56  
57      ((AbstractUILayoutBase) getLayoutManager()).encodeEnd(facesContext);
58      super.encodeEnd(facesContext);
59    }
60  
61    public void onComponentPopulated(final FacesContext facesContext, final UIComponent parent) {
62    }
63  
64    public List<LayoutComponent> getComponents() {
65      return LayoutUtils.findLayoutChildren(this);
66    }
67  
68    public LayoutManager getLayoutManager() {
69  
70      final UIComponent base;
71  
72      final UIComponent compositeFacet = getFacet(COMPOSITE_FACET_NAME);
73      if (compositeFacet != null) {
74        base = compositeFacet.getChildren().get(0);
75      } else {
76        base = this;
77      }
78  
79      final UIComponent layoutFacet = base.getFacet(Facets.LAYOUT);
80      if (layoutFacet != null) {
81        if (layoutFacet instanceof LayoutManager) {
82          return (LayoutManager) layoutFacet;
83        } else {
84          return (LayoutManager) ComponentUtils.findChild(layoutFacet, AbstractUILayoutBase.class);
85        }
86      } else {
87        final LayoutManager layoutManager = CreateComponentUtils.createAndInitLayout(
88            FacesContext.getCurrentInstance(), ComponentTypes.GRID_LAYOUT, RendererTypes.GRID_LAYOUT, base.getParent());
89        base.getFacets().put(Facets.LAYOUT, (AbstractUILayoutBase) layoutManager);
90        return layoutManager;
91      }
92    }
93  
94    public void setLayoutManager(final LayoutManager layoutManager) {
95      getFacets().put(Facets.LAYOUT, (AbstractUILayoutBase) layoutManager);
96    }
97  
98    public boolean isLayoutChildren() {
99      return isRendered();
100   }
101 }