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