001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.layout;
021
022 /**
023 * User: lofwyr
024 * Date: 28.05.2008 15:10:29
025 */
026
027 public class Box {
028
029 private Position position;
030 private Dimension dimension;
031
032 public Box(Position position, Dimension dimension) {
033 this.position = position;
034 this.dimension = dimension;
035 }
036
037 public Box(String string) {
038 int comma = string.indexOf(',');
039 if (comma >= 0) { // found first comma
040 comma = string.indexOf(',', comma + 1);
041 if (comma >= 0) { // found second comma
042 position = new Position(string.substring(0, comma));
043 dimension = new Dimension(string.substring(comma + 1));
044 return;
045 }
046 }
047 throw new IllegalArgumentException("Can't parse to a box: '" + string + "'");
048 }
049
050 /**
051 * Convenience method to get left + width.
052 */
053 public Measure getRight() {
054 return position.getLeft().add(dimension.getWidth());
055 }
056
057 /**
058 * Convenience method to get top + height.
059 */
060 public Measure getBottom() {
061 return position.getTop().add(dimension.getHeight());
062 }
063
064 public Measure getLeft() {
065 return position.getLeft();
066 }
067
068 public void setLeft(Measure left) {
069 position.setLeft(left);
070 }
071
072 public Measure getTop() {
073 return position.getTop();
074 }
075
076 public void setTop(Measure top) {
077 position.setTop(top);
078 }
079
080 public Measure getWidth() {
081 return dimension.getWidth();
082 }
083
084 public void setWidth(Measure width) {
085 dimension.setWidth(width);
086 }
087
088 public Measure getHeight() {
089 return dimension.getHeight();
090 }
091
092 public void setHeight(Measure height) {
093 dimension.setHeight(height);
094 }
095
096 @Override
097 public String toString() {
098 return new StringBuilder().append(position).append(',').append(dimension).toString();
099 }
100 }