1 19 20 package com.sslexplorer.core; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import javax.servlet.http.Cookie ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 40 public class PanelManager { 41 42 private Map <String , Panel> panels; 44 45 private static PanelManager instance; 47 48 51 private PanelManager() { 52 super(); 53 panels = new HashMap <String , Panel>(); 54 } 55 56 61 public void addPanel(Panel panel) { 62 panels.put(panel.getId(), panel); 63 } 64 65 70 public void removePanel(String id) { 71 panels.remove(id); 72 } 73 74 79 public static PanelManager getInstance() { 80 if (instance == null) { 81 instance = new PanelManager(); 82 } 83 return instance; 84 } 85 86 93 public Panel getPanel(String id) { 94 return (Panel) panels.get(id); 95 } 96 97 110 public List getPanels(int placement, HttpServletRequest request, HttpServletResponse response, String layout) { 111 List a = new ArrayList (); 112 for (Iterator i = panels.values().iterator(); i.hasNext();) { 113 Panel p = (Panel) i.next(); 114 if ((placement == -1 || p.getPlacement() == placement) && p.isAvailable(request, response, "main")) { 115 a.add(p); 116 } 117 } 118 Collections.sort(a, new Comparator () { 119 public int compare(Object arg0, Object arg1) { 120 Panel p1 = (Panel) arg0; 121 Panel p2 = (Panel) arg1; 122 return new Integer (p1.getWeight()).compareTo(new Integer (p2.getWeight())); 123 } 124 125 }); 126 return a; 127 } 128 129 143 public List <Panel> getPanels(int placement, HttpServletRequest request, HttpServletResponse response, String containerId, String layout) { 144 Map <String , PanelWrapper> a = new HashMap <String , PanelWrapper>(); 145 for (Panel p : panels.values()) { 146 if ((placement == -1 || p.getPlacement() == placement) && p.isAvailable(request, response, layout)) { 147 a.put(p.getId(), new PanelWrapper(p, p.getWeight())); 148 } 149 } 150 151 if (containerId != null) { 152 156 for (PanelWrapper p : new ArrayList <PanelWrapper>(a.values())) { 157 String cv = CoreUtil.getCookieValue("frame_component_" + p.getPanel().getId() + "_pos", request, ""); 158 159 if (!cv.equals("")) { 161 try { 162 Integer.parseInt(cv); 163 continue; 164 } catch (NumberFormatException nfe) { 165 try { 166 int idx = cv.lastIndexOf('_'); 167 String parent = cv.substring(0, idx); 168 if(!parent.equals(containerId) && p.getPanel().isDropable()) { 169 a.remove(p.getPanel().getId()); 170 } 171 172 } catch (Exception e) { 173 } 175 176 } 177 } 178 } 179 180 184 Cookie [] c = request.getCookies(); 185 if(c != null) { 186 for(int i = 0 ; i <c.length ; i++) { 187 if(c[i].getName().startsWith("frame_component_") && c[i].getName().endsWith("_pos")) { 188 String cv = c[i].getValue(); 189 try { 190 Integer.parseInt(cv); 191 continue; 192 } catch (NumberFormatException nfe) { 193 try { 194 int idx = cv.lastIndexOf('_'); 195 String parent = cv.substring(0, idx); 196 int order = Integer.parseInt(cv.substring(idx + 1)); 197 if(parent.equals(containerId)) { 198 String pnId = c[i].getName().substring(16, c[i].getName().length() - 4); 199 Panel pn = getPanel(pnId); 200 if(pn != null && pn.isDropable()) { 201 if (pn.isAvailable(request, response, layout)) { 202 PanelWrapper w = a.get(pnId); 203 if(w == null) { 204 a.put(pnId, w = new PanelWrapper(pn, order)); 205 } else { 206 w.setWeight(order); 207 } 208 209 } 210 } 211 } 212 213 } catch (Exception e) { 214 } 216 217 } 218 219 } 220 } 221 } 222 } 223 List <PanelWrapper> l = new ArrayList <PanelWrapper>(a.values()); 224 Collections.sort(l, new Comparator <PanelWrapper>() { 225 public int compare(PanelWrapper arg0, PanelWrapper arg1) { 226 return new Integer (arg0.getWeight()).compareTo(new Integer (arg1.getWeight())); 227 } 228 }); 229 List <Panel> ls = new ArrayList <Panel>(); 230 for(PanelWrapper w : l) { 231 ls.add(w.getPanel()); 232 } 233 return ls; 234 } 235 236 } 237 | Popular Tags |