aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManolo Carrasco <manolo@apache.org>2011-02-27 10:06:04 +0000
committerManolo Carrasco <manolo@apache.org>2011-02-27 10:06:04 +0000
commite12772a3f32eec63bdf386db5ee622e8e533ab32 (patch)
tree83fc056ebdeae9d77279ab1bce6143598ec978dc
parent84811d17bf8bc0d00064356d5a301c69220a5698 (diff)
downloadgwtquery-e12772a3f32eec63bdf386db5ee622e8e533ab32.tar.gz
gwtquery-e12772a3f32eec63bdf386db5ee622e8e533ab32.zip
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.
-rw-r--r--gwtquery-core/src/main/java/com/google/gwt/query/client/GQuery.java68
-rw-r--r--gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryCoreTest.java13
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<GQuery, LazyGQuery> {
/**
* Wrap a GQuery around a array of existing widget.
*/
- public static GQuery $(Widget... widgetArray){
+ public static <T extends Widget> GQuery $(T... widgets){
+ return $(Arrays.asList(widgets));
+ }
+
+ /**
+ * Wrap a GQuery around a List of existing widget.
+ */
+ public static <T extends Widget> GQuery $(List<T> 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<GQuery, LazyGQuery> {
return this;
}
-// /**
-// * Set CSS a single style property on every matched element using type-safe
-// * enumerations.
-// */
-// public <S, T extends TakeCssValue<S>> 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 <S extends HasCssName, T extends TakeCssValue<S>> GQuery css(T cssProperty, S value) {
+ return setCss(cssProperty.with(value));
+ }
/**
* Set CSS a single style property on every matched element using type-safe
@@ -2725,6 +2733,30 @@ public class GQuery implements Lazy<GQuery, LazyGQuery> {
}
/**
+ * 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 <W extends Widget> List<W> widgets(Class<W> clazz) {
+ List<W> ret = new ArrayList<W>();
+ 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"));
+
+ $("<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() {