1 4 package fr.improve.struts.taglib.layout.treeview; 5 6 import java.util.Collections ; 7 import java.util.HashSet ; 8 import java.util.Iterator ; 9 import java.util.Set ; 10 import java.util.StringTokenizer ; 11 12 import javax.servlet.http.Cookie ; 13 import javax.servlet.http.HttpServletRequest ; 14 import javax.servlet.http.HttpServletResponse ; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 import fr.improve.struts.taglib.layout.menu.MenuComponent; 20 21 22 27 public class TreeViewReconciler { 28 private static final String RECONCEILE_KEY = "fr.improve.struts.taglib.layout.treeview.TreeViewReconciler"; 29 private static final Log Log = LogFactory.getLog(TreeViewReconciler.class); 30 31 public static void reconceileFromCookie(MenuComponent in_menu, HttpServletRequest in_request) { 32 if (!isReconcile(in_menu, in_request)) { 33 String lc_cookie = getTreeviewCookie(in_request); 34 Log.debug("Reconceiling menu " + in_menu.getName() + " from cookie value " + lc_cookie); 35 if (lc_cookie!=null) { 36 in_menu.closeAll(); 37 StringTokenizer lc_tokenizer = new StringTokenizer (lc_cookie, "_"); 38 while (lc_tokenizer.hasMoreTokens()) { 39 String lc_token = lc_tokenizer.nextToken(); 40 if (lc_token.startsWith("treeView")) { 41 lc_token = lc_token.substring(8); 42 } 43 if (lc_token.startsWith(in_menu.getName() + "*")) { 44 MenuComponent lc_component = getComponentWithPath(in_menu, lc_token.substring(in_menu.getName().length()+1)); 45 if (lc_component!=null) { 46 lc_component.setOpen(true); 47 } 48 } 49 } 50 setReconceile(in_menu, in_request); 51 } 52 } 53 } 54 55 private static void setReconceile(MenuComponent in_menu, HttpServletRequest in_request) { 56 Set lc_map = (Set ) in_request.getAttribute(RECONCEILE_KEY); 57 if (lc_map==null) { 58 lc_map = new HashSet (); 59 in_request.setAttribute(RECONCEILE_KEY, lc_map); 60 } 61 lc_map.add(in_menu); 62 } 63 64 private static boolean isReconcile(MenuComponent in_menu, HttpServletRequest in_request) { 65 Set lc_map = (Set ) in_request.getAttribute(RECONCEILE_KEY); 66 if (lc_map==null) { 67 return false; 68 } 69 return lc_map.contains(in_menu); 70 } 71 72 private static Set getReconciledMenu(HttpServletRequest in_request) { 73 Set lc_map = (Set ) in_request.getAttribute(RECONCEILE_KEY); 74 if (lc_map==null) { 75 return Collections.EMPTY_SET; 76 } else { 77 return lc_map; 78 } 79 } 80 81 82 83 private static String getTreeviewCookie(HttpServletRequest in_request) { 84 Cookie [] lc_cookies = in_request.getCookies(); 85 86 if (lc_cookies==null) return null; 87 for (int i=0;i<lc_cookies.length;i++) { 88 Cookie lc_cookie = lc_cookies[i]; 89 if (lc_cookie.getName().equals("treeView")) { 90 return lc_cookie.getValue(); 91 } 92 } 93 return null; 94 } 95 96 static MenuComponent getComponentWithPath(MenuComponent in_menu, String in_path) { 97 int lc_starPos = in_path.indexOf('*'); 98 if(lc_starPos!=-1) { 99 String lc_id = in_path.substring(0, lc_starPos); 100 String lc_subPath = in_path.substring(lc_starPos+1); 101 MenuComponent lc_menu = in_menu.getChild(lc_id); 102 if (lc_menu!=null) { 103 return getComponentWithPath(lc_menu, lc_subPath); 104 } else { 105 return null; 106 } 107 } else { 108 return in_menu.getChild(in_path); 109 } 110 } 111 112 public static void reconceileFromMenu(HttpServletRequest in_request, HttpServletResponse in_response) { 113 Set lc_menus = getReconciledMenu(in_request); 114 Iterator lc_it = lc_menus.iterator(); 115 while (lc_it.hasNext()) { 116 MenuComponent lc_menu = (MenuComponent) lc_it.next(); 117 reconceileFromMenu(lc_menu, in_request, in_response); 118 lc_menu.closeAll(); 119 } 120 in_request.removeAttribute(RECONCEILE_KEY); 121 } 122 123 private static void reconceileFromMenu(MenuComponent in_menu, HttpServletRequest in_request, HttpServletResponse in_response) { 124 StringBuffer lc_cookievalue = new StringBuffer (); 125 String lc_path = in_menu.getName(); 126 reconceileFromMenu(in_menu, lc_cookievalue, lc_path); 127 if (lc_cookievalue.length()>0) { 128 lc_cookievalue.append("_"); 129 } 130 String lc_newCookieValue = lc_cookievalue.toString(); 131 Log.debug("Reconceiling cookie with menu " + in_menu.getName() + ". New cookie value is : " + lc_newCookieValue); 132 Cookie lc_cookie = new Cookie ("treeView", lc_newCookieValue); 133 lc_cookie.setPath(in_request.getContextPath()); 134 in_response.addCookie(lc_cookie); 135 136 Cookie [] lc_cookies = in_request.getCookies(); 137 if (lc_cookies!=null) { 138 for (int i=0;i<lc_cookies.length;i++) { 139 Cookie lc_oldCookie = lc_cookies[i]; 140 if (lc_oldCookie.getName().equals("treeView")) { 141 lc_oldCookie.setValue(lc_newCookieValue); 142 } 143 } 144 } 145 in_menu.closeAll(); 146 } 147 148 private static void reconceileFromMenu(MenuComponent in_menu, StringBuffer lc_cookievalue, String lc_path) { 149 if (in_menu.isOpen() || in_menu.getParent()==null) { 150 MenuComponent[] lc_children = (MenuComponent[]) in_menu.getMenuComponents(); 151 if (in_menu.getParent()!=null) { 152 lc_cookievalue.append("_treeView"); 153 lc_cookievalue.append(lc_path); 154 } 155 156 for (int i=0; i < lc_children.length; i++) { 157 MenuComponent lc_child = lc_children[i]; 158 String lc_id = lc_child.getId(); 159 if (lc_id==null) { 160 lc_id = String.valueOf(i); 161 } 162 reconceileFromMenu(lc_child, lc_cookievalue, lc_path + "*" + lc_id); 163 } 164 } 165 } 166 167 } 168 | Popular Tags |