processtreerowscallback.java

来自「exTreme taglib的使用」· Java 代码 · 共 100 行

JAVA
100
字号
/* * Copyright 2004 original author or authors. * * Licensed 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.extremecomponents.table.callback;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.Iterator;import java.util.List;import org.apache.commons.beanutils.BeanComparator;import org.apache.commons.collections.CollectionUtils;import org.apache.commons.collections.comparators.NullComparator;import org.apache.commons.collections.comparators.ReverseComparator;import org.extremecomponents.table.bean.TreeNode;import org.extremecomponents.table.core.BaseModel;import org.extremecomponents.table.core.TableConstants;import org.extremecomponents.table.core.TreeModelUtils;import org.extremecomponents.table.limit.Sort;/** * @author phorn */public final class ProcessTreeRowsCallback implements FilterRowsCallback, SortRowsCallback {    public Collection filterRows(BaseModel model, Collection rows)            throws Exception {        List results = new ArrayList();        FilterPredicate filterPredicate = new FilterPredicate(model);        CollectionUtils.select(rows, filterPredicate, results);        // Add back the parents        for (int i = 0; i < results.size(); i++) {            Object bean = results.get(i);            TreeModelUtils.findBeanParents(model, rows, results, bean);        }        return results;    }    public Collection sortRows(BaseModel model, Collection rows)            throws Exception {        List parents = new ArrayList();        for (Iterator iter = rows.iterator(); iter.hasNext();) {            TreeNode node = (TreeNode) iter.next();            if (node.getParent() == null)                parents.add(node);        }        List output = new ArrayList();        Sort sort = model.getLimit().getSort();        String property = sort.getProperty();        String sortOrder = sort.getSortOrder();        subSort(parents, property, sortOrder); // First sort the parents        recursiveSort(output, parents, property, sortOrder);        output.retainAll(rows); // Only keep the original nodes        rows.clear();        rows.addAll(output);        return rows;    }    private void recursiveSort(List output, List rows, String property, String sortOrder) {        for (Iterator iter = rows.iterator(); iter.hasNext();) {            TreeNode node = (TreeNode) iter.next();            output.add(node);            if (node.getChildren() != null && node.getChildren().size() > 0) {                subSort(node.getChildren(), property, sortOrder);                recursiveSort(output, node.getChildren(), property, sortOrder);            }        }    }    private void subSort(List rows, String property, String sortOrder) {        if (sortOrder.equals(TableConstants.SORT_ASC)) {            BeanComparator comparator = new BeanComparator(property, new NullComparator());            Collections.sort(rows, comparator);        } else if (sortOrder.equals(TableConstants.SORT_DESC)) {            BeanComparator reversedNaturalOrderBeanComparator = new BeanComparator(property, new ReverseComparator(                    new NullComparator()));            Collections.sort(rows, reversedNaturalOrderBeanComparator);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?