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.

AbstractTextFieldConnector.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.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. start = restrictTo(start, 0, textLength - 1);
  41. length = restrictTo(length, 0, textLength - start);
  42. getAbstractTextField().setSelectionRange(start, length);
  43. }
  44. private int restrictTo(int value, int min, int max) {
  45. if (value < min) {
  46. value = min;
  47. }
  48. if (value > max) {
  49. value = max;
  50. }
  51. return value;
  52. }
  53. @Override
  54. public void selectAll() {
  55. getAbstractTextField().selectAll();
  56. }
  57. }
  58. private int lastSentCursorPosition = -1;
  59. private ValueChangeHandler valueChangeHandler;
  60. @Override
  61. protected void init() {
  62. registerRpc(AbstractTextFieldClientRpc.class,
  63. new AbstractTextFieldClientRpcImpl());
  64. ConnectorFocusAndBlurHandler.addHandlers(this);
  65. valueChangeHandler = new ValueChangeHandler(this);
  66. // Ensures that the cursor position is sent when leaving the field
  67. // (if it has changed)
  68. if (getWidget() instanceof HasBlurHandlers) {
  69. ((HasBlurHandlers) getWidget())
  70. .addBlurHandler(event -> sendValueChange());
  71. }
  72. }
  73. protected ValueChangeHandler getValueChangeHandler() {
  74. return valueChangeHandler;
  75. }
  76. /**
  77. * Helper to cast {@link #getWidget()} to {@link AbstractTextField}. The
  78. * method exists only because getWidget() must return a {@link Widget} and
  79. * not an interface.
  80. *
  81. * @return the widget as an AbstractTextFieldWidget
  82. */
  83. private AbstractTextFieldWidget getAbstractTextField() {
  84. return (AbstractTextFieldWidget) getWidget();
  85. }
  86. @Override
  87. public AbstractTextFieldState getState() {
  88. return (AbstractTextFieldState) super.getState();
  89. }
  90. @OnStateChange("valueChangeMode")
  91. private void updateValueChangeMode() {
  92. valueChangeHandler.setValueChangeMode(getState().valueChangeMode);
  93. }
  94. @OnStateChange("valueChangeTimeout")
  95. private void updateValueChangeTimeout() {
  96. valueChangeHandler.setValueChangeTimeout(getState().valueChangeTimeout);
  97. }
  98. @OnStateChange("readOnly")
  99. private void updateReadOnly() {
  100. getAbstractTextField().setReadOnly(getState().readOnly);
  101. }
  102. private boolean hasStateChanged() {
  103. boolean textChanged = !getAbstractTextField().getValue()
  104. .equals(getState().text);
  105. boolean cursorPosChanged = getAbstractTextField()
  106. .getCursorPos() != lastSentCursorPosition;
  107. return textChanged || cursorPosChanged;
  108. }
  109. /**
  110. * Sends the updated value and cursor position to the server, if either one
  111. * has changed.
  112. */
  113. @Override
  114. public void sendValueChange() {
  115. if (!hasStateChanged()) {
  116. return;
  117. }
  118. lastSentCursorPosition = getAbstractTextField().getCursorPos();
  119. getRpcProxy(AbstractTextFieldServerRpc.class).setText(
  120. getAbstractTextField().getValue(), lastSentCursorPosition);
  121. getState().text = getAbstractTextField().getValue();
  122. }
  123. @Override
  124. public void flush() {
  125. super.flush();
  126. sendValueChange();
  127. }
  128. /**
  129. * {@inheritDoc}
  130. *
  131. * @since 8.0
  132. */
  133. @Override
  134. public boolean isWorkPending() {
  135. return getValueChangeHandler().isScheduled();
  136. }
  137. }