KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > PanelManager


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.core;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Comparator JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.http.Cookie JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 /**
35  * Allows <i>Panels</i> to be register.
36  *
37  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
38  * @see com.sslexplorer.core.Panel
39  */

40 public class PanelManager {
41
42     // Private instance variables
43
private Map JavaDoc<String JavaDoc, Panel> panels;
44
45     // Private statics
46
private static PanelManager instance;
47
48     /*
49      * Private constructor to prevent instantiation
50      */

51     private PanelManager() {
52         super();
53         panels = new HashMap JavaDoc<String JavaDoc, Panel>();
54     }
55
56     /**
57      * Add a panel
58      *
59      * @param panel panel to add
60      */

61     public void addPanel(Panel panel) {
62         panels.put(panel.getId(), panel);
63     }
64
65     /**
66      * Remove a panel
67      *
68      * @param id id of panel to remove
69      */

70     public void removePanel(String JavaDoc id) {
71         panels.remove(id);
72     }
73
74     /**
75      * Get an instance of the key store import type manager.
76      *
77      * @return key store import type manager
78      */

79     public static PanelManager getInstance() {
80         if (instance == null) {
81             instance = new PanelManager();
82         }
83         return instance;
84     }
85
86     /**
87      * Get a panel give its id. No check if the panel is available is made. If
88      * no such panel exists <code>null</code> will be returned.
89      *
90      * @param id panel id
91      * @return panel
92      */

93     public Panel getPanel(String JavaDoc id) {
94         return (Panel) panels.get(id);
95     }
96
97     /**
98      * Get a sort list of registered {@link Panel} objects to display for the
99      * given placement based on the current state. The
100      * {@link Panel#isAvailable(HttpServletRequest, HttpServletResponse, String)} method
101      * will be called and only if this returns <i>true</i> will the panel be in
102      * the list.
103      *
104      * @param placement placement
105      * @param request request
106      * @param response response
107      * @param layout the layout the page is currently in.
108      * @return list of appropriate panels
109      */

110     public List JavaDoc getPanels(int placement, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc layout) {
111         List JavaDoc a = new ArrayList JavaDoc();
112         for (Iterator JavaDoc 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 JavaDoc() {
119             public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
120                 Panel p1 = (Panel) arg0;
121                 Panel p2 = (Panel) arg1;
122                 return new Integer JavaDoc(p1.getWeight()).compareTo(new Integer JavaDoc(p2.getWeight()));
123             }
124
125         });
126         return a;
127     }
128
129     /**
130      * Get a sorted list of registered of {@link Panel} objects to display for the
131      * given placement based on the current state. The
132      * {@link Panel#isAvailable(HttpServletRequest, HttpServletResponse, String)} method
133      * will be called and only if this returns <i>true</i> will the panel be in
134      * the list.
135      *
136      * @param placement placement
137      * @param request request
138      * @param response response
139      * @param containerId the div id that contains these panels (used for drag and drop)
140      * @param layout the layout the page is currently in.
141      * @return list of appropriate panels
142      */

143     public List JavaDoc<Panel> getPanels(int placement, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc containerId, String JavaDoc layout) {
144         Map JavaDoc<String JavaDoc, PanelWrapper> a = new HashMap JavaDoc<String JavaDoc, 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             /*
153              * Now go through this and remove and panels that may have been
154              * moved to another container
155              */

156             for (PanelWrapper p : new ArrayList JavaDoc<PanelWrapper>(a.values())) {
157                 String JavaDoc cv = CoreUtil.getCookieValue("frame_component_" + p.getPanel().getId() + "_pos", request, "");
158
159                 // If the cookie value is a number then it is absolutely placed
160
if (!cv.equals("")) {
161                     try {
162                         Integer.parseInt(cv);
163                         continue;
164                     } catch (NumberFormatException JavaDoc nfe) {
165                         try {
166                             int idx = cv.lastIndexOf('_');
167                             String JavaDoc parent = cv.substring(0, idx);
168                             if(!parent.equals(containerId) && p.getPanel().isDropable()) {
169                                 a.remove(p.getPanel().getId());
170                             }
171
172                         } catch (Exception JavaDoc e) {
173 // e.printStackTrace();
174
}
175
176                     }
177                 }
178             }
179             
180             /*
181              * Now go through all the moved panels and add any that should
182              * be in this container
183              */

184             Cookie JavaDoc[] 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 JavaDoc cv = c[i].getValue();
189                         try {
190                             Integer.parseInt(cv);
191                             continue;
192                         } catch (NumberFormatException JavaDoc nfe) {
193                             try {
194                                 int idx = cv.lastIndexOf('_');
195                                 String JavaDoc parent = cv.substring(0, idx);
196                                 int order = Integer.parseInt(cv.substring(idx + 1));
197                                 if(parent.equals(containerId)) {
198                                     String JavaDoc 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 JavaDoc e) {
214 // e.printStackTrace();
215
}
216
217                         }
218                         
219                     }
220                 }
221             }
222         }
223         List JavaDoc<PanelWrapper> l = new ArrayList JavaDoc<PanelWrapper>(a.values());
224         Collections.sort(l, new Comparator JavaDoc<PanelWrapper>() {
225             public int compare(PanelWrapper arg0, PanelWrapper arg1) {
226                 return new Integer JavaDoc(arg0.getWeight()).compareTo(new Integer JavaDoc(arg1.getWeight()));
227             }
228         });
229         List JavaDoc<Panel> ls = new ArrayList JavaDoc<Panel>();
230         for(PanelWrapper w : l) {
231             ls.add(w.getPanel());
232         }
233         return ls;
234     }
235
236 }
237
Popular Tags