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.apt.annotation.Preliminary;
23  import org.apache.myfaces.tobago.component.Visual;
24  import org.apache.myfaces.tobago.internal.layout.LayoutUtils;
25  import org.apache.myfaces.tobago.layout.Measure;
26  import org.apache.myfaces.tobago.layout.Orientation;
27  
28  import javax.el.ELContext;
29  import javax.el.ValueExpression;
30  import javax.faces.component.UIComponent;
31  import javax.faces.context.FacesContext;
32  import java.util.List;
33  
34  /**
35   * <p>
36   * WARNING: This component is preliminary and may be changed without a major release.
37   * </p>
38   *
39   * {@link org.apache.myfaces.tobago.internal.taglib.sandbox.SplitLayoutTagDeclaration}
40   */
41  @Preliminary
42  public abstract class AbstractUISplitLayout extends AbstractUIGridLayout {
43  
44    private String submittedLayout;
45  
46    public void updateLayout(final int position) {
47      final List<UIComponent> components = LayoutUtils.findLayoutChildren(getParent());
48      final Visual firstComponent = (Visual) components.get(0);
49      final Visual secondComponent = (Visual) components.get(1);
50      final int oldPosition;
51  
52      final int currentSize1;
53      final int currentSize2;
54  /* XXX to be reimplemented: not using GridLayout, it might be better using flex?!
55      if (getOrientation() == Orientation.horizontal) {
56        oldPosition = secondComponent.getLeft().getPixel() - 5;
57        currentSize1 = firstComponent.getCurrentWidth().getPixel();
58        currentSize2 = secondComponent.getCurrentWidth().getPixel();
59      } else {
60        oldPosition = secondComponent.getTop().getPixel() - 5;
61        currentSize1 = firstComponent.getCurrentHeight().getPixel();
62        currentSize2 = secondComponent.getCurrentHeight().getPixel();
63      }
64  
65      final int offset = position - oldPosition;
66      final int newSize1 = currentSize1 + offset;
67      final int newSize2 = currentSize2 - offset;
68  
69      final int ggt = gcd(newSize1, newSize2);
70      submittedLayout = Integer.toString(newSize1 / ggt) + "*;" + Integer.toString(newSize2 / ggt) + "*";
71  */
72    }
73  
74    // TODO: MathUtils
75    public static int gcd(final int valueA, final int valueB) {
76      int a = valueA;
77      int b = valueB;
78      if (a < 0) {
79        a = -a;
80      }
81      if (b < 0) {
82        b = -b;
83      }
84      int t;
85      while (b != 0) {
86        t = a % b;
87        a = b;
88        b = t;
89      }
90      return a;
91    }
92  
93    @Override
94    public void processUpdates(final FacesContext facesContext) {
95      updateModel(facesContext);
96      super.processUpdates(facesContext);
97    }
98  
99    private void updateModel(final FacesContext facesContext) {
100     if (submittedLayout != null) {
101       final ValueExpression expression = getValueExpression("layout");
102       if (expression != null) {
103         final ELContext elContext = facesContext.getELContext();
104         expression.setValue(elContext, submittedLayout);
105         submittedLayout = null;
106       }
107     }
108   }
109 
110 @Override
111   public void setColumns(final String columns) {
112   }
113 
114   @Override
115   public String getColumns() {
116     return getOrientation() == Orientation.vertical ? "1*" : getLayout2();
117   }
118 
119 //  private String getLayout2() {
120 //    return getLayout().replace(";", ";5px;");
121 //  }
122 
123   @Override
124   public void setRows(final String rows) {
125   }
126 
127   @Override
128   public String getRows() {
129     return getOrientation() == Orientation.horizontal ? "1*" : getLayout2();
130   }
131 
132   private String getLayout2() {
133     return submittedLayout != null ? submittedLayout : getLayout();
134   }
135 
136   public abstract String getLayout();
137 
138   public abstract Orientation getOrientation();
139 
140   public abstract Measure getRowSpacing();
141 
142   public abstract Measure getColumnSpacing();
143 
144   @Override
145   public abstract boolean isRigid();
146 }