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