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
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
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
/**\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
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
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
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;
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.
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);
(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() {