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.

TransactionalPropertyWrapper.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 com.vaadin.data.Property;
  18. import com.vaadin.data.Property.ValueChangeEvent;
  19. import com.vaadin.data.Property.ValueChangeNotifier;
  20. /**
  21. * Wrapper class that helps implement two-phase commit for a non-transactional
  22. * property.
  23. *
  24. * When accessing the property through the wrapper, getting and setting the
  25. * property value take place immediately. However, the wrapper keeps track of
  26. * the old value of the property so that it can be set for the property in case
  27. * of a roll-back. This can result in the underlying property value changing
  28. * multiple times (first based on modifications made by the application, then
  29. * back upon roll-back).
  30. *
  31. * Value change events on the {@link TransactionalPropertyWrapper} are only
  32. * fired at the end of a successful transaction, whereas listeners attached to
  33. * the underlying property may receive multiple value change events.
  34. *
  35. * @see com.vaadin.data.Property.Transactional
  36. *
  37. * @author Vaadin Ltd
  38. * @since 7.0
  39. *
  40. * @param <T>
  41. */
  42. public class TransactionalPropertyWrapper<T> extends AbstractProperty<T>
  43. implements ValueChangeNotifier, Property.Transactional<T> {
  44. private Property<T> wrappedProperty;
  45. private boolean inTransaction = false;
  46. private boolean valueChangePending;
  47. private T valueBeforeTransaction;
  48. private final ValueChangeListener listener = new ValueChangeListener() {
  49. @Override
  50. public void valueChange(ValueChangeEvent event) {
  51. fireValueChange();
  52. }
  53. };
  54. public TransactionalPropertyWrapper(Property<T> wrappedProperty) {
  55. this.wrappedProperty = wrappedProperty;
  56. if (wrappedProperty instanceof ValueChangeNotifier) {
  57. ((ValueChangeNotifier) wrappedProperty)
  58. .addValueChangeListener(listener);
  59. }
  60. }
  61. /**
  62. * Removes the ValueChangeListener from wrapped Property that was added by
  63. * TransactionalPropertyWrapper.
  64. *
  65. * @since 7.1.15
  66. */
  67. public void detachFromProperty() {
  68. if (wrappedProperty instanceof ValueChangeNotifier) {
  69. ((ValueChangeNotifier) wrappedProperty)
  70. .removeValueChangeListener(listener);
  71. }
  72. }
  73. @Override
  74. public Class getType() {
  75. return wrappedProperty.getType();
  76. }
  77. @Override
  78. public T getValue() {
  79. return wrappedProperty.getValue();
  80. }
  81. @Override
  82. public void setValue(T newValue) throws ReadOnlyException {
  83. // Causes a value change to be sent to this listener which in turn fires
  84. // a new value change event for this property
  85. wrappedProperty.setValue(newValue);
  86. }
  87. @Override
  88. public void startTransaction() {
  89. inTransaction = true;
  90. valueBeforeTransaction = getValue();
  91. }
  92. @Override
  93. public void commit() {
  94. endTransaction();
  95. }
  96. @Override
  97. public void rollback() {
  98. try {
  99. wrappedProperty.setValue(valueBeforeTransaction);
  100. } finally {
  101. valueChangePending = false;
  102. endTransaction();
  103. }
  104. }
  105. protected void endTransaction() {
  106. inTransaction = false;
  107. valueBeforeTransaction = null;
  108. if (valueChangePending) {
  109. fireValueChange();
  110. }
  111. }
  112. @Override
  113. protected void fireValueChange() {
  114. if (inTransaction) {
  115. valueChangePending = true;
  116. } else {
  117. super.fireValueChange();
  118. }
  119. }
  120. public Property<T> getWrappedProperty() {
  121. return wrappedProperty;
  122. }
  123. @Override
  124. public boolean isReadOnly() {
  125. return wrappedProperty.isReadOnly();
  126. }
  127. @Override
  128. public void setReadOnly(boolean newStatus) {
  129. boolean oldStatus = isReadOnly();
  130. wrappedProperty.setReadOnly(newStatus);
  131. if (oldStatus != isReadOnly()) {
  132. fireReadOnlyStatusChange();
  133. }
  134. }
  135. }