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.layout;
21  
22  public class Box {
23  
24    private Position position;
25    private Dimension dimension;
26  
27    public Box(Position position, Dimension dimension) {
28      this.position = position;
29      this.dimension = dimension;
30    }
31  
32    public Box(String string) {
33      int comma = string.indexOf(',');
34      if (comma >= 0) { // found first comma
35        comma = string.indexOf(',', comma + 1);
36        if (comma >= 0) { // found second comma
37          position = new Position(string.substring(0, comma));
38          dimension = new Dimension(string.substring(comma + 1));
39          return;
40        }
41      }
42      throw new IllegalArgumentException("Can't parse to a box: '" + string + "'");
43    }
44  
45    /**
46     * Convenience method to get left + width.
47     */
48    public Measure getRight() {
49      return position.getLeft().add(dimension.getWidth());
50    }
51  
52    /**
53     * Convenience method to get top + height.
54     */
55    public Measure getBottom() {
56      return position.getTop().add(dimension.getHeight());
57    }
58  
59    public Measure getLeft() {
60      return position.getLeft();
61    }
62  
63    public void setLeft(Measure left) {
64      position.setLeft(left);
65    }
66  
67    public Measure getTop() {
68      return position.getTop();
69    }
70  
71    public void setTop(Measure top) {
72      position.setTop(top);
73    }
74  
75    public Measure getWidth() {
76      return dimension.getWidth();
77    }
78  
79    public void setWidth(Measure width) {
80      dimension.setWidth(width);
81    }
82  
83    public Measure getHeight() {
84      return dimension.getHeight();
85    }
86  
87    public void setHeight(Measure height) {
88      dimension.setHeight(height);
89    }
90  
91    @Override
92    public String toString() {
93      return new StringBuilder().append(position).append(',').append(dimension).toString();
94    }
95  }