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