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.util; 021 022import org.nuxeo.ecm.platform.annotations.gwt.client.AnnotationConstant; 023 024import com.google.gwt.dom.client.Document; 025import com.google.gwt.dom.client.Element; 026import com.google.gwt.dom.client.Node; 027 028/** 029 * @author Alexandre Russel 030 */ 031public class Utils { 032 033 public static native Document setDocument(Document document) /*-{ 034 $temp = $doc; 035 $doc = document; 036 return $temp 037 }-*/; 038 039 public static int[] getAbsoluteTopLeft(Element element, Document document) { 040 int[] result = new int[2]; 041 Document doc = Utils.setDocument(document); 042 result[0] = element.getAbsoluteTop(); 043 result[1] = element.getAbsoluteLeft(); 044 Utils.setDocument(doc); 045 return result; 046 } 047 048 public static native String getBaseHref() /*-{ 049 return top['baseHref']; 050 }-*/; 051 052 public static native Range getCurrentRange(Document document) /*-{ 053 if( document && 054 document.defaultView && 055 document.defaultView.getSelection() && 056 document.defaultView.getSelection().getRangeAt(0)) { 057 // W3C Range 058 var userSelection = document.defaultView.getSelection().getRangeAt(0); 059 var range = @org.nuxeo.ecm.platform.annotations.gwt.client.util.Range::new(Ljava/lang/String;Lcom/google/gwt/dom/client/Node;ILcom/google/gwt/dom/client/Node;I)(userSelection.toString(), userSelection.startContainer, userSelection.startOffset, userSelection.endContainer, userSelection.endOffset); 060 return range; 061 } else if(document.selection) { 062 // IE TextRange 063 var ieSelection = document.selection.createRange(); 064 var ieRange = new $wnd.InternetExplorerRange(ieSelection); 065 ieRange._init(); 066 var range = @org.nuxeo.ecm.platform.annotations.gwt.client.util.Range::new(Ljava/lang/String;Lcom/google/gwt/dom/client/Node;ILcom/google/gwt/dom/client/Node;I)(ieSelection.text, ieRange.startContainer, ieRange.startOffset, ieRange.endContainer, ieRange.endOffset); 067 return range; 068 } 069 return null; 070 }-*/; 071 072 public static String removeWhitespaces(String text, Node node) { 073 return removeWhitespaces(text, node, false); 074 } 075 076 public static String removeWhitespaces(String text, Node node, boolean forceIfOnlyWhitespaces) { 077 if (text == null) { 078 return ""; 079 } 080 if (!forceIfOnlyWhitespaces) { 081 if (text.matches("^\\s+$")) { 082 return text; 083 } 084 } 085 // Window.alert("Before removeWS: " + text); 086 Element prevSibling = (Element) node.getPreviousSibling(); 087 088 String processedText = text; 089 if (prevSibling == null 090 || !(new CSSClassManager(prevSibling).isClassPresent(AnnotationConstant.IGNORED_ELEMENT))) { 091 processedText = processedText.replaceAll("^\\s+", ""); 092 } 093 // Window.alert("in progress removeWS: " + processedText); 094 processedText = processedText.replaceAll("\\s+", " "); 095 // Window.alert("after removeWS: " + processedText); 096 return processedText; 097 } 098}