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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.data;
  5. /**
  6. * <p>
  7. * The <code>Property</code> is a simple data object that contains one typed
  8. * value. This interface contains methods to inspect and modify the stored value
  9. * and its type, and the object's read-only state.
  10. * </p>
  11. *
  12. * <p>
  13. * The <code>Property</code> also defines the events
  14. * <code>ReadOnlyStatusChangeEvent</code> and <code>ValueChangeEvent</code>,
  15. * and the associated <code>listener</code> and <code>notifier</code>
  16. * 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
  27. * value needs to be changed through the implementing class.
  28. * </p>
  29. *
  30. * @author IT Mill Ltd
  31. * @version
  32. * @VERSION@
  33. * @since 3.0
  34. */
  35. public interface Property {
  36. /**
  37. * Gets the value stored in the Property.
  38. *
  39. * @return the value stored in the Property
  40. */
  41. public Object getValue();
  42. /**
  43. * Sets the value of the Property.
  44. * <p>
  45. * Implementing this functionality is optional. If the functionality is
  46. * missing, one should declare the Property to be in read-only mode and
  47. * throw <code>Property.ReadOnlyException</code> in this function.
  48. * </p>
  49. * Note : It is not required, but highly recommended to support setting the
  50. * value also as a <code>String</code> in addition to the native type of
  51. * the Property (as given by the <code>getType</code> method). If the
  52. * <code>String</code> conversion fails or is unsupported, the method
  53. * should throw <code>Property.ConversionException</code>. The string
  54. * conversion should at least understand the format returned by the
  55. * <code>toString</code> method of the Property.
  56. *
  57. * @param newValue
  58. * New value of the Property. This should be assignable to
  59. * the type returned by getType, but also String type should
  60. * be supported
  61. *
  62. * @throws Property.ReadOnlyException
  63. * if the object is in read-only mode
  64. * @throws Property.ConversionException
  65. * if newValue can't be converted into the Property's native
  66. * type directly or through String
  67. */
  68. public void setValue(Object newValue) throws Property.ReadOnlyException,
  69. Property.ConversionException;
  70. /**
  71. * Returns the value of the Property in human readable textual format. The
  72. * return value should be assignable to the <code>setValue</code> method
  73. * if the Property is not in read-only mode.
  74. *
  75. * @return <code>String</code> representation of the value stored in the
  76. * Property
  77. */
  78. public String toString();
  79. /**
  80. * Returns the type of the Property. The methods <code>getValue</code> and
  81. * <code>setValue</code> must be compatible with this type: one must be
  82. * able to safely cast the value returned from <code>getValue</code> to
  83. * the given type and pass any variable assignable to this type as an
  84. * argument to <code>setValue</code>.
  85. *
  86. * @return type of the Property
  87. */
  88. public Class getType();
  89. /**
  90. * Tests if the Property is in read-only mode. In read-only mode calls to
  91. * the method <code>setValue</code> will throw
  92. * <code>ReadOnlyException</code> and will not modify the value of the
  93. * Property.
  94. *
  95. * @return <code>true</code> if the Property is in read-only mode,
  96. * <code>false</code> if it's not
  97. */
  98. public boolean isReadOnly();
  99. /**
  100. * Sets the Property's read-only mode to the specified status.
  101. *
  102. * This functionality is optional, but all properties must implement the
  103. * <code>isReadOnly</code> mode query correctly.
  104. *
  105. * @param newStatus
  106. * new read-only status of the Property
  107. */
  108. public void setReadOnly(boolean newStatus);
  109. /**
  110. * <code>Exception</code> object that signals that a requested Property
  111. * modification failed because it's in read-only mode.
  112. *
  113. * @author IT Mill Ltd.
  114. * @version
  115. * @VERSION@
  116. * @since 3.0
  117. */
  118. public class ReadOnlyException extends RuntimeException {
  119. /**
  120. * Serial generated by eclipse.
  121. */
  122. private static final long serialVersionUID = 3257571702287119410L;
  123. /**
  124. * Constructs a new <code>ReadOnlyException</code> without a detail
  125. * message.
  126. */
  127. public ReadOnlyException() {
  128. }
  129. /**
  130. * Constructs a new <code>ReadOnlyException</code> with the specified
  131. * detail message.
  132. *
  133. * @param msg
  134. * the detail message
  135. */
  136. public ReadOnlyException(String msg) {
  137. super(msg);
  138. }
  139. }
  140. /**
  141. * An exception that signals that the value passed to the
  142. * <code>setValue</code> method couldn't be converted to the native type
  143. * of the Property.
  144. *
  145. * @author IT Mill Ltd
  146. * @version
  147. * @VERSION@
  148. * @since 3.0
  149. */
  150. public class ConversionException extends RuntimeException {
  151. /**
  152. * Serial generated by eclipse.
  153. */
  154. private static final long serialVersionUID = 3257571706666366008L;
  155. /**
  156. * Constructs a new <code>ConversionException</code> without a detail
  157. * message.
  158. */
  159. public ConversionException() {
  160. }
  161. /**
  162. * Constructs a new <code>ConversionException</code> with the
  163. * specified detail message.
  164. *
  165. * @param msg
  166. * the detail message
  167. */
  168. public ConversionException(String msg) {
  169. super(msg);
  170. }
  171. /**
  172. * Constructs a new <code>ConversionException</code> from another
  173. * exception.
  174. *
  175. * @param cause
  176. * The cause of the the conversion failure
  177. */
  178. public ConversionException(Throwable cause) {
  179. super(cause.toString());
  180. }
  181. }
  182. /**
  183. * Interface implemented by the viewer classes capable of using a Property
  184. * as a data source.
  185. *
  186. * @author IT Mill Ltd.
  187. * @version
  188. * @VERSION@
  189. * @since 3.0
  190. */
  191. public interface Viewer {
  192. /**
  193. * Sets the Property that serves as the data source of the viewer.
  194. *
  195. * @param newDataSource
  196. * the new data source Property
  197. */
  198. public void setPropertyDataSource(Property newDataSource);
  199. /**
  200. * Gets the Property serving as the data source of the viewer.
  201. *
  202. * @return the Property serving as the viewers data source
  203. */
  204. public Property getPropertyDataSource();
  205. }
  206. /**
  207. * Interface implemented by the editor classes capable of editing the
  208. * Property.
  209. * <p>
  210. * Implementing this interface means that the Property serving as the data
  211. * source of the editor can be modified through the editor. It does not
  212. * restrict the editor from editing the Property internally, though if the
  213. * Property is in a read-only mode, attempts to modify it will result in the
  214. * <code>ReadOnlyException</code> being thrown.
  215. * </p>
  216. *
  217. * @author IT Mill Ltd.
  218. * @version
  219. * @VERSION@
  220. * @since 3.0
  221. */
  222. public interface Editor extends Property.Viewer {
  223. }
  224. /* Value change event ******************************************* */
  225. /**
  226. * An <code>Event</code> object specifying the Property whose value has
  227. * been changed.
  228. *
  229. * @author IT Mill Ltd.
  230. * @version
  231. * @VERSION@
  232. * @since 3.0
  233. */
  234. public interface ValueChangeEvent {
  235. /**
  236. * Retrieves the Property that has been modified.
  237. *
  238. * @return source Property of the event
  239. */
  240. public Property getProperty();
  241. }
  242. /**
  243. * The <code>listener</code> interface for receiving
  244. * <code>ValueChangeEvent</code> objects.
  245. *
  246. * @author IT Mill Ltd.
  247. * @version
  248. * @VERSION@
  249. * @since 3.0
  250. */
  251. public interface ValueChangeListener {
  252. /**
  253. * Notifies this listener that the Property's value has changed.
  254. *
  255. * @param event
  256. * value change event object
  257. */
  258. public void valueChange(Property.ValueChangeEvent event);
  259. }
  260. /**
  261. * The interface for adding and removing <code>ValueChangeEvent</code>
  262. * listeners. If a Property wishes to allow other objects to receive
  263. * <code>ValueChangeEvent</code> generated by it, it must implement this
  264. * interface.
  265. * <p>
  266. * Note : The general Java convention is not to explicitly declare that a
  267. * class generates events, but to directly define the
  268. * <code>addListener</code> and <code>removeListener</code> methods.
  269. * That way the caller of these methods has no real way of finding out if
  270. * the class really will send the events, or if it just defines the methods
  271. * to be able to implement an interface.
  272. * </p>
  273. *
  274. * @author IT Mill Ltd.
  275. * @version
  276. * @VERSION@
  277. * @since 3.0
  278. */
  279. public interface ValueChangeNotifier {
  280. /**
  281. * Registers a new value change listener for this Property.
  282. *
  283. * @param listener
  284. * the new Listener to be registered
  285. */
  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 removeListener(Property.ValueChangeListener listener);
  294. }
  295. /* ReadOnly Status change event ***************************************** */
  296. /**
  297. * An <code>Event</code> object specifying the Property whose read-only
  298. * status has been changed.
  299. *
  300. * @author IT Mill Ltd.
  301. * @version
  302. * @VERSION@
  303. * @since 3.0
  304. */
  305. public interface ReadOnlyStatusChangeEvent {
  306. /**
  307. * Property whose read-only state has changed.
  308. *
  309. * @return source Property of the event.
  310. */
  311. public Property getProperty();
  312. }
  313. /**
  314. * The listener interface for receiving
  315. * <code>ReadOnlyStatusChangeEvent</code> objects.
  316. *
  317. * @author IT Mill Ltd.
  318. * @version
  319. * @VERSION@
  320. * @since 3.0
  321. */
  322. public interface ReadOnlyStatusChangeListener {
  323. /**
  324. * Notifies this listener that a Property's read-only status has
  325. * changed.
  326. *
  327. * @param event
  328. * Read-only status change event object
  329. */
  330. public void readOnlyStatusChange(
  331. Property.ReadOnlyStatusChangeEvent event);
  332. }
  333. /**
  334. * The interface for adding and removing
  335. * <code>ReadOnlyStatusChangeEvent</code> listeners. If a Property wishes
  336. * to allow other objects to receive <code>ReadOnlyStatusChangeEvent</code>
  337. * generated by it, it must implement this interface.
  338. * <p>
  339. * Note : The general Java convention is not to explicitly declare that a
  340. * class generates events, but to directly define the
  341. * <code>addListener</code> and <code>removeListener</code> methods.
  342. * That way the caller of these methods has no real way of finding out if
  343. * the class really will send the events, or if it just defines the methods
  344. * to be able to implement an interface.
  345. * </p>
  346. *
  347. * @author IT Mill Ltd.
  348. * @version
  349. * @VERSION@
  350. * @since 3.0
  351. */
  352. public interface ReadOnlyStatusChangeNotifier {
  353. /**
  354. * Registers a new read-only status change listener for this Property.
  355. *
  356. * @param listener
  357. * the new Listener to be registered
  358. */
  359. public void addListener(Property.ReadOnlyStatusChangeListener listener);
  360. /**
  361. * Removes a previously registered read-only status change listener.
  362. *
  363. * @param listener
  364. * listener to be removed
  365. */
  366. public void removeListener(
  367. Property.ReadOnlyStatusChangeListener listener);
  368. }
  369. }