From e12772a3f32eec63bdf386db5ee622e8e533ab32 Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Sun, 27 Feb 2011 10:06:04 +0000 Subject: [PATCH] Adding a nice method able to look for widgeds of a specific class in the dom. Restoring css method removed in a previous release and deprecating it, in order to avoid to break existing apps using it. --- .../com/google/gwt/query/client/GQuery.java | 68 ++++++++++++++----- .../gwt/query/client/GQueryCoreTest.java | 13 +++- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java index fdf7f716..6cb97517 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java @@ -19,6 +19,12 @@ import static com.google.gwt.query.client.plugins.Effects.Effects; import static com.google.gwt.query.client.plugins.Events.Events; import static com.google.gwt.query.client.plugins.Widgets.Widgets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; + import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; @@ -33,9 +39,9 @@ import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.NodeList; import com.google.gwt.dom.client.OptionElement; import com.google.gwt.dom.client.SelectElement; -import com.google.gwt.dom.client.TextAreaElement; import com.google.gwt.dom.client.Style.Display; import com.google.gwt.dom.client.Style.HasCssName; +import com.google.gwt.dom.client.TextAreaElement; import com.google.gwt.query.client.css.CSS; import com.google.gwt.query.client.css.CssProperty; import com.google.gwt.query.client.css.TakeCssValue; @@ -48,11 +54,6 @@ import com.google.gwt.user.client.EventListener; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Widget; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; - /** * GwtQuery is a GWT clone of the popular jQuery library. */ @@ -353,9 +354,16 @@ public class GQuery implements Lazy { /** * Wrap a GQuery around a array of existing widget. */ - public static GQuery $(Widget... widgetArray){ + public static GQuery $(T... widgets){ + return $(Arrays.asList(widgets)); + } + + /** + * Wrap a GQuery around a List of existing widget. + */ + public static GQuery $(List widgets){ JSArray elements = JSArray.create(); - for (Widget w : widgetArray){ + for (Widget w : widgets){ elements.addNode(w.getElement()); } return $(elements); @@ -1008,16 +1016,16 @@ public class GQuery implements Lazy { return this; } -// /** -// * Set CSS a single style property on every matched element using type-safe -// * enumerations. -// */ -// public > GQuery css(T cssProperty, S value) { -// for (Element e : elements()) { -// cssProperty.set(e.getStyle(), value); -// } -// return this; -// } + /** + * Set CSS a single style property on every matched element using type-safe + * enumerations. + * + * @deprecated use css(TakeCssValue.with(...)) instead + */ + @Deprecated + public > GQuery css(T cssProperty, S value) { + return setCss(cssProperty.with(value)); + } /** * Set CSS a single style property on every matched element using type-safe @@ -2724,6 +2732,30 @@ public class GQuery implements Lazy { return widgets; } + /** + * Return the list of attached widgets instance of the provided class matching the query. + * + * This method is very useful for decoupled views, so as we can access widgets from other + * views without maintaining methods which export them. + * + */ + @SuppressWarnings("unchecked") + public List widgets(Class clazz) { + List ret = new ArrayList(); + for (Widget w: widgets()) { + // isAssignableFrom does not work in gwt. + Class c = w.getClass(); + do { + if (c.equals(clazz)) { + ret.add((W)w); + break; + } + c = c.getSuperclass(); + } while (c != null); + } + return ret; + } + /** * Get the current computed, pixel, width of the first matched element. * It does not include margin, padding nor border. diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java index 9d0d74aa..5d5be8fd 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java @@ -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")); + + $("").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() { -- 2.39.5