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.internal.layout.Interval;
23  import org.apache.myfaces.tobago.internal.layout.IntervalList;
24  import org.apache.myfaces.tobago.internal.layout.LayoutUtils;
25  import org.apache.myfaces.tobago.layout.LayoutComponent;
26  import org.apache.myfaces.tobago.layout.LayoutContainer;
27  import org.apache.myfaces.tobago.layout.LayoutManager;
28  import org.apache.myfaces.tobago.layout.Measure;
29  import org.apache.myfaces.tobago.layout.Orientation;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  public abstract class AbstractUITabGroupLayout extends AbstractUILayoutBase implements LayoutManager {
34  
35    private static final Logger LOG = LoggerFactory.getLogger(AbstractUIGridLayout.class);
36  
37    private boolean horizontalAuto;
38    private boolean verticalAuto;
39  
40    public void init() {
41      for (final LayoutComponent component : getLayoutContainer().getComponents()) {
42        if (component instanceof LayoutContainer && component.isRendered()) {
43          ((LayoutContainer) component).getLayoutManager().init();
44        }
45      }
46    }
47  
48    public void fixRelativeInsideAuto(final Orientation orientation, final boolean auto) {
49  
50      if (orientation == Orientation.HORIZONTAL) {
51        horizontalAuto = auto;
52      } else {
53        verticalAuto = auto;
54      }
55  
56      for (final LayoutComponent component : getLayoutContainer().getComponents()) {
57        if (component instanceof LayoutContainer && component.isRendered()) {
58          ((LayoutContainer) component).getLayoutManager().fixRelativeInsideAuto(orientation, auto);
59        }
60      }
61    }
62  
63    public void preProcessing(final Orientation orientation) {
64  
65      // process auto tokens
66      final IntervalList intervals = new IntervalList();
67      for (final LayoutComponent component : getLayoutContainer().getComponents()) {
68  
69        if (component instanceof LayoutContainer && component.isRendered()) {
70          ((LayoutContainer) component).getLayoutManager().preProcessing(orientation);
71        }
72  
73        if (orientation == Orientation.HORIZONTAL && horizontalAuto
74            || orientation == Orientation.VERTICAL && verticalAuto) {
75          intervals.add(new Interval(component, orientation));
76        }
77      }
78  
79      if (intervals.size() >= 1) {
80        intervals.evaluate();
81        final Measure size = intervals.getCurrent();
82  /*
83        size = size.add(LayoutUtils.getBorderBegin(orientation, getLayoutContainer()));
84        size = size.add(LayoutUtils.getBorderEnd(orientation, getLayoutContainer()));
85  */
86        LayoutUtils.setCurrentSize(orientation, getLayoutContainer(), size);
87      }
88    }
89  
90    public void mainProcessing(final Orientation orientation) {
91  
92      // find *
93      if (orientation == Orientation.HORIZONTAL && !horizontalAuto
94          || orientation == Orientation.VERTICAL && !verticalAuto) {
95        // find rest
96        final LayoutContainer container = getLayoutContainer();
97        final Measure available = LayoutUtils.getCurrentSize(orientation, container);
98        if (available != null) {
99          for (final LayoutComponent component : getLayoutContainer().getComponents()) {
100           LayoutUtils.setCurrentSize(orientation, component, available);
101         }
102       } else {
103         LOG.warn("No width/height set but needed for *!"); // todo: more information
104       }
105     }
106 
107     // call sub layout manager
108     for (final LayoutComponent component : getLayoutContainer().getComponents()) {
109       if (component instanceof LayoutContainer && component.isRendered()) {
110         ((LayoutContainer) component).getLayoutManager().mainProcessing(orientation);
111       }
112     }
113   }
114 
115   public void postProcessing(final Orientation orientation) {
116 
117     // set positions to all sub-layout-managers
118     for (final LayoutComponent component : getLayoutContainer().getComponents()) {
119       // call sub layout manager
120       if (component instanceof LayoutContainer && component.isRendered()) {
121         ((LayoutContainer) component).getLayoutManager().postProcessing(orientation);
122       }
123 
124       // todo: optimize: the AutoLayoutTokens with columnSpan=1 are already called
125     }
126   }
127 
128   private LayoutContainer getLayoutContainer() {
129     // todo: check with instanceof and do something in the error case
130     return ((LayoutContainer) getParent());
131   }
132 
133   @Override
134   public boolean getRendersChildren() {
135     return false;
136   }
137 }