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.view;
021
022import org.nuxeo.ecm.platform.pictures.tiles.gwt.client.util.Rectangle;
023
024import com.google.gwt.user.client.DOM;
025import com.google.gwt.user.client.Element;
026import com.google.gwt.user.client.ui.Panel;
027import com.google.gwt.user.client.ui.SimplePanel;
028import com.google.gwt.user.client.ui.Widget;
029
030/**
031 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
032 */
033public class SelectedArea {
034
035    private static final String FULL_MOVING_AREA_CSS_CLASS = "thumbnailMovingSelectedArea";
036
037    private static final String INNER_MOVING_AREA_CSS_CLASS = "thumbnailInnerMovingSelectedArea";
038
039    private static final String FULL_AREA_CSS_CLASS = "thumbnailSelectedArea";
040
041    private static final String INNER_AREA_CSS_CLASS = "thumbnailInnerSelectedArea";
042
043    private int maxWidth;
044
045    private int maxHeight;
046
047    private Rectangle area;
048
049    private final SimplePanel fullMovingArea = new SimplePanel();
050
051    private final SimplePanel innerMovingArea = new SimplePanel();
052
053    private final SimplePanel fullArea = new SimplePanel();
054
055    private final SimplePanel innerArea = new SimplePanel();
056
057    public SelectedArea(int maxWidth, int maxHeight, Panel parent) {
058        this.maxWidth = maxWidth;
059        this.maxHeight = maxHeight;
060
061        createAreas();
062        parent.add(fullArea);
063        parent.add(innerArea);
064        parent.add(fullMovingArea);
065        parent.add(innerMovingArea);
066    }
067
068    private void createAreas() {
069        fullMovingArea.setStyleName(FULL_MOVING_AREA_CSS_CLASS);
070        setDefaultStyleAttributes(fullMovingArea);
071
072        innerMovingArea.setStyleName(INNER_MOVING_AREA_CSS_CLASS);
073        setDefaultStyleAttributes(innerMovingArea);
074
075        fullArea.setStyleName(FULL_AREA_CSS_CLASS);
076        setDefaultStyleAttributes(fullArea);
077
078        innerArea.setStyleName(INNER_AREA_CSS_CLASS);
079        setDefaultStyleAttributes(innerArea);
080    }
081
082    private void setDefaultStyleAttributes(Widget w) {
083        Element element = w.getElement();
084        DOM.setStyleAttribute(element, "display", "block");
085        DOM.setStyleAttribute(element, "position", "absolute");
086    }
087
088    public void changeArea(Rectangle area) {
089        this.area = area;
090        drawArea();
091    }
092
093    public Rectangle getCurrentArea() {
094        return area;
095    }
096
097    public void move(int x, int y) {
098        area.move(x, y);
099        ensureValidArea();
100        drawArea();
101    }
102
103    public void centerOn(int x, int y) {
104        area.centerOn(x, y);
105        ensureValidArea();
106        drawArea();
107    }
108
109    public void putArea() {
110        updateAreaStyles(fullArea, area.getTopLeft().getX(), area.getTopLeft().getY(), area.getWidth(),
111                area.getHeight());
112        updateAreaStyles(innerArea, area.getTopLeft().getX(), area.getTopLeft().getY(), area.getWidth(),
113                area.getHeight());
114    }
115
116    private void ensureValidArea() {
117        int newX = area.getTopLeft().getX();
118        int newY = area.getTopLeft().getY();
119
120        if (newX + fullMovingArea.getOffsetWidth() > maxWidth) {
121            newX = maxWidth - fullMovingArea.getOffsetWidth();
122        } else if (newX < 0) {
123            newX = 0;
124        }
125        if (newY + fullMovingArea.getOffsetHeight() > maxHeight) {
126            newY = maxHeight - fullMovingArea.getOffsetHeight();
127        } else if (newY < 0) {
128            newY = 0;
129        }
130        area.setLocation(newX, newY);
131    }
132
133    private void drawArea() {
134        updateAreaStyles(fullMovingArea, area.getTopLeft().getX(), area.getTopLeft().getY(), area.getWidth(),
135                area.getHeight());
136        updateAreaStyles(innerMovingArea, area.getTopLeft().getX(), area.getTopLeft().getY(), area.getWidth(),
137                area.getHeight());
138    }
139
140    private static void updateAreaStyles(Widget w, int left, int top, int width, int height) {
141        Element element = w.getElement();
142        DOM.setStyleAttribute(element, "left", "" + left + "px");
143        DOM.setStyleAttribute(element, "top", "" + top + "px");
144        DOM.setStyleAttribute(element, "width", "" + width + "px");
145        DOM.setStyleAttribute(element, "height", "" + height + "px");
146    }
147
148}