示例#1
0
文件: Listheader.java 项目: apextw/zk
  /**/ boolean doSort(boolean ascending) {
    final Comparator cmpr = ascending ? _sortAsc : _sortDsc;
    if (cmpr == null) return false;

    final Listbox box = getListbox();
    if (box == null) return false;

    // comparator might be zscript
    Scopes.beforeInterpret(this);
    try {
      final ListModel model = box.getModel();
      boolean isPagingMold = box.inPagingMold();
      int activePg = isPagingMold ? box.getPaginal().getActivePage() : 0;
      if (model != null) { // live data
        if (model instanceof GroupsSortableModel) {
          sortGroupsModel(box, (GroupsSortableModel) model, cmpr, ascending);
        } else {
          if (!(model instanceof Sortable))
            throw new UiException(
                GroupsSortableModel.class
                    + " or "
                    + Sortable.class
                    + " must be implemented in "
                    + model.getClass().getName());
          sortListModel((Sortable) model, cmpr, ascending);
        }
      } else { // not live data
        sort0(box, cmpr);
      }
      if (isPagingMold) box.getPaginal().setActivePage(activePg);
      // Because of maintaining the number of the visible item, we cause
      // the wrong active page when dynamically add/remove the item (i.e. sorting).
      // Therefore, we have to reset the correct active page.
    } finally {
      Scopes.afterInterpret();
    }

    _ignoreSort = true;
    // maintain
    for (Iterator it = box.getListhead().getChildren().iterator(); it.hasNext(); ) {
      final Listheader hd = (Listheader) it.next();
      hd.setSortDirection(hd != this ? "natural" : ascending ? "ascending" : "descending");
    }
    _ignoreSort = false;

    // sometimes the items at client side are out of date
    box.invalidate();

    return true;
  }
示例#2
0
文件: Listheader.java 项目: apextw/zk
  /**
   * Ungroups and sorts the items ({@link Listitem}) based on the ascending. If the corresponding
   * comparator is not set, it returns false and does nothing.
   *
   * @param ascending whether to use {@link #getSortAscending}. If the corresponding comparator is
   *     not set, it returns false and does nothing.
   * @since 6.5.0
   */
  public void ungroup(boolean ascending) {
    final Comparator<?> cmpr = ascending ? _sortAsc : _sortDsc;
    if (cmpr != null) {

      final Listbox listbox = getListbox();
      if (listbox.getModel() == null) {

        // comparator might be zscript
        Scopes.beforeInterpret(this);
        try {
          final List<Listitem> items = listbox.getItems();
          if (listbox.hasGroup()) {
            for (Listgroup group : new ArrayList<Listgroup>(listbox.getGroups()))
              group.detach(); // Listgroupfoot is removed
            // automatically, if any.
          }

          Comparator<?> cmprx;
          if (cmpr instanceof GroupComparator) {
            cmprx = new GroupToComparator((GroupComparator) cmpr);
          } else {
            cmprx = cmpr;
          }

          final List<Listitem> children = new LinkedList<Listitem>(items);
          items.clear();
          sortCollection(children, cmprx);
          for (Component c : children) listbox.appendChild(c);
        } finally {
          Scopes.afterInterpret();
        }
      }
      fixDirection(listbox, ascending);

      // sometimes the items at client side are out of date
      listbox.invalidate();
    }
  }
示例#3
0
文件: Listheader.java 项目: apextw/zk
  /**
   * Groups and sorts the items ({@link Listitem}) based on {@link #getSortAscending}. If the
   * corresponding comparator is not set, it returns false and does nothing.
   *
   * @param ascending whether to use {@link #getSortAscending}. If the corresponding comparator is
   *     not set, it returns false and does nothing.
   * @return whether the rows are grouped.
   * @since 6.5.0
   */
  public boolean group(boolean ascending) {
    final String dir = getSortDirection();
    if (ascending) {
      if ("ascending".equals(dir)) return false;
    } else {
      if ("descending".equals(dir)) return false;
    }
    final Comparator<?> cmpr = ascending ? _sortAsc : _sortDsc;
    if (cmpr == null) return false;

    final Listbox listbox = getListbox();
    if (listbox == null) return false;

    // comparator might be zscript
    Scopes.beforeInterpret(this);
    try {
      final ListModel model = listbox.getModel();
      int index = listbox.getListhead().getChildren().indexOf(this);
      if (model != null) { // live data
        if (!(model instanceof GroupsSortableModel))
          throw new UiException(
              GroupsSortableModel.class + " must be implemented in " + model.getClass().getName());
        groupGroupsModel((GroupsSortableModel) model, cmpr, ascending, index);
      } else { // not live data
        final List<Listitem> items = listbox.getItems();
        if (items.isEmpty()) return false; // Avoid listbox with null group	
        if (listbox.hasGroup()) {
          for (Listgroup group : new ArrayList<Listgroup>(listbox.getGroups()))
            group.detach(); // Groupfoot is removed automatically, if any.
        }

        Comparator<?> cmprx;
        if (cmpr instanceof GroupComparator) {
          cmprx = new GroupToComparator((GroupComparator) cmpr);
        } else {
          cmprx = cmpr;
        }

        final List<Listitem> children = new LinkedList<Listitem>(items);
        items.clear();
        sortCollection(children, cmprx);

        Listitem previous = null;
        for (Listitem item : children) {
          if (previous == null || compare(cmprx, previous, item) != 0) {
            // new group
            final List<Listcell> cells = item.getChildren();
            if (cells.size() < index)
              throw new IndexOutOfBoundsException("Index: " + index + " but size: " + cells.size());
            Listgroup group;
            Listcell cell = cells.get(index);
            if (cell.getLabel() != null) {
              group = new Listgroup(cell.getLabel());
            } else {
              Component cc = cell.getFirstChild();
              if (cc instanceof Label) {
                String val = ((Label) cc).getValue();
                group = new Listgroup(val);
              } else {
                group = new Listgroup(Messages.get(MZul.GRID_OTHER));
              }
            }
            listbox.appendChild(group);
          }
          listbox.appendChild(item);
          previous = item;
        }

        if (cmprx != cmpr) sort0(listbox, cmpr); // need to sort each group
      }
    } finally {
      Scopes.afterInterpret();
    }

    fixDirection(listbox, ascending);

    // sometimes the items at client side are out of date
    listbox.invalidate();

    return true;
  }