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

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