Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ValueChangeHandler.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.core.client.Scheduler;
  18. import com.google.gwt.user.client.Timer;
  19. import com.vaadin.client.ComponentConnector;
  20. import com.vaadin.shared.ui.ValueChangeMode;
  21. /**
  22. * Helper for dealing with scheduling value change events based on a given mode
  23. * and possibly timeout.
  24. */
  25. public class ValueChangeHandler {
  26. /**
  27. * Must be implemented by any user of a ValueChangeHandler.
  28. */
  29. public interface Owner extends ComponentConnector {
  30. /**
  31. * Sends the current value to the server, if it has changed.
  32. */
  33. void sendValueChange();
  34. }
  35. private Owner owner;
  36. private boolean scheduled;
  37. private Timer valueChangeTrigger = new Timer() {
  38. @Override
  39. public void run() {
  40. Scheduler.get().scheduleDeferred(() -> {
  41. owner.sendValueChange();
  42. scheduled = false;
  43. });
  44. }
  45. };
  46. private int valueChangeTimeout = -1;
  47. private ValueChangeMode valueChangeMode;
  48. /**
  49. * Creates a value change handler for the given owner.
  50. *
  51. * @param owner
  52. * the owner connector
  53. */
  54. public ValueChangeHandler(Owner owner) {
  55. this.owner = owner;
  56. }
  57. /**
  58. * Called whenever a change in the value has been detected. Schedules a
  59. * value change to be sent to the server, depending on the current value
  60. * change mode.
  61. * <p>
  62. * Note that this method does not consider the {@link ValueChangeMode#BLUR}
  63. * mode but assumes that {@link #sendValueChange()} is called directly for
  64. * this mode.
  65. */
  66. public void scheduleValueChange() {
  67. switch (valueChangeMode) {
  68. case LAZY:
  69. lazyTextChange();
  70. break;
  71. case TIMEOUT:
  72. timeoutTextChange();
  73. break;
  74. case EAGER:
  75. eagerTextChange();
  76. break;
  77. case BLUR:
  78. // Nothing to schedule for this mode
  79. break;
  80. default:
  81. throw new IllegalStateException("Unknown mode: " + valueChangeMode);
  82. }
  83. }
  84. private void lazyTextChange() {
  85. scheduled = true;
  86. valueChangeTrigger.schedule(valueChangeTimeout);
  87. }
  88. private void timeoutTextChange() {
  89. if (valueChangeTrigger.isRunning()) {
  90. return;
  91. }
  92. scheduled = true;
  93. valueChangeTrigger.schedule(valueChangeTimeout);
  94. }
  95. private void eagerTextChange() {
  96. scheduled = true;
  97. valueChangeTrigger.run();
  98. }
  99. /**
  100. * Sets the value change mode to use.
  101. *
  102. * @see ValueChangeMode
  103. *
  104. * @param valueChangeMode
  105. * the value change mode to use
  106. */
  107. public void setValueChangeMode(ValueChangeMode valueChangeMode) {
  108. this.valueChangeMode = valueChangeMode;
  109. }
  110. /**
  111. * Sets the value change timeout to use.
  112. *
  113. * @see ValueChangeMode
  114. *
  115. * @param valueChangeTimeout
  116. * the value change timeout
  117. */
  118. public void setValueChangeTimeout(int valueChangeTimeout) {
  119. this.valueChangeTimeout = valueChangeTimeout;
  120. }
  121. public boolean isScheduled() {
  122. return scheduled;
  123. }
  124. }