📄 menutag.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.taglib.html;import java.io.IOException;import java.util.Iterator;import java.util.Locale;import javax.servlet.http.HttpSession;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.tagext.TagSupport;import org.butor.context.ContextService;import org.butor.context.IContext;import org.butor.log.Log;import org.butor.security.ISecurityChecker;import org.butor.web.menu.IMenuItem;import org.butor.web.menu.ButorMenu;import org.butor.web.menu.ButorMenuFactory;import org.butor.web.menu.MenuLabelComparator;import org.butor.web.menu.MenuSequenceComparator;/** * This class renders the <menu> tag. Currently support two sorting modes: by label or by sequence. * Also the menu can be displayed on both orientation, vertically and horizontally. Note that when * displayed in the horizontal orientation, only the first level menu items are displayed. * * Usage example: * <menu:menu id="app" orientation="vertical" sort="label" /> * * @author Nguyen The Hung */public class MenuTag extends TagSupport { public static final String ORIENTATION_VERT = "vertical"; public static final String ORIENTATION_HORZ = "horizontal"; public static final String SORT_LABEL = "label"; public static final String SORT_SEQ = "sequence"; private String f_menuId; private String f_orientation = ORIENTATION_VERT; private String f_sort = SORT_SEQ; private String f_styleClass; private String f_dynamic = "false"; private String f_top = "20" ; private String f_left = "120" ; private String f_lowBgColor="#FF0000"; // Background color when mouse is not over private String f_lowSubBgColor="#FF0000"; // Background color when mouse is not over on subs private String f_highBgColor="#B20505"; // Background color when mouse is over private String f_highSubBgColor="#B20505"; // Background color when mouse is over on subs private String f_fontLowColor="black"; // Font color when mouse is not over private String f_fontSubLowColor="black"; // Font color subs when mouse is not over private String f_fontHighColor="white"; // Font color when mouse is over private String f_fontSubHighColor="white"; // Font color subs when mouse is over private String f_borderColor="black"; // Border color private String f_borderSubColor="black"; // Border color for subs private String f_borderWidth="1"; // Border width private String f_borderBtwnElmnts="1"; // Border between elements 1 or 0 /** * Get the menu object. * @return org.butor.wak.menu.ButorMenu */ protected ButorMenu getMenu() { Locale locale = null; ButorMenu menu = null; ISecurityChecker securityChecker = null; boolean hasChanged = false; // Try to get the menu from the session HttpSession session = pageContext.getSession(); if (session != null) { menu = (ButorMenu) session.getAttribute("menu." +f_menuId); Object contextKey = Thread.currentThread(); IContext context = ContextService.getContext(contextKey); if (context == null) { Log.logStr(this, Log.LOG_TYPE_WARN, "getMenu()", "Can't find context."); } locale = context.getLocale(); securityChecker = context.getSecurityChecker(); if (securityChecker == null) { Log.logStr(this, Log.LOG_TYPE_WARN, "getMenu()", "Can't find security checker in context."); } if (menu != null) { Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "getMenu()", "Found menu in session."); } } if (menu == null) { // menu does not exist, then build it. menu = ButorMenuFactory.getMenu(f_menuId, locale == null ? Locale.ENGLISH : locale, securityChecker); hasChanged = true; } else { if (locale != null && locale != menu.getLocale()) { menu.setLocale(locale); hasChanged = true; } else if (f_sort.equals(SORT_SEQ)) { if (!(menu.getComparator() instanceof MenuSequenceComparator)) { hasChanged = true; } } else { if (!(menu.getComparator() instanceof MenuLabelComparator)) { hasChanged = true; } } } if (menu != null && hasChanged) { if (f_sort.equals(SORT_SEQ)) { menu.setComparator(new MenuSequenceComparator()); } else { menu.setComparator(new MenuLabelComparator()); } menu.sort(); if (session != null) { session.setAttribute("menu." +f_menuId, menu); } } return menu; } /** * Indenting */ protected void printEmptyColumn(JspWriter writer) throws IOException { writer.print(" "); } /** * Print the link */ protected void printLink(JspWriter writer, IMenuItem item) throws IOException { String action = item.getAction(); String target = item.getTarget(); if (item.isTitledAction()) { action = item.getActionWithTitle(); } String label = item.getLabel(); if (action == null || "".equals(action)) { if (label != null) { writer.print(label); } } else { writer.print("<a href=\""); writer.print(action); if (target != null && target.trim().length() > 0) { writer.print("\""); writer.print(" target=\""); writer.print(target); } writer.print("\">"); if (f_styleClass != null) { writer.print("<span class=" + f_styleClass + ">"); } if (label != null) { writer.print(label); } if (f_styleClass != null) { writer.print("</span>"); } writer.print("</a>"); } } protected void printDynamicLink(JspWriter writer, IMenuItem menuItems[], int index) throws IOException { IMenuItem item = menuItems[index]; String action = item.getAction(); String label = item.getLabel(); String target = item.getTarget(); Object contextKey = Thread.currentThread(); IContext context = ContextService.getContext(contextKey); if (context == null) { Log.logStr(this, Log.LOG_TYPE_WARN, "printDynamicLink()", "Can't find context."); } Locale locale = context.getLocale(); writer.print("<a href=\""); if (action == null || action.equals("")) { writer.print("#"); } else { writer.print(action); } IMenuItem currentDisplayedItem = null; StringBuffer sb = new StringBuffer(); writer.print("\" onmouseover=\"affiche("); for ( int i=0; i<menuItems.length; i++ ) { String selectedAction = pageContext.getRequest().getParameter(menuItems[i].getName()); if ( selectedAction != null ) { currentDisplayedItem = menuItems[i]; } sb.append("'"); sb.append(menuItems[i].getName()); sb.append("','"); sb.append(menuItems[i].getImageUrl()); if ( menuItems[i].isImageDependOnLanguage() ) { sb.append(locale.getLanguage() + ".gif"); } sb.append("',"); } writer.print(sb.toString()); writer.print("'"); writer.print(item.getName()); writer.print("','"); writer.print(item.getImageOverUrl()); if ( item.isImageDependOnLanguage() ) { writer.print(locale.getLanguage() + ".gif"); } writer.print("')\" onmouseout=\"quit('" + item.getName() + "', "); writer.print(sb.toString()); if ( currentDisplayedItem != null ) { writer.print("'"); writer.print(currentDisplayedItem.getName()); writer.print("','"); writer.print(currentDisplayedItem.getImageOverUrl()); if ( currentDisplayedItem.isImageDependOnLanguage() ) { writer.print(locale.getLanguage() + ".gif"); } } else { writer.print("'', '"); } writer.print("')"); if (target != null && target.trim().length() > 0) { writer.print(" target=\""); writer.print(target); } writer.print("\">"); if (f_styleClass != null) { writer.print("<span class=" + f_styleClass + ">"); } if ( item.getImageUrl() != null ) { writer.print("<IMG src=\"" + item.getImageUrl()); if ( item.isImageDependOnLanguage() ) { writer.print(locale.getLanguage() + ".gif"); } writer.print("\" border=0 name=" + item.getName() + ">"); } if ( (label != null) && (!label.equals("")) ) { writer.print(label); } if (f_styleClass != null) { writer.print("</span>"); } writer.print("</a>"); } //return true if their is an item that is passed as a request parameter. Because if not we'll search // for the selected Link in the session. private boolean getSelectedLinkFromSession(IMenuItem[] items) { boolean found = false; int i=0; while ( (!found) && (i < items.length) ) { found = (pageContext.getRequest().getParameter(items[i++].getName()) != null); } return !found; } /** * Recursively print the menu items */ protected void printMenu(JspWriter writer, IMenuItem item, int level, boolean getSelectionFromSession) throws IOException { if (f_orientation.equals(ORIENTATION_VERT)) { writer.print("<tr>"); writer.print("<td nowrap>"); for (int i = 0; i < level; i++) { printEmptyColumn(writer); } // print the +/- gif if (item.hasChild()) { writer.print("<img src=\"/_images/down_red.gif\">"); } else { writer.print("<img src=\"/_images/right_red.gif\">"); } // print the menu printLink(writer, item); writer.print("</td>"); writer.print("</tr>\n"); // print the sub-menu for (Iterator it = item.getChildren().iterator(); it.hasNext();) { IMenuItem child = (IMenuItem) it.next(); printMenu(writer, child, level + 1, false); } } else { Object contextKey = Thread.currentThread(); IContext context = ContextService.getContext(contextKey); if (context == null) { Log.logStr(this, Log.LOG_TYPE_WARN, "printDynamicLink()", "Can't find context."); } Locale locale = context.getLocale(); String imageUrl = item.getImageUrl(); if ( ( imageUrl != null ) && ( !imageUrl.equals("")) ) { String selectedLink = pageContext.getRequest().getParameter(item.getName()); if ( (selectedLink != null) || ( (getSelectionFromSession) && (pageContext.getAttribute(item.getName(),PageContext.SESSION_SCOPE) != null) ) ) { pageContext.setAttribute(item.getName(), "", PageContext.SESSION_SCOPE); writer.print("<IMG name='" + item.getName() + "' SRC=\""+ item.getImageClickUrl()); if ( item.isImageDependOnLanguage() ) { writer.print(locale.getLanguage() + ".gif"); } writer.print("\">"); } else { pageContext.removeAttribute(item.getName(), PageContext.SESSION_SCOPE); writer.print("<IMG name='" + item.getName() + "' SRC=\"" + imageUrl); if ( item.isImageDependOnLanguage() ) { writer.print(locale.getLanguage() + ".gif"); } writer.print(" \">"); } } printLink(writer, item); } } protected void createDynamicMenu(JspWriter writer, IMenuItem item, String suffix, Locale locale) throws IOException { writer.print("Menu"); writer.print(suffix); writer.print("=new Array(\""); if ( item.getImageUrl() != null ) { writer.print("rollover:"); writer.print(item.getImageUrl()); if ( item.isImageDependOnLanguage() ) { writer.print(locale.getLanguage() + ".gif"); } writer.print(":"); if ( item.getImageOverUrl() != null) { writer.print(item.getImageOverUrl()); if ( item.isImageDependOnLanguage() ) { writer.print(locale.getLanguage() + ".gif"); } } else { writer.print(item.getImageUrl()); if ( item.isImageDependOnLanguage() ) { writer.print(locale.getLanguage() + ".gif"); } } } else { writer.print(item.getLabel()); } writer.print("\",\""); writer.print(item.getAction()); writer.print("\",\"\","); writer.print(( (item.getChildren()!= null) ? item.getChildren().size() : 0 )); writer.println(","); writer.print(item.getHeight()); writer.print(","); writer.print(item.getWidth()); if ( item.getImageUrl() != null ) { writer.print(", \""); writer.print(item.getLabel()); writer.print("\""); } writer.println(");"); // print the sub-menu for (int it = 0; it < item.getChildren().toArray().length; it++) { IMenuItem child = (IMenuItem) item.getChildren().toArray()[it]; createDynamicMenu(writer, child, suffix + "_" + (it+1), locale); } } protected void printMenu(JspWriter writer, IMenuItem menuItems[], int index, int level) throws IOException { printDynamicLink(writer, menuItems, index) ; } protected void printSubMenu(JspWriter writer, IMenuItem menuItems[], int index) throws IOException { Object contextKey = Thread.currentThread(); IContext context = ContextService.getContext(contextKey); if (context == null) { Log.logStr(this, Log.LOG_TYPE_WARN, "printSubMenu()", "Can't find context."); } Locale locale = context.getLocale(); IMenuItem item = menuItems[index]; writer.print("<DIV id=box" + item.getName() + " class=\"subMenu\">\n"); if ( item.hasChild() ) { writer.print("<TABLE cellSpacing=0 cellPadding=0 border=0>\n"); writer.print("<TR>\n"); // print the sub-menu int i=0; boolean isDependOnLan = false; for (Iterator it = item.getChildren().iterator(); it.hasNext();) { IMenuItem child = (IMenuItem) it.next(); writer.print("<TD align=CENTER>\n"); writer.print("<a href=\"javascript:Go('" + child.getName() + "')\" onmouseover=\"quit('stop');swapImage('SM" + child.getName() + "','','" + child.getImageOverUrl()); isDependOnLan = child.isImageDependOnLanguage(); if ( isDependOnLan ) { writer.print(locale.getLanguage() + ".gif"); } writer.print("',1)\" onmouseout=\"swapImage('SM" + child.getName() + "','','" + child.getImageUrl()); if ( isDependOnLan ) { writer.print(locale.getLanguage() + ".gif"); } writer.print("',1);quit('submenu')\" ><IMG SRC=\"" + child.getImageUrl()); if ( isDependOnLan ) { writer.print(locale.getLanguage() + ".gif"); } writer.print("\" border=0 name=\"SM" + child.getName() + "\" >"); if (child.getLabel() != null && (!child.getLabel().equals(""))) { writer.print(child.getLabel()); } writer.print("</a>\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -