📄 gethierarchytreeaction.java
字号:
/* * CyberTester - J2EE Web application for creating, delivering and managing tests/exams/surveys. * Copyright (C) 2003 CyberDemia Research and Services Pty Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (see the file COPYING); if not, write to the * Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * See the COPYING file located in the top-level-directory of * the archive of this program for complete text of license. */package com.cyberdemia.cybertester.admin;import javax.servlet.http.*;import org.apache.struts.action.*;import com.cyberdemia.cybertester.Constants;import com.cyberdemia.cybertester.CyberTesterUtils;import com.cyberdemia.cybertester.common.TreeNode;import com.cyberdemia.school.*;import com.cyberdemia.user.UserManager;import com.cyberdemia.user.UserManagerHome;import java.util.logging.*;import java.util.*;/** * GetHierarchyTreeAction is a Struts action to render a hierarchy tree. * * @author Alexander Yap */public final class GetHierarchyTreeAction extends AbstractAdminAction{ public static final String EXPAND_NODE_ACTION = "expand"; public static final String COLLAPSE_NODE_ACTION = "collapse"; public static final String SELECT_NODE_ACTION = "select"; public static final String RESTORE_NODE_ACTION = "restore"; protected ActionForward doPerform( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception { Logger logger = CyberTesterUtils.getLogger(); String action = request.getParameter(Constants.ACTION_KEY); IHierarchy rootNode = SchoolConfig.getInstance().getHierarchyRoot(); // The hierId parameter tells this action which node of the tree // to "open", i.e. to request the children for. // If null, this may be a relogin so try to restore the previously // selected leaf-node (if any). String hierId = request.getParameter( "hierId"); if (hierId==null) { UserManagerHome userMgrHome = CyberTesterUtils.getUserManagerHome(); UserManager userMgr = (UserManager)userMgrHome.create(); Integer userId = (Integer)session.getAttribute(Constants.USER_ID_KEY); String[] selectedHierId = userMgr.getResourcesInUser( userId , SELECTED_HIERARCHY_ID_RESOURCE_CATEGORY); // Expect at most one element, but if there is more, just ignore the redundant ones. if ((selectedHierId.length>0) && (selectedHierId[0].length()>0)) { hierId = selectedHierId[0]; action = RESTORE_NODE_ACTION; logger.fine("Restoring previously selected node at "+hierId); } } // If there is no previously selected leaf-node, default hierId to // refer to the root node. if (hierId==null) { hierId = rootNode.getId().toString(); } logger.fine("Resolving node "+hierId); StringTokenizer tokenizer = new StringTokenizer(hierId, IHierarchy.HIERARCHY_ID_SEPARATOR); if (!tokenizer.hasMoreTokens()) throw new NumberFormatException("Hierarchy ID "+hierId+" is invalid."); Integer rootId = new Integer( tokenizer.nextToken() ); if (!rootNode.getId().equals(rootId)) { throw new NumberFormatException("Hierarchy ID "+hierId+" is invalid, wrong root node ID."); } LinkedList treeNodeList = new LinkedList(); // Add the rootNode to the first position. treeNodeList.add(0, new TreeNode(rootNode, false, -1) ); request.setAttribute(Constants.TREE_NODES_KEY, treeNodeList); if (COLLAPSE_NODE_ACTION.equalsIgnoreCase(action)) { openToTreeNode(rootNode, 0, tokenizer, treeNodeList); session.removeAttribute(AdminConstants.SELECTED_HIERARCHY_ID_KEY); } else if (SELECT_NODE_ACTION.equalsIgnoreCase(action)) { openToTreeNode(rootNode, 0, tokenizer, treeNodeList); session.setAttribute(AdminConstants.SELECTED_HIERARCHY_ID_KEY, hierId); // Remember selected node UserManagerHome userMgrHome = CyberTesterUtils.getUserManagerHome(); UserManager userMgr = (UserManager)userMgrHome.create(); Integer userId = (Integer)session.getAttribute(Constants.USER_ID_KEY); userMgr.setSingleResourceToUser(SELECTED_HIERARCHY_ID_RESOURCE_CATEGORY, hierId , userId); } else if (EXPAND_NODE_ACTION.equalsIgnoreCase(action)) { // We need to build a list of nodes to render the tree. // The tree must be expanded only down the path to the hierNode. // hierNode itself must be expanded, as well as all its direct ancestors. // Expand its children, all the way down to the referenced node int attachPos = openToTreeNode(rootNode, 0, tokenizer, treeNodeList); addChildrenNodes( attachPos, treeNodeList ); session.removeAttribute(AdminConstants.SELECTED_HIERARCHY_ID_KEY); } else if (RESTORE_NODE_ACTION.equalsIgnoreCase(action)) { // Expand to node to restore, followed by a select int attachPos = openToTreeNode(rootNode, 0, tokenizer, treeNodeList); addChildrenNodes( attachPos, treeNodeList ); session.setAttribute(AdminConstants.SELECTED_HIERARCHY_ID_KEY, hierId); } return mapping.findForward(Constants.SUCCESS); } private void addChildrenNodes( int parentPos, LinkedList treeNodeList ) { TreeNode parentNode = (TreeNode)treeNodeList.get(parentPos); parentNode.setExpanded( true ); IHierarchy[] children = parentNode.getHierarchyNode().getChildren(); for (int c=0; c<children.length; c++) { IHierarchy child = children[c]; treeNodeList.add(parentPos+c+1, new TreeNode(child, false, parentPos) ); } } private int openToTreeNode( IHierarchy hierNode, int nodePos, StringTokenizer hierIdTokenizer, LinkedList treeNodeList) { // Get the next child to go down. int childIdInPath = -1; if (hierIdTokenizer.hasMoreTokens()) { childIdInPath = Integer.parseInt(hierIdTokenizer.nextToken()); } if (childIdInPath<0) { return nodePos; } int childPosInPath = -1; IHierarchy childInPath =null; // Get its children, then insert them into positions following the hierNode. IHierarchy[] children = hierNode.getChildren(); TreeNode parentNode = (TreeNode)treeNodeList.get(nodePos); parentNode.setExpanded( children.length > 0 ); for (int c=0; c<children.length; c++) { IHierarchy child = children[c]; if ((childIdInPath!=-1) && (child.getId().intValue()==childIdInPath)) { childPosInPath=nodePos+c+1; childInPath = child; } treeNodeList.add(nodePos+c+1, new TreeNode(child, false, nodePos) ); } // Expand the next child in the path. int retPos = nodePos; if (childInPath!=null) { retPos = openToTreeNode(childInPath, childPosInPath, hierIdTokenizer, treeNodeList); } return retPos; } private static final String SELECTED_HIERARCHY_ID_RESOURCE_CATEGORY = "selectedHierId";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -