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/09/17 20:37:55 UTC

svn commit: r816324 - in /incubator/pivot/trunk: demos/src/org/apache/pivot/demos/tables/ tutorials/src/org/apache/pivot/tutorials/ wtk/src/org/apache/pivot/wtk/ wtk/src/org/apache/pivot/wtk/content/ wtk/src/org/apache/pivot/wtk/skin/terra/

Author: gbrown
Date: Thu Sep 17 18:37:54 2009
New Revision: 816324

URL: http://svn.apache.org/viewvc?rev=816324&view=rev
Log:
Complete implementation of TableView sort API changes (re-implementing sort handlers is still TBD).

Added:
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableViewRowComparator.java
Removed:
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableRow.java
Modified:
    incubator/pivot/trunk/demos/src/org/apache/pivot/demos/tables/fixed_column_table.wtkx
    incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/KitchenSink.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableView.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableViewColumnListener.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewHeaderSkin.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewSkin.java
    incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/terra_file_browser_skin.wtkx

Modified: incubator/pivot/trunk/demos/src/org/apache/pivot/demos/tables/fixed_column_table.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/demos/src/org/apache/pivot/demos/tables/fixed_column_table.wtkx?rev=816324&r1=816323&r2=816324&view=diff
==============================================================================
Binary files - no diff available.

Modified: incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/KitchenSink.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/KitchenSink.java?rev=816324&r1=816323&r2=816324&view=diff
==============================================================================
--- incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/KitchenSink.java (original)
+++ incubator/pivot/trunk/tutorials/src/org/apache/pivot/tutorials/KitchenSink.java Thu Sep 17 18:37:54 2009
@@ -24,6 +24,7 @@
 import java.util.Comparator;
 
 import org.apache.pivot.collections.ArrayList;
+import org.apache.pivot.collections.HashMap;
 import org.apache.pivot.collections.List;
 import org.apache.pivot.collections.Map;
 import org.apache.pivot.serialization.JSONSerializer;
@@ -77,7 +78,6 @@
 import org.apache.pivot.wtk.content.CalendarDateSpinnerData;
 import org.apache.pivot.wtk.content.ListItem;
 import org.apache.pivot.wtk.content.NumericSpinnerData;
-import org.apache.pivot.wtk.content.TableRow;
 import org.apache.pivot.wtk.content.TableViewHeaderData;
 import org.apache.pivot.wtk.content.TreeBranch;
 import org.apache.pivot.wtk.content.TreeNode;
@@ -531,7 +531,7 @@
                 ArrayList<Object> tableData = new ArrayList<Object>(10000);
 
                 for (int i = 0, n = tableData.getCapacity(); i < n; i++) {
-                    TableRow tableRow = new TableRow();
+                    HashMap<String, Integer> tableRow = new HashMap<String, Integer>();
 
                     tableRow.put("i", i);
                     tableRow.put("a", (int)Math.round(Math.random() * 10));

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableView.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableView.java?rev=816324&r1=816323&r2=816324&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableView.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableView.java Thu Sep 17 18:37:54 2009
@@ -49,7 +49,6 @@
         private Object headerData = null;
         private int width = 0;
         private boolean relative = false;
-        private SortDirection sortDirection = null;
         private Object filter = null;
         private CellRenderer cellRenderer = DEFAULT_CELL_RENDERER;
 
@@ -290,52 +289,6 @@
         }
 
         /**
-         * Returns the column's sort direction.
-         *
-         * @return
-         * The column's sort direction, or <tt>null</tt> if the column is not
-         * sorted.
-         */
-        public SortDirection getSortDirection() {
-            return sortDirection;
-        }
-
-        /**
-         * Sets the column's sort direction.
-         *
-         * @param sortDirection
-         * The column's sort direction, or <tt>null</tt> to specify no
-         * sort direction
-         */
-        public void setSortDirection(SortDirection sortDirection) {
-            SortDirection previousSortDirection = this.sortDirection;
-
-            if (previousSortDirection != sortDirection) {
-                this.sortDirection = sortDirection;
-
-                if (tableView != null) {
-                    tableView.tableViewColumnListeners.columnSortDirectionChanged(this,
-                        previousSortDirection);
-                }
-            }
-        }
-
-        /**
-         * Sets the column's sort direction.
-         *
-         * @param sortDirection
-         * The column's sort direction, or <tt>null</tt> to specify no
-         * sort direction
-         */
-        public final void setSortDirection(String sortDirection) {
-            if (sortDirection == null) {
-                setSortDirection((SortDirection)null);
-            } else {
-                setSortDirection(SortDirection.valueOf(sortDirection.toUpperCase()));
-            }
-        }
-
-        /**
          * Returns the column's filter.
          *
          * @return
@@ -930,13 +883,6 @@
         }
 
         @Override
-        public void columnSortDirectionChanged(Column column, SortDirection previousSortDirection) {
-            for (TableViewColumnListener listener : this) {
-                listener.columnSortDirectionChanged(column, previousSortDirection);
-            }
-        }
-
-        @Override
         public void columnFilterChanged(Column column, Object previousFilter) {
             for (TableViewColumnListener listener : this) {
                 listener.columnFilterChanged(column, previousFilter);
@@ -1104,6 +1050,7 @@
 
     private HashMap<String, SortDirection> sortMap = new HashMap<String, SortDirection>();
     private ArrayList<String> sortList = new ArrayList<String>();
+    private SortDictionary sortDictionary = new SortDictionary();
 
     private Filter<?> disabledRowFilter = null;
 
@@ -1635,6 +1582,44 @@
     }
 
     /**
+     * Returns the table view's sort dictionary.
+     */
+    public SortDictionary getSort() {
+        return sortDictionary;
+    }
+
+    /**
+     * Sets the table view's sort.
+     *
+     * @param sort
+     * A sequence of key/value pairs representing the sort. Keys represent column names and
+     * values represent sort direction.
+     */
+    public void setSort(Sequence<Dictionary.Pair<String, SortDirection>> sort) {
+        if (sort == null) {
+            throw new IllegalArgumentException();
+        }
+
+        // TODO
+    }
+
+    /**
+     * Sets the table view's sort.
+     *
+     * @param sort
+     * A JSON list containing JSON objects representing the sort.
+     *
+     * @see #setSort(Sequence)
+     */
+    public void setSort(String sort) {
+        if (sort == null) {
+            throw new IllegalArgumentException();
+        }
+
+        // TODO
+    }
+
+    /**
      * Returns the disabled state of a given row.
      *
      * @param index

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableViewColumnListener.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableViewColumnListener.java?rev=816324&r1=816323&r2=816324&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableViewColumnListener.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/TableViewColumnListener.java Thu Sep 17 18:37:54 2009
@@ -65,14 +65,6 @@
     public void columnWidthChanged(TableView.Column column, int previousWidth, boolean previousRelative);
 
     /**
-     * Called when a column's sort direction has changed.
-     *
-     * @param column
-     * @param previousSortDirection
-     */
-    public void columnSortDirectionChanged(TableView.Column column, SortDirection previousSortDirection);
-
-    /**
      * Called when a column's filter has changed.
      *
      * @param column

Added: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableViewRowComparator.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableViewRowComparator.java?rev=816324&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableViewRowComparator.java (added)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/content/TableViewRowComparator.java Thu Sep 17 18:37:54 2009
@@ -0,0 +1,90 @@
+/*
+ * 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.wtk.content;
+
+import java.util.Comparator;
+
+import org.apache.pivot.beans.BeanDictionary;
+import org.apache.pivot.collections.Dictionary;
+import org.apache.pivot.wtk.SortDirection;
+import org.apache.pivot.wtk.TableView;
+
+/**
+ * Compares two rows. The dictionary values are expected to implement {@link Comparable}. If
+ * not, the string representation of the value will be used.
+ */
+public class TableViewRowComparator implements Comparator<Object> {
+    private TableView tableView;
+
+    public TableViewRowComparator(TableView tableView) {
+        if (tableView == null) {
+            throw new IllegalArgumentException();
+        }
+
+        this.tableView = tableView;
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public int compare(Object o1, Object o2) {
+        int result;
+
+        TableView.SortDictionary sort = tableView.getSort();
+
+        if (sort.getLength() > 0) {
+            // TODO Support sorting on multiple columns
+            if (sort.getLength() > 1) {
+                throw new UnsupportedOperationException("Multi-column sort not yet supported.");
+            }
+
+            Dictionary<String, ?> row1;
+            if (o1 instanceof Dictionary<?, ?>) {
+                row1 = (Dictionary<String, ?>)o1;
+            } else {
+                row1 = new BeanDictionary(o1);
+            }
+
+            Dictionary<String, ?> row2;
+            if (o2 instanceof Dictionary<?, ?>) {
+                row2 = (Dictionary<String, ?>)o2;
+            } else {
+                row2 = new BeanDictionary(o2);
+            }
+
+            String columnName = sort.get(0).key;
+            SortDirection sortDirection = sort.get(0).value;
+
+            Comparable<Object> comparable = (Comparable<Object>)row1.get(columnName);
+            Object value = row2.get(columnName);
+
+            if (comparable == null
+                && value == null) {
+                result = 0;
+            } else if (comparable == null) {
+                result = 1;
+            } else if (value == null) {
+                result = -1;
+            } else {
+                result = (comparable.compareTo(value)) * (sortDirection == SortDirection.ASCENDING ? 1 : -1);
+            }
+        } else {
+            result = 0;
+        }
+
+        return result;
+    }
+}

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java?rev=816324&r1=816323&r2=816324&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraFileBrowserSkin.java Thu Sep 17 18:37:54 2009
@@ -61,8 +61,6 @@
 import org.apache.pivot.wtk.SortDirection;
 import org.apache.pivot.wtk.Span;
 import org.apache.pivot.wtk.TableView;
-import org.apache.pivot.wtk.TableViewHeader;
-import org.apache.pivot.wtk.TableViewHeaderPressListener;
 import org.apache.pivot.wtk.TableViewSelectionListener;
 import org.apache.pivot.wtk.TextInput;
 import org.apache.pivot.wtk.TextInputTextListener;
@@ -321,36 +319,6 @@
     }
 
     /**
-     * File sort handler.
-     */
-    public static class SortHandler implements TableViewHeaderPressListener {
-        @Override
-        @SuppressWarnings("unchecked")
-        public void headerPressed(TableViewHeader tableViewHeader, int index) {
-            TableView tableView = tableViewHeader.getTableView();
-            TableView.ColumnSequence columns = tableView.getColumns();
-            TableView.Column column = columns.get(index);
-
-            SortDirection sortDirection = column.getSortDirection();
-
-            if (sortDirection == null
-                || sortDirection == SortDirection.DESCENDING) {
-                sortDirection = SortDirection.ASCENDING;
-            } else {
-                sortDirection = SortDirection.DESCENDING;
-            }
-
-            List<File> files = (List<File>)tableView.getTableData();
-            files.setComparator(new FileComparator(column.getName(), sortDirection));
-
-            for (int i = 0, n = columns.getLength(); i < n; i++) {
-                column = columns.get(i);
-                column.setSortDirection(i == index ? sortDirection : null);
-            }
-        }
-    }
-
-    /**
      * File name filter.
      */
     public static class FileNameFilter implements Filter<File> {

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewHeaderSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewHeaderSkin.java?rev=816324&r1=816323&r2=816324&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewHeaderSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewHeaderSkin.java Thu Sep 17 18:37:54 2009
@@ -25,6 +25,7 @@
 import java.awt.geom.GeneralPath;
 
 import org.apache.pivot.collections.ArrayList;
+import org.apache.pivot.collections.Dictionary;
 import org.apache.pivot.collections.Sequence;
 import org.apache.pivot.wtk.Bounds;
 import org.apache.pivot.wtk.Component;
@@ -38,17 +39,18 @@
 import org.apache.pivot.wtk.TableViewColumnListener;
 import org.apache.pivot.wtk.TableViewHeader;
 import org.apache.pivot.wtk.TableViewHeaderListener;
+import org.apache.pivot.wtk.TableViewSortListener;
 import org.apache.pivot.wtk.Theme;
 import org.apache.pivot.wtk.TableViewHeader.SortMode;
 import org.apache.pivot.wtk.media.Image;
 import org.apache.pivot.wtk.skin.ComponentSkin;
 
-
 /**
  * Table view header skin.
  */
 public class TerraTableViewHeaderSkin extends ComponentSkin
-    implements TableViewHeader.Skin, TableViewHeaderListener, TableViewColumnListener {
+    implements TableViewHeader.Skin, TableViewHeaderListener, TableViewColumnListener,
+        TableViewSortListener {
     private class SortIndicatorImage extends Image {
         private SortDirection sortDirection = null;
 
@@ -156,6 +158,7 @@
         TableView tableView = tableViewHeader.getTableView();
         if (tableView != null) {
             tableView.getTableViewColumnListeners().add(this);
+            tableView.getTableViewSortListeners().add(this);
         }
     }
 
@@ -167,6 +170,7 @@
         TableView tableView = tableViewHeader.getTableView();
         if (tableView != null) {
             tableView.getTableViewColumnListeners().remove(this);
+            tableView.getTableViewSortListeners().remove(this);
         }
 
         super.uninstall();
@@ -308,7 +312,8 @@
 
                 // Draw the sort image
                 Image sortImage = null;
-                SortDirection sortDirection = column.getSortDirection();
+                String columnName = column.getName();
+                SortDirection sortDirection = tableView.getSort().get(columnName);
 
                 if (sortDirection != null) {
                     switch (sortDirection) {
@@ -762,11 +767,13 @@
         TableView previousTableView) {
         if (previousTableView != null) {
             previousTableView.getTableViewColumnListeners().remove(this);
+            previousTableView.getTableViewSortListeners().remove(this);
         }
 
         TableView tableView = tableViewHeader.getTableView();
         if (tableView != null) {
             tableView.getTableViewColumnListeners().add(this);
+            tableView.getTableViewSortListeners().add(this);
         }
 
         invalidateComponent();
@@ -810,11 +817,6 @@
     }
 
     @Override
-    public void columnSortDirectionChanged(TableView.Column column, SortDirection previousSortDirection) {
-        repaintComponent();
-    }
-
-    @Override
     public void columnFilterChanged(TableView.Column column, Object previousFilter) {
         // No-op
     }
@@ -823,4 +825,24 @@
     public void columnCellRendererChanged(TableView.Column column, TableView.CellRenderer previousCellRenderer) {
         // No-op
     }
+
+    // Table view sort events
+    public void sortAdded(TableView tableView, String columnName) {
+        repaintComponent();
+    }
+
+    public void sortUpdated(TableView tableView, String columnName,
+        SortDirection previousSortDirection) {
+        repaintComponent();
+    }
+
+    public void sortRemoved(TableView tableView, String columnName,
+        SortDirection sortDirection) {
+        repaintComponent();
+    }
+
+    public void sortChanged(TableView tableView,
+        Sequence<Dictionary.Pair<String, SortDirection>> previousSort) {
+        repaintComponent();
+    }
 }

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewSkin.java?rev=816324&r1=816323&r2=816324&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewSkin.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/TerraTableViewSkin.java Thu Sep 17 18:37:54 2009
@@ -300,7 +300,9 @@
                 TableView.Column column = columns.get(columnIndex);
                 int columnWidth = getColumnWidth(columnIndex);
 
-                if (column.getSortDirection() != null) {
+                String columnName = column.getName();
+                SortDirection sortDirection = tableView.getSort().get(columnName);
+                if (sortDirection != null) {
                     graphics.fillRect(columnX, 0, columnWidth, height);
                 }
 
@@ -1095,13 +1097,6 @@
     }
 
     @Override
-    public void columnSortDirectionChanged(TableView.Column column, SortDirection previousSortDirection) {
-        TableView tableView = column.getTableView();
-        int columnIndex = tableView.getColumns().indexOf(column);
-        repaintComponent(getColumnBounds(columnIndex));
-    }
-
-    @Override
     public void columnFilterChanged(TableView.Column column, Object previousFilter) {
         // No-op
     }

Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/terra_file_browser_skin.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/skin/terra/terra_file_browser_skin.wtkx?rev=816324&r1=816323&r2=816324&view=diff
==============================================================================
Binary files - no diff available.