KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > html > Page


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.html;
21
22 import za.org.coefficient.core.Constants;
23 import za.org.coefficient.core.Project;
24 import za.org.coefficient.interfaces.CoefficientContext;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Serializable JavaDoc;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.TreeMap JavaDoc;
34
35 import javax.naming.Context JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37
38 import za.org.coefficient.util.common.InvokerFactory;
39
40 /**
41  * The Page object is like a sheet of paper on which the content is added.
42  * The theme is like a set of color pens and other printing devices and
43  * the modules are what are added to the sheet of paper. Once
44  * everything is in place, the Page is sent to the web browser.
45  *
46  * The Page instance is created by the CoefficientServlet object and
47  * is passed along through the Coefficient Context to all modules.
48  *
49  * @see za.org.coefficient.servlet.CoefficientServlet CoefficientServlet
50  *
51  *
52  * @author <a HREF="mailto:detkin@csir.co.za">Dylan Etkin</a>
53  * @version $Revision: 1.26 $
54  */

55 public class Page implements Serializable JavaDoc {
56     //~ Static fields/initializers =============================================
57

58     private static final String JavaDoc JAVASCRIPT =
59         "<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" HREF=\"images?image=/calendar-win2k-cold-1.css\" title=\"win2k-cold-1\" />\n <script type=\"text/javascript\" SRC=\"images?image=/calendar.js\"></script>\n <script type=\"text/javascript\" SRC=\"images?image=/calendar-en.js\"></script>\n <script type=\"text/javascript\" SRC=\"images?image=/calendar-setup.js\"></script>\n <script language=\"Javascript\">\n<!--\nfunction render(t) {\nif (document.getElementById(t).style.display == \"none\") {\ndocument.getElementById(t).style.display=\"block\"\n} else {\ndocument.getElementById(t).style.display=\"none\"\n}\n}\n//-->\n</script>";
60     private static final String JavaDoc SIDE_WRAP_1 =
61         "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><br><a style=\"color:blue;cursor:hand;FONT-FAMILY: Verdana;FONT-SIZE: 14px;\" name=\"";
62     private static final String JavaDoc SIDE_WRAP_2 = "\" onClick=\"render('";
63     private static final String JavaDoc SIDE_WRAP_3 =
64         "')\">+/-</a></td></tr></table><table id=\"";
65     private static final String JavaDoc SIDE_WRAP_4 =
66         "\" style=\"display:block\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" ><tr><td width=\"100%\">";
67     private static final String JavaDoc SIDE_WRAP_5 = "</td></tr></table>";
68     private static final String JavaDoc NAV_BLOCK_1 =
69         "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" >\n<tr>\n<td class=\"navBlock\">\n";
70     //"<table width=\"100%\">\n<tr>\n<td class=\"navBlock\">\n";
71
private static final String JavaDoc NAV_BLOCK_2 = "\n</td>\n</tr>\n<tr><td>";
72     private static final String JavaDoc NAV_BLOCK_3 = "\n<br></td></tr></table>";
73
74     //~ Instance fields ========================================================
75

76     private CoefficientContext context = null;
77     private ArrayList JavaDoc errors;
78     private Project project = null;
79
80     /** The theme used to render the page. */
81     private String JavaDoc theme = null;
82     private TreeMap JavaDoc centerContent = new TreeMap JavaDoc();
83     private HashMap JavaDoc centerModulePositions = new HashMap JavaDoc();
84     private TreeMap JavaDoc eastContent = new TreeMap JavaDoc();
85     private HashMap JavaDoc eastModulePositions = new HashMap JavaDoc();
86     private TreeMap JavaDoc northContent = new TreeMap JavaDoc();
87     private HashMap JavaDoc northModulePositions = new HashMap JavaDoc();
88     private TreeMap JavaDoc southContent = new TreeMap JavaDoc();
89     private HashMap JavaDoc southModulePositions = new HashMap JavaDoc();
90     private TreeMap JavaDoc westContent = new TreeMap JavaDoc();
91     private HashMap JavaDoc westModulePositions = new HashMap JavaDoc();
92     private int centerContentMax = Integer.MAX_VALUE;
93     private int eastContentMax = Integer.MAX_VALUE;
94     private int northContentMax = Integer.MAX_VALUE;
95     private int southContentMax = Integer.MAX_VALUE;
96     private int westContentMax = Integer.MAX_VALUE;
97
98     //~ Constructors ===========================================================
99
//
100

101     /**
102      * Constructor. The constructor just store the stream this page will
103      * write to.
104      */

105     public Page() {
106         errors = new ArrayList JavaDoc();
107     }
108
109     public Page(CoefficientContext ctx) {
110         errors = new ArrayList JavaDoc();
111         context = ctx;
112         theme = determineTheme();
113         project = ctx.getProject();
114     }
115
116     //~ Methods ================================================================
117

118     public Project getProject() {
119         return project;
120     }
121
122     public String JavaDoc getCenter() throws Exception JavaDoc {
123         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
124         //sb.append("<td width=\"");
125

126         Object JavaDoc[] params = { this };
127         sb.append("<td width=\"");
128         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getCenterPercentage", params));
129         sb.append("\" valign=\"top\" >");
130         sb.append(getErrors());
131         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getCenter", params));
132         sb.append("</td>");
133
134         return sb.toString();
135     }
136
137     public void setCenterContent(String JavaDoc html, Integer JavaDoc position, String JavaDoc moduleName) {
138         if (position == null) {
139             position = new Integer JavaDoc(centerContentMax);
140             centerContentMax--;
141         }
142         this.centerContent.put(position, html);
143         this.centerModulePositions.put(moduleName, position);
144     }
145
146     public String JavaDoc getCenterContent() {
147         return getCenterContent(false);
148     }
149
150     public String JavaDoc getCenterContent(boolean includeNavBlock) {
151         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
152         for (Iterator JavaDoc it = centerContent.keySet().iterator(); it.hasNext();) {
153             Integer JavaDoc key = (Integer JavaDoc) it.next();
154             String JavaDoc moduleName = null;
155             for(Iterator JavaDoc itr = centerModulePositions.keySet().iterator();
156                 itr.hasNext();){
157                 String JavaDoc key2 = (String JavaDoc)itr.next();
158                 Integer JavaDoc value = (Integer JavaDoc)centerModulePositions.get(key2);
159                 if(value.equals(key)) {
160                     moduleName = key2;
161                     break;
162                 }
163             }
164             String JavaDoc content = (String JavaDoc) centerContent.get(key);
165             if (includeNavBlock && !content.trim().equals("")
166                 && moduleName != null && !"".equals(moduleName.trim())) {
167                 sb.append(NAV_BLOCK_1);
168                 sb.append(moduleName);
169                 sb.append(NAV_BLOCK_2);
170                 sb.append(content);
171                 sb.append(NAV_BLOCK_3);
172             } else {
173                 sb.append(content);
174             }
175         }
176         
177         return sb.toString();
178     }
179
180     public String JavaDoc getEast() throws Exception JavaDoc {
181         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
182         Object JavaDoc[] params = { this };
183     /*
184         sb.append("<td width=\"");
185         sb.append((String)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getEastPercentage", params));
186         sb.append("\" class=\"line2\" valign=\"top\" bgcolor=\"");
187         sb.append((String)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getEastBackGround", params));
188         sb.append("\">\n");
189     */

190         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getEast", params));
191     /*
192         sb.append("</td>\n");
193     */

194
195         return sb.toString();
196     }
197
198     public void setEastContent(String JavaDoc html, Integer JavaDoc position, String JavaDoc moduleName) {
199         if (position == null) {
200             position = new Integer JavaDoc(eastContentMax);
201             eastContentMax--;
202         }
203         this.eastContent.put(position, html);
204         this.eastModulePositions.put(moduleName, position);
205     }
206
207     public String JavaDoc getEastContent() {
208         return getEastContent(true);
209     }
210
211     public String JavaDoc getEastContent(boolean includeNavBlock) {
212         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
213         for (Iterator JavaDoc it = eastContent.keySet().iterator(); it.hasNext();) {
214             Integer JavaDoc key = (Integer JavaDoc) it.next();
215             String JavaDoc moduleName = null;
216             for(Iterator JavaDoc itr = eastModulePositions.keySet().iterator();
217                 itr.hasNext();){
218                 String JavaDoc key2 = (String JavaDoc)itr.next();
219                 Integer JavaDoc value = (Integer JavaDoc)eastModulePositions.get(key2);
220                 if(value.equals(key)) {
221                     moduleName = key2;
222                     break;
223                 }
224             }
225             String JavaDoc content = (String JavaDoc) eastContent.get(key);
226             if (includeNavBlock && !content.trim().equals("")
227                 && moduleName != null && !"".equals(moduleName.trim())) {
228                 sb.append(NAV_BLOCK_1);
229                 sb.append(moduleName);
230                 sb.append(NAV_BLOCK_2);
231                 sb.append(content);
232                 sb.append(NAV_BLOCK_3);
233             } else {
234                 sb.append(content);
235             }
236         }
237         
238         return sb.toString();
239     }
240
241     public void setError(String JavaDoc html) {
242         this.errors.add("<font color=\"red\">" + html + "</font>");
243     }
244
245     public String JavaDoc getErrors() {
246         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
247         for (Iterator JavaDoc it = errors.iterator(); it.hasNext();) {
248             String JavaDoc error = (String JavaDoc) it.next();
249             sb.append("<li>" + error + "</li><br>");
250         }
251
252         return sb.toString();
253     }
254
255     public String JavaDoc getFooter() throws Exception JavaDoc {
256         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
257         Object JavaDoc[] params = { this };
258         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getFooter", params));
259         sb.append("</body>\n</html>\n");
260
261         return sb.toString();
262     }
263
264     public String JavaDoc getHeader() throws Exception JavaDoc {
265         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
266         Object JavaDoc[] params = { this };
267         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getHeader", params));
268         sb.append(JAVASCRIPT);
269
270         return sb.toString();
271     }
272
273     public void setModuleContent(String JavaDoc html, String JavaDoc name) throws Exception JavaDoc {
274         this.setModuleContent(html, name, null);
275     }
276
277     public void setModuleContent(String JavaDoc html, String JavaDoc name, String JavaDoc utilityTheme) throws Exception JavaDoc {
278         Object JavaDoc[] params = { this, name, html };
279         if (utilityTheme==null) {
280             InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "format", params);
281         }
282         else
283             {
284                 try {
285                     Context initialContext = za.org.coefficient.util.common.ContextUtil.getContext();
286                     if (initialContext.lookup(Constants.JNDI_CONTEXT + "/"
287                                               + "utilityThemes" + "/"+utilityTheme)!=null) {
288                         InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(utilityTheme, "format", params);
289                         theme = utilityTheme;
290                     }
291                 } catch (NamingException JavaDoc ne) {
292                     ne.printStackTrace();
293                 }
294             }
295     }
296
297     public String JavaDoc getNorth() throws Exception JavaDoc {
298         Object JavaDoc[] params = { this };
299         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
300         sb.append("<table width=\"");
301         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getTableWidth", params));
302         sb.append("\" border=\"0\" cellspacing=\"");
303         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getCellSpacing", params));
304         sb.append("\"");
305         sb.append(" cellpadding=\"");
306         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getCellPadding", params));
307         sb.append("\" bgcolor=\"");
308         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getNorthBackGround", params));
309         sb.append("\">");
310         sb.append("<tr><td>");
311         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getNorth", params));
312
313         return sb.toString();
314     }
315
316     public void setNorthContent(String JavaDoc html, Integer JavaDoc position, String JavaDoc moduleName) {
317         if (position == null) {
318             position = new Integer JavaDoc(northContentMax);
319             northContentMax--;
320         }
321         this.northContent.put(position, html);
322         this.northModulePositions.put(moduleName, position);
323     }
324
325     public String JavaDoc getNorthContent() {
326         return getNorthContent(false);
327     }
328
329     public String JavaDoc getNorthContent(boolean includeNavBlock) {
330         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
331         for (Iterator JavaDoc it = northContent.keySet().iterator(); it.hasNext();) {
332             Integer JavaDoc key = (Integer JavaDoc) it.next();
333             String JavaDoc moduleName = null;
334             for(Iterator JavaDoc itr = northModulePositions.keySet().iterator();
335                 itr.hasNext();){
336                 String JavaDoc key2 = (String JavaDoc)itr.next();
337                 Integer JavaDoc value = (Integer JavaDoc)northModulePositions.get(key2);
338                 if(value.equals(key)) {
339                     moduleName = key2;
340                     break;
341                 }
342             }
343             String JavaDoc content = (String JavaDoc) northContent.get(key);
344             if (includeNavBlock && !content.trim().equals("")
345                 && moduleName != null && !"".equals(moduleName.trim())) {
346                 sb.append(NAV_BLOCK_1);
347                 sb.append(moduleName);
348                 sb.append(NAV_BLOCK_2);
349                 sb.append(content);
350                 sb.append(NAV_BLOCK_3);
351             } else {
352                 sb.append(content);
353             }
354         }
355         return sb.toString();
356     }
357
358     public String JavaDoc getSouth() throws Exception JavaDoc {
359         Object JavaDoc[] params = { this };
360         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
361         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getSouth", params));
362
363         return sb.toString();
364     }
365
366     public void setSouthContent(String JavaDoc html, Integer JavaDoc position, String JavaDoc moduleName) {
367         if (position == null) {
368             position = new Integer JavaDoc(southContentMax);
369             southContentMax--;
370         }
371         this.southContent.put(position, html);
372         this.southModulePositions.put(moduleName, position);
373     }
374
375     public String JavaDoc getSouthContent() {
376         return getSouthContent(false);
377     }
378
379     public String JavaDoc getSouthContent(boolean includeNavBlock) {
380         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
381         for (Iterator JavaDoc it = southContent.keySet().iterator(); it.hasNext();) {
382             Integer JavaDoc key = (Integer JavaDoc) it.next();
383             String JavaDoc moduleName = null;
384             for(Iterator JavaDoc itr = southModulePositions.keySet().iterator();
385                 itr.hasNext();){
386                 String JavaDoc key2 = (String JavaDoc)itr.next();
387                 Integer JavaDoc value = (Integer JavaDoc)southModulePositions.get(key2);
388                 if(value.equals(key)) {
389                     moduleName = key2;
390                     break;
391                 }
392             }
393             String JavaDoc content = (String JavaDoc) southContent.get(key);
394             if (includeNavBlock && !content.trim().equals("")
395                 && moduleName != null && !"".equals(moduleName.trim())) {
396                 sb.append(NAV_BLOCK_1);
397                 sb.append(moduleName);
398                 sb.append(NAV_BLOCK_2);
399                 sb.append(content);
400                 sb.append(NAV_BLOCK_3);
401             } else {
402                 sb.append(content);
403             }
404         }
405         return sb.toString();
406     }
407
408     public String JavaDoc getThemeWelcomeContent() throws Exception JavaDoc {
409         return (String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getWelcomePageContent", null);
410     }
411
412     public String JavaDoc getWest() throws Exception JavaDoc {
413         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
414         sb.append("<!--here-->\n<tr bgcolor=\"#FFFFFF\">");
415         Object JavaDoc[] params = { this };
416         sb.append((String JavaDoc)InvokerFactory.getRemoteInvoker().invokeMethodOnTheme(theme, "getWest", params));
417         return sb.toString();
418     }
419
420     public void setWestContent(String JavaDoc html, Integer JavaDoc position, String JavaDoc moduleName) {
421         if (position == null) {
422             position = new Integer JavaDoc(westContentMax);
423             westContentMax--;
424         }
425         this.westContent.put(position, html);
426         this.westModulePositions.put(moduleName, position);
427     }
428
429     public String JavaDoc getWestContent() {
430         return getWestContent(true);
431     }
432
433     public String JavaDoc getWestContent(boolean includeNavBlock) {
434         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
435         for (Iterator JavaDoc it = westContent.keySet().iterator(); it.hasNext();) {
436             Integer JavaDoc key = (Integer JavaDoc) it.next();
437             String JavaDoc moduleName = null;
438             for(Iterator JavaDoc itr = westModulePositions.keySet().iterator();
439                 itr.hasNext();){
440                 String JavaDoc key2 = (String JavaDoc)itr.next();
441                 Integer JavaDoc value = (Integer JavaDoc)westModulePositions.get(key2);
442                 if(value.equals(key)) {
443                     moduleName = key2;
444                     break;
445                 }
446             }
447             String JavaDoc content = (String JavaDoc) westContent.get(key);
448             if (includeNavBlock && !content.trim().equals("")
449                 && moduleName != null && !"".equals(moduleName.trim())) {
450                 sb.append(NAV_BLOCK_1);
451                 sb.append(moduleName);
452                 sb.append(NAV_BLOCK_2);
453                 sb.append(content);
454                 sb.append(NAV_BLOCK_3);
455             } else {
456                 sb.append(content);
457             }
458         }
459         return sb.toString();
460     }
461
462     public String JavaDoc displayFootMessage() {
463         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
464         sb.append("<font class=\"pn-sub\">\n${config:" + Constants.CFG_FOOT1
465                   + "}<br>\n</font>\n");
466
467         return sb.toString();
468     }
469
470     public long process(PrintWriter JavaDoc out) throws IOException JavaDoc, Exception JavaDoc {
471         long time = -System.currentTimeMillis();
472
473         // this is just a special case when the user is generating
474
// print content that the project link is not on the page
475
//
476
if ( theme != null && theme.indexOf("PrinterUtil") == -1 ) {
477             out.println(getHeader());
478             out.println(getNorth());
479             out.println(getWest());
480             out.println(getCenter());
481             out.println(getEast());
482             out.println(getSouth());
483             out.println(getFooter());
484         }
485         else {
486             out.println(getCenterContent());
487         }
488         
489         time += System.currentTimeMillis();
490         
491         return time;
492     }
493
494     /**
495      * This method can be used to clear a modules set content from the
496      * page before it has been rendered. The content is returned so you
497      * can do what you would like with the content but it will not show
498      * up in the page unless placed there manually.
499      *
500      * @param moduleName is the name of the modules content to clear.
501      * @return is the content html string if content has been set for the
502      * module, null otherwise.
503      */

504     public String JavaDoc clearModuleContent(String JavaDoc moduleName) {
505         // Search through the content holders for the module
506
Integer JavaDoc position = (Integer JavaDoc)centerModulePositions.get(moduleName);
507         if(position != null) {
508             centerModulePositions.remove(moduleName);
509             return (String JavaDoc)centerContent.remove(position);
510         }
511         position = (Integer JavaDoc)eastModulePositions.get(moduleName);
512         if(position != null) {
513             eastModulePositions.remove(moduleName);
514             return (String JavaDoc)eastContent.remove(position);
515         }
516         position = (Integer JavaDoc)westModulePositions.get(moduleName);
517         if(position != null) {
518             westModulePositions.remove(moduleName);
519             return (String JavaDoc)westContent.remove(position);
520         }
521         position = (Integer JavaDoc)northModulePositions.get(moduleName);
522         if(position != null) {
523             northModulePositions.remove(moduleName);
524             return (String JavaDoc)northContent.remove(position);
525         }
526         position = (Integer JavaDoc)southModulePositions.get(moduleName);
527         if(position != null) {
528             southModulePositions.remove(moduleName);
529             return (String JavaDoc)southContent.remove(position);
530         }
531         return null;
532     }
533
534     public String JavaDoc getPositionForModule(String JavaDoc moduleName) {
535         Object JavaDoc[] params = {moduleName};
536         try {
537             return (String JavaDoc)InvokerFactory.getRemoteInvoker()
538                 .invokeMethodOnTheme(theme, "getPositionForModule", params);
539         } catch(Exception JavaDoc e) {
540             return Theme.NULL_POSITION;
541         }
542     }
543
544     private String JavaDoc determineTheme() {
545         if ( context == null ) {
546             return ThemeSupport.getCurrentThemeLocalName();
547         }
548         else {
549             String JavaDoc t = context.getTheme();
550             if ( t == null || t.equals(ThemeSupport.getCurrentThemeLocalName()) ) {
551                 if ( context.getProject() != null ) {
552                     String JavaDoc projectTheme = context.getProject().getThemeName();
553                     if ( projectTheme != null && !projectTheme.trim().equals("") )
554                         return projectTheme;
555                     else
556                         return ThemeSupport.getCurrentThemeLocalName();
557                 }
558                 else {
559                     return ThemeSupport.getCurrentThemeLocalName();
560                 }
561             }
562             else {
563                 return t;
564             }
565         }
566     }
567 }
568
Popular Tags