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