From 78c544807878d83da2d79ed86b122d1aeb9acc84 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Wed, 1 Apr 2009 11:43:35 +0000 Subject: [PATCH] #2808 merged to 6.0: IndexedContainer.indexOfId() fix and an interactive test class svn changeset:7266/svn branch:6.0 --- .../toolkit/data/util/IndexedContainer.java | 27 ++-- .../IndexedContainerFilteringTest.java | 139 ++++++++++++++++++ 2 files changed, 152 insertions(+), 14 deletions(-) create mode 100644 src/com/itmill/toolkit/tests/containers/IndexedContainerFilteringTest.java diff --git a/src/com/itmill/toolkit/data/util/IndexedContainer.java b/src/com/itmill/toolkit/data/util/IndexedContainer.java index 27af4f5178..0ad853c19b 100644 --- a/src/com/itmill/toolkit/data/util/IndexedContainer.java +++ b/src/com/itmill/toolkit/data/util/IndexedContainer.java @@ -16,6 +16,7 @@ import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.LinkedList; +import java.util.List; import java.util.NoSuchElementException; import com.itmill.toolkit.data.Container; @@ -461,13 +462,10 @@ public class IndexedContainer implements Container.Indexed, */ public Object nextItemId(Object itemId) { if (filteredItemIds != null) { - if (!filteredItemIds.contains(itemId)) { + if (itemId == null || !filteredItemIds.contains(itemId)) { return null; } final Iterator i = filteredItemIds.iterator(); - if (itemId == null) { - return null; - } while (i.hasNext() && !itemId.equals(i.next())) { ; } @@ -629,15 +627,15 @@ public class IndexedContainer implements Container.Indexed, if (itemId == null) { return -1; } - try { - for (final Iterator i = filteredItemIds.iterator(); itemId - .equals(i.next());) { - index++; + final Iterator i = filteredItemIds.iterator(); + while (i.hasNext()) { + Object id = i.next(); + if (itemId.equals(id)) { + return index; } - return index; - } catch (final NoSuchElementException e) { - return -1; + index++; } + return -1; } return itemIds.indexOf(itemId); } @@ -655,6 +653,7 @@ public class IndexedContainer implements Container.Indexed, return null; } + // TODO should add based on a filtered index! // Adds the Item to container itemIds.add(index, newItemId); Hashtable t = new Hashtable(); @@ -1363,8 +1362,8 @@ public class IndexedContainer implements Container.Indexed, public synchronized void sort(Object[] propertyId, boolean[] ascending) { // Removes any non-sortable property ids - final ArrayList ids = new ArrayList(); - final ArrayList orders = new ArrayList(); + final List ids = new ArrayList(); + final List orders = new ArrayList(); final Collection sortable = getSortableContainerPropertyIds(); for (int i = 0; i < propertyId.length; i++) { if (sortable.contains(propertyId[i])) { @@ -1380,7 +1379,7 @@ public class IndexedContainer implements Container.Indexed, sortPropertyId = ids.toArray(); sortDirection = new boolean[orders.size()]; for (int i = 0; i < sortDirection.length; i++) { - sortDirection[i] = ((Boolean) orders.get(i)).booleanValue(); + sortDirection[i] = (orders.get(i)).booleanValue(); } // Sort diff --git a/src/com/itmill/toolkit/tests/containers/IndexedContainerFilteringTest.java b/src/com/itmill/toolkit/tests/containers/IndexedContainerFilteringTest.java new file mode 100644 index 0000000000..00cb1e3355 --- /dev/null +++ b/src/com/itmill/toolkit/tests/containers/IndexedContainerFilteringTest.java @@ -0,0 +1,139 @@ +package com.itmill.toolkit.tests.containers; + +import com.itmill.toolkit.data.Item; +import com.itmill.toolkit.data.util.IndexedContainer; +import com.itmill.toolkit.terminal.Sizeable; +import com.itmill.toolkit.tests.components.TestBase; +import com.itmill.toolkit.ui.Button; +import com.itmill.toolkit.ui.CheckBox; +import com.itmill.toolkit.ui.Label; +import com.itmill.toolkit.ui.Table; +import com.itmill.toolkit.ui.TextField; +import com.itmill.toolkit.ui.VerticalLayout; +import com.itmill.toolkit.ui.Button.ClickEvent; + +public class IndexedContainerFilteringTest extends TestBase { + + private Table table; + private IndexedContainer container; + private TextField filterString; + private TextField position; + private int nextToAdd = 1; + private Label nextLabel; + + @Override + protected String getDescription() { + return "Adding items to a filtered IndexedContainer inserts the items at the wrong location."; + } + + @Override + protected Integer getTicketNumber() { + return new Integer(2809); + } + + @Override + protected void setup() { + table = new Table(); + container = (IndexedContainer) table.getContainerDataSource(); + + table.setWidth(300, Sizeable.UNITS_PIXELS); + table.setSelectable(true); + table.setMultiSelect(false); + table.addContainerProperty("column1", String.class, "test"); + + for (int i = 0; i < 25; ++i) { + table.addItem(new Object[] { "Item " + i }, "Item " + i); + } + + VerticalLayout vl = new VerticalLayout(); + + // activate & deactivate filtering + filterString = new TextField("Filter string:", "1"); + vl.addComponent(filterString); + + final CheckBox cb = new CheckBox("Filter", new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + container.removeAllContainerFilters(); + if (((CheckBox) event.getSource()).booleanValue()) { + container.addContainerFilter("column1", filterString + .getValue().toString(), false, false); + } + } + }); + cb.setImmediate(true); + vl.addComponent(cb); + + nextLabel = new Label(); + nextLabel.setCaption("Next id: " + nextToAdd); + vl.addComponent(nextLabel); + + // addItemAt(idx), addItemAfter(selection), addItem() + + final Button addItemButton = new Button("addItem()", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + Item item = container.addItem("addItem() " + nextToAdd); + if (item != null) { + item.getItemProperty("column1").setValue( + "addItem() " + nextToAdd); + } + nextToAdd++; + nextLabel.setCaption("Next id: " + nextToAdd); + } + }); + addItemButton.setImmediate(true); + vl.addComponent(addItemButton); + + final Button addItemAfterButton = new Button("addItemAfter()", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + Object selection = table.getValue(); + if (selection == null) { + return; + } + String id = "addItemAfter() " + nextToAdd; + Item item = container.addItemAfter(selection, id); + if (item != null) { + item.getItemProperty("column1").setValue(id); + table.setValue(id); + } else { + getMainWindow().showNotification( + "Adding item after " + selection + + " failed"); + } + nextToAdd++; + nextLabel.setCaption("Next id: " + nextToAdd); + } + }); + addItemAfterButton.setImmediate(true); + vl.addComponent(addItemAfterButton); + + position = new TextField("Position:", "0"); + vl.addComponent(position); + + final Button addItemAtButton = new Button("addItemAt()", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + int index = Integer.parseInt(position.getValue() + .toString()); + String id = "addItemAt() " + nextToAdd; + Item item = container.addItemAt(index, id); + if (item != null) { + item.getItemProperty("column1").setValue(id); + table.setValue(id); + } else { + getMainWindow().showNotification( + "Adding item at index " + + position.getValue() + " failed"); + } + nextToAdd++; + nextLabel.setCaption("Next id: " + nextToAdd); + } + }); + addItemAtButton.setImmediate(true); + vl.addComponent(addItemAtButton); + + getLayout().addComponent(table); + getLayout().addComponent(vl); + } +} -- 2.39.5