]> source.dussan.org Git - gwtquery.git/commitdiff
improve widgets plugins
authorJulien Dramaix <julien.dramaix@gmail.com>
Thu, 3 Mar 2011 22:45:25 +0000 (22:45 +0000)
committerJulien Dramaix <julien.dramaix@gmail.com>
Thu, 3 Mar 2011 22:45:25 +0000 (22:45 +0000)
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/LazyWidgets.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/Widgets.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/AbstractWidgetFactory.java [deleted file]
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/ButtonWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/PasswordTextBoxWidgetFactory.java [new file with mode: 0644]
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/TabPanelWidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/TextBoxWidgetFactory.java [new file with mode: 0644]
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetFactory.java
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java [new file with mode: 0644]
samples/src/main/java/gwtquery/samples/client/GwtQueryWidgetModule.java
samples/src/main/java/gwtquery/samples/public/GwtQueryWidgets.html

index 12df5e3ce1b9f8aa1223e05ddeaec527c5f8624b..fbfee85d78ad9aeba4aa9a66fe2be0c3cd7611b1 100644 (file)
  */
 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<T> extends LazyBase<T>{
   /**
    * 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 <code>initFunctions</code> 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 <code>initFunctions</code> 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<T> tabPanels();
+  LazyWidgets<T> 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<T> tabPanels(TabPanelOptions o);
+  LazyWidgets<T> tabPanels(TabPanelOptions o, Function... initFunctions);
 
   /**
    * Create an return a {@link Button} widget with the first element of the
-   * query
+   * query.The <code>initFunctions</code> 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
+   * <code>initFunctions</code> will be called on each new {@link Button}
+   * created by passing them in parameter.
+   * 
    */
-  Button button(ButtonOptions o);
+  LazyWidgets<T> 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 <code>initFunctions</code> will be called on the new
+   * {@link TextBox} created by passing it in parameter.
    * 
+   * A {@link TextBox} is created if the element is a <i>input</i> with type
+   * text, a <i>div</i> or a<i>span</i> element.
    */
-  LazyWidgets<T> 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
+   * <code>initFunctions</code> will be called on each new {@link TextBox}
+   * created by passing them in parameter.
+   *
+   * A {@link TextBox} is created if the element is a <i>input</i> with type
+   * text, a <i>div</i> or a<i>span</i> element.
    */
-  LazyWidgets<T> buttons(ButtonOptions o);
+  LazyWidgets<T> textBoxes(Function... initFunctions);
 
   /**
    * Create and return a widget using the given factory and the given options
    */
-  <W extends Widget, O extends WidgetOptions> W widget( WidgetFactory<W, O> factory, O options);
+  <W extends Widget> W widget(WidgetFactory<W> 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.
    */
-  <W extends Widget, O extends WidgetOptions> LazyWidgets<T> widgets( WidgetFactory<W, O> factory, O options);
+  <W extends Widget> LazyWidgets<T> widgets(WidgetFactory<W> factory, Function... initFunctions);
 
 }
index 49848cdb15e260907fa62008d940ce2d52598e45..13aaefb08541033807ea4f517e23b1a9e4f5cfd8 100755 (executable)
 package com.google.gwt.query.client.plugins;\r
 \r
 import com.google.gwt.dom.client.Element;\r
+import com.google.gwt.query.client.Function;\r
 import com.google.gwt.query.client.GQuery;\r
 import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory;\r
 import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory;\r
+import com.google.gwt.query.client.plugins.widgets.TextBoxWidgetFactory;\r
 import com.google.gwt.query.client.plugins.widgets.WidgetFactory;\r
 import com.google.gwt.query.client.plugins.widgets.WidgetOptions;\r
-import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory.ButtonOptions;\r
 import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory.TabPanelOptions;\r
 import com.google.gwt.user.client.ui.Button;\r
 import com.google.gwt.user.client.ui.TabPanel;\r
+import com.google.gwt.user.client.ui.TextBox;\r
 import com.google.gwt.user.client.ui.Widget;\r
 \r
 import java.util.ArrayList;\r
 import java.util.List;\r
 \r
 /**\r
- * Widgets plugin for Gwt Query.\r
- * Be careful, this plugin is still experimental. The api can change in next releases.\r
+ * Widgets plugin for Gwt Query. Be careful, this plugin is still experimental.\r
+ * The api can change in next releases.\r
  */\r
 public class Widgets extends QueuePlugin<Widgets> {\r
 \r
@@ -53,18 +55,21 @@ public class Widgets extends QueuePlugin<Widgets> {
   /**\r
    * Create an return a {@link TabPanel} widget with the first selected\r
    * elements. Each div element will create a tab and the first h3 element\r
-   * inside the div will be used as title\r
+   * inside the div will be used as title. The <code>initFunctions</code> will\r
+   * be called on the new {@link TabPanel} created by passing it in parameter.\r
+   * \r
    */\r
-  public TabPanel tabPanel() {\r
+  public TabPanel tabPanel(Function... initFunctions) {\r
     return tabPanel(new TabPanelOptions());\r
   }\r
 \r
   /**\r
    * Create an return a {@link TabPanel} widget with the first selected elements\r
-   * by using a {@link TabPanelOptions}\r
+   * by using a {@link TabPanelOptions}. The <code>initFunctions</code> will be\r
+   * called on each new {@link Button} created by passing them in parameter.\r
    */\r
-  public TabPanel tabPanel(TabPanelOptions o) {\r
-    return widget(new TabPanelWidgetFactory(), o);\r
+  public TabPanel tabPanel(TabPanelOptions o, Function... initFunctions) {\r
+    return widget(new TabPanelWidgetFactory(o), initFunctions);\r
   }\r
 \r
   /**\r
@@ -72,8 +77,8 @@ public class Widgets extends QueuePlugin<Widgets> {
    * will create a tab and the first h3 element inside the div will be used as\r
    * title\r
    */\r
-  public Widgets tabPanels() {\r
-    return tabPanels(new TabPanelOptions());\r
+  public Widgets tabPanels(Function... initFunctions) {\r
+    return tabPanels(new TabPanelOptions(), initFunctions);\r
   }\r
 \r
   /**\r
@@ -81,49 +86,61 @@ public class Widgets extends QueuePlugin<Widgets> {
    * element inside a selected element will create a tab and the first h3\r
    * element inside the div will be used as title\r
    */\r
-  public Widgets tabPanels(TabPanelOptions o) {\r
-    return widgets(new TabPanelWidgetFactory(), o);\r
+  public Widgets tabPanels(TabPanelOptions o, Function... initFunctions) {\r
+    return widgets(new TabPanelWidgetFactory(o), initFunctions);\r
   }\r
 \r
   /**\r
    * Create an return a {@link Button} widget with the first element of the\r
-   * query\r
+   * query.The <code>initFunctions</code> will be called on the new\r
+   * {@link Button} created by passing it in parameter.\r
+   * \r
    */\r
-  public Button button() {\r
-    return button(new ButtonOptions());\r
+  public Button button(Function... initFunctions) {\r
+    return widget(new ButtonWidgetFactory(), initFunctions);\r
   }\r
 \r
   /**\r
-   * Create and return a {@link Button} widget with the first element of the\r
-   * query by using a {@link ButtonOptions}\r
+   * Create a {@link Button} widget for each selected element. The\r
+   * <code>initFunctions</code> will be called on each new {@link Button}\r
+   * created by passing them in parameter.\r
+   * \r
    */\r
-  public Button button(ButtonOptions o) {\r
-    return widget(new ButtonWidgetFactory(), o);\r
+  public Widgets buttons(Function... initFunctions) {\r
+    return widgets(new ButtonWidgetFactory(), initFunctions);\r
   }\r
 \r
   /**\r
-   * Create a {@link Button} widget for each selected element.\r
+   * Create an return a {@link TextBox} widget with the first element of the\r
+   * query.The <code>initFunctions</code> will be called on the new\r
+   * {@link TextBox} created by passing it in parameter.\r
    * \r
+   * A {@link TextBox} is created if the element is a <i>input</i> with type\r
+   * text, a <i>div</i> or a<i>span</i> element.\r
    */\r
-  public Widgets buttons() {\r
-    return buttons(new ButtonOptions());\r
+  public TextBox textBox(Function... initFunctions) {\r
+    return widget(new TextBoxWidgetFactory(), initFunctions);\r
   }\r
 \r
   /**\r
-   * Create a {@link Button} widget for each selected element by using a\r
-   * {@link ButtonOptions}\r
-   * \r
+   * Create a {@link TextBox} widget for each selected element. The\r
+   * <code>initFunctions</code> will be called on each new {@link TextBox}\r
+   * created by passing them in parameter.\r
+   *\r
+   * A {@link TextBox} is created if the element is a <i>input</i> with type\r
+   * text, a <i>div</i> or a<i>span</i> element.\r
    */\r
-  public Widgets buttons(ButtonOptions o) {\r
-    return widgets(new ButtonWidgetFactory(), o);\r
+  public Widgets textBoxes(Function... initFunctions) {\r
+    return widgets(new TextBoxWidgetFactory(), initFunctions);\r
   }\r
 \r
   /**\r
    * Create and return a widget using the given factory and the given options\r
    */\r
-  public <W extends Widget, O extends WidgetOptions> W widget(\r
-      WidgetFactory<W, O> factory, O options) {\r
-    return widget(get(0), factory, options);\r
+  public <W extends Widget> W widget(WidgetFactory<W> factory,\r
+      Function... initFunctions) {\r
+\r
+    return widget(get(0), factory, initFunctions);\r
   }\r
 \r
   /**\r
@@ -131,12 +148,15 @@ public class Widgets extends QueuePlugin<Widgets> {
    * each element of the query. Returns a new gquery set of elements with the\r
    * new widgets created.\r
    */\r
-  public <W extends Widget, O extends WidgetOptions> Widgets widgets(\r
-      WidgetFactory<W, O> factory, O options) {\r
+  public <W extends Widget> Widgets widgets(WidgetFactory<W> factory,\r
+      Function... initFunctions) {\r
+\r
     List<Element> result = new ArrayList<Element>();\r
+\r
     for (Element e : elements()) {\r
-      result.add(widget(e, factory, options).getElement());\r
+      result.add(widget(e, factory, initFunctions).getElement());\r
     }\r
+\r
     return $(result).as(Widgets);\r
   }\r
 \r
@@ -144,8 +164,18 @@ public class Widgets extends QueuePlugin<Widgets> {
    * Create and return a widget using the given factory and the given options\r
    */\r
   protected <W extends Widget, O extends WidgetOptions> W widget(Element e,\r
-      WidgetFactory<W, O> factory, O options) {\r
-    return factory.create(e, options);\r
+      WidgetFactory<W> factory, Function... initFunctions) {\r
+\r
+    W widget = factory.create(e);\r
+\r
+    if (initFunctions != null) {\r
+      for (Function initFunction : initFunctions) {\r
+        initFunction.f(widget);\r
+      }\r
+    }\r
+\r
+    return widget;\r
+\r
   }\r
 \r
 }\r
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/AbstractWidgetFactory.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/AbstractWidgetFactory.java
deleted file mode 100644 (file)
index 928301e..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-package com.google.gwt.query.client.plugins.widgets;
-
-import com.google.gwt.dom.client.Element;
-import com.google.gwt.user.client.ui.Widget;
-
-/**
- * Abstract factory containing useful methods for widget creation
- * 
- */
-public abstract class AbstractWidgetFactory<W extends Widget, O extends WidgetOptions>
-    implements WidgetFactory<W, O> {
-
-  public W create(Element e, O options) {
-    W w = createWidget(e);
-
-    initialize(w, options, e);
-
-    return w;
-  }
-
-  protected abstract void initialize(W widget, O options, Element e);
-
-  protected abstract W createWidget(Element e);
-
-  /**
-   * Test if the tag name of the element is one of tag names given in parameter
-   * 
-   * @param tagNames
-   * @return
-   */
-  protected boolean matchesTags(Element e, String... tagNames) {
-
-    assert e != null : "Element cannot be null";
-
-    StringBuilder regExp = new StringBuilder("^(");
-    int tagNameLenght = tagNames != null ? tagNames.length : 0;
-    for (int i = 0; i < tagNameLenght; i++) {
-      regExp.append(tagNames[i].toUpperCase());
-      if (i < tagNameLenght - 1) {
-        regExp.append("|");
-      }
-    }
-    regExp.append(")$");
-
-    return e.getTagName().toUpperCase().matches(regExp.toString());
-
-  }
-
-}
\ No newline at end of file
index 9cafbe2230ce32d4ddc2709b7144fabaefb25d74..f0482857839685b6ccbf1d477239b5c99ba344ac 100644 (file)
@@ -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 <i>button</i>, <i>div></i>, <i>span</i> or <i>a</i>
  * element (should be extends to other element).
  */
-public class ButtonWidgetFactory extends
-    AbstractWidgetFactory<Button, ButtonWidgetFactory.ButtonOptions> {
-
-  /**
-   * Options used to initialize new {@link Button}
-   * 
-   */
-  public static class ButtonOptions implements WidgetOptions {
-
-    private List<ClickHandler> clickHandlers;
-
-    public ButtonOptions() {
-      clickHandlers = new ArrayList<ClickHandler>();
-    }
-
-    public void addClickHandler(ClickHandler clickHandler) {
-      clickHandlers.add(clickHandler);
-    }
-
-    public List<ClickHandler> 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<Button> {
 
-  protected Button createWidget(Element e) {
+  public Button create(Element e) {
 
     if ("button".equalsIgnoreCase(e.getTagName())) {
       return Button.wrap(e);
     }
 
-    if (matchesTags(e, "div", "span", "a")) {
+    if (WidgetsUtils.matchesTags(e, "div", "span", "a")) {
       ButtonElement buttonElement = Document.get().createPushButtonElement();
-      e.getParentElement().insertAfter(buttonElement, e);
-      // detach the original element (can be maybe hidden instead of detach
-      // it?)
-      e.removeFromParent();
+      
+      WidgetsUtils.replace(e, buttonElement);
 
       Button b = Button.wrap(buttonElement);
       b.setHTML(e.getInnerHTML()); // maybe use setText and getInnerText
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/PasswordTextBoxWidgetFactory.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/PasswordTextBoxWidgetFactory.java
new file mode 100644 (file)
index 0000000..ce28bad
--- /dev/null
@@ -0,0 +1,40 @@
+package com.google.gwt.query.client.plugins.widgets;
+
+import static com.google.gwt.query.client.GQuery.$;
+
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.InputElement;
+import com.google.gwt.query.client.GQuery;
+import com.google.gwt.user.client.ui.PasswordTextBox;
+
+/**
+ * Factory used to create a {@link PasswordTextBox} widget. A {@link PasswordTextBox} is created
+ * if the element is a <i>input</i> with type <i>password</i>, a <i>div</i> or a<i>span</i> element.
+ * 
+ */
+public class PasswordTextBoxWidgetFactory implements WidgetFactory<PasswordTextBox> {
+
+  public PasswordTextBox create(Element e) {
+
+    GQuery input = $(e).filter("input[type='passowrd']");
+    
+    if (input.get(0) != null) {
+      return PasswordTextBox.wrap(e);
+    }
+
+    if (WidgetsUtils.matchesTags(e, "div", "span")) {
+      InputElement inputElement = Document.get().createPasswordInputElement();
+      
+      WidgetsUtils.replace(e, inputElement);
+      
+
+      PasswordTextBox textBox = PasswordTextBox.wrap(inputElement);
+      textBox.setValue(e.getInnerText()); // maybe use setText and getInnerText
+
+      return textBox;
+    }
+
+    return null;
+  }
+}
\ No newline at end of file
index 9fafebbce6330118ea6ede27669e9d8904fa8cc4..928a54866fc8b1b362a00d63bd20ded95c890043 100644 (file)
@@ -9,23 +9,13 @@ import com.google.gwt.user.client.ui.HTMLPanel;
 import com.google.gwt.user.client.ui.RootPanel;
 import com.google.gwt.user.client.ui.TabPanel;
 
-
 /**
  * Factory used to create a {@link Button} widget. A {@link Button} is created
  * if the element is a <i>button</i>, <i>div></i>, <i>span</i> or <i>a</i>
  * element (should be extends to other element).
  */
-public class TabPanelWidgetFactory extends
-    AbstractWidgetFactory<TabPanel, TabPanelWidgetFactory.TabPanelOptions> {
-  
-  public static class ExtendedTabPanel extends TabPanel{
-    
-    
-    void attach(){
-      onAttach();
-      RootPanel.detachOnWindowClose(this);
-    }
-  }
+public class TabPanelWidgetFactory implements WidgetFactory<TabPanel> {
+
 
   /**
    * Options used to initialize new {@link Button}
@@ -61,8 +51,31 @@ public class TabPanelWidgetFactory extends
       titleSelector = "h3";
     }
   }
+  
+  private static class ExtendedTabPanel extends TabPanel {
+
+    void attach() {
+      onAttach();
+      RootPanel.detachOnWindowClose(this);
+    }
+  }
+
+  private TabPanelOptions options;
+
+  public TabPanelWidgetFactory(TabPanelOptions o) {
+    this.options = o;
+  }
+
+  public TabPanel create(Element e) {
+    ExtendedTabPanel tabPanel = new ExtendedTabPanel();
+
+    initialize(tabPanel, options, e);
+
+    return tabPanel;
+  }
+
 
-  protected void initialize(TabPanel tabPanel, TabPanelOptions options,
+  protected void initialize(ExtendedTabPanel tabPanel, TabPanelOptions options,
       Element e) {
 
     GQuery tabs = $(options.getTabSelector(), e);
@@ -74,24 +87,15 @@ public class TabPanelWidgetFactory extends
 
       tabPanel.add(new HTMLPanel(tab.getString()), title != null
           ? title.getInnerText() : "Tab " + (i + 1));
-      
-      
 
     }
-    
-    if (tabs.length() > 0){
+
+    if (tabs.length() > 0) {
       tabPanel.selectTab(0);
     }
 
-    // the tab panel is initialized, attach it to the dom ;
-    e.getParentElement().insertBefore(tabPanel.getElement(), e);
-    ((ExtendedTabPanel)tabPanel).attach();
-
-    // detach the element as it is replaced by the tab panel !
-    e.removeFromParent();
-  }
+    WidgetsUtils.replace(e, tabPanel.getElement());
 
-  protected TabPanel createWidget(Element e) {
-    return new ExtendedTabPanel();
+    tabPanel.attach();
   }
 }
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/TextBoxWidgetFactory.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/TextBoxWidgetFactory.java
new file mode 100644 (file)
index 0000000..3efc5d8
--- /dev/null
@@ -0,0 +1,39 @@
+package com.google.gwt.query.client.plugins.widgets;
+
+import static com.google.gwt.query.client.GQuery.$;
+
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.InputElement;
+import com.google.gwt.query.client.GQuery;
+import com.google.gwt.user.client.ui.TextBox;
+
+/**
+ * Factory used to create a {@link TextBox} widget. A {@link TextBox} is created
+ * if the element is a <i>input</i> with type text, a <i>div</i> or a<i>span</i> element.
+ * 
+ */
+public class TextBoxWidgetFactory implements WidgetFactory<TextBox> {
+
+  public TextBox create(Element e) {
+
+    GQuery input = $(e).filter("input[type='text']");
+    
+    if (input.get(0) != null) {
+      return TextBox.wrap(e);
+    }
+
+    if (WidgetsUtils.matchesTags(e, "div", "span")) {
+      InputElement inputElement = Document.get().createTextInputElement();
+      
+      WidgetsUtils.replace(e, inputElement);
+
+      TextBox textBox = TextBox.wrap(inputElement);
+      textBox.setValue(e.getInnerText()); // maybe use setText and getInnerText
+
+      return textBox;
+    }
+
+    return null;
+  }
+}
\ No newline at end of file
index 41c23ed5d7c7826f6c6875a2d26014a2bd9a37c5..9b434fdf833f89c5672f716c84d9c890ccfaed74 100644 (file)
@@ -9,6 +9,6 @@ import com.google.gwt.user.client.ui.Widget;
    * @param <W>
    * @param <O>
    */
-  public interface WidgetFactory<W extends Widget, O extends WidgetOptions> {
-    public W create(Element e, O options);
+  public interface WidgetFactory<W extends Widget> {
+    public W create(Element e);
   }
\ No newline at end of file
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java
new file mode 100644 (file)
index 0000000..360afcf
--- /dev/null
@@ -0,0 +1,44 @@
+package com.google.gwt.query.client.plugins.widgets;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.query.client.GQuery;
+
+public class WidgetsUtils {
+
+  /**
+   * Test if the tag name of the element is one of tag names given in parameter
+   * 
+   * @param tagNames
+   * @return
+   */
+  static boolean matchesTags(Element e, String... tagNames) {
+
+    assert e != null : "Element cannot be null";
+
+    StringBuilder regExp = new StringBuilder("^(");
+    int tagNameLenght = tagNames != null ? tagNames.length : 0;
+    for (int i = 0; i < tagNameLenght; i++) {
+      regExp.append(tagNames[i].toUpperCase());
+      if (i < tagNameLenght - 1) {
+        regExp.append("|");
+      }
+    }
+    regExp.append(")$");
+
+    return e.getTagName().toUpperCase().matches(regExp.toString());
+
+  }
+
+  /**
+   * replace the <code>oldElement</code> by the <code>newElement</code>
+   * 
+   * @param oldElement
+   * @param newElement
+   */
+   static void replace(Element oldElement, Element newElement) {
+    assert oldElement != null && newElement != null;
+    
+    GQuery.$(oldElement).replaceWith(newElement);
+
+  }
+}
index e2bb19dcc3f04b70fb6d6888effa310e27c1a46c..cff828ddbd5b4e1baa9bac3388671b544b8f0323 100644 (file)
@@ -22,94 +22,42 @@ import com.google.gwt.core.client.EntryPoint;
 import com.google.gwt.event.dom.client.ClickEvent;\r
 import com.google.gwt.event.dom.client.ClickHandler;\r
 import com.google.gwt.query.client.Function;\r
-import com.google.gwt.query.client.GQuery;\r
-import com.google.gwt.query.client.css.CSS;\r
-import com.google.gwt.query.client.css.Length;\r
-import com.google.gwt.query.client.css.RGBColor;\r
-import com.google.gwt.query.client.css.UriValue;\r
-import com.google.gwt.query.client.css.BackgroundAttachmentProperty.BackgroundAttachment;\r
-import com.google.gwt.query.client.css.BackgroundPositionProperty.BackgroundPosition;\r
-import com.google.gwt.query.client.css.BackgroundRepeatProperty.BackgroundRepeat;\r
-import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory.ButtonOptions;\r
-import com.google.gwt.user.client.Window;\r
 import com.google.gwt.user.client.ui.Button;\r
 import com.google.gwt.user.client.ui.Label;\r
 import com.google.gwt.user.client.ui.PopupPanel;\r
+import com.google.gwt.user.client.ui.Widget;\r
 \r
 public class GwtQueryWidgetModule implements EntryPoint {\r
 \r
-/*  public void onModuleLoad() {\r
-    $("<button>Enhance</button>").appendTo(".outer").one(Event.ONCLICK, null, new Function() {\r
-      public boolean f(Event e) {\r
-        $(".btn:nth-child(odd)").each(new Function(){\r
-          public void f(Element el) {\r
-            // Replace odd labels by a button\r
-            GQuery g = $("<button/>");\r
-            $(el).hide().after(g);\r
-            \r
-            // Use the Widgets plugin to convert the button element to a button.\r
-            Button b = g.as(Widgets.Widgets).widget();\r
-            b.setText("Foo");\r
-            b.addClickHandler(new ClickHandler() {\r
-              public void onClick(ClickEvent clickEvent) {\r
-                Window.alert("You Clicked the Button");\r
-              }\r
-            });\r
-          }\r
-        });\r
-        return true;\r
-      }\r
-    });\r
-  }*/\r
-  \r
   public void onModuleLoad() {\r
+\r
+    $(".inputText").as(Widgets).textBoxes();\r
     \r
-    \r
-    // Let gquery syntax to help gwt developers. \r
-    GQuery buttons = $(".btn").as(Widgets).buttons().click(new Function() {\r
-      public void f() {\r
-        Label l = new Label("You click on a GWT Button !");\r
-        PopupPanel panel = new PopupPanel(true, true);\r
-        panel.setGlassEnabled(true);\r
-        panel.add(l);\r
-        panel.center();\r
-      }\r
-    });\r
-    \r
-    // The user use a widget in the traditional way\r
-    buttons.eq(0).<Button>widget().addClickHandler(new ClickHandler() {\r
-      public void onClick(ClickEvent event) {\r
-        Window.alert("You clicked in the first button");\r
-      }\r
-    });\r
-    \r
-    $("#tabs").as(Widgets).tabPanel();\r
-    \r
-    // IDE suggestions are not available with this syntax. -> I agree\r
-    // Also it implies that the css method in GQuery needs more overloads\r
-    // $("#aaa").css(CSS.POSITION, Position.ABSOLUTE);\r
-    // $("#aaa").css(CSS.TOP, Length.cm(15));\r
-    // $("#aaa").css(CSS.BACKGROUND, RGBColor.RED, ImageValue.url(""), BackgroundRepeat.NO_REPEAT, BackgroundAttachment.FIXED, BackgroundPosition.CENTER);\r
-    \r
-    $("#aaa").css(CSS.TOP.with(Length.cm(15)));\r
-    $("#aaa").css(CSS.BACKGROUND.with(RGBColor.SILVER, UriValue.url(""), BackgroundRepeat.NO_REPEAT, BackgroundAttachment.FIXED, BackgroundPosition.CENTER)); \r
-    $("#aaa").css(CSS.BACKGROUND_ATTACHMENT.with(BackgroundAttachment.FIXED));\r
-  }\r
-  \r
-  private ButtonOptions createButtonOptions(){\r
-    ButtonOptions options = new ButtonOptions();\r
-    options.addClickHandler(new ClickHandler() {\r
+    $(".btn").as(Widgets).buttons(new Function() {\r
       \r
-      public void onClick(ClickEvent event) {\r
-        Label l = new Label("You click on a GWT Button !");\r
-        PopupPanel panel = new PopupPanel(true, true);\r
-        panel.setGlassEnabled(true);\r
-        panel.add(l);\r
-        panel.center();\r
+      public void f(Widget w) {\r
         \r
+        Button button = (Button) w;\r
+        button.addClickHandler(new ClickHandler() {\r
+\r
+          public void onClick(ClickEvent event) {\r
+            Label l = new Label("You click on a GWT Button !");\r
+            PopupPanel panel = new PopupPanel(true, true);\r
+            panel.setGlassEnabled(true);\r
+            panel.add(l);\r
+            panel.center();\r
+\r
+          }\r
+        });\r
+\r
       }\r
+      \r
     });\r
+\r
+    $("#tabs").as(Widgets).tabPanel();\r
     \r
-    return options;\r
+    \r
+\r
   }\r
+\r
 }\r
index 0b189ab4cb9e16ca17d07849646a9cfc7eb457d4..8e95b165a9198a461e4ab0a78d80b92a7eaffe36 100644 (file)
@@ -5,7 +5,7 @@
     src="gwtquery.samples.GwtQueryWidgets.nocache.js"></script>\r
 </head>\r
 <body>\r
-<div id='aaa'>aaa</div>\r
+\r
 <div class="outer">\r
 <div class="btn">Make me a button 1!</div>\r
 <a class="btn">Make me a button 2!</a> <span class="btn">Make me\r
@@ -15,6 +15,10 @@ a button 3!</span>
 <div class="btn">Make me a button 6!</div>\r
 </div>\r
 \r
+<div class="inputText">I will be an Input</div>\r
+<span class="inputText">I will be an Input</span>\r
+<input class="inputText" type="text"></input>\r
+\r
 <div id="tabs">\r
 <h3>First tab</h3>\r
 <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut\r