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

📄 butormenu.java

📁 一个实用工具类
💻 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.net.URLEncoder;import java.util.Arrays;import java.util.Collection;import java.util.Comparator;import java.util.Iterator;import java.util.Locale;import org.butor.log.Log;import org.butor.messageResources.MessageResourcesService;/** * Menu items' wrapper class. This class provides sorting facility. * How the items are sorted is based on an Comparator object. * * @author Nguyen The Hung */public class ButorMenu {	/** 	 * Top node (dummy node, i.e. placeholder for all children nodes).	 */	private Branch f_topNode;	private String f_id;	/**	 * Comparator for sorting.	 */	private Comparator f_comparator;	private Locale f_locale;	private boolean f_hasChanged;	/**	 * Constructor for ButorMenu.	 */	public ButorMenu(String id, MenuItem[] topLevelBranches, Locale locale) {		super();		f_id = id;		f_topNode = new Branch("", "");		for (int i = 0; i < topLevelBranches.length; i++) {			f_topNode.addBranch(topLevelBranches[i]);		}		setLocale(locale);	}	/**	 * Recursively change the label for the current node and all the children nodes.	 * @param MenuItem org.butor.wak.menu.MenuItem	 */	protected void changeLabel(MenuItem node) {		if (node == null) {			Log.logStr(this, Log.LOG_TYPE_ERROR, "changeLabel()", "Cannot change menu label [null].");			return;		}				String label = MessageResourcesService.getMessage(f_locale, node.getLabelKey());		node.setLabel(label);		if (node.isTitledAction()) {			String action = node.getAction();			String actionWithTitle = mergeTitleToAction(action, node.getLabelKey());			node.setActionWithTitle(actionWithTitle);		}		if (node.hasChild()) {			// change the label for the children nodes			Collection nodes = node.getChildren();			for (Iterator it = nodes.iterator(); it.hasNext();) {				changeLabel((MenuItem) it.next());			}		}	}	protected String mergeTitleToAction(String action, String labelKey) {		// If the menu item is flagged as titled_action, append the label to the		// title parameter on action URL.		if (action != null) {			StringBuffer actionBuffer = new StringBuffer(action);			if (action.indexOf('?') != -1) {				actionBuffer.append("&");			} else {				actionBuffer.append("?");			}			actionBuffer.append("titleKey=" + URLEncoder.encode(labelKey));						action = actionBuffer.toString();		}		return action;	}		/**	 * Set locale.	 * @param locale java.util.Locale	 */	public void setLocale(Locale locale) {		Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "setLocale()", "");				if (f_locale != locale) {			f_locale = locale;			f_hasChanged = true;			Collection nodes = f_topNode.getChildren();			for (Iterator it = nodes.iterator(); it.hasNext();) {				changeLabel((MenuItem) it.next());			}		}	}	/**	 * Set the comparator for sorting	 * @param comparator java.util.comparator	 */	public void setComparator(Comparator comparator) {		Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "setComparator()", "");				if (f_comparator != comparator) {			f_comparator = comparator;			f_hasChanged = true;		}	}		/**	 * Sort the nodes using the current comparator.	 */	public void sort() {				Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "sort()", "");				if (f_hasChanged) {			sort(f_topNode);			f_hasChanged = false;		}	}		/**	 * Recursively sort the children nodes.	 * @param Node org.butor.wak.menu	 */	protected void sort(Branch node) {		if (f_comparator != null && node.hasChild()) {						Collection branches = node.getChildren();			Branch nodes[] = new Branch[branches.size()];			branches.toArray(nodes);						try {				Arrays.sort(nodes, f_comparator);			} catch (ClassCastException e) {				Log.logException(this, Log.LOG_TYPE_WARN, "sort(Branch node)", e);				return;			}			node.removeAllChildren();			for (int i = 0; i < nodes.length; i++) {				node.addBranch(nodes[i]);			}						// sort the "grand-children"			for (int i = 0; i < nodes.length; i++) {				sort(nodes[i]);			}		}	}		/**	 * Return the name of the menu	 * @return java.lang.String	 */	public String getId() {		return f_id;	}		/**	 * Return the locale	 * @return java.util.Locale	 */	public Locale getLocale() {		return f_locale;	}		/**	 * Return the comparator object.	 * @return java.util.Comparator	 */	public Comparator getComparator() {		return f_comparator;	}		/**	 * Return the menu items	 * @return org.butor.wak.menu.IMenuItem []	 */	public IMenuItem[] getItems() {		Collection menus = f_topNode.getChildren();		MenuItem menuItems[] = new MenuItem[menus.size()];		menus.toArray(menuItems);		return menuItems;	}	/**	 * Return a menu item	 * @return org.butor.wak.menu.IMenuItem	 */	public IMenuItem getItem(String id) {		return (IMenuItem)f_topNode.getBranch(id);	}}

⌨️ 快捷键说明

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