Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

AbstractTextFieldConnector.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2000-2021 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.ui.textfield;
  17. import com.google.gwt.event.dom.client.HasBlurHandlers;
  18. import com.google.gwt.user.client.ui.Widget;
  19. import com.vaadin.client.DeferredWorker;
  20. import com.vaadin.client.annotations.OnStateChange;
  21. import com.vaadin.client.ui.AbstractFieldConnector;
  22. import com.vaadin.client.ui.AbstractTextFieldWidget;
  23. import com.vaadin.client.ui.ConnectorFocusAndBlurHandler;
  24. import com.vaadin.shared.ui.textfield.AbstractTextFieldClientRpc;
  25. import com.vaadin.shared.ui.textfield.AbstractTextFieldServerRpc;
  26. import com.vaadin.shared.ui.textfield.AbstractTextFieldState;
  27. import com.vaadin.ui.AbstractTextField;
  28. /**
  29. * Connector class for AbstractTextField.
  30. *
  31. * @since 8.0
  32. */
  33. public abstract class AbstractTextFieldConnector extends AbstractFieldConnector
  34. implements ValueChangeHandler.Owner, DeferredWorker {
  35. private class AbstractTextFieldClientRpcImpl
  36. implements AbstractTextFieldClientRpc {
  37. @Override
  38. public void selectRange(int start, int length) {
  39. int textLength = getAbstractTextField().getValue().length();
  40. // Enable setting cursor position after the last character
  41. start = restrictTo(start, 0,
  42. length == 0 ? textLength : (textLength - 1));
  43. length = restrictTo(length, 0, textLength - start);
  44. getAbstractTextField().setSelectionRange(start, length);
  45. }
  46. private int restrictTo(int value, int min, int max) {
  47. if (value < min) {
  48. value = min;
  49. }
  50. if (value > max) {
  51. value = max;
  52. }
  53. return value;
  54. }
  55. @Override
  56. public void selectAll() {
  57. getAbstractTextField().selectAll();
  58. }
  59. }
  60. private int lastSentCursorPosition = -1;
  61. private ValueChangeHandler valueChangeHandler;
  62. @Override
  63. protected void init() {
  64. registerRpc(AbstractTextFieldClientRpc.class,
  65. new AbstractTextFieldClientRpcImpl());
  66. ConnectorFocusAndBlurHandler.addHandlers(this);
  67. valueChangeHandler = new ValueChangeHandler(this);
  68. // Ensures that the cursor position is sent when leaving the field
  69. // (if it has changed)
  70. if (getWidget() instanceof HasBlurHandlers) {
  71. ((HasBlurHandlers) getWidget())
  72. .addBlurHandler(event -> sendValueChange());
  73. }
  74. }
  75. /**
  76. * Returns the internal value change handler.
  77. *
  78. * @return the value change handler
  79. */
  80. protected ValueChangeHandler getValueChangeHandler() {
  81. return valueChangeHandler;
  82. }
  83. /**
  84. * Helper to cast {@link #getWidget()} to {@link AbstractTextField}. The
  85. * method exists only because getWidget() must return a {@link Widget} and
  86. * not an interface.
  87. *
  88. * @return the widget as an AbstractTextFieldWidget
  89. */
  90. private AbstractTextFieldWidget getAbstractTextField() {
  91. return (AbstractTextFieldWidget) getWidget();
  92. }
  93. @Override
  94. public AbstractTextFieldState getState() {
  95. return (AbstractTextFieldState) super.getState();
  96. }
  97. @OnStateChange("valueChangeMode")
  98. private void updateValueChangeMode() {
  99. valueChangeHandler.setValueChangeMode(getState().valueChangeMode);
  100. }
  101. @OnStateChange("valueChangeTimeout")
  102. private void updateValueChangeTimeout() {
  103. valueChangeHandler.setValueChangeTimeout(getState().valueChangeTimeout);
  104. }
  105. @OnStateChange("readOnly")
  106. private void updateReadOnly() {
  107. getAbstractTextField().setReadOnly(getState().readOnly);
  108. }
  109. private boolean hasStateChanged() {
  110. boolean textChanged = !getAbstractTextField().getValue()
  111. .equals(getState().text);
  112. boolean cursorPosChanged = getAbstractTextField()
  113. .getCursorPos() != lastSentCursorPosition;
  114. return textChanged || cursorPosChanged;
  115. }
  116. /**
  117. * Sends the updated value and cursor position to the server, if either one
  118. * has changed.
  119. */
  120. @Override
  121. public void sendValueChange() {
  122. if (!hasStateChanged()) {
  123. return;
  124. }
  125. lastSentCursorPosition = getAbstractTextField().getCursorPos();
  126. getRpcProxy(AbstractTextFieldServerRpc.class).setText(
  127. getAbstractTextField().getValue(), lastSentCursorPosition);
  128. getState().text = getAbstractTextField().getValue();
  129. }
  130. @Override
  131. public void flush() {
  132. super.flush();
  133. sendValueChange();
  134. }
  135. /**
  136. * {@inheritDoc}
  137. *
  138. * @since 8.0
  139. */
  140. @Override
  141. public boolean isWorkPending() {
  142. return getValueChangeHandler().isScheduled();
  143. }
  144. }