]> source.dussan.org Git - gwtquery.git/commitdiff
Added RichTextArea and DateBox widgetfactories. These are example of widgets which...
authorManolo Carrasco <manolo@apache.org>
Fri, 4 Mar 2011 12:53:33 +0000 (12:53 +0000)
committerManolo Carrasco <manolo@apache.org>
Fri, 4 Mar 2011 12:53:33 +0000 (12:53 +0000)
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/Attachable.java [new file with mode: 0644]
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/DateBoxWidgetFactory.java [new file with mode: 0644]
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/RichTextWidgetFactory.java [new file with mode: 0644]
gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java
samples/src/main/java/gwtquery/samples/client/GwtQueryWidgetModule.java
samples/src/main/java/gwtquery/samples/public/GwtQueryWidgets.html

index 13aaefb08541033807ea4f517e23b1a9e4f5cfd8..ea0fee16de91c524dfb29013e3df7fdaac454e61 100755 (executable)
  */\r
 package com.google.gwt.query.client.plugins;\r
 \r
+import java.util.ArrayList;\r
+import java.util.List;\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.DateBoxWidgetFactory;\r
+import com.google.gwt.query.client.plugins.widgets.RichTextWidgetFactory;\r
 import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory;\r
+import com.google.gwt.query.client.plugins.widgets.TabPanelWidgetFactory.TabPanelOptions;\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.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
+import com.google.gwt.user.datepicker.client.DateBox;\r
 \r
 /**\r
  * Widgets plugin for Gwt Query. Be careful, this plugin is still experimental.\r
@@ -110,6 +113,20 @@ public class Widgets extends QueuePlugin<Widgets> {
     return widgets(new ButtonWidgetFactory(), initFunctions);\r
   }\r
 \r
+  /**\r
+   * Create a {@link DateBox} 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
+  public Widgets datebox(Function... initFunctions) {\r
+    return widgets(new DateBoxWidgetFactory(), initFunctions);\r
+  }\r
+  \r
+  public Widgets richtext(Function... initFunctions) {\r
+    return widgets(new RichTextWidgetFactory(), initFunctions);\r
+  }\r
+\r
+\r
   /**\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
@@ -150,13 +167,13 @@ public class Widgets extends QueuePlugin<Widgets> {
    */\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, initFunctions).getElement());\r
+      W w = widget(e, factory, initFunctions);\r
+      if (w != null) {\r
+        result.add(w.getElement());\r
+      }\r
     }\r
-\r
     return $(result).as(Widgets);\r
   }\r
 \r
@@ -178,4 +195,5 @@ public class Widgets extends QueuePlugin<Widgets> {
 \r
   }\r
 \r
+\r
 }\r
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/Attachable.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/Attachable.java
new file mode 100644 (file)
index 0000000..86039ce
--- /dev/null
@@ -0,0 +1,5 @@
+package com.google.gwt.query.client.plugins.widgets;
+
+public interface Attachable {
+  void attach();
+}
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/DateBoxWidgetFactory.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/DateBoxWidgetFactory.java
new file mode 100644 (file)
index 0000000..91af68e
--- /dev/null
@@ -0,0 +1,66 @@
+package com.google.gwt.query.client.plugins.widgets;
+
+import static com.google.gwt.query.client.GQuery.$;
+
+import java.util.Date;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.i18n.client.DateTimeFormat;
+import com.google.gwt.query.client.GQuery;
+import com.google.gwt.user.client.ui.RootPanel;
+import com.google.gwt.user.datepicker.client.DateBox;
+import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;
+
+/**
+ * Factory used to create a {@link DateBox} widget. A {@link DateBox} is created
+ * if the element is a <i>input</i> with type text, a <i>div</i> or a<i>span</i> element.
+ * 
+ * The content of the element has to be empty, a valid date or a valid date-format string.
+ */
+public class DateBoxWidgetFactory implements WidgetFactory<DateBox> {
+  
+  // DateBox needs to call the onAttach method
+  public static class AttachableDateBox extends DateBox implements Attachable {
+    public void attach(){
+      onAttach();
+      RootPanel.detachOnWindowClose(this);
+    }
+  }
+  
+  public DateBox create(Element e) {
+    String v = null;
+    if ($(e).filter("input[type='text']").size() == 1) {
+      v = GQuery.$(e).val();
+    } else if (WidgetsUtils.matchesTags(e, "div", "span")) {
+      v = GQuery.$(e).text();
+    }
+    if (v!=null) {
+      DateBox w = create(v);
+      WidgetsUtils.replace(e, w);
+      return w;
+    }
+    return null;
+  }
+  
+  @SuppressWarnings("deprecation")
+  private DateBox create(String v) {
+    Date d = new Date();
+    DateTimeFormat f = null;
+    if (v != null) {
+      try {
+        d = new Date(v);
+      } catch (Exception e) {
+        try {
+          f = DateTimeFormat.getFormat(v);
+        } catch (Exception e2) {
+        }
+      }
+    }
+    DateBox b = new AttachableDateBox();
+    b.setValue(d);
+    if (f != null) {
+      b.setFormat(new DefaultFormat(f));
+    }
+    return b;
+  }
+}
\ No newline at end of file
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/RichTextWidgetFactory.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/RichTextWidgetFactory.java
new file mode 100644 (file)
index 0000000..44ba49b
--- /dev/null
@@ -0,0 +1,47 @@
+package com.google.gwt.query.client.plugins.widgets;
+
+import static com.google.gwt.query.client.GQuery.$;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.RichTextArea;
+
+/**
+ * Factory used to create a {@link RichTextArea} widget. 
+ * A {@link Button} is created if the element is a <i>textarea</i>, <i>div></i> or <i>span</i>.
+ * The content of the element will be copied to the rich text area.
+ */
+// TODO: inject page styles to the iframe. Provide a Toolbar.
+public class RichTextWidgetFactory implements WidgetFactory<RichTextArea> {
+  
+  
+  public static class AttachableRichTextArea extends RichTextArea implements Attachable {
+    public void attach(){
+      onAttach();
+      // This fails with richtextarea, 
+      // RootPanel.detachOnWindowClose(this);
+    }
+  }
+
+  public RichTextArea create(Element e) {
+    String v = null;
+    if ("textarea".equalsIgnoreCase(e.getTagName())) {
+      v = $(e).val();
+    } else if (WidgetsUtils.matchesTags(e, "div", "span")) {
+      v = $(e).html();
+    }
+    if (v != null) {
+      RichTextArea w = create(v);
+      WidgetsUtils.replace(e, w);
+      return w;
+    }
+    return null;
+  }
+
+  private RichTextArea create(String v) {
+    RichTextArea b = new AttachableRichTextArea();
+    b.setHTML(v);
+    return b;
+  }
+
+}
\ No newline at end of file
index 360afcff1fa11c5bf3f848361b2407372f7c7a38..eb7cec7ddb5aea9e8d2e5f0f94771fba3167c06b 100644 (file)
@@ -2,6 +2,7 @@ package com.google.gwt.query.client.plugins.widgets;
 
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.query.client.GQuery;
+import com.google.gwt.user.client.ui.Widget;
 
 public class WidgetsUtils {
 
@@ -37,8 +38,22 @@ public class WidgetsUtils {
    */
    static void replace(Element oldElement, Element newElement) {
     assert oldElement != null && newElement != null;
-    
     GQuery.$(oldElement).replaceWith(newElement);
 
-  }
+    String c = oldElement.getClassName();
+    if (!c.isEmpty()) {
+      newElement.addClassName(c);
+    }
+   }
+   
+   static void replace(Element e, Widget widget)  {
+     assert e != null && widget != null;
+     replace(e, widget.getElement());
+
+     if (widget instanceof Attachable) {
+       ((Attachable)widget).attach();
+     }
+   }
+
+   
 }
index cff828ddbd5b4e1baa9bac3388671b544b8f0323..20d59ccb2aac23b517032555c57e74a5d69fa062 100644 (file)
@@ -25,7 +25,9 @@ import com.google.gwt.query.client.Function;
 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.RichTextArea;\r
 import com.google.gwt.user.client.ui.Widget;\r
+import com.google.gwt.user.datepicker.client.DateBox;\r
 \r
 public class GwtQueryWidgetModule implements EntryPoint {\r
 \r
@@ -55,9 +57,13 @@ public class GwtQueryWidgetModule implements EntryPoint {
     });\r
 \r
     $("#tabs").as(Widgets).tabPanel();\r
+\r
+    DateBox b = $(".date").as(Widgets).datebox().widget();\r
+    System.out.println(b);\r
     \r
+    RichTextArea a = $(".editable").as(Widgets).richtext().widget(0);\r
+    System.out.println(a);\r
     \r
-\r
   }\r
 \r
 }\r
index 8e95b165a9198a461e4ab0a78d80b92a7eaffe36..1b6c485c87a23eb54cfa4a14715dda09b8231ad4 100644 (file)
@@ -13,6 +13,7 @@ a button 3!</span>
 <button class="btn">Make me a button 4!</button>\r
 <div class="btn">Make me a button 5!</div>\r
 <div class="btn">Make me a button 6!</div>\r
+<input type="text" class="date" value="dd/MM/yyyy"/>\r
 </div>\r
 \r
 <div class="inputText">I will be an Input</div>\r
@@ -47,7 +48,7 @@ egestas, mi sapien ullamcorper diam, sit amet molestie mauris odio at
 lacus.</div>\r
 \r
 <h3>Third tab</h3>\r
-<div>Vestibulum turpis eros, tempor et hendrerit sit amet,\r
+<div class='editable'>Vestibulum turpis eros, tempor et hendrerit sit amet,\r
 hendrerit ut urna. Integer aliquam, neque non sollicitudin pulvinar,\r
 orci neque faucibus mauris, a molestie massa ligula vitae eros. Proin\r
 egestas, purus in pharetra aliquam, dolor odio faucibus lorem, ac\r