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.

Property.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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;
  17. import java.io.Serializable;
  18. /**
  19. * <p>
  20. * The <code>Property</code> is a simple data object that contains one typed
  21. * value. This interface contains methods to inspect and modify the stored value
  22. * and its type, and the object's read-only state.
  23. * </p>
  24. *
  25. * <p>
  26. * The <code>Property</code> also defines the events
  27. * <code>ReadOnlyStatusChangeEvent</code> and <code>ValueChangeEvent</code>, and
  28. * the associated <code>listener</code> and <code>notifier</code> interfaces.
  29. * </p>
  30. *
  31. * <p>
  32. * The <code>Property.Viewer</code> interface should be used to attach the
  33. * Property to an external data source. This way the value in the data source
  34. * can be inspected using the <code>Property</code> interface.
  35. * </p>
  36. *
  37. * <p>
  38. * The <code>Property.editor</code> interface should be implemented if the value
  39. * needs to be changed through the implementing class.
  40. * </p>
  41. *
  42. * @param T
  43. * type of values of the property
  44. *
  45. * @author Vaadin Ltd
  46. * @since 3.0
  47. */
  48. public interface Property<T> extends Serializable {
  49. /**
  50. * Gets the value stored in the Property. The returned object is compatible
  51. * with the class returned by getType().
  52. *
  53. * @return the value stored in the Property
  54. */
  55. public T getValue();
  56. /**
  57. * Sets the value of the Property.
  58. * <p>
  59. * Implementing this functionality is optional. If the functionality is
  60. * missing, one should declare the Property to be in read-only mode and
  61. * throw <code>Property.ReadOnlyException</code> in this function.
  62. * </p>
  63. *
  64. * Note : Since Vaadin 7.0, setting the value of a non-String property as a
  65. * String is no longer supported.
  66. *
  67. * @param newValue
  68. * New value of the Property. This should be assignable to the
  69. * type returned by getType
  70. *
  71. * @throws Property.ReadOnlyException
  72. * if the object is in read-only mode
  73. */
  74. public void setValue(T newValue) throws Property.ReadOnlyException;
  75. /**
  76. * Returns the type of the Property. The methods <code>getValue</code> and
  77. * <code>setValue</code> must be compatible with this type: one must be able
  78. * to safely cast the value returned from <code>getValue</code> to the given
  79. * type and pass any variable assignable to this type as an argument to
  80. * <code>setValue</code>.
  81. *
  82. * @return type of the Property
  83. */
  84. public Class<? extends T> getType();
  85. /**
  86. * Tests if the Property is in read-only mode. In read-only mode calls to
  87. * the method <code>setValue</code> will throw
  88. * <code>ReadOnlyException</code> and will not modify the value of the
  89. * Property.
  90. *
  91. * @return <code>true</code> if the Property is in read-only mode,
  92. * <code>false</code> if it's not
  93. */
  94. public boolean isReadOnly();
  95. /**
  96. * Sets the Property's read-only mode to the specified status.
  97. *
  98. * This functionality is optional, but all properties must implement the
  99. * <code>isReadOnly</code> mode query correctly.
  100. *
  101. * @param newStatus
  102. * new read-only status of the Property
  103. */
  104. public void setReadOnly(boolean newStatus);
  105. /**
  106. * A Property that is capable of handle a transaction that can end in commit
  107. * or rollback.
  108. *
  109. * Note that this does not refer to e.g. database transactions but rather
  110. * two-phase commit that allows resetting old field values (in e.g. a
  111. * FieldGroup) if the commit of one of the properties fails after other
  112. * properties have already been committed.
  113. *
  114. * @param <T>
  115. * The type of the property
  116. * @author Vaadin Ltd
  117. * @since 7.0
  118. */
  119. public interface Transactional<T> extends Property<T> {
  120. /**
  121. * Starts a transaction.
  122. *
  123. * <p>
  124. * If the value is set during a transaction the value must not replace
  125. * the original value until {@link #commit()} is called. Still,
  126. * {@link #getValue()} must return the current value set in the
  127. * transaction. Calling {@link #rollback()} while in a transaction must
  128. * rollback the value to what it was before the transaction started.
  129. * </p>
  130. * <p>
  131. * {@link ValueChangeEvent}s must not be emitted for internal value
  132. * changes during a transaction. If the value changes as a result of
  133. * {@link #commit()}, a {@link ValueChangeEvent} should be emitted.
  134. * </p>
  135. */
  136. public void startTransaction();
  137. /**
  138. * Commits and ends the transaction that is in progress.
  139. * <p>
  140. * If the value is changed as a result of this operation, a
  141. * {@link ValueChangeEvent} is emitted if such are supported.
  142. * <p>
  143. * This method has no effect if there is no transaction is in progress.
  144. * <p>
  145. * This method must never throw an exception.
  146. */
  147. public void commit();
  148. /**
  149. * Aborts and rolls back the transaction that is in progress.
  150. * <p>
  151. * The value is reset to the value before the transaction started. No
  152. * {@link ValueChangeEvent} is emitted as a result of this.
  153. * <p>
  154. * This method has no effect if there is no transaction is in progress.
  155. * <p>
  156. * This method must never throw an exception.
  157. */
  158. public void rollback();
  159. }
  160. /**
  161. * <code>Exception</code> object that signals that a requested Property
  162. * modification failed because it's in read-only mode.
  163. *
  164. * @author Vaadin Ltd.
  165. * @since 3.0
  166. */
  167. @SuppressWarnings("serial")
  168. public class ReadOnlyException extends RuntimeException {
  169. /**
  170. * Constructs a new <code>ReadOnlyException</code> without a detail
  171. * message.
  172. */
  173. public ReadOnlyException() {
  174. }
  175. /**
  176. * Constructs a new <code>ReadOnlyException</code> with the specified
  177. * detail message.
  178. *
  179. * @param msg
  180. * the detail message
  181. */
  182. public ReadOnlyException(String msg) {
  183. super(msg);
  184. }
  185. }
  186. /**
  187. * Interface implemented by the viewer classes capable of using a Property
  188. * as a data source.
  189. *
  190. * @author Vaadin Ltd.
  191. * @since 3.0
  192. */
  193. public interface Viewer extends Serializable {
  194. /**
  195. * Sets the Property that serves as the data source of the viewer.
  196. *
  197. * @param newDataSource
  198. * the new data source Property
  199. */
  200. public void setPropertyDataSource(Property newDataSource);
  201. /**
  202. * Gets the Property serving as the data source of the viewer.
  203. *
  204. * @return the Property serving as the viewers data source
  205. */
  206. public Property getPropertyDataSource();
  207. }
  208. /**
  209. * Interface implemented by the editor classes capable of editing the
  210. * Property.
  211. * <p>
  212. * Implementing this interface means that the Property serving as the data
  213. * source of the editor can be modified through the editor. It does not
  214. * restrict the editor from editing the Property internally, though if the
  215. * Property is in a read-only mode, attempts to modify it will result in the
  216. * <code>ReadOnlyException</code> being thrown.
  217. * </p>
  218. *
  219. * @author Vaadin Ltd.
  220. * @since 3.0
  221. */
  222. public interface Editor extends Property.Viewer, Serializable {
  223. }
  224. /* Value change event */
  225. /**
  226. * An <code>Event</code> object specifying the Property whose value has been
  227. * changed.
  228. *
  229. * @author Vaadin Ltd.
  230. * @since 3.0
  231. */
  232. public interface ValueChangeEvent extends Serializable {
  233. /**
  234. * Retrieves the Property that has been modified.
  235. *
  236. * @return source Property of the event
  237. */
  238. public Property getProperty();
  239. }
  240. /**
  241. * The <code>listener</code> interface for receiving
  242. * <code>ValueChangeEvent</code> objects.
  243. *
  244. * @author Vaadin Ltd.
  245. * @since 3.0
  246. */
  247. public interface ValueChangeListener extends Serializable {
  248. /**
  249. * Notifies this listener that the Property's value has changed.
  250. *
  251. * @param event
  252. * value change event object
  253. */
  254. public void valueChange(Property.ValueChangeEvent event);
  255. }
  256. /**
  257. * The interface for adding and removing <code>ValueChangeEvent</code>
  258. * listeners. If a Property wishes to allow other objects to receive
  259. * <code>ValueChangeEvent</code> generated by it, it must implement this
  260. * interface.
  261. * <p>
  262. * Note : The general Java convention is not to explicitly declare that a
  263. * class generates events, but to directly define the
  264. * <code>addListener</code> and <code>removeListener</code> methods. That
  265. * way the caller of these methods has no real way of finding out if the
  266. * class really will send the events, or if it just defines the methods to
  267. * be able to implement an interface.
  268. * </p>
  269. *
  270. * @author Vaadin Ltd.
  271. * @since 3.0
  272. */
  273. public interface ValueChangeNotifier extends Serializable {
  274. /**
  275. * Registers a new value change listener for this Property.
  276. *
  277. * @param listener
  278. * the new Listener to be registered
  279. */
  280. public void addValueChangeListener(Property.ValueChangeListener listener);
  281. /**
  282. * @deprecated As of 7.0, replaced by
  283. * {@link #addValueChangeListener(ValueChangeListener)}
  284. **/
  285. @Deprecated
  286. public void addListener(Property.ValueChangeListener listener);
  287. /**
  288. * Removes a previously registered value change listener.
  289. *
  290. * @param listener
  291. * listener to be removed
  292. */
  293. public void removeValueChangeListener(
  294. Property.ValueChangeListener listener);
  295. /**
  296. * @deprecated As of 7.0, replaced by
  297. * {@link #removeValueChangeListener(ValueChangeListener)}
  298. **/
  299. @Deprecated
  300. public void removeListener(Property.ValueChangeListener listener);
  301. }
  302. /* ReadOnly Status change event */
  303. /**
  304. * An <code>Event</code> object specifying the Property whose read-only
  305. * status has been changed.
  306. *
  307. * @author Vaadin Ltd.
  308. * @since 3.0
  309. */
  310. public interface ReadOnlyStatusChangeEvent extends Serializable {
  311. /**
  312. * Property whose read-only state has changed.
  313. *
  314. * @return source Property of the event.
  315. */
  316. public Property getProperty();
  317. }
  318. /**
  319. * The listener interface for receiving
  320. * <code>ReadOnlyStatusChangeEvent</code> objects.
  321. *
  322. * @author Vaadin Ltd.
  323. * @since 3.0
  324. */
  325. public interface ReadOnlyStatusChangeListener extends Serializable {
  326. /**
  327. * Notifies this listener that a Property's read-only status has
  328. * changed.
  329. *
  330. * @param event
  331. * Read-only status change event object
  332. */
  333. public void readOnlyStatusChange(
  334. Property.ReadOnlyStatusChangeEvent event);
  335. }
  336. /**
  337. * The interface for adding and removing
  338. * <code>ReadOnlyStatusChangeEvent</code> listeners. If a Property wishes to
  339. * allow other objects to receive <code>ReadOnlyStatusChangeEvent</code>
  340. * generated by it, it must implement this interface.
  341. * <p>
  342. * Note : The general Java convention is not to explicitly declare that a
  343. * class generates events, but to directly define the
  344. * <code>addListener</code> and <code>removeListener</code> methods. That
  345. * way the caller of these methods has no real way of finding out if the
  346. * class really will send the events, or if it just defines the methods to
  347. * be able to implement an interface.
  348. * </p>
  349. *
  350. * @author Vaadin Ltd.
  351. * @since 3.0
  352. */
  353. public interface ReadOnlyStatusChangeNotifier extends Serializable {
  354. /**
  355. * Registers a new read-only status change listener for this Property.
  356. *
  357. * @param listener
  358. * the new Listener to be registered
  359. */
  360. public void addReadOnlyStatusChangeListener(
  361. Property.ReadOnlyStatusChangeListener listener);
  362. /**
  363. * @deprecated As of 7.0, replaced by
  364. * {@link #addReadOnlyStatusChangeListener(ReadOnlyStatusChangeListener)}
  365. **/
  366. @Deprecated
  367. public void addListener(Property.ReadOnlyStatusChangeListener listener);
  368. /**
  369. * Removes a previously registered read-only status change listener.
  370. *
  371. * @param listener
  372. * listener to be removed
  373. */
  374. public void removeReadOnlyStatusChangeListener(
  375. Property.ReadOnlyStatusChangeListener listener);
  376. /**
  377. * @deprecated As of 7.0, replaced by
  378. * {@link #removeReadOnlyStatusChangeListener(ReadOnlyStatusChangeListener)}
  379. **/
  380. @Deprecated
  381. public void removeListener(
  382. Property.ReadOnlyStatusChangeListener listener);
  383. }
  384. }