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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  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.vaadin.v7.client.ui.textfield;
  17. import com.google.gwt.core.client.Scheduler;
  18. import com.google.gwt.user.client.Command;
  19. import com.vaadin.client.ApplicationConnection;
  20. import com.vaadin.client.Paintable;
  21. import com.vaadin.client.UIDL;
  22. import com.vaadin.client.Util;
  23. import com.vaadin.shared.ui.Connect;
  24. import com.vaadin.shared.ui.Connect.LoadStyle;
  25. import com.vaadin.v7.client.ui.AbstractFieldConnector;
  26. import com.vaadin.v7.client.ui.VTextField;
  27. import com.vaadin.v7.shared.ui.textfield.AbstractTextFieldState;
  28. import com.vaadin.v7.shared.ui.textfield.TextFieldConstants;
  29. import com.vaadin.v7.ui.TextField;
  30. @Deprecated
  31. @Connect(value = TextField.class, loadStyle = LoadStyle.EAGER)
  32. public class TextFieldConnector extends AbstractFieldConnector
  33. implements Paintable {
  34. @Override
  35. public AbstractTextFieldState getState() {
  36. return (AbstractTextFieldState) super.getState();
  37. }
  38. @Override
  39. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  40. VTextField textField = getWidget();
  41. // Save details
  42. textField.client = client;
  43. textField.paintableId = uidl.getId();
  44. if (!isRealUpdate(uidl)) {
  45. return;
  46. }
  47. textField.setReadOnly(isReadOnly());
  48. textField.setInputPrompt(getState().inputPrompt);
  49. textField.setMaxLength(getState().maxLength);
  50. textField.setImmediate(getState().immediate);
  51. textField.listenTextChangeEvents = hasEventListener("ie");
  52. if (textField.listenTextChangeEvents) {
  53. textField.textChangeEventMode = uidl.getStringAttribute(
  54. TextFieldConstants.ATTR_TEXTCHANGE_EVENTMODE);
  55. if (textField.textChangeEventMode
  56. .equals(TextFieldConstants.TEXTCHANGE_MODE_EAGER)) {
  57. textField.textChangeEventTimeout = 1;
  58. } else {
  59. textField.textChangeEventTimeout = uidl.getIntAttribute(
  60. TextFieldConstants.ATTR_TEXTCHANGE_TIMEOUT);
  61. if (textField.textChangeEventTimeout < 1) {
  62. // Sanitize and allow lazy/timeout with timeout set to 0 to
  63. // work as eager
  64. textField.textChangeEventTimeout = 1;
  65. }
  66. }
  67. textField.sinkEvents(VTextField.TEXTCHANGE_EVENTS);
  68. textField.attachCutEventListener(textField.getElement());
  69. }
  70. textField.setColumns(getState().columns);
  71. String text = getState().text;
  72. if (text == null) {
  73. text = "";
  74. }
  75. /*
  76. * We skip the text content update if field has been repainted, but text
  77. * has not been changed (#6588). Additional sanity check verifies there
  78. * is no change in the queue (in which case we count more on the server
  79. * side value). <input> is updated only when it looses focus, so we
  80. * force updating if not focused. Lost focus issue appeared in (#15144)
  81. */
  82. if (Util.getFocusedElement() != textField.getElement()
  83. || !uidl.getBooleanAttribute(
  84. TextFieldConstants.ATTR_NO_VALUE_CHANGE_BETWEEN_PAINTS)
  85. || textField.valueBeforeEdit == null
  86. || !text.equals(textField.valueBeforeEdit)) {
  87. textField.updateFieldContent(text);
  88. }
  89. if (uidl.hasAttribute("selpos")) {
  90. final int pos = uidl.getIntAttribute("selpos");
  91. final int length = uidl.getIntAttribute("sellen");
  92. /*
  93. * Gecko defers setting the text so we need to defer the selection.
  94. */
  95. Scheduler.get().scheduleDeferred(new Command() {
  96. @Override
  97. public void execute() {
  98. textField.setSelectionRange(pos, length);
  99. }
  100. });
  101. }
  102. }
  103. @Override
  104. public VTextField getWidget() {
  105. return (VTextField) super.getWidget();
  106. }
  107. @Override
  108. public void flush() {
  109. getWidget().valueChange(false);
  110. }
  111. }