Ejemplo n.º 1
0
 /** Sorts the items. If with group, each group is sorted independently. */
 @SuppressWarnings("unchecked")
 private static void sort0(Listbox box, Comparator cmpr) {
   if (box.hasGroup())
     for (Listgroup g : box.getGroups()) {
       int index = g.getIndex() + 1;
       Components.sort(box.getItems(), index, index + g.getItemCount(), cmpr);
     }
   else Components.sort(box.getItems(), cmpr);
 }
Ejemplo n.º 2
0
 /* returns the value of the first item in the listbox, if any */
 private String getFirstListboxItem() {
   Option[] items = (Option[]) lbxItems.getItems();
   if ((items != null) && items.length > 0) {
     Option item = items[0];
     if (item != null) return (String) item.getValue();
   }
   return null;
 }
Ejemplo n.º 3
0
  /**
   * 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();
    }
  }
Ejemplo n.º 4
0
  /**
   * 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;
  }