View Javadoc

1   package org.apache.myfaces.tobago.component;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.myfaces.tobago.OnComponentCreated;
23  import org.apache.myfaces.tobago.layout.Display;
24  import org.apache.myfaces.tobago.layout.Interval;
25  import org.apache.myfaces.tobago.layout.IntervalList;
26  import org.apache.myfaces.tobago.layout.LayoutComponent;
27  import org.apache.myfaces.tobago.layout.LayoutContainer;
28  import org.apache.myfaces.tobago.layout.LayoutContext;
29  import org.apache.myfaces.tobago.layout.LayoutManager;
30  import org.apache.myfaces.tobago.layout.LayoutUtils;
31  import org.apache.myfaces.tobago.layout.Measure;
32  
33  import javax.faces.component.UIComponent;
34  import javax.faces.context.FacesContext;
35  
36  public abstract class AbstractUITabGroupLayout extends UILayout implements LayoutManager, OnComponentCreated {
37  
38    private static final Log LOG = LogFactory.getLog(AbstractUIGridLayout.class);
39  
40    private boolean horizontalAuto;
41    private boolean verticalAuto;
42  
43    public void onComponentCreated(FacesContext context, UIComponent component) {
44    }
45  
46    public void init() {
47      for (LayoutComponent component : getLayoutContainer().getComponents()) {
48        if (component instanceof LayoutContainer) {
49          ((LayoutContainer) component).getLayoutManager().init();
50        }
51      }
52    }
53  
54    public void fixRelativeInsideAuto(boolean orientation, boolean auto) {
55  
56      if (orientation) {
57        horizontalAuto = auto;
58      } else {
59        verticalAuto = auto;
60      }
61  
62      for (LayoutComponent component : getLayoutContainer().getComponents()) {
63        if (component instanceof LayoutContainer) {
64          ((LayoutContainer) component).getLayoutManager().fixRelativeInsideAuto(orientation, auto);
65        }
66      }
67    }
68  
69    public void preProcessing(boolean orientation) {
70  
71      // process auto tokens
72      int i = 0;
73      IntervalList intervals = new IntervalList();
74      for (LayoutComponent component : getLayoutContainer().getComponents()) {
75  
76        if (component instanceof LayoutContainer) {
77          ((LayoutContainer) component).getLayoutManager().preProcessing(orientation);
78        }
79  
80        if (orientation && horizontalAuto || !orientation && verticalAuto) {
81          intervals.add(new Interval(component, orientation));
82        }
83      }
84  
85      if (intervals.size() >= 1) {
86        Measure size = intervals.computeAuto();
87        size = size.add(LayoutUtils.getBeginOffset(orientation, getLayoutContainer()));
88        size = size.add(LayoutUtils.getEndOffset(orientation, getLayoutContainer()));
89        LayoutUtils.setSize(orientation, getLayoutContainer(), size);
90      }
91    }
92  
93    public void mainProcessing(boolean orientation) {
94  
95      // find *
96      if (orientation && !horizontalAuto || !orientation && !verticalAuto) {
97        // find rest
98        LayoutContainer container = getLayoutContainer();
99        Measure available = LayoutUtils.getSize(orientation, container);
100       if (available != null) {
101         available = available.substractNotNegative(LayoutUtils.getBeginOffset(orientation, container));
102         available = available.substractNotNegative(LayoutUtils.getEndOffset(orientation, container));
103 
104         for (LayoutComponent component : getLayoutContainer().getComponents()) {
105 
106           component.setDisplay(Display.BLOCK); // TODO: use CSS via classes and style.css
107           LayoutUtils.setSize(orientation, component, available);
108 
109 
110           // call sub layout manager
111           if (component instanceof LayoutContainer) {
112             ((LayoutContainer) component).getLayoutManager().mainProcessing(orientation);
113           }
114         }
115       } else {
116         LOG.warn("No width/height set but needed for *!"); // todo: more information
117       }
118     }
119   }
120 
121   public void postProcessing(boolean orientation) {
122 
123     // set positions to all sub-layout-managers
124 
125     for (LayoutComponent component : getLayoutContainer().getComponents()) {
126 
127       component.setDisplay(Display.BLOCK); // TODO: use CSS via classes and style.css
128 
129       // compute the position of the cell
130       Measure position = LayoutUtils.getBeginOffset(orientation, getLayoutContainer());
131       if (orientation) {
132         component.setLeft(position);
133       } else {
134         component.setTop(position);
135       }
136 
137       // call sub layout manager
138       if (component instanceof LayoutContainer) {
139         ((LayoutContainer) component).getLayoutManager().postProcessing(orientation);
140       }
141 
142       // todo: optimize: the AutoLayoutTokens with columnSpan=1 are already called
143     }
144   }
145 
146   public void collect(LayoutContext layoutContext, LayoutContainer container, int horizontalIndex, int verticalIndex) {
147   }
148 
149   public void distribute(LayoutContext layoutContext, LayoutContainer container) {
150   }
151 
152   private LayoutContainer getLayoutContainer() {
153     // todo: check with instanceof and do something in the error case
154     return ((LayoutContainer) getParent());
155   }
156 
157   @Override
158   public boolean getRendersChildren() {
159     return false;
160   }
161 }