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 * troger 016 */ 017package org.nuxeo.ecm.platform.annotations.gwt.client.view.decorator; 018 019import org.nuxeo.ecm.platform.annotations.gwt.client.AnnotationConstant; 020import org.nuxeo.ecm.platform.annotations.gwt.client.controler.AnnotationController; 021import org.nuxeo.ecm.platform.annotations.gwt.client.model.Annotation; 022 023import com.google.gwt.dom.client.Document; 024import com.google.gwt.dom.client.Element; 025import com.google.gwt.dom.client.Node; 026import com.google.gwt.dom.client.SpanElement; 027 028/** 029 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 030 */ 031public class NuxeoSelectedTextDecoratorVisitor extends NuxeoDecoratorVisitor { 032 033 public NuxeoSelectedTextDecoratorVisitor(Annotation annotation, AnnotationController controller) { 034 super(annotation, controller); 035 } 036 037 @Override 038 protected Element decorateTextWithSpan(String data) { 039 if (data.trim().length() == 0) { 040 // don't add span to empty text 041 return null; 042 } 043 044 Document document = currentNode.getOwnerDocument(); 045 SpanElement spanElement = getSpanElement(document); 046 spanElement.setInnerText(data); 047 Node parent = currentNode.getParentNode(); 048 String className = AnnotationConstant.SELECTED_TEXT_CLASS_NAME; 049 if (parent.getNodeName().equalsIgnoreCase("span")) { 050 String parentClassName = ((SpanElement) parent.cast()).getClassName(); 051 if (parentClassName.indexOf(AnnotationConstant.SELECTED_TEXT_CLASS_NAME) != -1) { 052 className = parentClassName + AnnotationConstant.SELECTED_TEXT_CLASS_NAME; 053 } 054 } 055 spanElement.setClassName(className); 056 insertBefore(parent, currentNode.getNextSibling(), spanElement); 057 return spanElement; 058 } 059 060 @Override 061 protected SpanElement getSpanElement(Document document) { 062 SpanElement spanElement = document.createSpanElement(); 063 return spanElement; 064 } 065 066}