001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     troger
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.pictures.tiles.gwt.client.util;
021
022import java.util.Arrays;
023import java.util.List;
024
025/**
026 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
027 */
028public class Rectangle {
029
030    private Point topLeft;
031
032    private int width;
033
034    private int height;
035
036    public Rectangle() {
037        this(new Point(), 0, 0);
038    }
039
040    public Rectangle(Point topLeft, int width, int height) {
041        this.topLeft = topLeft;
042        this.width = width;
043        this.height = height;
044    }
045
046    public void move(int x, int y) {
047        topLeft.move(x, y);
048    }
049
050    public void setLocation(int x, int y) {
051        topLeft.setLocation(x, y);
052    }
053
054    public void centerOn(int x, int y) {
055        topLeft.setLocation(x - (width / 2), y - (height / 2));
056    }
057
058    /**
059     * @return the topLeft.
060     */
061    public Point getTopLeft() {
062        return topLeft;
063    }
064
065    public int getWidth() {
066        return width;
067    }
068
069    public int getHeight() {
070        return height;
071    }
072
073    public Point getCenter() {
074        int x = topLeft.getX() + (width / 2);
075        int y = topLeft.getY() + (height / 2);
076        return new Point(x, y);
077    }
078
079    public boolean contains(Point p) {
080        Point bottomRight = new Point(topLeft.getX() + width, topLeft.getY() + height);
081        if (p.getX() >= topLeft.getX() && p.getX() <= bottomRight.getX() && p.getY() >= topLeft.getY()
082                && p.getY() <= bottomRight.getY()) {
083            return true;
084        }
085        return false;
086    }
087
088    public boolean containsAny(Point... points) {
089        return containsAny(Arrays.asList(points));
090    }
091
092    public boolean containsAny(List<Point> points) {
093        for (Point p : points) {
094            if (contains(p)) {
095                return true;
096            }
097        }
098        return false;
099    }
100
101    public boolean containsAll(Point... points) {
102        return containsAll(Arrays.asList(points));
103    }
104
105    public boolean containsAll(List<Point> points) {
106        for (Point p : points) {
107            if (!contains(p)) {
108                return false;
109            }
110        }
111        return true;
112    }
113
114}