Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractField.java 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.tokka.ui.components.fields;
  17. import java.io.Serializable;
  18. import java.util.Objects;
  19. import java.util.function.Consumer;
  20. import com.vaadin.tokka.event.Registration;
  21. import com.vaadin.tokka.ui.components.HasValue;
  22. import com.vaadin.ui.AbstractComponent;
  23. public abstract class AbstractField<T> extends AbstractComponent
  24. implements HasValue<T> {
  25. @Override
  26. public void setValue(T value) {
  27. setValue(value, false);
  28. }
  29. public <L extends Consumer<T> & Serializable> Registration onValueChange(
  30. L listener) {
  31. return addValueChangeListener(e -> listener.accept(e.getValue()));
  32. }
  33. /**
  34. * Sets the value of this field.
  35. *
  36. * @param value
  37. * the new value to set
  38. * @param userOriginated
  39. * whether the value change originates from the user
  40. */
  41. protected <E extends ValueChange<T>> void setValue(T value,
  42. boolean userOriginated) {
  43. if (userOriginated && isReadOnly()) {
  44. return;
  45. }
  46. if (Objects.equals(value, getValue())) {
  47. return;
  48. }
  49. doSetValue(value);
  50. if (!userOriginated) {
  51. markAsDirty();
  52. }
  53. fireEvent(createValueChange(userOriginated));
  54. }
  55. /**
  56. * Stores the given field value to the shared state.
  57. *
  58. * @param value
  59. * the new value of the field
  60. */
  61. protected abstract void doSetValue(T value);
  62. /**
  63. * Returns a new value change event instance.
  64. *
  65. * @param userOriginated
  66. * whether the value change originates from the user
  67. * @return the new event
  68. */
  69. protected abstract ValueChange<T> createValueChange(boolean userOriginated);
  70. }