Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Widgets.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright 2011, The gwtquery team.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.gwt.query.client.plugins;
  17. import com.google.gwt.dom.client.Element;
  18. import com.google.gwt.query.client.GQuery;
  19. import com.google.gwt.query.client.plugins.widgets.ButtonWidgetFactory;
  20. import com.google.gwt.query.client.plugins.widgets.HtmlPanelWidgetFactory;
  21. import com.google.gwt.query.client.plugins.widgets.LabelWidgetFactory;
  22. import com.google.gwt.query.client.plugins.widgets.PasswordTextBoxWidgetFactory;
  23. import com.google.gwt.query.client.plugins.widgets.TextAreaWidgetFactory;
  24. import com.google.gwt.query.client.plugins.widgets.TextBoxWidgetFactory;
  25. import com.google.gwt.query.client.plugins.widgets.WidgetFactory;
  26. import com.google.gwt.query.client.plugins.widgets.WidgetInitializer;
  27. import com.google.gwt.query.client.plugins.widgets.WidgetsUtils;
  28. import com.google.gwt.user.client.ui.Button;
  29. import com.google.gwt.user.client.ui.Label;
  30. import com.google.gwt.user.client.ui.PasswordTextBox;
  31. import com.google.gwt.user.client.ui.TextArea;
  32. import com.google.gwt.user.client.ui.TextBox;
  33. import com.google.gwt.user.client.ui.Widget;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. /**
  37. * Widgets plugin for Gwt Query. Be careful, this plugin is still experimental.
  38. * The api can change in next releases.
  39. */
  40. public class Widgets extends QueuePlugin<Widgets> {
  41. public static final Class<Widgets> Widgets = Widgets.class;
  42. // list of html tags that cannot be replaced by a widget, in order to avoid to
  43. // break the html structure
  44. private static final String[] excludedTags = {
  45. "html", "body", "head", "tr", "thead", "tfoot", "options", "script",
  46. "noscript", "style", "title"};
  47. static {
  48. GQuery.registerPlugin(Widgets.class, new Plugin<Widgets>() {
  49. public Widgets init(GQuery gq) {
  50. return new Widgets(gq);
  51. }
  52. });
  53. }
  54. protected Widgets(GQuery gq) {
  55. super(gq);
  56. }
  57. /**
  58. * Try to create a widget using the given factory and the given options for
  59. * each element of the query. Returns a new gquery set of elements with the
  60. * new widgets created.
  61. */
  62. public <W extends Widget> Widgets widgets(WidgetFactory<W> factory,
  63. WidgetInitializer<W> initializers) {
  64. List<Element> result = new ArrayList<>();
  65. for (Element e : elements()) {
  66. W w = widget(e, factory, initializers);
  67. if (w != null) {
  68. result.add(w.getElement());
  69. }
  70. }
  71. return $(result).as(Widgets);
  72. }
  73. protected boolean isWidgetCreationAuthorizedFrom(Element e) {
  74. return !WidgetsUtils.matchesTags(e, excludedTags);
  75. }
  76. /**
  77. * Create and return a widget using the given factory and the given options.
  78. */
  79. protected <W extends Widget> W widget(Element e, WidgetFactory<W> factory,
  80. WidgetInitializer<W> initializer) {
  81. if (!isWidgetCreationAuthorizedFrom(e)) {
  82. return null;
  83. }
  84. W widget = factory.create(e);
  85. if (initializer != null) {
  86. initializer.initialize(widget, e);
  87. }
  88. return widget;
  89. }
  90. /**
  91. * Create and return a widget using the given factory and the given options.
  92. */
  93. protected <W extends Widget> W widget(WidgetFactory<W> factory,
  94. WidgetInitializer<W> initializers) {
  95. return widget(get(0), factory, initializers);
  96. }
  97. /**
  98. * Create a {@link Button} widget for each selected element.
  99. */
  100. public Widgets button() {
  101. return widgets(new ButtonWidgetFactory(), null);
  102. }
  103. /**
  104. * Create a {@link Button} widget for each selected element. The
  105. * <code>initializers</code> will be called on each new {@link Button} created
  106. * by passing them in parameter.
  107. *
  108. */
  109. public Widgets button(WidgetInitializer<Button> initializers) {
  110. return widgets(new ButtonWidgetFactory(), initializers);
  111. }
  112. /**
  113. * Create a {@link Panel} widget for each selected element.
  114. */
  115. public Widgets panel() {
  116. return widgets(new HtmlPanelWidgetFactory(), null);
  117. }
  118. /**
  119. * Create a {@link Label} widget for each selected element.
  120. */
  121. public Widgets label() {
  122. return widgets(new LabelWidgetFactory(), null);
  123. }
  124. /**
  125. * Create a {@link Label} widget for each selected element. The
  126. * <code>initializers</code> will be called on each new {@link Label} created
  127. * by passing them in parameter.
  128. */
  129. public Widgets label(WidgetInitializer<Label> initializers) {
  130. return widgets(new LabelWidgetFactory(), initializers);
  131. }
  132. /**
  133. * Create a {@link PasswordTextBox} widget for each selected element.
  134. */
  135. public Widgets passwordBox() {
  136. return widgets(new PasswordTextBoxWidgetFactory(), null);
  137. }
  138. /**
  139. * Create a {@link PasswordTextBox} widget for each selected element. The
  140. * <code>initializers</code> will be called on each new
  141. * {@link PasswordTextBox} created by passing them in parameter.
  142. *
  143. */
  144. public Widgets passwordBox(WidgetInitializer<PasswordTextBox> initializers) {
  145. return widgets(new PasswordTextBoxWidgetFactory(), initializers);
  146. }
  147. /**
  148. * Create a {@link TextBox} widget for each selected element. The
  149. * <code>initializers</code> will be called on each new {@link TextBox}
  150. * created by passing them in parameter.
  151. *
  152. */
  153. public Widgets textBox() {
  154. return widgets(new TextBoxWidgetFactory(), null);
  155. }
  156. /**
  157. * Create a {@link TextBox} widget for each selected element. The
  158. * <code>initializers</code> will be called on each new {@link TextBox}
  159. * created by passing them in parameter.
  160. *
  161. */
  162. public Widgets textBox(WidgetInitializer<TextBox> initializers) {
  163. return widgets(new TextBoxWidgetFactory(), initializers);
  164. }
  165. /**
  166. * Create a {@link TextArea} widget for each selected element. The
  167. * <code>initializers</code> will be called on each new {@link TextBox}
  168. * created by passing them in parameter.
  169. *
  170. */
  171. public Widgets textArea() {
  172. return widgets(new TextAreaWidgetFactory(), null);
  173. }
  174. /**
  175. * Create a {@link TextArea} widget for each selected element. The
  176. * <code>initializers</code> will be called on each new {@link TextBox}
  177. * created by passing them in parameter.
  178. *
  179. */
  180. public Widgets textArea(WidgetInitializer<TextArea> initializers) {
  181. return widgets(new TextAreaWidgetFactory(), initializers);
  182. }
  183. }