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.

LegacyTextFieldConnector.java 4.9KB

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