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.annotater;
021
022import org.nuxeo.ecm.platform.annotations.gwt.client.controler.AnnotationController;
023import org.nuxeo.ecm.platform.annotations.gwt.client.model.Annotation;
024import org.nuxeo.ecm.platform.annotations.gwt.client.model.Container;
025import org.nuxeo.ecm.platform.annotations.gwt.client.util.Range;
026import org.nuxeo.ecm.platform.annotations.gwt.client.util.Utils;
027import org.nuxeo.ecm.platform.annotations.gwt.client.util.XPathUtil;
028import org.nuxeo.ecm.platform.annotations.gwt.client.view.NewAnnotationPopup;
029
030import com.allen_sauer.gwt.log.client.Log;
031import com.google.gwt.dom.client.Document;
032import com.google.gwt.dom.client.Element;
033import com.google.gwt.dom.client.Node;
034import com.google.gwt.dom.client.Text;
035import com.google.gwt.user.client.Event;
036
037/**
038 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
039 */
040public class TextAnnotater extends AbstractAnnotater {
041
042    private final XPathUtil xpathUtil = new XPathUtil();
043
044    public TextAnnotater(AnnotationController controller) {
045        super(controller, false);
046    }
047
048    @Override
049    public void onMouseUp(Event event) {
050        Log.debug("TextAnnotater#onMouseUp; eventId= " + event.getType() + "; source: " + event.getCurrentTarget());
051        Range currentRange = Utils.getCurrentRange(Document.get());
052        if (currentRange != null && currentRange.getSelectedText().length() != 0) {
053            Element startElement = Element.as(currentRange.getStartContainer());
054            String pointer = xpathUtil.getSelectionXPointer(currentRange);
055            controller.createNewAnnotation(pointer);
056
057            Container startContainer = getStartContainer(currentRange);
058            Container endContainer = getEndContainer(currentRange);
059
060            Annotation annotation = controller.getNewAnnotation();
061            annotation.setStartContainer(startContainer);
062            annotation.setEndContainer(endContainer);
063
064            NewAnnotationPopup popup = new NewAnnotationPopup(startElement, controller, false, "local");
065            controller.setNewAnnotationPopup(popup);
066            addAnnotationPopup();
067        } else {
068            controller.setNewAnnotationPopup(null);
069        }
070
071        super.onMouseUp(event);
072    }
073
074    private Container getStartContainer(Range range) {
075        Node startNode = range.getStartContainer();
076        int startOffset = range.getStartOffset();
077        // Window.alert("startOffset: " + startOffset);
078        startOffset = computeNewOffset(startNode, startOffset);
079        // Window.alert("startOffset after compute: " + startOffset);
080
081        if (startNode.getNodeType() == Node.TEXT_NODE) {
082            return getCustomContainer(startNode, startOffset);
083        }
084        return new Container(xpathUtil.getXPath(startNode), startOffset);
085    }
086
087    private Container getCustomContainer(Node node, int currentOffset) {
088        int offset = 0;
089        Node n = node.getPreviousSibling();
090        while (n != null) {
091            if (n.getNodeType() == Node.TEXT_NODE) {
092                Text text = (Text) n;
093                offset += text.getLength();
094            } else if (n.getNodeType() == Node.ELEMENT_NODE) {
095                Element ele = (Element) n;
096                offset += ele.getInnerText().length();
097            }
098            n = n.getPreviousSibling();
099        }
100        node = node.getParentNode();
101        currentOffset += offset;
102        return new Container(xpathUtil.getXPath(node), currentOffset);
103    }
104
105    private static int computeNewOffset(Node node, int currentOffset) {
106        if (currentOffset <= 0) {
107            return currentOffset;
108        }
109        int difference = 0;
110        String text = node.getNodeValue();
111        if (text != null) {
112            text = text.substring(0, currentOffset);
113            String processedText = Utils.removeWhitespaces(text, node, true);
114            difference = text.length() - processedText.length();
115        }
116        return currentOffset - difference;
117    }
118
119    private Container getEndContainer(Range range) {
120        Node endNode = range.getEndContainer();
121        int endOffset = range.getEndOffset();
122        endOffset = computeNewOffset(endNode, endOffset);
123
124        if (endNode.getNodeType() == Node.TEXT_NODE) {
125            return getCustomContainer(endNode, endOffset);
126        }
127
128        return new Container(xpathUtil.getXPath(endNode), endOffset);
129    }
130
131}