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