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