⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treemodelutils.java

📁 分页查询控件 分页查询控件
💻 JAVA
字号:
/* * 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.tree.core;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.PropertyUtils;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.extremecomponents.table.core.ParameterRegistry;import org.extremecomponents.tree.bean.TreeNode;/** * org.extremecomponents.tree.model.TreeModelUtils.java - *  * @author phorn */public class TreeModelUtils {    private static Log logger = LogFactory.getLog(TreeModelUtils.class);    /**     * Recursively loads the tree structure into the treeList attribute and sets     * the treeList as the collection to use.     *      * @param model     * @param inputList     * @throws Exception     */    public static List loadTreeStructure(TreeModel model, Collection inputList)            throws Exception {        List result = new ArrayList();        List searchList = new ArrayList();        searchList.addAll(inputList);        for (Iterator iter = searchList.iterator(); iter.hasNext();) {            Object bean = iter.next();            Object parentId = BeanUtils.getProperty(bean, model.getParentAttribute());            if (parentId == null || StringUtils.isBlank(parentId + "")) {                //Load up the top level parents                TreeNode node = new TreeNode(bean, getBeanId(model, bean), 0);                result.add(node);                iter.remove();                loadChildren(model, result, searchList, node, 0);            }        }        return result;    }    /**     * @param model     * @param displayList     * @param searchList     * @param node     * @param currentDepth     * @throws Exception     */    public static void loadChildren(TreeModel model, List displayList, List searchList, TreeNode node, int currentDepth)            throws Exception {        currentDepth++;        List subList = new ArrayList();        subList.addAll(searchList);        Object id = node.getIdentifier();        String key = getNodeKey(model, id);        if (model.getOpenNodes().get(key) != null) {            node.setOpen(true);        } else {            node.setOpen(false);        }        for (Iterator iter = subList.iterator(); iter.hasNext();) {            Object bean = iter.next();            if (nodeIsBeanParent(model, node, bean)) {                TreeNode childNode = new TreeNode(bean, getBeanId(model, bean), currentDepth);                node.addChild(childNode);                childNode.setParent(node);                iter.remove();                if (isOpen(model, node, true)) {                    displayList.add(childNode);                }                loadChildren(model, displayList, subList, childNode, currentDepth); //Recurse            }        }    }    /**     * Used by the filter function to add back the filtered out parents.     *      * @param model     * @param searchList     * @return     * @throws Exception     */    public static Collection findParents(TreeModel model, Collection searchList)            throws Exception {        logger.debug("TreeModel.findParents()");        List result = new ArrayList();        for (Iterator iter = searchList.iterator(); iter.hasNext();) {            Object bean = iter.next();            if (!result.contains(bean)) {                result.add(bean);            }            findBeanParents(model, searchList, result, bean);        }        return result;    }    /**     * Recursive call used by the filter function to add back the filtered out     * parents.     *      * @param model     * @param parents     * @param bean     * @throws Exception     */    public static void findBeanParents(TreeModel model, Collection searchList, Collection parents, Object bean)            throws Exception {        Object parent = null;                if (bean instanceof Map) {            parent = ((Map) bean).get(model.getParentAttribute());        } else {            parent = PropertyUtils.getProperty(bean, model.getParentAttribute());        }        if (parent == null) {            return;        } else {            Object parentBean = findByIdentifierOrReference(model, (List) searchList, parent);                        if (parentBean == null) {                //Not found - we shouldn't ever get here                return;            }            if (!parents.contains(parentBean)) {                //Already added so do nothing                parents.add(parentBean);            }            findBeanParents(model, searchList, parents, parentBean);        }    }        public static Object findByIdentifierOrReference(TreeModel model, List searchList, Object bean) {        if (searchList.contains(bean)) {            return bean; //Not the identifier but the actual bean reference        }        for (int i = 0; i < searchList.size(); i++) {            Object row = searchList.get(i);            try {                if (bean.equals(BeanUtils.getProperty(row, model.getIdentifier()))) {                    return row;                }            } catch (IllegalAccessException e) {                e.printStackTrace();            } catch (InvocationTargetException e) {                e.printStackTrace();            } catch (NoSuchMethodException e) {                e.printStackTrace();            }        }        return null;    }    /**     * Used by the filter function to add back the filtered out children.     *      * @param model     * @param displayList     * @throws Exception     */    public static void addClosedChildren(TreeModel model, Collection displayList)            throws Exception {        logger.debug("TreeModel.addClosedChildren()");        List parents = new ArrayList();        parents.addAll(displayList);        for (Iterator iter = parents.iterator(); iter.hasNext();) {            TreeNode node = (TreeNode) iter.next();            if ((node.getChildren() == null) || (node.getChildren().size() == 0)) {                addChildren(model, displayList, node);            }        }    }    /**     * Recursive call used by the filter function to add back the filtered out     * children but keeps them collapsed unless they have been openned manually     * be the open parameter.     *      * @param model     * @param displayList     * @param node     * @throws Exception     */    public static void addChildren(TreeModel model, Collection displayList, TreeNode node)            throws Exception {        for (Iterator iter = model.getTableHandler().getCollectionOfBeans().iterator(); iter.hasNext();) {            Object bean = iter.next();            if (nodeIsBeanParent(model, node, bean)) {                TreeNode childNode = new TreeNode(bean, getBeanId(model, bean), node.getDepth() + 1);                node.addChild(childNode);                Object id = node.getIdentifier();                String openParam = (String) model.getOpenNodes().get(getNodeKey(model, id));                if (isOpen(model, node, false) || "true".equals(openParam)) {                    node.setOpen(true);                    int parentPosition = ((List) displayList).indexOf(node);                    ((List) displayList).add(parentPosition + 1, childNode);                }                addChildren(model, displayList, childNode);            }        }    }    /**     * @param model     * @param node     * @param bean     * @return     * @throws Exception     */    public static boolean nodeIsBeanParent(TreeModel model, TreeNode node, Object bean)            throws Exception {        Object parent = null;                if (bean instanceof Map) {            parent = ((Map) bean).get(model.getParentAttribute());        } else {            parent = PropertyUtils.getProperty(bean, model.getParentAttribute());        }                if (parent != null) logger.debug("parent instanceof " + parent.getClass().getName());        if (parent == null || StringUtils.isBlank(parent + "")) {            return false;        }        Object nodeId = node.getIdentifier();        Object parentId = getBeanId(model, parent);                if (node.getBean().equals(parent)) return true;        if (nodeId.equals(parentId)) return true;                return false;//        return (node.getBean().equals(parent) || nodeId.equals(parentId));    }    /**     * @param model     * @param bean     * @return     * @throws Exception     */    public static Object getBeanId(TreeModel model, Object bean)            throws Exception {        try {            if (bean instanceof Map) {                return ((Map) bean).get(model.getIdentifier());            } else {                return PropertyUtils.getProperty(bean, model.getIdentifier());            }        } catch (NoSuchMethodException e) {            return bean; //This actually is the parent id and not the paren bean         }    }    /**     * Find out if the node and all parents are open.     *      * @param model     * @param node     * @param filterControlled     * @return     */    public static boolean isOpen(TreeModel model, TreeNode node, boolean filterControlled) {        if (filterControlled && model.getFilterHandler().doFilter() && !model.getFilterHandler().doClear()) {            node.setOpen(true);            return true;        }        if (!node.isOpen()) {            return false;        }        if (node.getParent() == null) {            return true;        }        return isOpen(model, node.getParent(), filterControlled);    }    /**     * @param model     * @param id     *            value of the node.     * @return Returns the parameter key used to indicate this node is open.     */    public static String getNodeKey(TreeModel model, Object id) {        return model.getTableHandler().prefixWithCollection() + ParameterRegistry.OPEN + id;    }}

⌨️ 快捷键说明

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