From 11b54244a60443d7a5a8edd1deb13833e8d24e59 Mon Sep 17 00:00:00 2001 From: Julien Dramaix Date: Thu, 3 Mar 2011 22:45:25 +0000 Subject: [PATCH] improve widgets plugins --- .../gwt/query/client/plugins/LazyWidgets.java | 56 ++++++---- .../gwt/query/client/plugins/Widgets.java | 100 ++++++++++++------ .../plugins/widgets/ButtonWidgetFactory.java | 49 +-------- .../widgets/PasswordTextBoxWidgetFactory.java | 40 +++++++ .../widgets/TabPanelWidgetFactory.java | 56 +++++----- .../plugins/widgets/TextBoxWidgetFactory.java | 39 +++++++ .../client/plugins/widgets/WidgetFactory.java | 4 +- ...ctWidgetFactory.java => WidgetsUtils.java} | 37 +++---- .../samples/client/GwtQueryWidgetModule.java | 100 +++++------------- .../samples/public/GwtQueryWidgets.html | 6 +- 10 files changed, 262 insertions(+), 225 deletions(-) create mode 100644 gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/PasswordTextBoxWidgetFactory.java create mode 100644 gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/TextBoxWidgetFactory.java rename gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/{AbstractWidgetFactory.java => WidgetsUtils.java} (55%) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyWidgets.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyWidgets.java index 12df5e3c..fbfee85d 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyWidgets.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyWidgets.java @@ -15,15 +15,17 @@ */ package com.google.gwt.query.client.plugins; import com.google.gwt.dom.client.Element; +import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory; import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory; +import com.google.gwt.query.client.plugins.widgets.TextBoxWidgetFactory; import com.google.gwt.query.client.plugins.widgets.WidgetFactory; import com.google.gwt.query.client.plugins.widgets.WidgetOptions; -import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory.ButtonOptions; import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory.TabPanelOptions; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.TabPanel; +import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.Widget; import java.util.ArrayList; import java.util.List; @@ -34,65 +36,79 @@ public interface LazyWidgets extends LazyBase{ /** * Create an return a {@link TabPanel} widget with the first selected * elements. Each div element will create a tab and the first h3 element - * inside the div will be used as title + * inside the div will be used as title. The initFunctions will + * be called on the new {@link TabPanel} created by passing it in parameter. + * */ - TabPanel tabPanel(); + TabPanel tabPanel(Function... initFunctions); /** * Create an return a {@link TabPanel} widget with the first selected elements - * by using a {@link TabPanelOptions} + * by using a {@link TabPanelOptions}. The initFunctions will be + * called on each new {@link Button} created by passing them in parameter. */ - TabPanel tabPanel(TabPanelOptions o); + TabPanel tabPanel(TabPanelOptions o, Function... initFunctions); /** * Create {@link TabPanel} widget for each selected elements. Each div element * will create a tab and the first h3 element inside the div will be used as * title */ - LazyWidgets tabPanels(); + LazyWidgets tabPanels(Function... initFunctions); /** * Create a {@link TabPanel} widget for each selected elements. Each div * element inside a selected element will create a tab and the first h3 * element inside the div will be used as title */ - LazyWidgets tabPanels(TabPanelOptions o); + LazyWidgets tabPanels(TabPanelOptions o, Function... initFunctions); /** * Create an return a {@link Button} widget with the first element of the - * query + * query.The initFunctions will be called on the new + * {@link Button} created by passing it in parameter. + * */ - Button button(); + Button button(Function... initFunctions); /** - * Create and return a {@link Button} widget with the first element of the - * query by using a {@link ButtonOptions} + * Create a {@link Button} widget for each selected element. The + * initFunctions will be called on each new {@link Button} + * created by passing them in parameter. + * */ - Button button(ButtonOptions o); + LazyWidgets buttons(Function... initFunctions); /** - * Create a {@link Button} widget for each selected element. + * Create an return a {@link TextBox} widget with the first element of the + * query.The initFunctions will be called on the new + * {@link TextBox} created by passing it in parameter. * + * A {@link TextBox} is created if the element is a input with type + * text, a div or aspan element. */ - LazyWidgets buttons(); + TextBox textBox(Function... initFunctions); /** - * Create a {@link Button} widget for each selected element by using a - * {@link ButtonOptions} - * + * Create a {@link TextBox} widget for each selected element. The + * initFunctions will be called on each new {@link TextBox} + * created by passing them in parameter. + * + * A {@link TextBox} is created if the element is a input with type + * text, a div or aspan element. */ - LazyWidgets buttons(ButtonOptions o); + LazyWidgets textBoxes(Function... initFunctions); /** * Create and return a widget using the given factory and the given options */ - W widget( WidgetFactory factory, O options); + W widget(WidgetFactory factory, Function... initFunctions); /** * Try to create a widget using the given factory and the given options for * each element of the query. Returns a new gquery set of elements with the * new widgets created. */ - LazyWidgets widgets( WidgetFactory factory, O options); + LazyWidgets widgets(WidgetFactory factory, Function... initFunctions); } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Widgets.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Widgets.java index 49848cdb..13aaefb0 100755 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Widgets.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Widgets.java @@ -16,23 +16,25 @@ package com.google.gwt.query.client.plugins; import com.google.gwt.dom.client.Element; +import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory; import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory; +import com.google.gwt.query.client.plugins.widgets.TextBoxWidgetFactory; import com.google.gwt.query.client.plugins.widgets.WidgetFactory; import com.google.gwt.query.client.plugins.widgets.WidgetOptions; -import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory.ButtonOptions; import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory.TabPanelOptions; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.TabPanel; +import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.Widget; import java.util.ArrayList; import java.util.List; /** - * Widgets plugin for Gwt Query. - * Be careful, this plugin is still experimental. The api can change in next releases. + * Widgets plugin for Gwt Query. Be careful, this plugin is still experimental. + * The api can change in next releases. */ public class Widgets extends QueuePlugin { @@ -53,18 +55,21 @@ public class Widgets extends QueuePlugin { /** * Create an return a {@link TabPanel} widget with the first selected * elements. Each div element will create a tab and the first h3 element - * inside the div will be used as title + * inside the div will be used as title. The initFunctions will + * be called on the new {@link TabPanel} created by passing it in parameter. + * */ - public TabPanel tabPanel() { + public TabPanel tabPanel(Function... initFunctions) { return tabPanel(new TabPanelOptions()); } /** * Create an return a {@link TabPanel} widget with the first selected elements - * by using a {@link TabPanelOptions} + * by using a {@link TabPanelOptions}. The initFunctions will be + * called on each new {@link Button} created by passing them in parameter. */ - public TabPanel tabPanel(TabPanelOptions o) { - return widget(new TabPanelWidgetFactory(), o); + public TabPanel tabPanel(TabPanelOptions o, Function... initFunctions) { + return widget(new TabPanelWidgetFactory(o), initFunctions); } /** @@ -72,8 +77,8 @@ public class Widgets extends QueuePlugin { * will create a tab and the first h3 element inside the div will be used as * title */ - public Widgets tabPanels() { - return tabPanels(new TabPanelOptions()); + public Widgets tabPanels(Function... initFunctions) { + return tabPanels(new TabPanelOptions(), initFunctions); } /** @@ -81,49 +86,61 @@ public class Widgets extends QueuePlugin { * element inside a selected element will create a tab and the first h3 * element inside the div will be used as title */ - public Widgets tabPanels(TabPanelOptions o) { - return widgets(new TabPanelWidgetFactory(), o); + public Widgets tabPanels(TabPanelOptions o, Function... initFunctions) { + return widgets(new TabPanelWidgetFactory(o), initFunctions); } /** * Create an return a {@link Button} widget with the first element of the - * query + * query.The initFunctions will be called on the new + * {@link Button} created by passing it in parameter. + * */ - public Button button() { - return button(new ButtonOptions()); + public Button button(Function... initFunctions) { + return widget(new ButtonWidgetFactory(), initFunctions); } /** - * Create and return a {@link Button} widget with the first element of the - * query by using a {@link ButtonOptions} + * Create a {@link Button} widget for each selected element. The + * initFunctions will be called on each new {@link Button} + * created by passing them in parameter. + * */ - public Button button(ButtonOptions o) { - return widget(new ButtonWidgetFactory(), o); + public Widgets buttons(Function... initFunctions) { + return widgets(new ButtonWidgetFactory(), initFunctions); } /** - * Create a {@link Button} widget for each selected element. + * Create an return a {@link TextBox} widget with the first element of the + * query.The initFunctions will be called on the new + * {@link TextBox} created by passing it in parameter. * + * A {@link TextBox} is created if the element is a input with type + * text, a div or aspan element. */ - public Widgets buttons() { - return buttons(new ButtonOptions()); + public TextBox textBox(Function... initFunctions) { + return widget(new TextBoxWidgetFactory(), initFunctions); } /** - * Create a {@link Button} widget for each selected element by using a - * {@link ButtonOptions} - * + * Create a {@link TextBox} widget for each selected element. The + * initFunctions will be called on each new {@link TextBox} + * created by passing them in parameter. + * + * A {@link TextBox} is created if the element is a input with type + * text, a div or aspan element. */ - public Widgets buttons(ButtonOptions o) { - return widgets(new ButtonWidgetFactory(), o); + public Widgets textBoxes(Function... initFunctions) { + return widgets(new TextBoxWidgetFactory(), initFunctions); } /** * Create and return a widget using the given factory and the given options */ - public W widget( - WidgetFactory factory, O options) { - return widget(get(0), factory, options); + public W widget(WidgetFactory factory, + Function... initFunctions) { + + return widget(get(0), factory, initFunctions); } /** @@ -131,12 +148,15 @@ public class Widgets extends QueuePlugin { * each element of the query. Returns a new gquery set of elements with the * new widgets created. */ - public Widgets widgets( - WidgetFactory factory, O options) { + public Widgets widgets(WidgetFactory factory, + Function... initFunctions) { + List result = new ArrayList(); + for (Element e : elements()) { - result.add(widget(e, factory, options).getElement()); + result.add(widget(e, factory, initFunctions).getElement()); } + return $(result).as(Widgets); } @@ -144,8 +164,18 @@ public class Widgets extends QueuePlugin { * Create and return a widget using the given factory and the given options */ protected W widget(Element e, - WidgetFactory factory, O options) { - return factory.create(e, options); + WidgetFactory factory, Function... initFunctions) { + + W widget = factory.create(e); + + if (initFunctions != null) { + for (Function initFunction : initFunctions) { + initFunction.f(widget); + } + } + + return widget; + } } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/ButtonWidgetFactory.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/ButtonWidgetFactory.java index 9cafbe22..f0482857 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/ButtonWidgetFactory.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/ButtonWidgetFactory.java @@ -3,64 +3,25 @@ package com.google.gwt.query.client.plugins.widgets; import com.google.gwt.dom.client.ButtonElement; import com.google.gwt.dom.client.Document; import com.google.gwt.dom.client.Element; -import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.user.client.ui.Button; -import java.util.ArrayList; -import java.util.List; - /** * Factory used to create a {@link Button} widget. A {@link Button} is created * if the element is a button, div>, span or a * element (should be extends to other element). */ -public class ButtonWidgetFactory extends - AbstractWidgetFactory { - - /** - * Options used to initialize new {@link Button} - * - */ - public static class ButtonOptions implements WidgetOptions { - - private List clickHandlers; - - public ButtonOptions() { - clickHandlers = new ArrayList(); - } - - public void addClickHandler(ClickHandler clickHandler) { - clickHandlers.add(clickHandler); - } - - public List getClickHandlers() { - return clickHandlers; - } - } - - protected void initialize(Button button, ButtonOptions options, Element e) { - if (button == null || options == null) { - return; - } - - for (ClickHandler handler : options.getClickHandlers()) { - button.addClickHandler(handler); - } - - } +public class ButtonWidgetFactory implements WidgetFactory").appendTo(".outer").one(Event.ONCLICK, null, new Function() { - public boolean f(Event e) { - $(".btn:nth-child(odd)").each(new Function(){ - public void f(Element el) { - // Replace odd labels by a button - GQuery g = $("