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.layout;
21  
22  import org.apache.myfaces.tobago.layout.LayoutComponent;
23  import org.apache.myfaces.tobago.layout.Measure;
24  import org.apache.myfaces.tobago.layout.Orientation;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  
29  public class Interval {
30  
31    private static final Logger LOG = LoggerFactory.getLogger(Interval.class);
32  
33    private final Measure minimum;
34  
35    private final Measure preferred;
36  
37    private final Measure maximum;
38  
39    private final Measure current;
40  
41    public Interval(final LayoutComponent component, final Orientation orientation) {
42      this(
43          orientation == Orientation.HORIZONTAL ? component.getMinimumWidth() : component.getMinimumHeight(),
44          orientation == Orientation.HORIZONTAL ? component.getPreferredWidth() : component.getPreferredHeight(),
45          orientation == Orientation.HORIZONTAL ? component.getMaximumWidth() : component.getMaximumHeight(),
46          orientation == Orientation.HORIZONTAL ? component.getCurrentWidth() : component.getCurrentHeight());
47    }
48  
49    public Interval(final Measure minimum, final Measure preferred, final Measure maximum, final Measure current) {
50      if (LOG.isWarnEnabled()) {
51        if (minimum != null && minimum.greaterThan(preferred)) {
52          LOG.warn("Minimum '{}' is not smaller or equals preferred '{}'.", minimum, preferred);
53        }
54        if (preferred != null && preferred.greaterThan(maximum)) {
55          LOG.warn("Preferred '{}' is not smaller or equals maximum '{}'.", preferred, maximum);
56        }
57      }
58      this.minimum = minimum;
59      this.preferred = preferred;
60      this.maximum = maximum;
61      this.current = current;
62    }
63  
64    // XXX what about rounding??? may use multiply instead of divide
65    public Interval(final Interval interval, final int divider) {
66      this.minimum = interval.minimum != null
67          ? interval.minimum.divide(divider) : null; // XXX may add one for rounding up.
68      this.preferred = interval.preferred != null ? interval.preferred.divide(divider) : null;
69      this.maximum = interval.maximum != null ? interval.maximum.divide(divider) : null;
70      this.current = interval.current != null ? interval.current.divide(divider) : null;
71    }
72  
73    public Measure getMinimum() {
74      return minimum;
75    }
76  
77    public Measure getPreferred() {
78      return preferred;
79    }
80  
81    public Measure getMaximum() {
82      return maximum;
83    }
84  
85    public Measure getCurrent() {
86      return current;
87    }
88  
89    @Override
90    public String toString() {
91      final StringBuilder builder = new StringBuilder();
92      builder.append("[");
93      builder.append(minimum);
94      builder.append("<=");
95      builder.append(preferred);
96      builder.append("<=");
97      builder.append(maximum);
98      builder.append(",");
99      builder.append(current);
100     builder.append("]");
101     return builder.toString();
102   }
103 }