package com.google.gwt.query.client.plugins.widgets;

import com.google.gwt.dom.client.Element;
import com.google.gwt.query.client.GQuery;
import com.google.gwt.user.client.ui.Widget;

public class WidgetsUtils {

  /**
   * Test if the tag name of the element is one of tag names given in parameter
   * 
   * @param tagNames
   * @return
   */
  static boolean matchesTags(Element e, String... tagNames) {

    assert e != null : "Element cannot be null";

    StringBuilder regExp = new StringBuilder("^(");
    int tagNameLenght = tagNames != null ? tagNames.length : 0;
    for (int i = 0; i < tagNameLenght; i++) {
      regExp.append(tagNames[i].toUpperCase());
      if (i < tagNameLenght - 1) {
        regExp.append("|");
      }
    }
    regExp.append(")$");

    return e.getTagName().toUpperCase().matches(regExp.toString());

  }

  /**
   * replace the <code>oldElement</code> by the <code>newElement</code>
   * 
   * @param oldElement
   * @param newElement
   */
   static void replace(Element oldElement, Element newElement) {
    assert oldElement != null && newElement != null;
    GQuery.$(oldElement).replaceWith(newElement);

    String c = oldElement.getClassName();
    if (!c.isEmpty()) {
      newElement.addClassName(c);
    }
   }
   
   static void replace(Element e, Widget widget)  {
     assert e != null && widget != null;
     replace(e, widget.getElement());

     if (widget instanceof Attachable) {
       ((Attachable)widget).attach();
     }
   }

   
}

@@ -341,16 +341,23 @@
    */%0A  
+public 
 static b
@@ -933,54 +933,61 @@
   * 
-%0A   * @param oldElement%0A   * @param newElement
+Old element classes will be copied to the new widget.
 %0A   
@@ -988,24 +988,31 @@
 t.%0A   */%0A   
+public 
 static void 
@@ -1283,16 +1283,214 @@
 %0A   %0A   
+/**%0A    * Replace a dom element by a widget.%0A    * If the widget implements Attachable the method attach will be called.%0A    * Old element classes will be copied to the new widget.%0A    */%0A   public 
 static v

change visibility of methods so as they are available for external plugins