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 *     Alexandre Russel
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.gwt.client.view.decorator;
021
022import org.nuxeo.ecm.platform.annotations.gwt.client.AnnotationConstant;
023import org.nuxeo.ecm.platform.annotations.gwt.client.controler.AnnotationController;
024import org.nuxeo.ecm.platform.annotations.gwt.client.model.Annotation;
025import org.nuxeo.ecm.platform.annotations.gwt.client.view.annotater.ImageAnnotater;
026import org.nuxeo.ecm.platform.annotations.gwt.client.view.listener.AnnotationPopupEventListener;
027
028import com.google.gwt.dom.client.DivElement;
029import com.google.gwt.dom.client.ImageElement;
030import com.google.gwt.user.client.DOM;
031import com.google.gwt.user.client.Element;
032import com.google.gwt.user.client.Event;
033import com.google.gwt.user.client.EventListener;
034
035/**
036 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
037 */
038public class ImageDecorator {
039
040    private AnnotationController controller;
041
042    public ImageDecorator(AnnotationController controller) {
043        this.controller = controller;
044    }
045
046    public DivElement addAnnotatedArea(final int ax, final int ay, final int bx, final int by, final ImageAnnotater img) {
047        int top = (by > ay ? ay : by);
048        int height = Math.abs(ay - by);
049        int left = (ax > bx ? bx : ax);
050        int width = Math.abs(ax - bx);
051        final DivElement divElement = img.getImage().getOwnerDocument().createDivElement();
052        divElement.setClassName(AnnotationConstant.IGNORED_ELEMENT + " " + AnnotationConstant.DECORATE_AREA_CLASS_NAME);
053        divElement.getStyle().setProperty("left", "" + left + "px");
054        divElement.getStyle().setProperty("top", "" + top + "px");
055        divElement.getStyle().setProperty("width", "" + width + "px");
056        divElement.getStyle().setProperty("height", "" + height + "px");
057        divElement.getStyle().setProperty("display", "block");
058        DOM.sinkEvents((Element) divElement.cast(), Event.ONMOUSEMOVE | Event.ONMOUSEUP);
059        DOM.setEventListener((Element) divElement.cast(), new EventListener() {
060            public void onBrowserEvent(Event event) {
061                img.manageEvent(event);
062            }
063        });
064        img.getImage().getParentElement().appendChild(divElement);
065        return divElement;
066    }
067
068    public void updateAnnotatedArea(int ax, int ay, int bx, int by, ImageElement img, DivElement divElement) {
069        int top = (by > ay ? ay : by) + img.getOffsetTop();
070        int height = Math.abs(ay - by);
071        int left = (ax > bx ? bx : ax) + img.getOffsetLeft();
072        int width = Math.abs(ax - bx);
073        divElement.getStyle().setProperty("left", "" + left + "px");
074        divElement.getStyle().setProperty("top", "" + top + "px");
075        divElement.getStyle().setProperty("width", "" + width + "px");
076        divElement.getStyle().setProperty("height", "" + height + "px");
077    }
078
079    public void addAnnotatedArea(int ax, int ay, int bx, int by, ImageElement img, Annotation annotation,
080            AnnotationController controller) {
081        int top = (by > ay ? ay : by) + img.getOffsetTop();
082        int height = Math.abs(ay - by);
083        int left = (ax > bx ? bx : ax) + img.getOffsetLeft();
084        int width = Math.abs(ax - bx);
085        Element element = null;
086        element = createDivElement(img, annotation, top, height, left, width);
087        DOM.sinkEvents(element, Event.ONMOUSEOVER | Event.ONMOUSEOUT);
088        DOM.setEventListener(element,
089                AnnotationPopupEventListener.getAnnotationPopupEventListener(annotation, controller));
090    }
091
092    private Element createDivElement(ImageElement img, Annotation annotation, int top, int height, int left, int width) {
093        DivElement divElement = img.getOwnerDocument().createDivElement();
094        divElement.setClassName(controller.getDecorateClassName() + " " + AnnotationConstant.DECORATE_CLASS_NAME
095                + annotation.getId());
096        divElement.getStyle().setProperty("left", "" + left + "px");
097        divElement.getStyle().setProperty("top", "" + top + "px");
098        divElement.getStyle().setProperty("width", "" + width + "px");
099        divElement.getStyle().setProperty("height", "" + height + "px");
100        divElement.getStyle().setProperty("display", "block");
101        divElement.getStyle().setProperty("position", "absolute");
102        img.getParentElement().appendChild(divElement);
103        return (Element) divElement.cast();
104    }
105}