You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/08/03 21:34:41 UTC

svn commit: r800509 - in /incubator/pivot/trunk: core/src/org/apache/pivot/collections/FilteredList.java core/test/org/apache/pivot/collections/test/FilteredListTest.java wtk/src/org/apache/pivot/wtk/skin/terra/TerraMenuButtonSkin.java

Author: gbrown
Date: Mon Aug  3 19:34:41 2009
New Revision: 800509

URL: http://svn.apache.org/viewvc?rev=800509&view=rev
Log:
Add a basic unit test for FilteredList; fix some bugs in FilteredList identified by unit test.

Added:
    incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/FilteredListTest.java
Modified:
    incubator/pivot/trunk/core/src/org/apache/pivot/collections/FilteredList.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraMenuButtonSkin.java

Modified: incubator/pivot/trunk/core/src/org/apache/pivot/collections/FilteredList.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/src/org/apache/pivot/collections/FilteredList.java?rev=800509&r1=800508&r2=800509&view=diff
==============================================================================
--- incubator/pivot/trunk/core/src/org/apache/pivot/collections/FilteredList.java (original)
+++ incubator/pivot/trunk/core/src/org/apache/pivot/collections/FilteredList.java Mon Aug  3 19:34:41 2009
@@ -75,9 +75,11 @@
                     T item = items.get(i);
 
                     int viewIndex = view.indexOf(item);
-                    Sequence<T> removed = view.remove(viewIndex, 1);
 
-                    listListeners.itemsRemoved(FilteredList.this, viewIndex, removed);
+                    if (viewIndex != -1) {
+                        Sequence<T> removed = view.remove(viewIndex, 1);
+                        listListeners.itemsRemoved(FilteredList.this, viewIndex, removed);
+                    }
                 }
             }
         }
@@ -347,7 +349,7 @@
             listListeners.itemUpdated(this, index, previousItem);
 
             // Update the item in the source
-            source.update(source.indexOf(item), item);
+            source.update(source.indexOf(previousItem), item);
         } finally {
             updating = false;
         }
@@ -491,4 +493,24 @@
     public ListenerList<FilteredListListener<T>> getFilteredListListeners() {
         return filteredListListeners;
     }
+
+    @Override
+    public String toString() {
+        StringBuilder sb = new StringBuilder();
+
+        sb.append(getClass().getName());
+        sb.append(" [");
+
+        for (int i = 0; i < getLength(); i++) {
+            if (i > 0) {
+                sb.append(", ");
+            }
+
+            sb.append(get(i));
+        }
+
+        sb.append("]");
+
+        return sb.toString();
+    }
 }

Added: incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/FilteredListTest.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/FilteredListTest.java?rev=800509&view=auto
==============================================================================
--- incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/FilteredListTest.java (added)
+++ incubator/pivot/trunk/core/test/org/apache/pivot/collections/test/FilteredListTest.java Mon Aug  3 19:34:41 2009
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.collections.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Comparator;
+
+import org.apache.pivot.collections.ArrayList;
+import org.apache.pivot.collections.FilteredList;
+import org.apache.pivot.util.Filter;
+import org.junit.Test;
+
+public class FilteredListTest {
+    @Test
+    public void basicTest() {
+        // Create a list
+        ArrayList<String> sourceList = new ArrayList<String>("Apple", "Banana", "Cherry", "Donut");
+        FilteredList<String> filteredList = new FilteredList<String>(sourceList);
+
+        assertEquals(filteredList.getLength(), 4);
+
+        for (int i = 0, n = filteredList.getLength(); i < n; i++) {
+            assertEquals(filteredList.get(i), sourceList.get(i));
+        }
+
+        // Filter the list
+        filteredList.setFilter(new Filter<String>() {
+            public boolean include(String string) {
+                return !string.startsWith("D");
+            }
+        });
+
+        assertEquals(filteredList.getLength(), 3);
+
+        for (int i = 0, n = filteredList.getLength(); i < n; i++) {
+            assertEquals(filteredList.get(i), sourceList.get(i));
+        }
+
+        // Add items to the source list
+        sourceList.add("Eggplant");
+        sourceList.add("Dr. Pepper");
+        assertEquals(filteredList.getLength(), 4);
+
+        // Add items to the filtered list
+        filteredList.add("Fig");
+        filteredList.add("Diet Coke");
+        assertEquals(filteredList.getLength(), 5);
+        assertEquals(sourceList.getLength(), 8);
+
+        // Remove items from the source list
+        sourceList.remove("Banana");
+        assertEquals(filteredList.getLength(), 4);
+        assertEquals(sourceList.getLength(), 7);
+
+        sourceList.remove("Diet Coke");
+        assertEquals(filteredList.getLength(), 4);
+        assertEquals(sourceList.getLength(), 6);
+
+        // Remove items from the filtered list
+        filteredList.remove("Cherry");
+        assertEquals(filteredList.getLength(), 3);
+        assertEquals(sourceList.getLength(), 5);
+
+        // Update items in the source list
+        sourceList.update(0, "Doorknob");
+        assertEquals(filteredList.getLength(), 2);
+
+        sourceList.update(0, "Window");
+        assertEquals(filteredList.getLength(), 3);
+
+        // Update items in the filtered list
+        filteredList.update(0, "Pickle");
+        assertEquals(filteredList.get(0), "Pickle");
+
+        try {
+            filteredList.update(0, "Delivery");
+        } catch(IllegalArgumentException exception) {
+            assertTrue(true);
+        }
+
+        // Apply different comparators
+        sourceList.setComparator(new Comparator<String>() {
+            public int compare(String s1, String s2) {
+                return -s1.compareTo(s2);
+            }
+        });
+
+        filteredList.setComparator(new Comparator<String>() {
+            public int compare(String s1, String s2) {
+                return s1.compareTo(s2);
+            }
+        });
+
+        assertEquals(sourceList.get(0), "Window");
+        assertEquals(filteredList.get(2), "Window");
+
+        // Clear the filtered list
+        filteredList.clear();
+        assertEquals(sourceList.getLength(), 2);
+
+        for (String string : sourceList) {
+            assertEquals(Character.toLowerCase(string.charAt(0)), 'd');
+        }
+    }
+}

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraMenuButtonSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraMenuButtonSkin.java?rev=800509&r1=800508&r2=800509&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraMenuButtonSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraMenuButtonSkin.java Mon Aug  3 19:34:41 2009
@@ -102,10 +102,6 @@
         MenuButton menuButton = (MenuButton)getComponent();
         Button.DataRenderer dataRenderer = menuButton.getDataRenderer();
 
-        if (height != -1) {
-            height = Math.max(height - (padding.top + padding.bottom + 2), 0);
-        }
-
         dataRenderer.render(menuButton.getButtonData(), menuButton, false);
 
         int preferredWidth = dataRenderer.getPreferredWidth(-1) + TRIGGER_WIDTH
@@ -203,8 +199,8 @@
         triggerGraphics.setStroke(new BasicStroke(0));
         triggerGraphics.setPaint(color);
 
-        int tx = triggerBounds.x + Math.round((triggerBounds.width - triggerIconShape.getBounds().width) / 2);
-        int ty = triggerBounds.y + Math.round((triggerBounds.height - triggerIconShape.getBounds().height) / 2f);
+        int tx = triggerBounds.x + (triggerBounds.width - triggerIconShape.getBounds().width) / 2;
+        int ty = triggerBounds.y + (triggerBounds.height - triggerIconShape.getBounds().height) / 2;
         triggerGraphics.translate(tx, ty);
 
         triggerGraphics.draw(triggerIconShape);