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.

AbstractProperty.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.util.Collection;
  6. import java.util.Collections;
  7. import java.util.LinkedList;
  8. import com.vaadin.data.Property;
  9. /**
  10. * Abstract base class for {@link Property} implementations.
  11. *
  12. * Handles listener management for {@link ValueChangeListener}s and
  13. * {@link ReadOnlyStatusChangeListener}s.
  14. *
  15. * @since 6.6
  16. */
  17. public abstract class AbstractProperty<T> implements Property<T>,
  18. Property.ValueChangeNotifier, Property.ReadOnlyStatusChangeNotifier {
  19. /**
  20. * List of listeners who are interested in the read-only status changes of
  21. * the Property
  22. */
  23. private LinkedList<ReadOnlyStatusChangeListener> readOnlyStatusChangeListeners = null;
  24. /**
  25. * List of listeners who are interested in the value changes of the Property
  26. */
  27. private LinkedList<ValueChangeListener> valueChangeListeners = null;
  28. /**
  29. * Is the Property read-only?
  30. */
  31. private boolean readOnly;
  32. /**
  33. * {@inheritDoc}
  34. *
  35. * Override for additional restrictions on what is considered a read-only
  36. * property.
  37. */
  38. @Override
  39. public boolean isReadOnly() {
  40. return readOnly;
  41. }
  42. @Override
  43. public void setReadOnly(boolean newStatus) {
  44. boolean oldStatus = isReadOnly();
  45. readOnly = newStatus;
  46. if (oldStatus != isReadOnly()) {
  47. fireReadOnlyStatusChange();
  48. }
  49. }
  50. /**
  51. * Returns the value of the <code>Property</code> in human readable textual
  52. * format.
  53. *
  54. * @return String representation of the value stored in the Property
  55. * @deprecated use {@link #getValue()} instead and possibly toString on that
  56. */
  57. @Deprecated
  58. @Override
  59. public String toString() {
  60. throw new UnsupportedOperationException(
  61. "Use Property.getValue() instead of " + getClass()
  62. + ".toString()");
  63. }
  64. /* Events */
  65. /**
  66. * An <code>Event</code> object specifying the Property whose read-only
  67. * status has been changed.
  68. */
  69. protected static class ReadOnlyStatusChangeEvent extends
  70. java.util.EventObject implements Property.ReadOnlyStatusChangeEvent {
  71. /**
  72. * Constructs a new read-only status change event for this object.
  73. *
  74. * @param source
  75. * source object of the event.
  76. */
  77. protected ReadOnlyStatusChangeEvent(Property source) {
  78. super(source);
  79. }
  80. /**
  81. * Gets the Property whose read-only state has changed.
  82. *
  83. * @return source Property of the event.
  84. */
  85. @Override
  86. public Property getProperty() {
  87. return (Property) getSource();
  88. }
  89. }
  90. /**
  91. * Registers a new read-only status change listener for this Property.
  92. *
  93. * @param listener
  94. * the new Listener to be registered.
  95. */
  96. @Override
  97. public void addListener(Property.ReadOnlyStatusChangeListener listener) {
  98. if (readOnlyStatusChangeListeners == null) {
  99. readOnlyStatusChangeListeners = new LinkedList<ReadOnlyStatusChangeListener>();
  100. }
  101. readOnlyStatusChangeListeners.add(listener);
  102. }
  103. /**
  104. * Removes a previously registered read-only status change listener.
  105. *
  106. * @param listener
  107. * the listener to be removed.
  108. */
  109. @Override
  110. public void removeListener(Property.ReadOnlyStatusChangeListener listener) {
  111. if (readOnlyStatusChangeListeners != null) {
  112. readOnlyStatusChangeListeners.remove(listener);
  113. }
  114. }
  115. /**
  116. * Sends a read only status change event to all registered listeners.
  117. */
  118. protected void fireReadOnlyStatusChange() {
  119. if (readOnlyStatusChangeListeners != null) {
  120. final Object[] l = readOnlyStatusChangeListeners.toArray();
  121. final Property.ReadOnlyStatusChangeEvent event = new ReadOnlyStatusChangeEvent(
  122. this);
  123. for (int i = 0; i < l.length; i++) {
  124. ((Property.ReadOnlyStatusChangeListener) l[i])
  125. .readOnlyStatusChange(event);
  126. }
  127. }
  128. }
  129. /**
  130. * An <code>Event</code> object specifying the Property whose value has been
  131. * changed.
  132. */
  133. private static class ValueChangeEvent extends java.util.EventObject
  134. implements Property.ValueChangeEvent {
  135. /**
  136. * Constructs a new value change event for this object.
  137. *
  138. * @param source
  139. * source object of the event.
  140. */
  141. protected ValueChangeEvent(Property source) {
  142. super(source);
  143. }
  144. /**
  145. * Gets the Property whose value has changed.
  146. *
  147. * @return source Property of the event.
  148. */
  149. @Override
  150. public Property getProperty() {
  151. return (Property) getSource();
  152. }
  153. }
  154. @Override
  155. public void addListener(ValueChangeListener listener) {
  156. if (valueChangeListeners == null) {
  157. valueChangeListeners = new LinkedList<ValueChangeListener>();
  158. }
  159. valueChangeListeners.add(listener);
  160. }
  161. @Override
  162. public void removeListener(ValueChangeListener listener) {
  163. if (valueChangeListeners != null) {
  164. valueChangeListeners.remove(listener);
  165. }
  166. }
  167. /**
  168. * Sends a value change event to all registered listeners.
  169. */
  170. protected void fireValueChange() {
  171. if (valueChangeListeners != null) {
  172. final Object[] l = valueChangeListeners.toArray();
  173. final Property.ValueChangeEvent event = new ValueChangeEvent(this);
  174. for (int i = 0; i < l.length; i++) {
  175. ((Property.ValueChangeListener) l[i]).valueChange(event);
  176. }
  177. }
  178. }
  179. public Collection<?> getListeners(Class<?> eventType) {
  180. if (Property.ValueChangeEvent.class.isAssignableFrom(eventType)) {
  181. if (valueChangeListeners == null) {
  182. return Collections.EMPTY_LIST;
  183. } else {
  184. return Collections.unmodifiableCollection(valueChangeListeners);
  185. }
  186. } else if (Property.ReadOnlyStatusChangeEvent.class
  187. .isAssignableFrom(eventType)) {
  188. if (readOnlyStatusChangeListeners == null) {
  189. return Collections.EMPTY_LIST;
  190. } else {
  191. return Collections
  192. .unmodifiableCollection(readOnlyStatusChangeListeners);
  193. }
  194. }
  195. return Collections.EMPTY_LIST;
  196. }
  197. }