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.

ReadOnlyHasValue.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2000-2018 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.data;
  17. import com.vaadin.server.SerializableConsumer;
  18. import com.vaadin.shared.Registration;
  19. import com.vaadin.ui.Label;
  20. import java.io.Serializable;
  21. import java.util.LinkedHashSet;
  22. import java.util.Objects;
  23. /**
  24. * Generic {@link HasValue} to use any type of component with Vaadin data
  25. * binding.
  26. * <p>
  27. * Example:
  28. *
  29. * <pre>
  30. * Label label = new Label();
  31. * ReadOnlyHasValue&lt;String&gt; hasValue = new ReadOnlyHasValue&lt;&gt;(
  32. * label::setCaption);
  33. * binder.forField(hasValue).bind(SomeBean::getName);
  34. * </pre>
  35. *
  36. * @param <V>
  37. * the value type
  38. * @since 8.4
  39. */
  40. public class ReadOnlyHasValue<V> implements HasValue<V>, Serializable {
  41. private V value;
  42. private final SerializableConsumer<V> valueProcessor;
  43. private final V emptyValue;
  44. private LinkedHashSet<ValueChangeListener<V>> listenerList;
  45. /**
  46. * Creates new {@code ReadOnlyHasValue}.
  47. *
  48. * @param valueProcessor
  49. * the value valueProcessor, e.g. {@link Label#setValue}
  50. * @param emptyValue
  51. * the value to be used as empty, {@code null} by default
  52. */
  53. public ReadOnlyHasValue(SerializableConsumer<V> valueProcessor,
  54. V emptyValue) {
  55. this.valueProcessor = valueProcessor;
  56. this.emptyValue = emptyValue;
  57. }
  58. /**
  59. * Creates new {@code ReadOnlyHasValue} with {@code null} as an empty value.
  60. *
  61. * @param valueProcessor
  62. * the value valueProcessor, e.g. {@link Label#setValue}
  63. */
  64. public ReadOnlyHasValue(SerializableConsumer<V> valueProcessor) {
  65. this(valueProcessor, null);
  66. }
  67. @Override
  68. public void setValue(V value) {
  69. V oldValue = this.value;
  70. this.value = value;
  71. valueProcessor.accept(value);
  72. if (listenerList != null && !Objects.equals(oldValue, value)) {
  73. for (ValueChangeListener<V> valueChangeListener : listenerList) {
  74. valueChangeListener.valueChange(
  75. new ValueChangeEvent<>(null, this, oldValue, false));
  76. }
  77. }
  78. }
  79. @Override
  80. public V getValue() {
  81. return value;
  82. }
  83. @Override
  84. public Registration addValueChangeListener(
  85. ValueChangeListener<V> listener) {
  86. Objects.requireNonNull(listener, "Listener must not be null.");
  87. if (listenerList == null) {
  88. listenerList = new LinkedHashSet<>();
  89. }
  90. listenerList.add(listener);
  91. return () -> listenerList.remove(listener);
  92. }
  93. @Override
  94. public boolean isRequiredIndicatorVisible() {
  95. return false;
  96. }
  97. @Override
  98. public void setRequiredIndicatorVisible(boolean requiredIndicatorVisible) {
  99. if (requiredIndicatorVisible)
  100. throw new IllegalArgumentException("Not Writable");
  101. }
  102. @Override
  103. public void setReadOnly(boolean readOnly) {
  104. if (!readOnly)
  105. throw new IllegalArgumentException("Not Writable");
  106. }
  107. @Override
  108. public boolean isReadOnly() {
  109. return true;
  110. }
  111. @Override
  112. public V getEmptyValue() {
  113. return emptyValue;
  114. }
  115. }