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.

DetailsConnectorChange.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 2000-2018 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.v7.shared.ui.grid;
  17. import java.io.Serializable;
  18. import java.util.Comparator;
  19. import com.vaadin.shared.Connector;
  20. /**
  21. * A description of an indexing modification for a connector. This is used by
  22. * Grid for internal bookkeeping updates.
  23. *
  24. * @since 7.5.0
  25. * @author Vaadin Ltd
  26. */
  27. public class DetailsConnectorChange implements Serializable {
  28. public static final Comparator<DetailsConnectorChange> REMOVED_FIRST_COMPARATOR = new Comparator<DetailsConnectorChange>() {
  29. @Override
  30. public int compare(DetailsConnectorChange a, DetailsConnectorChange b) {
  31. boolean deleteA = a.getNewIndex() == null;
  32. boolean deleteB = b.getNewIndex() == null;
  33. if (deleteA && !deleteB) {
  34. return -1;
  35. } else if (!deleteA && deleteB) {
  36. return 1;
  37. } else {
  38. return 0;
  39. }
  40. }
  41. };
  42. private Connector connector;
  43. private Integer oldIndex;
  44. private Integer newIndex;
  45. private boolean shouldStillBeVisible;
  46. /** Create a new connector index change. */
  47. public DetailsConnectorChange() {
  48. }
  49. /**
  50. * Convenience constructor for setting all the fields in one line.
  51. * <p>
  52. * Calling this constructor will also assert that the state of the pojo is
  53. * consistent by internal assumptions.
  54. *
  55. * @param connector
  56. * the changed connector
  57. * @param oldIndex
  58. * the old index
  59. * @param newIndex
  60. * the new index
  61. * @param shouldStillBeVisible
  62. * details should be visible regardless of {@code connector}
  63. */
  64. public DetailsConnectorChange(Connector connector, Integer oldIndex,
  65. Integer newIndex, boolean shouldStillBeVisible) {
  66. this.connector = connector;
  67. this.oldIndex = oldIndex;
  68. this.newIndex = newIndex;
  69. this.shouldStillBeVisible = shouldStillBeVisible;
  70. assert assertStateIsOk();
  71. }
  72. private boolean assertStateIsOk() {
  73. boolean connectorAndNewIndexIsNotNull = connector != null
  74. && newIndex != null;
  75. boolean connectorAndNewIndexIsNullThenOldIndexIsSet = connector == null
  76. && newIndex == null && oldIndex != null;
  77. assert (connectorAndNewIndexIsNotNull
  78. || connectorAndNewIndexIsNullThenOldIndexIsSet) : "connector: "
  79. + nullityString(connector) + ", oldIndex: "
  80. + nullityString(oldIndex) + ", newIndex: "
  81. + nullityString(newIndex);
  82. return true;
  83. }
  84. private static String nullityString(Object object) {
  85. return object == null ? "null" : "non-null";
  86. }
  87. /**
  88. * Gets the old index for the connector.
  89. * <p>
  90. * If <code>null</code>, the connector is recently added. This means that
  91. * {@link #getConnector()} is expected not to return <code>null</code>.
  92. *
  93. * @return the old index for the connector
  94. */
  95. public Integer getOldIndex() {
  96. assert assertStateIsOk();
  97. return oldIndex;
  98. }
  99. /**
  100. * Gets the new index for the connector.
  101. * <p>
  102. * If <code>null</code>, the connector should be removed. This means that
  103. * {@link #getConnector()} is expected to return <code>null</code> as well.
  104. *
  105. * @return the new index for the connector
  106. */
  107. public Integer getNewIndex() {
  108. assert assertStateIsOk();
  109. return newIndex;
  110. }
  111. /**
  112. * Gets the changed connector.
  113. *
  114. * @return the changed connector. Might be <code>null</code>
  115. */
  116. public Connector getConnector() {
  117. assert assertStateIsOk();
  118. return connector;
  119. }
  120. /**
  121. * Sets the changed connector.
  122. *
  123. * @param connector
  124. * the changed connector. May be <code>null</code>
  125. */
  126. public void setConnector(Connector connector) {
  127. this.connector = connector;
  128. }
  129. /**
  130. * Sets the old index.
  131. *
  132. * @param oldIndex
  133. * the old index. May be <code>null</code> if a new connector is
  134. * being inserted
  135. */
  136. public void setOldIndex(Integer oldIndex) {
  137. this.oldIndex = oldIndex;
  138. }
  139. /**
  140. * Sets the new index.
  141. *
  142. * @param newIndex
  143. * the new index. May be <code>null</code> if a connector is
  144. * being removed
  145. */
  146. public void setNewIndex(Integer newIndex) {
  147. this.newIndex = newIndex;
  148. }
  149. /**
  150. * Checks whether whether the details should remain open, even if connector
  151. * might be <code>null</code>.
  152. *
  153. * @return <code>true</code> if the details should remain open, even if
  154. * connector might be <code>null</code>
  155. */
  156. public boolean isShouldStillBeVisible() {
  157. return shouldStillBeVisible;
  158. }
  159. /**
  160. * Sets whether the details should remain open, even if connector might be
  161. * <code>null</code>.
  162. *
  163. * @param shouldStillBeVisible
  164. * <code>true</code> if the details should remain open, even if
  165. * connector might be <code>null</code>
  166. */
  167. public void setShouldStillBeVisible(boolean shouldStillBeVisible) {
  168. this.shouldStillBeVisible = shouldStillBeVisible;
  169. }
  170. }