Uploaded image for project: 'ZK'
  1. ZK
  2. ZK-266

Sort call to model not receiving the right parameters

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Normal
    • Resolution: Fixed
    • Affects Version/s: 5.0.7
    • Fix Version/s: 5.0.8
    • Component/s: None
    • Labels:
      None

      Description

      original thread
      https://sourceforge.net/tracker/?func=detail&aid=3344647&group_id=152762&atid=785191

      We have a Grid that has a ListModelExt attached. We have implemented/overridden the method: sort(Comparator c, boolean ascending)
      The columns of our grid has custom Ascending and Descending comparators.

      When a user clicks on a column (not using the column-context menu) everything works fine.
      The first click calls sort(ASC_comparator, true),
      and the next calls sort(DESC_comparator, false), and so on

      However, if the user uses the column context-menu and chooses "Sort Descending", the call to the ListModelExt receives the Ascending sort comparator, instead of the Descending one.

      I discovered that this only happens if the column's sort-direction is "natural". If the column sort-direction has been explicitly set to "ascending" (even though not ever sorted), then it passes in the Descending Comparator

        Activity

        Hide
        benbai benbai added a comment -

        Date: 2011-07-11 16:05:22 CST
        Sender: whkuoattaiwan

        Hide
        I tested with following sample code, and can't reproduce the problem.
        I always get the correct comparator.
        As System.out.println(cmpr); printed, it's correct comparator

        zul:

        <zk>
        <window >
        <zscript><![CDATA[
        ListModel strset = new org.zkoss.zktest.test2.grid.FakeListModel(10);
        class MyRowComparator implements Comparator {
        private boolean _asc;
        public MyRowComparator(boolean ascending) {
        _asc = ascending;
        }
        public int compare(Object o1, Object o2) {
        Row r1 = (Row)o1, r2 = (Row)o2;
        int v = ((Comparable)r1.getValue()).compareTo(r2.getValue());
        return _asc ? v: -v;
        }
        public String toString()

        { return "MyRowComparator [_asc=" + _asc +"]"; } }

        Comparator asc = new MyRowComparator(true);
        Comparator dsc = new MyRowComparator(false);
        ]]>
        </zscript>

        <grid id="grid" width="200px" model="$

        {strset}

        ">
        <columns menupopup="auto">
        <column label="options" sort="auto" sortAscending="$

        {asc}

        "
        sortDescending="$

        {dsc}

        "/>
        </columns>
        </grid>

        </window>
        </zk>

        java:

        /* MainWindow.java

        {{IS_NOTE
        Purpose:

        Description:

        History:
        Oct ,31st 2007, Created by Jeff Liu
        }}IS_NOTE

        Copyright (C) 2007 Potix Corporation. All Rights Reserved.

        {{IS_RIGHT
        This program is distributed under GPL Version 3.0 in the hope that
        it will be useful, but WITHOUT ANY WARRANTY.
        }}IS_RIGHT
        */
        package org.zkoss.zktest.test2.grid;

        import java.util.Comparator;

        import org.zkoss.zul.AbstractListModel;
        import org.zkoss.zul.ListModelExt;
        import org.zkoss.zul.event.ListDataEvent;
        /**

        • @author Jeff
          *
          */
          public class FakeListModel extends AbstractListModel implements
          ListModelExt {

        private int _size;
        private boolean _asc = true;

        public FakeListModel() {
        this(10000);
        }
        public FakeListModel(int size) {
        _size = size;
        }

        // ListModelExt
        public void sort(Comparator cmpr, boolean asc) {
        // System.out.println("==================SORT DATA================");
        System.out.println(cmpr);
        _asc = asc;
        invalidate();
        }
        public void invalidate() {
        fireEvent(ListDataEvent.CONTENTS_CHANGED, -1, -1);
        }

        //AbstractListModel
        public Object getElementAt(int v) {
        String value = "Option "+(_asc ? v: _size - v - 1);
        // System.out.println(value);
        return value;
        }

        //AbstractListModel
        public int getSize() {
        return _size;
        }

        //AbstractListModel
        public void setSize(int size){
        _size = size;
        }
        }

        Show
        benbai benbai added a comment - Date: 2011-07-11 16:05:22 CST Sender: whkuoattaiwan Hide I tested with following sample code, and can't reproduce the problem. I always get the correct comparator. As System.out.println(cmpr); printed, it's correct comparator zul: <zk> <window > <zscript><![CDATA[ ListModel strset = new org.zkoss.zktest.test2.grid.FakeListModel(10); class MyRowComparator implements Comparator { private boolean _asc; public MyRowComparator(boolean ascending) { _asc = ascending; } public int compare(Object o1, Object o2) { Row r1 = (Row)o1, r2 = (Row)o2; int v = ((Comparable)r1.getValue()).compareTo(r2.getValue()); return _asc ? v: -v; } public String toString() { return "MyRowComparator [_asc=" + _asc +"]"; } } Comparator asc = new MyRowComparator(true); Comparator dsc = new MyRowComparator(false); ]]> </zscript> <grid id="grid" width="200px" model="$ {strset} "> <columns menupopup="auto"> <column label="options" sort="auto" sortAscending="$ {asc} " sortDescending="$ {dsc} "/> </columns> </grid> </window> </zk> java: /* MainWindow.java {{IS_NOTE Purpose: Description: History: Oct ,31st 2007, Created by Jeff Liu }}IS_NOTE Copyright (C) 2007 Potix Corporation. All Rights Reserved. {{IS_RIGHT This program is distributed under GPL Version 3.0 in the hope that it will be useful, but WITHOUT ANY WARRANTY. }}IS_RIGHT */ package org.zkoss.zktest.test2.grid; import java.util.Comparator; import org.zkoss.zul.AbstractListModel; import org.zkoss.zul.ListModelExt; import org.zkoss.zul.event.ListDataEvent; /** @author Jeff * */ public class FakeListModel extends AbstractListModel implements ListModelExt { private int _size; private boolean _asc = true; public FakeListModel() { this(10000); } public FakeListModel(int size) { _size = size; } // ListModelExt public void sort(Comparator cmpr, boolean asc) { // System.out.println("==================SORT DATA================"); System.out.println(cmpr); _asc = asc; invalidate(); } public void invalidate() { fireEvent(ListDataEvent.CONTENTS_CHANGED, -1, -1); } //AbstractListModel public Object getElementAt(int v) { String value = "Option "+(_asc ? v: _size - v - 1); // System.out.println(value); return value; } //AbstractListModel public int getSize() { return _size; } //AbstractListModel public void setSize(int size){ _size = size; } }
        Hide
        benbai benbai added a comment -

        Date: 2011-08-02 22:20:06 CST
        Sender: dariuscooper

        Hide
        Thanks for the response. I tested your source code, and it actually has the
        same issue. So, maybe I didn't describe what happens.

        If I run your example, and pull up the zul file, I see "Option 0", "Option
        1", sorted ascending. If i click on the header, nothing seems to change (it
        stays sorted ascending). This is as expected. So far, so good. If I click
        again, it sorts descending. This sequence works well.

        Now, refresh or pull up the page again. Do not click on the header.
        Instead, open the up the context-menu for the column and select the "Sort
        Descending" option.

        The sort that is requested is Ascending, even though I'd have expected it
        to be descending.

        Note: The only way to test this is to choose descending the FIRST time,
        before the column has already been sorted one way or the other.

        Thanks

        Show
        benbai benbai added a comment - Date: 2011-08-02 22:20:06 CST Sender: dariuscooper Hide Thanks for the response. I tested your source code, and it actually has the same issue. So, maybe I didn't describe what happens. If I run your example, and pull up the zul file, I see "Option 0", "Option 1", sorted ascending. If i click on the header, nothing seems to change (it stays sorted ascending). This is as expected. So far, so good. If I click again, it sorts descending. This sequence works well. Now, refresh or pull up the page again. Do not click on the header. Instead, open the up the context-menu for the column and select the "Sort Descending" option. The sort that is requested is Ascending, even though I'd have expected it to be descending. Note: The only way to test this is to choose descending the FIRST time , before the column has already been sorted one way or the other. Thanks
        Hide
        SimonPai SimonPai added a comment -

        Fixed since 2011/8/4.

        Show
        SimonPai SimonPai added a comment - Fixed since 2011/8/4.

          People

          • Assignee:
            SimonPai SimonPai
            Reporter:
            benbai benbai
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: