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