1 package fr.improve.struts.taglib.layout; 2 3 import java.util.HashMap ; 4 import java.util.Iterator ; 5 import java.util.Locale ; 6 import java.util.MissingResourceException ; 7 import java.util.ResourceBundle ; 8 import java.util.Set ; 9 import java.util.Vector ; 10 import java.util.Map.Entry; 11 12 import javax.servlet.jsp.JspException ; 13 import javax.servlet.jsp.PageContext ; 14 15 import fr.improve.struts.taglib.layout.menu.MenuComponent; 16 import fr.improve.struts.taglib.layout.util.LayoutUtils; 17 import fr.improve.struts.taglib.layout.util.MenuInterface; 18 import fr.improve.struts.taglib.layout.util.TagUtils; 19 20 27 public class DynMenuTag2 extends PanelTag implements MenuInterface{ 28 protected Vector menus = new Vector (); 29 protected String config = "fr.improve.struts.taglib.layout.DefaultDynMenuConfig"; 30 31 protected String ID = "fr.improve.struts.taglib.layout.DynMenuTag2.ID"; 32 protected int menuNumber; 34 35 protected String top; 36 protected String left; 37 38 protected boolean includeScript = false; 39 protected static final String SCRIPT_KEY = "fr.improve.struts.taglib.layout.DynMenuTag2.SCRIPT_KEY"; 40 41 public static void includeScriptCode(StringBuffer in_buffer, PageContext in_pg) { 42 if (in_pg.getRequest().getAttribute(SCRIPT_KEY)!=null) { 43 in_buffer.append("<SCRIPT LANGUAGE=\"JavaScript1.2\" SRC=\""); 44 in_buffer.append(LayoutUtils.getSkin(in_pg.getSession()).getConfigDirectory(in_pg.getRequest())); 45 in_buffer.append("/HM_Loader.js\" TYPE='text/javascript'></SCRIPT>\n"); 46 in_pg.getRequest().removeAttribute(SCRIPT_KEY); 47 } 48 } 49 50 public void addItem(MenuComponent item) { 51 menus.add(item); 52 } 53 public int doEndLayoutTag() throws JspException { 54 StringBuffer buffer = new StringBuffer (); 55 56 buffer.append("<script LANGUAGE='JavaScript1.2' TYPE='text/javascript'>\n"); 57 58 doPrintMenu(buffer, (MenuComponent[])menus.toArray(new MenuComponent[menus.size()]), ""); 59 buffer.append("</script>\n"); 60 61 pageContext.getRequest().setAttribute(SCRIPT_KEY, ""); 62 63 if (includeScript) { 64 includeScriptCode(buffer, pageContext); 65 } 66 67 TagUtils.write(pageContext,buffer.toString()); 68 69 pageContext.getRequest().setAttribute(ID, new Integer (menuNumber+1)); 70 71 menus.removeAllElements(); 73 74 return EVAL_PAGE; 75 } 76 77 public void release() { 78 super.release(); 79 config ="fr.improve.struts.taglib.layout.DefaultDynMenuConfig"; 80 left = null; 81 top = null; 82 includeScript = false; 83 } 84 public void doPrintMenu(StringBuffer buffer, MenuComponent[] menus, String level) throws JspException { 85 HashMap subMenusMap = new HashMap (); 86 87 buffer.append("HM_Array"); 88 buffer.append(menuNumber); 89 buffer.append(level); 90 buffer.append(" = [\n"); 91 if (level.equals("")) doPrintMenuConfig(buffer); 92 else buffer.append("[],\n"); 93 94 for (int i=0;i<menus.length;i++) { 95 MenuComponent item = (MenuComponent) menus[i]; 96 String link = item.getLocation(); 97 String forward = item.getForward(); 98 String page = item.getPage(); 99 String target = item.getTarget(); 100 String action = item.getAction(); 101 key = item.getTitle(); 102 MenuComponent[] subMenus = item.getMenuComponents(); 103 104 if (i!=0) buffer.append(","); 105 106 buffer.append("[\""); 107 buffer.append(getLabel()); 108 buffer.append("\",\""); 109 if (link!=null || page!=null || forward!=null || action!=null) { 110 if (target!=null) { 111 buffer.append("javascript:window.open('").append(LayoutUtils.computeURL(pageContext, forward, link, page, action, null, null, null, false, null)).append("','").append(target); 112 buffer.append("','')"); 113 } else { 114 buffer.append(LayoutUtils.computeURL(pageContext, forward, link, page, action, null, null, null, false, null)); 115 } 116 } 117 buffer.append("\",1,0,"); 118 if (subMenus==null || subMenus.length==0) buffer.append("0]\n"); else { 119 buffer.append("1]\n"); 120 subMenusMap.put(level + "_" + (i+1),subMenus); 121 } 122 123 } 124 125 buffer.append("]\n\n"); 126 127 Set set = subMenusMap.entrySet(); 128 Iterator iterator = set.iterator(); 129 while (iterator.hasNext()) { 130 Entry entry = (Entry) iterator.next(); 131 String subLevel = (String ) entry.getKey(); 132 MenuComponent[] subMenus = (MenuComponent[]) entry.getValue(); 133 doPrintMenu(buffer, subMenus, subLevel); 134 } 135 136 } 137 protected void doPrintMenuConfig(StringBuffer buffer) throws JspException { 138 String skin = LayoutUtils.getSkin(pageContext.getSession()).getCssFileName(); 140 skin = skin.substring(0, skin.length()-4); 141 142 try { 144 if (left!=null) { 145 Integer.parseInt(left); 146 } 147 if (top!=null) { 148 Integer.parseInt(top); 149 } 150 } catch (NumberFormatException e) { 151 JspException jspe = new JspException ("dynMenu: left or top position is not a valid number"); 152 TagUtils.saveException(pageContext, jspe); 153 throw jspe; 154 } 155 156 try { 158 ResourceBundle bundle = ResourceBundle.getBundle(config, new Locale (skin,"")); 159 buffer.append("["); 160 buffer.append(bundle.getString("width")); 161 buffer.append(","); 162 buffer.append(left !=null ? left : bundle.getString("left_position")); 163 buffer.append(","); 164 buffer.append(top !=null ? top : bundle.getString("top_position")); 165 buffer.append(","); 166 buffer.append(bundle.getString("font_color")); 167 buffer.append(","); 168 buffer.append(bundle.getString("mouseover_font_color")); 169 buffer.append(","); 170 buffer.append(bundle.getString("background_color")); 171 buffer.append(","); 172 buffer.append(bundle.getString("mouseover_background_color")); 173 buffer.append(","); 174 buffer.append(bundle.getString("border_color")); 175 buffer.append(","); 176 buffer.append(bundle.getString("separator_color")); 177 buffer.append(","); 178 buffer.append(bundle.getString("top_is_permanent")); 179 buffer.append(","); 180 buffer.append(bundle.getString("top_is_horizontal")); 181 buffer.append(","); 182 buffer.append(bundle.getString("tree_is_horizontal")); 183 buffer.append(","); 184 buffer.append(bundle.getString("position_under")); 185 buffer.append(","); 186 buffer.append(bundle.getString("top_more_images_visible")); 187 buffer.append(","); 188 buffer.append(bundle.getString("tree_more_images_visible")); 189 buffer.append(","); 190 buffer.append(bundle.getString("evaluate_upon_tree_show")); 191 buffer.append(","); 192 buffer.append(bundle.getString("evaluate_upon_tree_hide")); 193 buffer.append("],\n"); 194 } catch (MissingResourceException e) { 195 JspException jspe = new JspException ("dynMenu: missing attribute " + e.getMessage()); 196 TagUtils.saveException(pageContext, jspe); 197 throw jspe; 198 } 199 200 } 201 public int doStartLayoutTag() throws JspException { 202 Integer lc_int = (Integer ) pageContext.getRequest().getAttribute(ID); 203 if (lc_int==null) { 204 menuNumber = 1; 205 } else { 206 menuNumber = lc_int.intValue(); 207 } 208 return EVAL_BODY_INCLUDE; 209 } 210 public void setConfig(String config) { 211 this.config = config; 212 } 213 public void setLeft(String left) { 214 this.left = left; 215 } 216 public void setTop(String top) { 217 this.top = top; 218 } 219 public void setIncludeScript(boolean in_b) { 220 includeScript = in_b; 221 } 222 223 } 224 | Popular Tags |