You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextFieldConnector.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.google.gwt.core.client.GWT;
  6. import com.google.gwt.core.client.Scheduler;
  7. import com.google.gwt.user.client.Command;
  8. import com.google.gwt.user.client.Event;
  9. import com.google.gwt.user.client.ui.Widget;
  10. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  11. import com.vaadin.terminal.gwt.client.Paintable;
  12. import com.vaadin.terminal.gwt.client.UIDL;
  13. import com.vaadin.terminal.gwt.client.ui.Component.LoadStyle;
  14. import com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.BeforeShortcutActionListener;
  15. import com.vaadin.ui.TextField;
  16. @Component(value = TextField.class, loadStyle = LoadStyle.EAGER)
  17. public class TextFieldConnector extends AbstractFieldConnector implements
  18. Paintable, BeforeShortcutActionListener {
  19. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  20. // Save details
  21. getWidget().client = client;
  22. getWidget().paintableId = uidl.getId();
  23. if (!isRealUpdate(uidl)) {
  24. return;
  25. }
  26. getWidget().setReadOnly(isReadOnly());
  27. getWidget().inputPrompt = uidl
  28. .getStringAttribute(VTextField.ATTR_INPUTPROMPT);
  29. getWidget().setMaxLength(
  30. uidl.hasAttribute("maxLength") ? uidl
  31. .getIntAttribute("maxLength") : -1);
  32. getWidget().immediate = getState().isImmediate();
  33. getWidget().listenTextChangeEvents = hasEventListener("ie");
  34. if (getWidget().listenTextChangeEvents) {
  35. getWidget().textChangeEventMode = uidl
  36. .getStringAttribute(VTextField.ATTR_TEXTCHANGE_EVENTMODE);
  37. if (getWidget().textChangeEventMode
  38. .equals(VTextField.TEXTCHANGE_MODE_EAGER)) {
  39. getWidget().textChangeEventTimeout = 1;
  40. } else {
  41. getWidget().textChangeEventTimeout = uidl
  42. .getIntAttribute(VTextField.ATTR_TEXTCHANGE_TIMEOUT);
  43. if (getWidget().textChangeEventTimeout < 1) {
  44. // Sanitize and allow lazy/timeout with timeout set to 0 to
  45. // work as eager
  46. getWidget().textChangeEventTimeout = 1;
  47. }
  48. }
  49. getWidget().sinkEvents(VTextField.TEXTCHANGE_EVENTS);
  50. getWidget().attachCutEventListener(getWidget().getElement());
  51. }
  52. if (uidl.hasAttribute("cols")) {
  53. getWidget().setColumns(
  54. new Integer(uidl.getStringAttribute("cols")).intValue());
  55. }
  56. final String text = uidl.getStringVariable("text");
  57. /*
  58. * We skip the text content update if field has been repainted, but text
  59. * has not been changed. Additional sanity check verifies there is no
  60. * change in the que (in which case we count more on the server side
  61. * value).
  62. */
  63. if (!(uidl
  64. .getBooleanAttribute(VTextField.ATTR_NO_VALUE_CHANGE_BETWEEN_PAINTS)
  65. && getWidget().valueBeforeEdit != null && text
  66. .equals(getWidget().valueBeforeEdit))) {
  67. getWidget().updateFieldContent(text);
  68. }
  69. if (uidl.hasAttribute("selpos")) {
  70. final int pos = uidl.getIntAttribute("selpos");
  71. final int length = uidl.getIntAttribute("sellen");
  72. /*
  73. * Gecko defers setting the text so we need to defer the selection.
  74. */
  75. Scheduler.get().scheduleDeferred(new Command() {
  76. public void execute() {
  77. getWidget().setSelectionRange(pos, length);
  78. }
  79. });
  80. }
  81. // Here for backward compatibility; to be moved to TextArea.
  82. // Optimization: server does not send attribute for the default 'true'
  83. // state.
  84. if (uidl.hasAttribute("wordwrap")
  85. && uidl.getBooleanAttribute("wordwrap") == false) {
  86. getWidget().setWordwrap(false);
  87. } else {
  88. getWidget().setWordwrap(true);
  89. }
  90. }
  91. @Override
  92. protected Widget createWidget() {
  93. return GWT.create(VTextField.class);
  94. }
  95. @Override
  96. public VTextField getWidget() {
  97. return (VTextField) super.getWidget();
  98. }
  99. public void onBeforeShortcutAction(Event e) {
  100. getWidget().valueChange(false);
  101. }
  102. }