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.

ObjectProperty.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.lang.reflect.Constructor;
  6. import com.vaadin.data.Property;
  7. /**
  8. * A simple data object containing one typed value. This class is a
  9. * straightforward implementation of the the {@link com.vaadin.data.Property}
  10. * interface.
  11. *
  12. * @author Vaadin Ltd.
  13. * @version
  14. * @VERSION@
  15. * @since 3.0
  16. */
  17. @SuppressWarnings("serial")
  18. public class ObjectProperty<T> extends AbstractProperty {
  19. /**
  20. * The value contained by the Property.
  21. */
  22. private T value;
  23. /**
  24. * Data type of the Property's value.
  25. */
  26. private final Class<T> type;
  27. /**
  28. * Creates a new instance of ObjectProperty with the given value. The type
  29. * of the property is automatically initialized to be the type of the given
  30. * value.
  31. *
  32. * @param value
  33. * the Initial value of the Property.
  34. */
  35. @SuppressWarnings("unchecked")
  36. // the cast is safe, because an object of type T has class Class<T>
  37. public ObjectProperty(T value) {
  38. this(value, (Class<T>) value.getClass());
  39. }
  40. /**
  41. * Creates a new instance of ObjectProperty with the given value and type.
  42. *
  43. * Any value of type Object is accepted because, if the type class contains
  44. * a string constructor, the toString of the value is used to create the new
  45. * value. See {@link #setValue(Object)}.
  46. *
  47. * @param value
  48. * the Initial value of the Property.
  49. * @param type
  50. * the type of the value. The value must be assignable to given
  51. * type.
  52. */
  53. public ObjectProperty(Object value, Class<T> type) {
  54. // Set the values
  55. this.type = type;
  56. setValue(value);
  57. }
  58. /**
  59. * Creates a new instance of ObjectProperty with the given value, type and
  60. * read-only mode status.
  61. *
  62. * Any value of type Object is accepted, see
  63. * {@link #ObjectProperty(Object, Class)}.
  64. *
  65. * @param value
  66. * the Initial value of the property.
  67. * @param type
  68. * the type of the value. <code>value</code> must be assignable
  69. * to this type.
  70. * @param readOnly
  71. * Sets the read-only mode.
  72. */
  73. public ObjectProperty(Object value, Class<T> type, boolean readOnly) {
  74. this(value, type);
  75. setReadOnly(readOnly);
  76. }
  77. /**
  78. * Returns the type of the ObjectProperty. The methods <code>getValue</code>
  79. * and <code>setValue</code> must be compatible with this type: one must be
  80. * able to safely cast the value returned from <code>getValue</code> to the
  81. * given type and pass any variable assignable to this type as an argument
  82. * to <code>setValue</code>.
  83. *
  84. * @return type of the Property
  85. */
  86. public final Class<T> getType() {
  87. return type;
  88. }
  89. /**
  90. * Gets the value stored in the Property.
  91. *
  92. * @return the value stored in the Property
  93. */
  94. public T getValue() {
  95. return value;
  96. }
  97. /**
  98. * Sets the value of the property. This method supports setting from
  99. * <code>String</code> if either <code>String</code> is directly assignable
  100. * to property type, or the type class contains a string constructor.
  101. *
  102. * @param newValue
  103. * the New value of the property.
  104. * @throws <code>Property.ReadOnlyException</code> if the object is in
  105. * read-only mode
  106. * @throws <code>Property.ConversionException</code> if the newValue can't
  107. * be converted into the Property's native type directly or through
  108. * <code>String</code>
  109. */
  110. public void setValue(Object newValue) throws Property.ReadOnlyException,
  111. Property.ConversionException {
  112. // Checks the mode
  113. if (isReadOnly()) {
  114. throw new Property.ReadOnlyException();
  115. }
  116. // Tries to assign the compatible value directly
  117. if (newValue == null || type.isAssignableFrom(newValue.getClass())) {
  118. @SuppressWarnings("unchecked")
  119. // the cast is safe after an isAssignableFrom check
  120. T value = (T) newValue;
  121. this.value = value;
  122. } else {
  123. try {
  124. // Gets the string constructor
  125. final Constructor<T> constr = getType().getConstructor(
  126. new Class[] { String.class });
  127. // Creates new object from the string
  128. value = constr
  129. .newInstance(new Object[] { newValue.toString() });
  130. } catch (final java.lang.Exception e) {
  131. throw new Property.ConversionException(e);
  132. }
  133. }
  134. fireValueChange();
  135. }
  136. }