package com.vaadin.tests.tickets;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.vaadin.Application;
import com.vaadin.data.Container.Filter;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window;

/**
 * Test for #8291 and #7666: NegativeArraySizeException when Table scrolled to
 * the end and its size reduced.
 */
public class Ticket8291 extends Application {

    @Override
    public void init() {
        setMainWindow(new Window("", new TestView()));
    }

    private static class DecimateFilter implements Filter {
        public boolean passesFilter(Object itemId, Item item)
                throws UnsupportedOperationException {
            return ((((TestObject) itemId).property3 % 10) == 0);
        }

        public boolean appliesToProperty(Object propertyId) {
            return true;
        }
    }

    private static class TestView extends HorizontalLayout {

        private Filter filter = null;

        private boolean reduceData;

        private TestView() {
            final Table table = new Table();
            List<TestObject> data = createData(1000);
            final BeanItemContainer<TestObject> container = new BeanItemContainer<TestObject>(
                    TestObject.class, data) {

                @Override
                public int size() {
                    if (reduceData) {
                        return 100;
                    } else {
                        return super.size();
                    }
                }
            };
            table.setContainerDataSource(container);
            addComponent(table);
            Button button = new Button("Click");
            button.addListener(new Button.ClickListener() {
                public void buttonClick(ClickEvent event) {
                    reduceData = !reduceData;
                    table.refreshRowCache();
                }
            });
            addComponent(button);
            Button button2 = new Button("Filter");
            button2.addListener(new Button.ClickListener() {
                public void buttonClick(ClickEvent event) {
                    if (filter != null) {
                        container.removeAllContainerFilters();
                        filter = null;
                    } else {
                        filter = new DecimateFilter();
                        container.addContainerFilter(filter);
                    }
                    table.refreshRowCache();
                }
            });
            addComponent(button2);
        }
    }

    private static List<TestObject> createData(int count) {
        ArrayList<TestObject> data = new ArrayList<TestObject>(count);
        for (int i = 0; i < count; i++) {
            data.add(new TestObject("string-" + i, new Date(), i));
        }
        return data;
    }

    public static class TestObject {

        private String property1;
        private Date property2;
        private Integer property3;

        public TestObject(String property1, Date property2, Integer property3) {
            this.property1 = property1;
            this.property2 = property2;
            this.property3 = property3;
        }

        public String getProperty1() {
            return property1;
        }

        public Date getProperty2() {
            return property2;
        }

        public Integer getProperty3() {
            return property3;
        }

    }

}

@@ -107,39 +107,8 @@
 t;%0A%0A
-import com.vaadin.Application;%0A
 impo
@@ -216,24 +216,67 @@
 mContainer;%0A
+import com.vaadin.terminal.WrappedRequest;%0A
 import com.v
@@ -396,42 +396,40 @@
 .ui.
-Table;%0Aimport com.vaadin.ui.Window
+Root;%0Aimport com.vaadin.ui.Table
 ;%0A%0A/
@@ -583,19 +583,12 @@
 nds 
-Application
+Root
  %7B%0A%0A
@@ -626,49 +626,53 @@
 nit(
-) %7B%0A        setMainWindow(new Window(%22%22, 
+WrappedRequest request) %7B%0A        setContent(
 new 
@@ -682,17 +682,16 @@
 tView())
-)
 ;%0A    %7D%0A
@@ -751,16 +751,34 @@
 ilter %7B%0A
+        @Override%0A
         
@@ -955,32 +955,50 @@
  0);%0A        %7D%0A%0A
+        @Override%0A
         public b
@@ -1945,32 +1945,58 @@
 ickListener() %7B%0A
+                @Override%0A
                 
@@ -2302,32 +2302,58 @@
 ickListener() %7B%0A
+                @Override%0A
                 

Extend Root instead of Application