Revision history [back]

click to hide/show revision 1
initial version

answered 2013-07-05 05:59:58 +0800

benbai gravatar image benbai

http://www.zkoss.org

For 30000 records is okay I think, you can also try implement ListSubModel by your self to get live data from DB directly instead of put all data in memory (trade off: probably increase the processing time).

e.g., the updated TestVM.java, keep only selection in memory

package test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.ListSubModel;
import org.zkoss.zul.event.ListDataListener;
import org.zkoss.zul.ext.Selectable;

/**
 * tested with ZK 6.5.2
 * 
 * @author benbai
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TestVM {

    public ListModel getModel () {
        return new CustomSubModel(false);
    }

    public static class CustomSubModel<E> implements ListModel<E>, ListSubModel<E>, 
        Selectable<E>, java.io.Serializable {
        private boolean _multiple;
        private Set _selection = new HashSet();
        private ListModelList<E> _model = new ListModelList(new ArrayList(), true);;

        public CustomSubModel (boolean multiple) {
            _multiple = multiple;
        }
        public ListModel getSubModel(java.lang.Object value, int nRows) {
            Set oldSelection = null;
            Set newSelection = new HashSet();
            List datas = new ArrayList();
            if (value != null && !value.toString().isEmpty()) {
                // dynamically create data
                // assume fetch data from DB here
                for (int i = 0; i < 5; i++) {
                    datas.add(new Data(value.toString() + "__" + i, value.toString() + "Desc__" + i));
                }
            }
            if (_model != null) {
                // join current selection
                oldSelection = _model.getSelection();
                _selection.addAll(oldSelection);
            }

            for (Object o : _selection) {
                Data d = (Data)o;
                if (datas.contains(o)) {
                    newSelection.add(o);
                }
            }
            final Selectable model =  new ListModelList(datas, true);
            model.setMultiple(_multiple);
            model.setSelection(newSelection);
            _model = (ListModelList)model;
            return (ListModel)model;
        }
        public E getElementAt(int index) {
            return _model.getElementAt(index);
        }

        public int getSize() {
            return _model.getSize();
        }

        public void addListDataListener(ListDataListener l) {
            _model.addListDataListener(l);
        }

        public void removeListDataListener(ListDataListener l) {
            _model.removeListDataListener(l);
        }

        @SuppressWarnings("unchecked")
        private Selectable<E> getSelectModel() {
            return (Selectable<E>) _model;
        }

        public Set<E> getSelection() {
            return getSelectModel().getSelection();
        }


        public void setSelection(Collection<? extends E> selection) {
            getSelectModel().setSelection(selection);
        }


        public boolean isSelected(Object obj) {
            return getSelectModel().isSelected(obj);
        }


        public boolean isSelectionEmpty() {
            return getSelectModel().isSelectionEmpty();
        }


        public boolean addToSelection(E obj) {
            return getSelectModel().addToSelection(obj);
        }


        public boolean removeFromSelection(Object obj) {
            return getSelectModel().removeFromSelection(obj);
        }


        public void clearSelection() {
            getSelectModel().clearSelection();

        }


        public void setMultiple(boolean multiple) {
            getSelectModel().setMultiple(multiple);

        }


        public boolean isMultiple() {
            return getSelectModel().isMultiple();
        }
    }
    public static class Data {
        String _label;
        String _desc;
        public Data (String label, String desc) {
            _label = label;
            _desc = desc;
        }
        public String getLabel () {
            return _label;
        }
        public String getDesc () {
            return _desc;
        }
        public boolean equals (Object o) {
            if (o != null && (o instanceof Data)) {
                Data d = (Data)o;
                return d.getLabel().equals(_label)
                    && d.getDesc().equals(_desc);
            }
            return false;
        }
    }
}

For 30000 records is okay I think, you can also try implement ListSubModel by your self to get live data from DB directly instead of put all data in memory (trade off: probably increase the processing time).time). Or you can try keep a static list as the data provider and fetch data from it dynamically.

e.g., the updated TestVM.java, keep only selection in memory

package test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.ListSubModel;
import org.zkoss.zul.event.ListDataListener;
import org.zkoss.zul.ext.Selectable;

/**
 * tested with ZK 6.5.2
 * 
 * @author benbai
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TestVM {

    public ListModel getModel () {
        return new CustomSubModel(false);
    }

    public static class CustomSubModel<E> implements ListModel<E>, ListSubModel<E>, 
        Selectable<E>, java.io.Serializable {
        private boolean _multiple;
        private Set _selection = new HashSet();
        private ListModelList<E> _model = new ListModelList(new ArrayList(), true);;

        public CustomSubModel (boolean multiple) {
            _multiple = multiple;
        }
        public ListModel getSubModel(java.lang.Object value, int nRows) {
            Set oldSelection = null;
            Set newSelection = new HashSet();
            List datas = new ArrayList();
            if (value != null && !value.toString().isEmpty()) {
                // dynamically create data
                // assume fetch data from DB here
                for (int i = 0; i < 5; i++) {
                    datas.add(new Data(value.toString() + "__" + i, value.toString() + "Desc__" + i));
                }
            }
            if (_model != null) {
                // join current selection
                oldSelection = _model.getSelection();
                _selection.addAll(oldSelection);
            }

            for (Object o : _selection) {
                Data d = (Data)o;
                if (datas.contains(o)) {
                    newSelection.add(o);
                }
            }
            final Selectable model =  new ListModelList(datas, true);
            model.setMultiple(_multiple);
            model.setSelection(newSelection);
            _model = (ListModelList)model;
            return (ListModel)model;
        }
        public E getElementAt(int index) {
            return _model.getElementAt(index);
        }

        public int getSize() {
            return _model.getSize();
        }

        public void addListDataListener(ListDataListener l) {
            _model.addListDataListener(l);
        }

        public void removeListDataListener(ListDataListener l) {
            _model.removeListDataListener(l);
        }

        @SuppressWarnings("unchecked")
        private Selectable<E> getSelectModel() {
            return (Selectable<E>) _model;
        }

        public Set<E> getSelection() {
            return getSelectModel().getSelection();
        }


        public void setSelection(Collection<? extends E> selection) {
            getSelectModel().setSelection(selection);
        }


        public boolean isSelected(Object obj) {
            return getSelectModel().isSelected(obj);
        }


        public boolean isSelectionEmpty() {
            return getSelectModel().isSelectionEmpty();
        }


        public boolean addToSelection(E obj) {
            return getSelectModel().addToSelection(obj);
        }


        public boolean removeFromSelection(Object obj) {
            return getSelectModel().removeFromSelection(obj);
        }


        public void clearSelection() {
            getSelectModel().clearSelection();

        }


        public void setMultiple(boolean multiple) {
            getSelectModel().setMultiple(multiple);

        }


        public boolean isMultiple() {
            return getSelectModel().isMultiple();
        }
    }
    public static class Data {
        String _label;
        String _desc;
        public Data (String label, String desc) {
            _label = label;
            _desc = desc;
        }
        public String getLabel () {
            return _label;
        }
        public String getDesc () {
            return _desc;
        }
        public boolean equals (Object o) {
            if (o != null && (o instanceof Data)) {
                Data d = (Data)o;
                return d.getLabel().equals(_label)
                    && d.getDesc().equals(_desc);
            }
            return false;
        }
    }
}

For 30000 records is okay I think, you can also try implement ListSubModel by your self to get live data from DB directly instead of put all data in memory (trade off: probably increase the processing time). Or you can try keep a static list as the data provider and fetch data from it dynamically.

e.g., the updated TestVM.java, keep only selection in memory

package test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.ListSubModel;
import org.zkoss.zul.event.ListDataListener;
import org.zkoss.zul.ext.Selectable;

/**
 * tested with ZK 6.5.2
 * 
 * @author benbai
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class TestVM {

    public ListModel getModel () {
        return new CustomSubModel(false);
    }

    public static class CustomSubModel<E> implements ListModel<E>, ListSubModel<E>, 
        Selectable<E>, java.io.Serializable {
        private boolean _multiple;
        private Set _selection = new HashSet();
        private ListModelList<E> _model = new ListModelList(new ArrayList(), true);;

        public CustomSubModel (boolean multiple) {
            _multiple = multiple;
        }
        public ListModel getSubModel(java.lang.Object value, int nRows) {
            Set oldSelection = null;
            Set newSelection = new HashSet();
            List datas = new ArrayList();
            if (value != null && !value.toString().isEmpty()) {
                // dynamically create data
                // assume fetch data from DB here
                for (int i = 0; i < 5; i++) {
                    datas.add(new Data(value.toString() + "__" + i, value.toString() + "Desc__" + i));
                }
            }
            if (_model != null) {
                // join current selection
                oldSelection = _model.getSelection();
                _selection.addAll(oldSelection);
            }

            for (Object o : _selection) {
                Data d = (Data)o;
                if (datas.contains(o)) {
                    newSelection.add(o);
                }
            }
            final Selectable model =  new ListModelList(datas, true);
            model.setMultiple(_multiple);
            model.setSelection(newSelection);
            _model = (ListModelList)model;
            return (ListModel)model;
        }
        public E getElementAt(int index) {
            return _model.getElementAt(index);
        }

        public int getSize() {
            return _model.getSize();
        }

        public void addListDataListener(ListDataListener l) {
            _model.addListDataListener(l);
        }

        public void removeListDataListener(ListDataListener l) {
            _model.removeListDataListener(l);
        }

        @SuppressWarnings("unchecked")
        private Selectable<E> getSelectModel() {
            return (Selectable<E>) _model;
        }

        public Set<E> getSelection() {
            return getSelectModel().getSelection();
        }


        public void setSelection(Collection<? extends E> selection) {
            getSelectModel().setSelection(selection);
        }


        public boolean isSelected(Object obj) {
            return getSelectModel().isSelected(obj);
        }


        public boolean isSelectionEmpty() {
            return getSelectModel().isSelectionEmpty();
        }


        public boolean addToSelection(E obj) {
            return getSelectModel().addToSelection(obj);
        }


        public boolean removeFromSelection(Object obj) {
            return getSelectModel().removeFromSelection(obj);
        }


        public void clearSelection() {
            getSelectModel().clearSelection();

        }


        public void setMultiple(boolean multiple) {
            getSelectModel().setMultiple(multiple);

        }


        public boolean isMultiple() {
            return getSelectModel().isMultiple();
        }
    }
    public static class Data {
        String _label;
        String _desc;
        public Data (String label, String desc) {
            _label = label;
            _desc = desc;
        }
        public String getLabel () {
            return _label;
        }
        public String getDesc () {
            return _desc;
        }
        public boolean equals (Object o) {
            if (o != null && (o instanceof Data)) {
                Data d = (Data)o;
                return d.getLabel().equals(_label)
                    && d.getDesc().equals(_desc);
            }
            return false;
        }
    }
}

For more information, please refer to the code:

ListModels.java

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More