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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.data.util;
  17. import java.util.Collection;
  18. import java.util.Collections;
  19. import java.util.LinkedList;
  20. import java.util.logging.Logger;
  21. import com.vaadin.data.Property;
  22. /**
  23. * Abstract base class for {@link Property} implementations.
  24. *
  25. * Handles listener management for {@link ValueChangeListener}s and
  26. * {@link ReadOnlyStatusChangeListener}s.
  27. *
  28. * @since 6.6
  29. */
  30. public abstract class AbstractProperty<T> implements Property<T>,
  31. Property.ValueChangeNotifier, Property.ReadOnlyStatusChangeNotifier {
  32. /**
  33. * List of listeners who are interested in the read-only status changes of
  34. * the Property
  35. */
  36. private LinkedList<ReadOnlyStatusChangeListener> readOnlyStatusChangeListeners = null;
  37. /**
  38. * List of listeners who are interested in the value changes of the Property
  39. */
  40. private LinkedList<ValueChangeListener> valueChangeListeners = null;
  41. /**
  42. * Is the Property read-only?
  43. */
  44. private boolean readOnly;
  45. /**
  46. * {@inheritDoc}
  47. *
  48. * Override for additional restrictions on what is considered a read-only
  49. * property.
  50. */
  51. @Override
  52. public boolean isReadOnly() {
  53. return readOnly;
  54. }
  55. @Override
  56. public void setReadOnly(boolean newStatus) {
  57. boolean oldStatus = isReadOnly();
  58. readOnly = newStatus;
  59. if (oldStatus != isReadOnly()) {
  60. fireReadOnlyStatusChange();
  61. }
  62. }
  63. /**
  64. * Returns a string representation of this object. The returned string
  65. * representation depends on if the legacy Property toString mode is enabled
  66. * or disabled.
  67. * <p>
  68. * If legacy Property toString mode is enabled, returns the value of the
  69. * <code>Property</code> converted to a String.
  70. * </p>
  71. * <p>
  72. * If legacy Property toString mode is disabled, the string representation
  73. * has no special meaning
  74. * </p>
  75. *
  76. * @see LegacyPropertyHelper#isLegacyToStringEnabled()
  77. *
  78. * @return A string representation of the value value stored in the Property
  79. * or a string representation of the Property object.
  80. * @deprecated As of 7.0. To get the property value, use {@link #getValue()}
  81. * instead (and possibly toString on that)
  82. */
  83. @Deprecated
  84. @Override
  85. public String toString() {
  86. if (!LegacyPropertyHelper.isLegacyToStringEnabled()) {
  87. return super.toString();
  88. } else {
  89. return LegacyPropertyHelper.legacyPropertyToString(this);
  90. }
  91. }
  92. /* Events */
  93. /**
  94. * An <code>Event</code> object specifying the Property whose read-only
  95. * status has been changed.
  96. */
  97. protected static class ReadOnlyStatusChangeEvent extends
  98. java.util.EventObject implements Property.ReadOnlyStatusChangeEvent {
  99. /**
  100. * Constructs a new read-only status change event for this object.
  101. *
  102. * @param source
  103. * source object of the event.
  104. */
  105. protected ReadOnlyStatusChangeEvent(Property source) {
  106. super(source);
  107. }
  108. /**
  109. * Gets the Property whose read-only state has changed.
  110. *
  111. * @return source Property of the event.
  112. */
  113. @Override
  114. public Property getProperty() {
  115. return (Property) getSource();
  116. }
  117. }
  118. /**
  119. * Registers a new read-only status change listener for this Property.
  120. *
  121. * @param listener
  122. * the new Listener to be registered.
  123. */
  124. @Override
  125. public void addReadOnlyStatusChangeListener(
  126. Property.ReadOnlyStatusChangeListener listener) {
  127. if (readOnlyStatusChangeListeners == null) {
  128. readOnlyStatusChangeListeners = new LinkedList<ReadOnlyStatusChangeListener>();
  129. }
  130. readOnlyStatusChangeListeners.add(listener);
  131. }
  132. /**
  133. * @deprecated As of 7.0, replaced by
  134. * {@link #addReadOnlyStatusChangeListener(com.vaadin.data.Property.ReadOnlyStatusChangeListener)}
  135. **/
  136. @Override
  137. @Deprecated
  138. public void addListener(Property.ReadOnlyStatusChangeListener listener) {
  139. addReadOnlyStatusChangeListener(listener);
  140. }
  141. /**
  142. * Removes a previously registered read-only status change listener.
  143. *
  144. * @param listener
  145. * the listener to be removed.
  146. */
  147. @Override
  148. public void removeReadOnlyStatusChangeListener(
  149. Property.ReadOnlyStatusChangeListener listener) {
  150. if (readOnlyStatusChangeListeners != null) {
  151. readOnlyStatusChangeListeners.remove(listener);
  152. }
  153. }
  154. /**
  155. * @deprecated As of 7.0, replaced by
  156. * {@link #removeReadOnlyStatusChangeListener(com.vaadin.data.Property.ReadOnlyStatusChangeListener)}
  157. **/
  158. @Override
  159. @Deprecated
  160. public void removeListener(Property.ReadOnlyStatusChangeListener listener) {
  161. removeReadOnlyStatusChangeListener(listener);
  162. }
  163. /**
  164. * Sends a read only status change event to all registered listeners.
  165. */
  166. protected void fireReadOnlyStatusChange() {
  167. if (readOnlyStatusChangeListeners != null) {
  168. final Object[] l = readOnlyStatusChangeListeners.toArray();
  169. final Property.ReadOnlyStatusChangeEvent event = new ReadOnlyStatusChangeEvent(
  170. this);
  171. for (int i = 0; i < l.length; i++) {
  172. ((Property.ReadOnlyStatusChangeListener) l[i])
  173. .readOnlyStatusChange(event);
  174. }
  175. }
  176. }
  177. /**
  178. * An <code>Event</code> object specifying the Property whose value has been
  179. * changed.
  180. */
  181. private static class ValueChangeEvent extends java.util.EventObject
  182. implements Property.ValueChangeEvent {
  183. /**
  184. * Constructs a new value change event for this object.
  185. *
  186. * @param source
  187. * source object of the event.
  188. */
  189. protected ValueChangeEvent(Property source) {
  190. super(source);
  191. }
  192. /**
  193. * Gets the Property whose value has changed.
  194. *
  195. * @return source Property of the event.
  196. */
  197. @Override
  198. public Property getProperty() {
  199. return (Property) getSource();
  200. }
  201. }
  202. @Override
  203. public void addValueChangeListener(ValueChangeListener listener) {
  204. if (valueChangeListeners == null) {
  205. valueChangeListeners = new LinkedList<ValueChangeListener>();
  206. }
  207. valueChangeListeners.add(listener);
  208. }
  209. /**
  210. * @deprecated As of 7.0, replaced by
  211. * {@link #addValueChangeListener(com.vaadin.data.Property.ValueChangeListener)}
  212. **/
  213. @Override
  214. @Deprecated
  215. public void addListener(ValueChangeListener listener) {
  216. addValueChangeListener(listener);
  217. }
  218. @Override
  219. public void removeValueChangeListener(ValueChangeListener listener) {
  220. if (valueChangeListeners != null) {
  221. valueChangeListeners.remove(listener);
  222. }
  223. }
  224. /**
  225. * @deprecated As of 7.0, replaced by
  226. * {@link #removeValueChangeListener(com.vaadin.data.Property.ValueChangeListener)}
  227. **/
  228. @Override
  229. @Deprecated
  230. public void removeListener(ValueChangeListener listener) {
  231. removeValueChangeListener(listener);
  232. }
  233. /**
  234. * Sends a value change event to all registered listeners.
  235. */
  236. protected void fireValueChange() {
  237. if (valueChangeListeners != null) {
  238. final Object[] l = valueChangeListeners.toArray();
  239. final Property.ValueChangeEvent event = new ValueChangeEvent(this);
  240. for (int i = 0; i < l.length; i++) {
  241. ((Property.ValueChangeListener) l[i]).valueChange(event);
  242. }
  243. }
  244. }
  245. public Collection<?> getListeners(Class<?> eventType) {
  246. if (Property.ValueChangeEvent.class.isAssignableFrom(eventType)) {
  247. if (valueChangeListeners == null) {
  248. return Collections.EMPTY_LIST;
  249. } else {
  250. return Collections.unmodifiableCollection(valueChangeListeners);
  251. }
  252. } else if (Property.ReadOnlyStatusChangeEvent.class
  253. .isAssignableFrom(eventType)) {
  254. if (readOnlyStatusChangeListeners == null) {
  255. return Collections.EMPTY_LIST;
  256. } else {
  257. return Collections
  258. .unmodifiableCollection(readOnlyStatusChangeListeners);
  259. }
  260. }
  261. return Collections.EMPTY_LIST;
  262. }
  263. private static Logger getLogger() {
  264. return Logger.getLogger(AbstractProperty.class.getName());
  265. }
  266. }