📄 branch.java
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file. */package org.butor.web.menu;import java.util.ArrayList;import java.util.Collection;import org.butor.log.Log;/** * This class represents an item in the item hierarchy. A branch can be * a leaf or it can contain other children branches. This clas provides * methods to manage the children item of this branch. * * @author Nguyen The Hung */public class Branch implements IBranch { /** * Name of this node */ protected String f_name; /** * Name of the parent node */ protected String f_parentName; /** * Array of children nodes */ protected ArrayList f_children; /** * Constructor for Branch. * @param name java.lang.String * @param parentName java.lang.String */ public Branch(String name, String parentName) { super(); f_name = name; f_parentName = parentName; f_children = new ArrayList(); } /** * Add a child node to the current node. * @param child org.butor.wak.menu.IBranch */ protected void addBranch(IBranch child) { if (child == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "addBranch()", "Cannot add branch [null]."); return; } Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "addBranch()", "Name : " + child.getName()); f_children.add(child); } /** * Add a node. * @param parentName java.lang.String Name of the parent node to which the child node will be attached. * @param child org.butor.wak.menu.IBranch * @return org.butor.wak.menu.Branch The node to which the child is attached or null if there is no parent node. */ protected Branch addBranch(String parentName, IBranch child) { if (parentName == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "addBranch()", "Cannot add branch. Parent name = [null]."); return null; } if (child == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "addBranch()", "Cannot add branch. Child = [null]."); return null; } // Find the parent node Branch parentNode = (Branch) getBranch(parentName); if (parentNode == null) { StringBuffer buff = new StringBuffer(); buff.append("Cannot add branch. [Parent Id: "); buff.append(parentName); buff.append("; Id : "); buff.append(child.getName()); buff.append("]"); Log.logStr(this, Log.LOG_TYPE_WARN, "addBranch()", buff.toString()); } else { parentNode.addBranch(child); } return parentNode; } /** * Remove a child node from the current node. * @param child org.butor.wak.menu.IBranch * @return boolean */ protected boolean removeBranch(IBranch child) { if (child == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "removeBranch()", "Cannot remove branch [null]."); return false; } String name = child.getName(); if (name == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "removeBranch()", "Cannot remove branch. Name = [null]."); return false; } Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "removeBranch()", "Name : " + name); for (int i = 0; i < f_children.size(); i++) { IBranch node = (IBranch) f_children.get(i); if (node.getName().equals(name)) { if (node.hasChild()) { Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "removeBranch()", "Cannot remove branch (got child(s))."); return false; } else { f_children.remove(i); return true; } } } Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "removeBranch()", "Branch not found."); return false; } /** * Remove a child node. * @param parentName java.lang.String Name of the parent node from which the child node will be removed. * @param child org.butor.wak.menu.IBranch * @return boolean */ protected boolean removeBranch(String parentName, IBranch child) { if (parentName == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "removeBranch()", "Cannot remove branch. Parent name = [null]."); return false; } if (child == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "removeBranch()", "Cannot remove branch. Child = [null]."); return false; } // Find the parent node Branch parentNode = (Branch) getBranch(parentName); if (parentNode == null) { StringBuffer buff = new StringBuffer(); buff.append("Cannot remove branch. [Parent Id: "); buff.append(parentName); buff.append("; Id : "); buff.append(child.getName()); buff.append("]"); Log.logStr(this, Log.LOG_TYPE_WARN, "removeBranch()", buff.toString()); return false; } else { parentNode.removeBranch(child); return true; } } /** * Remove all the children nodes */ protected void removeAllChildren() { Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "removeAllChildren()", ""); f_children.clear(); } /** * Get the node of the specified id (down the hierarchy). * @param name java.lang.String * @return org.butor.wak.menu.IBranch */ public IBranch getBranch(String name) { if (name == null) { Log.logStr(this, Log.LOG_TYPE_ERROR, "getBranch()", "Cannot get branch. Name = [null]."); return null; } // If the current node has the same name, then return self if (name.equals(f_name)) { return this; } // Loop thru the children nodes to find the node with the specified id. for (int i = 0; i < f_children.size(); i++) { IBranch child = (IBranch) f_children.get(i); IBranch node = child.getBranch(name); if (node != null) { return node; } } return null; } /** * Get the children nodes. * @return java.util.Collection */ public Collection getChildren() { return f_children; } /** * Return the whether the current node has any child node. * @return boolean */ public boolean hasChild() { return !f_children.isEmpty(); } /************************ Gettter/Setter Methods ************************/ /** * Return the id of the current node. * @return java.lang.String */ public String getName() { return f_name; } /** * Return the name of the parent node. * @return java.lang.String */ public String getParentName() { return f_parentName; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -