123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- package com.vaadin.tests.components.table;
-
- import java.util.AbstractList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.List;
- import java.util.logging.Logger;
-
- import com.vaadin.server.VaadinSession;
- import com.vaadin.ui.Component;
- import com.vaadin.ui.Label;
- import com.vaadin.v7.data.Container;
- import com.vaadin.v7.data.Item;
- import com.vaadin.v7.data.Property;
- import com.vaadin.v7.data.util.AbstractContainer;
- import com.vaadin.v7.data.util.BeanItem;
-
- @SuppressWarnings("serial")
- public class ExpandingContainer extends AbstractContainer
- implements Container.Indexed, Container.ItemSetChangeNotifier {
-
- public static final List<String> PROPERTY_IDS = Arrays.asList("id",
- "column1", "column2");
-
- private final Label sizeLabel;
- private final Logger log = Logger.getLogger(this.getClass().getName());
-
- private int currentSize = 300;
-
- private boolean loggingEnabled;
-
- public ExpandingContainer(Label sizeLabel) {
- this.sizeLabel = sizeLabel;
- updateLabel();
- }
-
- private void log(String message) {
- if (loggingEnabled) {
- log.info(message);
- }
- }
-
- // Expand container if we scroll past 85%
- public int checkExpand(int index) {
- log("checkExpand(" + index + ")");
- if (index >= currentSize * 0.85) {
- final int oldsize = currentSize;
- currentSize = (int) (oldsize * 1.3333);
- log("*** getSizeWithHint(" + index + "): went past 85% of size="
- + oldsize + ", new size=" + currentSize);
- updateLabel();
- }
- return currentSize;
- }
-
- @Override
- public void fireItemSetChange() {
- super.fireItemSetChange();
- }
-
- private void updateLabel() {
- sizeLabel.setValue("Container size: " + currentSize);
- }
-
- public void triggerItemSetChange() {
- log("*** triggerItemSetChange(): scheduling item set change event");
- final VaadinSession session = VaadinSession.getCurrent();
- new Thread() {
- @Override
- public void run() {
- ExpandingContainer.this.invoke(session, () -> {
- log("*** Firing item set change event");
- ExpandingContainer.this.fireItemSetChange();
- });
- }
- }.start();
- }
-
- private void invoke(VaadinSession session, Runnable action) {
- session.lock();
- VaadinSession previousSession = VaadinSession.getCurrent();
- VaadinSession.setCurrent(session);
- try {
- action.run();
- } finally {
- session.unlock();
- VaadinSession.setCurrent(previousSession);
- }
- }
-
- // Container
-
- @Override
- public BeanItem<MyBean> getItem(Object itemId) {
- if (!(itemId instanceof Integer)) {
- return null;
- }
- final int index = ((Integer) itemId).intValue();
- return new BeanItem<>(new MyBean(index));
- }
-
- @Override
- public Collection<Integer> getItemIds() {
- return new IntList(size());
- }
-
- @Override
- public List<String> getContainerPropertyIds() {
- return PROPERTY_IDS;
- }
-
- @Override
- @SuppressWarnings("rawtypes")
- public Property/* <?> */ getContainerProperty(Object itemId,
- Object propertyId) {
- BeanItem<MyBean> item = getItem(itemId);
- return item != null ? item.getItemProperty(propertyId) : null;
- }
-
- @Override
- public Class<?> getType(Object propertyId) {
- return Component.class;
- }
-
- @Override
- public int size() {
- return currentSize;
- }
-
- @Override
- public boolean containsId(Object itemId) {
- if (!(itemId instanceof Integer)) {
- return false;
- }
- int index = ((Integer) itemId).intValue();
- checkExpand(index);
- return index >= 0 && index < currentSize;
- }
-
- /**
- * @throws UnsupportedOperationException
- * always
- */
- @Override
- public Item addItem(Object itemId) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * @throws UnsupportedOperationException
- * always
- */
- @Override
- public Item addItem() {
- throw new UnsupportedOperationException();
- }
-
- /**
- * @throws UnsupportedOperationException
- * always
- */
- @Override
- public boolean removeItem(Object itemId) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * @throws UnsupportedOperationException
- * always
- */
- @Override
- public boolean addContainerProperty(Object propertyId, Class<?> type,
- Object defaultValue) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * @throws UnsupportedOperationException
- * always
- */
- @Override
- public boolean removeContainerProperty(Object propertyId) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * @throws UnsupportedOperationException
- * always
- */
- @Override
- public boolean removeAllItems() {
- throw new UnsupportedOperationException();
- }
-
- // Container.Indexed
-
- /**
- * @throws UnsupportedOperationException
- * always
- */
- @Override
- public Object addItemAt(int index) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * @throws UnsupportedOperationException
- * always
- */
- @Override
- public Item addItemAt(int index, Object newItemId) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Integer getIdByIndex(int index) {
- if (index < 0) {
- throw new IndexOutOfBoundsException("index < " + index);
- }
- final int size = currentSize;
- if (index >= size) {
- throw new IndexOutOfBoundsException(
- "index=" + index + " but size=" + size);
- }
- checkExpand(index);
- return index;
- }
-
- @Override
- public List<Integer> getItemIds(int startIndex, int numberOfItems) {
- if (numberOfItems < 0) {
- throw new IllegalArgumentException("numberOfItems < 0");
- }
- final int size = currentSize;
- checkExpand(startIndex);
- if (startIndex < 0 || startIndex > size) {
- throw new IndexOutOfBoundsException(
- "startIndex=" + startIndex + " but size=" + size);
- }
- if (startIndex + numberOfItems > size) {
- numberOfItems = size - startIndex;
- }
- return new IntList(startIndex, numberOfItems);
- }
-
- @Override
- public int indexOfId(Object itemId) {
- if (!(itemId instanceof Integer)) {
- return -1;
- }
- final int index = ((Integer) itemId).intValue();
- checkExpand(index);
- if (index < 0 || index >= currentSize) {
- return -1;
- }
- return index;
- }
-
- // Container.Ordered
-
- @Override
- public Integer nextItemId(Object itemId) {
- if (!(itemId instanceof Integer)) {
- return null;
- }
- int index = ((Integer) itemId).intValue();
- checkExpand(index);
- if (index < 0 || index + 1 >= currentSize) {
- return null;
- }
- return index + 1;
- }
-
- @Override
- public Integer prevItemId(Object itemId) {
- if (!(itemId instanceof Integer)) {
- return null;
- }
- int index = ((Integer) itemId).intValue();
- checkExpand(index);
- if (index - 1 < 0 || index >= currentSize) {
- return null;
- }
- return index - 1;
- }
-
- @Override
- public Integer firstItemId() {
- return currentSize == 0 ? null : 0;
- }
-
- @Override
- public Integer lastItemId() {
- final int size = currentSize;
- return size == 0 ? null : size - 1;
- }
-
- @Override
- public boolean isFirstId(Object itemId) {
- if (!(itemId instanceof Integer)) {
- return false;
- }
- final int index = ((Integer) itemId).intValue();
- checkExpand(index);
- final int size = currentSize;
- return size > 0 && index == 0;
- }
-
- @Override
- public boolean isLastId(Object itemId) {
- if (!(itemId instanceof Integer)) {
- return false;
- }
- int index = ((Integer) itemId).intValue();
- checkExpand(index);
- int size = currentSize;
- return size > 0 && index == size - 1;
- }
-
- /**
- * @throws UnsupportedOperationException
- * always
- */
- @Override
- public Item addItemAfter(Object previousItemId) {
- throw new UnsupportedOperationException();
- }
-
- /**
- * @throws UnsupportedOperationException
- * always
- */
- @Override
- public Item addItemAfter(Object previousItemId, Object newItemId) {
- throw new UnsupportedOperationException();
- }
-
- // Container.ItemSetChangeNotifier
-
- @Override
- @SuppressWarnings("deprecation")
- public void addListener(Container.ItemSetChangeListener listener) {
- super.addListener(listener);
- }
-
- @Override
- public void addItemSetChangeListener(
- Container.ItemSetChangeListener listener) {
- super.addItemSetChangeListener(listener);
- }
-
- @Override
- @SuppressWarnings("deprecation")
- public void removeListener(Container.ItemSetChangeListener listener) {
- super.removeListener(listener);
- }
-
- @Override
- public void removeItemSetChangeListener(
- Container.ItemSetChangeListener listener) {
- super.removeItemSetChangeListener(listener);
- }
-
- // IntList
-
- private static class IntList extends AbstractList<Integer> {
-
- private final int min;
- private final int size;
-
- public IntList(int size) {
- this(0, size);
- }
-
- public IntList(int min, int size) {
- if (size < 0) {
- throw new IllegalArgumentException("size < 0");
- }
- this.min = min;
- this.size = size;
- }
-
- @Override
- public int size() {
- return size;
- }
-
- @Override
- public Integer get(int index) {
- if (index < 0 || index >= size) {
- throw new IndexOutOfBoundsException();
- }
- return min + index;
- }
- }
-
- // MyBean
- public class MyBean {
-
- private final int index;
-
- public MyBean(int index) {
- this.index = index;
- }
-
- public String getId() {
- return "ROW #" + index;
- }
-
- public String getColumn1() {
- return genText();
- }
-
- public String getColumn2() {
- return genText();
- }
-
- private String genText() {
- return "this is a line of text in row #" + index;
- }
- }
-
- public void logDetails(boolean enabled) {
- loggingEnabled = enabled;
- }
- }
|