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.

AbstractListing.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2000-2016 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.ui;
  17. import java.util.Objects;
  18. import com.vaadin.data.Listing;
  19. import com.vaadin.server.AbstractExtension;
  20. import com.vaadin.server.data.DataCommunicator;
  21. import com.vaadin.server.data.DataSource;
  22. import com.vaadin.server.data.TypedDataGenerator;
  23. /**
  24. * Base class for {@link Listing} components. Provides common handling for
  25. * {@link DataCommunicator} and {@link TypedDataGenerator}s.
  26. *
  27. * @param <T>
  28. * listing data type
  29. */
  30. public abstract class AbstractListing<T> extends AbstractComponent
  31. implements Listing<T> {
  32. /**
  33. * Helper base class for creating extensions for Listing components. This
  34. * class provides helpers for accessing the underlying parts of the
  35. * component and its communicational mechanism.
  36. *
  37. * @param <T>
  38. * listing data type
  39. */
  40. public abstract static class AbstractListingExtension<T>
  41. extends AbstractExtension implements TypedDataGenerator<T> {
  42. /**
  43. * {@inheritDoc}
  44. * <p>
  45. * Note: AbstractListingExtensions need parent to be of type
  46. * AbstractListing.
  47. *
  48. * @throws IllegalArgument
  49. * if parent is not an AbstractListing
  50. */
  51. public void extend(Listing<T> listing) {
  52. if (listing instanceof AbstractListing) {
  53. AbstractListing<T> parent = (AbstractListing<T>) listing;
  54. super.extend(parent);
  55. parent.addDataGenerator(this);
  56. } else {
  57. throw new IllegalArgumentException(
  58. "Parent needs to extend AbstractListing");
  59. }
  60. }
  61. @Override
  62. public void remove() {
  63. getParent().removeDataGenerator(this);
  64. super.remove();
  65. }
  66. /**
  67. * Gets a data object based on its client-side identifier key.
  68. *
  69. * @param key
  70. * key for data object
  71. * @return the data object
  72. */
  73. protected T getData(String key) {
  74. return getParent().getDataCommunicator().getKeyMapper().get(key);
  75. }
  76. @Override
  77. @SuppressWarnings("unchecked")
  78. public AbstractListing<T> getParent() {
  79. return (AbstractListing<T>) super.getParent();
  80. }
  81. /**
  82. * Helper method for refreshing a single data object.
  83. *
  84. * @param data
  85. * data object to refresh
  86. */
  87. protected void refresh(T data) {
  88. getParent().getDataCommunicator().refresh(data);
  89. }
  90. }
  91. /* DataCommunicator for this Listing component */
  92. private final DataCommunicator<T> dataCommunicator;
  93. /**
  94. * Constructs an {@link AbstractListing}, extending it with a
  95. * {@link DataCommunicator}.
  96. */
  97. protected AbstractListing() {
  98. this(new DataCommunicator<>());
  99. }
  100. /**
  101. * Constructs an {@link AbstractListing}, extending it with given
  102. * {@link DataCommunicator}.
  103. * <p>
  104. * <strong>Note:</strong> This method is for creating an
  105. * {@link AbstractListing} with a custom {@link DataCommunicator}. In the
  106. * common case {@link AbstractListing#AbstractListing()} should be used.
  107. *
  108. * @param dataCommunicator
  109. * a customized data communicator instance
  110. */
  111. protected AbstractListing(DataCommunicator<T> dataCommunicator) {
  112. Objects.requireNonNull(dataCommunicator,
  113. "The data communicator can't be null");
  114. this.dataCommunicator = dataCommunicator;
  115. addExtension(dataCommunicator);
  116. }
  117. @Override
  118. public void setDataSource(DataSource<T> dataSource) {
  119. getDataCommunicator().setDataSource(dataSource);
  120. }
  121. @Override
  122. public DataSource<T> getDataSource() {
  123. return getDataCommunicator().getDataSource();
  124. }
  125. /**
  126. * Adds a {@link TypedDataGenerator} for the {@link DataCommunicator} of
  127. * this Listing component.
  128. *
  129. * @param generator
  130. * typed data generator
  131. */
  132. protected void addDataGenerator(TypedDataGenerator<T> generator) {
  133. dataCommunicator.addDataGenerator(generator);
  134. }
  135. /**
  136. * Removed a {@link TypedDataGenerator} from the {@link DataCommunicator} of
  137. * this Listing component.
  138. *
  139. * @param generator
  140. * typed data generator
  141. */
  142. protected void removeDataGenerator(TypedDataGenerator<T> generator) {
  143. dataCommunicator.removeDataGenerator(generator);
  144. }
  145. /**
  146. * Get the {@link DataCommunicator} of this Listing component.
  147. *
  148. * @return data provider
  149. */
  150. public DataCommunicator<T> getDataCommunicator() {
  151. return dataCommunicator;
  152. }
  153. }