001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     troger
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.pictures.tiles.gwt.client.util;
023
024import java.util.Arrays;
025import java.util.List;
026
027/**
028 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
029 */
030public class Rectangle {
031
032    private Point topLeft;
033
034    private int width;
035
036    private int height;
037
038    public Rectangle() {
039        this(new Point(), 0, 0);
040    }
041
042    public Rectangle(Point topLeft, int width, int height) {
043        this.topLeft = topLeft;
044        this.width = width;
045        this.height = height;
046    }
047
048    public void move(int x, int y) {
049        topLeft.move(x, y);
050    }
051
052    public void setLocation(int x, int y) {
053        topLeft.setLocation(x, y);
054    }
055
056    public void centerOn(int x, int y) {
057        topLeft.setLocation(x - (width / 2), y - (height / 2));
058    }
059
060    /**
061     * @return the topLeft.
062     */
063    public Point getTopLeft() {
064        return topLeft;
065    }
066
067    public int getWidth() {
068        return width;
069    }
070
071    public int getHeight() {
072        return height;
073    }
074
075    public Point getCenter() {
076        int x = topLeft.getX() + (width / 2);
077        int y = topLeft.getY() + (height / 2);
078        return new Point(x, y);
079    }
080
081    public boolean contains(Point p) {
082        Point bottomRight = new Point(topLeft.getX() + width, topLeft.getY() + height);
083        if (p.getX() >= topLeft.getX() && p.getX() <= bottomRight.getX() && p.getY() >= topLeft.getY()
084                && p.getY() <= bottomRight.getY()) {
085            return true;
086        }
087        return false;
088    }
089
090    public boolean containsAny(Point... points) {
091        return containsAny(Arrays.asList(points));
092    }
093
094    public boolean containsAny(List<Point> points) {
095        for (Point p : points) {
096            if (contains(p)) {
097                return true;
098            }
099        }
100        return false;
101    }
102
103    public boolean containsAll(Point... points) {
104        return containsAll(Arrays.asList(points));
105    }
106
107    public boolean containsAll(List<Point> points) {
108        for (Point p : points) {
109            if (!contains(p)) {
110                return false;
111            }
112        }
113        return true;
114    }
115
116}