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.layout.LayoutComponent;
23  import org.apache.myfaces.tobago.layout.LayoutContainer;
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.context.FacesContext;
30  
31  public abstract class AbstractUISplitLayout extends AbstractUIGridLayout {
32  
33    public static final String VERTICAL = Orientation.VERTICAL.name();
34    public static final String HORIZONTAL = Orientation.HORIZONTAL.name();
35  
36    private String submittedLayout;
37  
38    public void updateLayout(final int position) {
39      final LayoutContainer container = (LayoutContainer) getParent();
40      final LayoutComponent firstComponent = container.getComponents().get(0);
41      final LayoutComponent secondComponent = container.getComponents().get(1);
42      final int oldPosition;
43  
44      final int currentSize1;
45      final int currentSize2;
46      if (HORIZONTAL.equals(getOrientation())) {
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 = new StringBuilder()
62          .append(Integer.toString(newSize1 / ggt)).append("*;")
63          .append(Integer.toString(newSize2 / ggt)).append("*")
64          .toString();
65    }
66  
67    // TODO: MathUtils
68    public static int gcd(int a, int b) {
69      if (a < 0) {
70        a = -a;
71      }
72      if (b < 0) {
73        b = -b;
74      }
75      int t;
76      while (b != 0) {
77        t = a % b;
78        a = b;
79        b = t;
80      }
81      return a;
82    }
83  
84    @Override
85    public void processUpdates(final FacesContext facesContext) {
86      updateModel(facesContext);
87      super.processUpdates(facesContext);
88    }
89  
90    private void updateModel(final FacesContext facesContext) {
91      if (submittedLayout != null) {
92        final ValueExpression expression = getValueExpression("layout");
93        if (expression != null) {
94          final ELContext elContext = facesContext.getELContext();
95          expression.setValue(elContext, submittedLayout);
96          submittedLayout = null;
97        }
98      }
99    }
100 
101   public Measure getSpacing(final Orientation orientation) {
102     return orientation == Orientation.HORIZONTAL ? getColumnSpacing() : getRowSpacing();
103   }
104 
105 @Override
106   public void setColumns(final String columns) {
107   }
108 
109   @Override
110   public String getColumns() {
111     return VERTICAL.equals(getOrientation()) ? "1*" : getLayout2();
112   }
113 
114 //  private String getLayout2() {
115 //    return getLayout().replace(";", ";5px;");
116 //  }
117 
118   @Override
119   public void setRows(final String rows) {
120   }
121 
122   @Override
123   public String getRows() {
124     return HORIZONTAL.equals(getOrientation()) ? "1*" : getLayout2();
125   }
126 
127   private String getLayout2() {
128     return submittedLayout != null ? submittedLayout : getLayout();
129   }
130 
131   @Override
132   public boolean isRowOverflow() {
133     return false;
134   }
135 
136   @Override
137   public boolean isColumnOverflow() {
138     return false;
139   }
140 
141   public abstract String getLayout();
142 
143   public abstract String getOrientation();
144 
145   @Deprecated
146   public abstract Measure getCellspacing();
147 
148   public abstract Measure getRowSpacing();
149 
150   public abstract Measure getColumnSpacing();
151 
152   public abstract Measure getMarginLeft();
153 
154   public abstract Measure getMarginTop();
155 
156   public abstract Measure getMarginRight();
157 
158   public abstract Measure getMarginBottom();
159 
160   public abstract boolean isRigid();
161 }