]> source.dussan.org Git - gwtquery.git/commitdiff
Adding a nice method able to look for widgeds of a specific class in the dom. Restori...
authorManolo Carrasco <manolo@apache.org>
Sun, 27 Feb 2011 10:06:04 +0000 (10:06 +0000)
committerManolo Carrasco <manolo@apache.org>
Sun, 27 Feb 2011 10:06:04 +0000 (10:06 +0000)
gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java
gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java

index fdf7f71656402abc9019451cc91595a37fc9eed2..6cb97517998006aeb528b020946ff2d06cfb2320 100644 (file)
@@ -19,6 +19,12 @@ import static com.google.gwt.query.client.plugins.Effects.Effects;
 import static com.google.gwt.query.client.plugins.Events.Events;\r
 import static com.google.gwt.query.client.plugins.Widgets.Widgets;\r
 \r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+\r
 import com.google.gwt.core.client.GWT;\r
 import com.google.gwt.core.client.JavaScriptObject;\r
 import com.google.gwt.core.client.JsArray;\r
@@ -33,9 +39,9 @@ import com.google.gwt.dom.client.Node;
 import com.google.gwt.dom.client.NodeList;\r
 import com.google.gwt.dom.client.OptionElement;\r
 import com.google.gwt.dom.client.SelectElement;\r
-import com.google.gwt.dom.client.TextAreaElement;\r
 import com.google.gwt.dom.client.Style.Display;\r
 import com.google.gwt.dom.client.Style.HasCssName;\r
+import com.google.gwt.dom.client.TextAreaElement;\r
 import com.google.gwt.query.client.css.CSS;\r
 import com.google.gwt.query.client.css.CssProperty;\r
 import com.google.gwt.query.client.css.TakeCssValue;\r
@@ -48,11 +54,6 @@ import com.google.gwt.user.client.EventListener;
 import com.google.gwt.user.client.Window;\r
 import com.google.gwt.user.client.ui.Widget;\r
 \r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-\r
 /**\r
  * GwtQuery is a GWT clone of the popular jQuery library.\r
  */\r
@@ -353,9 +354,16 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
   /**\r
    * Wrap a GQuery around a array of existing widget.\r
    */\r
-  public static GQuery $(Widget... widgetArray){\r
+  public static <T extends Widget> GQuery $(T... widgets){\r
+    return $(Arrays.asList(widgets));\r
+  }\r
+\r
+  /**\r
+   * Wrap a GQuery around a List of existing widget.\r
+   */\r
+  public static <T extends Widget> GQuery $(List<T> widgets){\r
     JSArray elements = JSArray.create();\r
-    for (Widget w : widgetArray){\r
+    for (Widget w : widgets){\r
       elements.addNode(w.getElement());\r
     }\r
     return $(elements);\r
@@ -1008,16 +1016,16 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
     return this;\r
   }\r
 \r
-//  /**\r
-//   * Set CSS a single style property on every matched element using type-safe\r
-//   * enumerations.\r
-//   */\r
-//  public <S, T extends TakeCssValue<S>> GQuery css(T cssProperty, S value) {\r
-//    for (Element e : elements()) {\r
-//      cssProperty.set(e.getStyle(), value);\r
-//    }\r
-//    return this;\r
-//  }\r
+  /**\r
+   * Set CSS a single style property on every matched element using type-safe\r
+   * enumerations.\r
+   * \r
+   * @deprecated use css(TakeCssValue.with(...)) instead\r
+   */\r
+  @Deprecated\r
+  public <S extends HasCssName, T extends TakeCssValue<S>> GQuery css(T cssProperty, S value) {\r
+    return setCss(cssProperty.with(value));\r
+  }\r
   \r
   /**\r
    * Set CSS a single style property on every matched element using type-safe\r
@@ -2724,6 +2732,30 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
    return widgets;\r
   }\r
 \r
+  /**\r
+   * Return the list of attached widgets instance of the provided class matching the query.\r
+   * \r
+   * This method is very useful for decoupled views, so as we can access widgets from other\r
+   * views without maintaining methods which export them.\r
+   *  \r
+   */\r
+  @SuppressWarnings("unchecked")\r
+  public <W extends Widget> List<W> widgets(Class<W> clazz) {\r
+    List<W> ret = new ArrayList<W>();\r
+    for (Widget w: widgets()) {\r
+      // isAssignableFrom does not work in gwt.\r
+      Class<?> c = w.getClass();\r
+      do {\r
+        if (c.equals(clazz)) {\r
+          ret.add((W)w);\r
+          break;\r
+        }\r
+        c = c.getSuperclass();\r
+      } while (c != null);\r
+    }\r
+   return ret;\r
+  }\r
+\r
   /**\r
    * Get the current computed, pixel, width of the first matched element.\r
    * It does not include margin, padding nor border.\r
index 9d0d74aac16740a01d3c92f544ba82ce6732377f..5d5be8fd15e85099ac7f3d1345027f2f3b8d7d35 100644 (file)
@@ -25,6 +25,7 @@ import junit.framework.Assert;
 
 import com.google.gwt.dom.client.Document;
 import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.Node;
 import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
 import com.google.gwt.junit.client.GWTTestCase;
@@ -33,8 +34,9 @@ import com.google.gwt.query.client.impl.SelectorEngineSizzle;
 import com.google.gwt.user.client.Event;
 import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.HTML;
+import com.google.gwt.user.client.ui.Label;
 import com.google.gwt.user.client.ui.RootPanel;
-import com.google.gwt.dom.client.Node;
+import com.google.gwt.user.client.ui.TextArea;
 
 /**
  * Test class for testing gwtquery-core api.
@@ -784,9 +786,11 @@ public class GQueryCoreTest extends GWTTestCase {
     assertEquals(true, isAttachedToTheDOM);
   }
   
+  @SuppressWarnings("unchecked")
   public void testGQueryWidgets() {
     final Button b1 = new Button("click-me");
     RootPanel.get().add(b1);
+    
     GQuery g = $(b1);
     Button b2 = (Button) g.asWidget();
     assertEquals(b1, b2);
@@ -800,6 +804,13 @@ public class GQueryCoreTest extends GWTTestCase {
     
     (b2).click();
     assertEquals("red", $(b1).css("color"));
+    
+    $("<button>Click-me</button>").appendTo(document);
+    assertEquals(3, $("button").size());
+    assertEquals(2, $("button").widgets(Button.class).size());
+    assertEquals(2, $($("button").widgets(Button.class)).size());
+    
+    assertEquals(2, $(new Label(""), new TextArea()).size());
   }
   
   public void testGQueryMap() {