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     * Date: 28.05.2008 15:10:29
022     */
023    
024    public class Box {
025    
026      private Position position;
027      private Dimension dimension;
028    
029      public Box(Position position, Dimension dimension) {
030        this.position = position;
031        this.dimension = dimension;
032      }
033    
034      public Box(String string) {
035        int comma = string.indexOf(',');
036        if (comma >= 0) { // found first comma
037          comma = string.indexOf(',', comma + 1);
038          if (comma >= 0) { // found second comma
039            position = new Position(string.substring(0, comma));
040            dimension = new Dimension(string.substring(comma + 1));
041            return;
042          }
043        }
044        throw new IllegalArgumentException("Can't parse to a box: '" + string + "'");
045      }
046    
047      /**
048       * Convenience method to get left + width.
049       */
050      public Measure getRight() {
051        return position.getLeft().add(dimension.getWidth());
052      }
053    
054      /**
055       * Convenience method to get top + height.
056       */
057      public Measure getBottom() {
058        return position.getTop().add(dimension.getHeight());
059      }
060    
061      public Measure getLeft() {
062        return position.getLeft();
063      }
064    
065      public void setLeft(Measure left) {
066        position.setLeft(left);
067      }
068    
069      public Measure getTop() {
070        return position.getTop();
071      }
072    
073      public void setTop(Measure top) {
074        position.setTop(top);
075      }
076    
077      public Measure getWidth() {
078        return dimension.getWidth();
079      }
080    
081      public void setWidth(Measure width) {
082        dimension.setWidth(width);
083      }
084    
085      public Measure getHeight() {
086        return dimension.getHeight();
087      }
088    
089      public void setHeight(Measure height) {
090        dimension.setHeight(height);
091      }
092    
093      @Override
094      public String toString() {
095        return new StringBuilder().append(position).append(',').append(dimension).toString();
096      }
097    }