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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2000-2016 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. // Save details
  41. getWidget().client = client;
  42. getWidget().paintableId = uidl.getId();
  43. if (!isRealUpdate(uidl)) {
  44. return;
  45. }
  46. getWidget().setReadOnly(isReadOnly());
  47. getWidget().setInputPrompt(getState().inputPrompt);
  48. getWidget().setMaxLength(getState().maxLength);
  49. getWidget().setImmediate(getState().immediate);
  50. getWidget().listenTextChangeEvents = hasEventListener("ie");
  51. if (getWidget().listenTextChangeEvents) {
  52. getWidget().textChangeEventMode = uidl.getStringAttribute(
  53. TextFieldConstants.ATTR_TEXTCHANGE_EVENTMODE);
  54. if (getWidget().textChangeEventMode
  55. .equals(TextFieldConstants.TEXTCHANGE_MODE_EAGER)) {
  56. getWidget().textChangeEventTimeout = 1;
  57. } else {
  58. getWidget().textChangeEventTimeout = uidl.getIntAttribute(
  59. TextFieldConstants.ATTR_TEXTCHANGE_TIMEOUT);
  60. if (getWidget().textChangeEventTimeout < 1) {
  61. // Sanitize and allow lazy/timeout with timeout set to 0 to
  62. // work as eager
  63. getWidget().textChangeEventTimeout = 1;
  64. }
  65. }
  66. getWidget().sinkEvents(VTextField.TEXTCHANGE_EVENTS);
  67. getWidget().attachCutEventListener(getWidget().getElement());
  68. }
  69. getWidget().setColumns(getState().columns);
  70. String text = getState().text;
  71. if (text == null) {
  72. text = "";
  73. }
  74. /*
  75. * We skip the text content update if field has been repainted, but text
  76. * has not been changed (#6588). Additional sanity check verifies there
  77. * is no change in the queue (in which case we count more on the server
  78. * side value). <input> is updated only when it looses focus, so we
  79. * force updating if not focused. Lost focus issue appeared in (#15144)
  80. */
  81. if (!(Util.getFocusedElement() == getWidget().getElement())
  82. || !uidl.getBooleanAttribute(
  83. TextFieldConstants.ATTR_NO_VALUE_CHANGE_BETWEEN_PAINTS)
  84. || getWidget().valueBeforeEdit == null
  85. || !text.equals(getWidget().valueBeforeEdit)) {
  86. getWidget().updateFieldContent(text);
  87. }
  88. if (uidl.hasAttribute("selpos")) {
  89. final int pos = uidl.getIntAttribute("selpos");
  90. final int length = uidl.getIntAttribute("sellen");
  91. /*
  92. * Gecko defers setting the text so we need to defer the selection.
  93. */
  94. Scheduler.get().scheduleDeferred(new Command() {
  95. @Override
  96. public void execute() {
  97. getWidget().setSelectionRange(pos, length);
  98. }
  99. });
  100. }
  101. }
  102. @Override
  103. public VTextField getWidget() {
  104. return (VTextField) super.getWidget();
  105. }
  106. @Override
  107. public void flush() {
  108. getWidget().valueChange(false);
  109. }
  110. }