001/*
002 * (C) Copyright 2006-2009 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 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.annotations.gwt.client.view.listener;
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.util.CSSClassManager;
025import org.nuxeo.ecm.platform.annotations.gwt.client.view.annotater.Annotater;
026import org.nuxeo.ecm.platform.annotations.gwt.client.view.annotater.ImageAnnotater;
027import org.nuxeo.ecm.platform.annotations.gwt.client.view.annotater.TextAnnotater;
028
029import com.google.gwt.dom.client.Element;
030import com.google.gwt.user.client.Event;
031import com.google.gwt.user.client.EventListener;
032
033public class AnnotatedEventListener implements EventListener {
034    private final Annotater imageAnnotater;
035
036    private final Annotater textAnnotater;
037
038    private final AnnotationController controller;
039
040    private Annotater lastUsedAnnotater;
041
042    public AnnotatedEventListener(AnnotationController controller) {
043        this.imageAnnotater = new ImageAnnotater(controller);
044        this.textAnnotater = new TextAnnotater(controller);
045        this.controller = controller;
046    }
047
048    public void onBrowserEvent(Event event) {
049        if (!controller.canAnnotate()) {
050            return;
051        }
052
053        if (event.getTarget() == null) {// FF2 send a onload event.
054            return;
055        }
056
057        if (event.getTypeInt() == Event.ONMOUSEOUT) {
058            manageOnMouseOutEvent(event);
059            return;
060        }
061
062        if (isOnImage(event)) {
063            annotateImage(event);
064        } else if (!controller.isImageOnly()) {
065            annotateText(event);
066        }
067    }
068
069    private void manageOnMouseOutEvent(Event event) {
070        if (lastUsedAnnotater != null) {
071            lastUsedAnnotater.manageEvent(event);
072            lastUsedAnnotater = null;
073        }
074    }
075
076    private boolean isOnImage(Event event) {
077        Element element = event.getTarget();
078        if (element.getNodeName().equalsIgnoreCase("div")) {
079            CSSClassManager cssManager = new CSSClassManager(element);
080            if (cssManager.isClassPresent(AnnotationConstant.DECORATE_CLASS_NAME)
081                    || cssManager.isClassPresent(AnnotationConstant.DECORATE_NOT_CLASS_NAME)
082                    || cssManager.isClassPresent(AnnotationConstant.DECORATE_AREA_CLASS_NAME)) {
083                return true;
084            }
085        } else if (element.getNodeName().equalsIgnoreCase("img")) {
086            return true;
087        }
088        return false;
089    }
090
091    private void annotateImage(Event event) {
092        imageAnnotater.manageEvent(event);
093    }
094
095    private void annotateText(Event event) {
096        textAnnotater.manageEvent(event);
097        lastUsedAnnotater = textAnnotater;
098    }
099
100}