KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > servlet > viewhandler > DefaultViewHandler


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.servlet.viewhandler;
66
67 import com.jcorporate.expresso.core.controller.Block;
68 import com.jcorporate.expresso.core.controller.ControllerElement;
69 import com.jcorporate.expresso.core.controller.ControllerException;
70 import com.jcorporate.expresso.core.controller.ControllerResponse;
71 import com.jcorporate.expresso.core.controller.ErrorCollection;
72 import com.jcorporate.expresso.core.controller.Input;
73 import com.jcorporate.expresso.core.controller.Output;
74 import com.jcorporate.expresso.core.controller.State;
75 import com.jcorporate.expresso.core.controller.Transition;
76 import com.jcorporate.expresso.core.db.DBException;
77 import com.jcorporate.expresso.core.dbobj.ValidValue;
78 import com.jcorporate.expresso.core.jsdkapi.GenericSession;
79 import com.jcorporate.expresso.core.misc.ConfigManager;
80 import com.jcorporate.expresso.core.misc.ConfigurationException;
81 import com.jcorporate.expresso.core.misc.StringUtil;
82 import com.jcorporate.expresso.services.dbobj.Setup;
83 import com.jcorporate.expresso.services.html.Break;
84 import com.jcorporate.expresso.services.html.Button;
85 import com.jcorporate.expresso.services.html.Cell;
86 import com.jcorporate.expresso.services.html.CheckBox;
87 import com.jcorporate.expresso.services.html.DropDown;
88 import com.jcorporate.expresso.services.html.FileInput;
89 import com.jcorporate.expresso.services.html.Form;
90 import com.jcorporate.expresso.services.html.Heading;
91 import com.jcorporate.expresso.services.html.HiddenField;
92 import com.jcorporate.expresso.services.html.HtmlElement;
93 import com.jcorporate.expresso.services.html.HtmlException;
94 import com.jcorporate.expresso.services.html.Page;
95 import com.jcorporate.expresso.services.html.Paragraph;
96 import com.jcorporate.expresso.services.html.Radio;
97 import com.jcorporate.expresso.services.html.Row;
98 import com.jcorporate.expresso.services.html.Table;
99 import com.jcorporate.expresso.services.html.Text;
100 import com.jcorporate.expresso.services.html.TextArea;
101 import com.jcorporate.expresso.services.html.TextField;
102 import org.apache.log4j.Logger;
103 import org.apache.struts.action.ActionMessage;
104
105 import javax.servlet.ServletException JavaDoc;
106 import javax.servlet.http.HttpServletRequest JavaDoc;
107 import javax.servlet.http.HttpServletResponse JavaDoc;
108 import java.io.IOException JavaDoc;
109 import java.util.Enumeration JavaDoc;
110 import java.util.Hashtable JavaDoc;
111 import java.util.Iterator JavaDoc;
112 import java.util.Vector JavaDoc;
113
114
115 /**
116  * ViewHandler designed to generate a default view in HTML when no other view is specified
117  *
118  * @author Michael Nash
119  */

120 public class DefaultViewHandler
121         extends ViewHandler {
122     private static Logger log = Logger.getLogger(DefaultViewHandler.class);
123
124     /* No-args constructor for dynamic instantiation */
125     public DefaultViewHandler() {
126     }
127
128     /**
129      * Display the response from a controller with a "default" view generated in HTML
130      *
131      * @param con The ControllerResponse object to render
132      * @param req the Servlet Request.
133      * @param res the Servlet response
134      * @throws ServletException upon HTML rendering error
135      * @throws ControllerException upon error querying controller response object
136      * @throws IOException upon i/o error
137      */

138     public void handleView(ControllerResponse con, HttpServletRequest JavaDoc req,
139                            HttpServletResponse JavaDoc res)
140             throws ServletException JavaDoc, IOException JavaDoc, ControllerException {
141         try {
142             if (log.isDebugEnabled()) {
143                 log.debug("Begin processing default view");
144             }
145
146             Page myPage = new Page(con.getTitle());
147             Paragraph p1 = new Paragraph(new Text(con.getTitle()));
148             p1.setCSSClass("jc-pageheader");
149             myPage.add(p1);
150
151             State currState = con.getCurrentState();
152             myPage.add(new Heading(currState.getDescription(), 4));
153
154             Vector JavaDoc blocks = con.getBlocks();
155             Vector JavaDoc inputs = con.getInputs();
156             Vector JavaDoc outputs = con.getOutputs();
157             Vector JavaDoc transitions = con.getTransitions();
158             ErrorCollection ee = con.getErrors();
159
160             if (ee != null) {
161                 if (!ee.isEmpty()) {
162                     displayErrors(myPage, ee);
163                 }
164             }
165             if ((blocks == null) && (inputs == null) && (outputs == null) &&
166                     (transitions == null)) {
167                 if (log.isDebugEnabled()) {
168                     log.debug("No results to display");
169                 }
170                 throw new ServletException JavaDoc("No input, output or transitions " +
171                         "defined for this Controller");
172             }
173             if (outputs != null) {
174                 Table outputTable = new Table("Output Table");
175                 outputTable.setAlignment("center");
176
177                 // outputTable.setWidth("95%");
178
outputTable.setBorder(0);
179                 add(con, outputs, outputTable);
180
181                 if (outputTable.getContentCount() > 0) {
182                     myPage.add(outputTable);
183                 }
184             } /* if outputs not null */
185
186
187             Form f = new Form("Input and transitions");
188             f.setCSSClass("jc-default");
189             String JavaDoc encoding = null;
190             f.setAction(Setup.getValueRequired(con.getDBName(), "ContextPath") +
191                     con.getRequestPath());
192
193             /* If the controller has an attribute that specifies a particular */
194
195             /* form encoding - use it */
196             encoding = StringUtil.notNull(con.getAttribute("formEncoding"));
197
198             if (!encoding.equals("")) {
199                 f.setEncoding(encoding);
200             }
201             if (blocks != null) {
202                 for (Enumeration JavaDoc eb = blocks.elements(); eb.hasMoreElements();) {
203                     addBlock(con, f, (Block) eb.nextElement());
204                 }
205             } /* if inputs not null */
206
207             if (inputs != null) {
208
209                 /* Check to see if we have any "file" type inputs */
210
211                 /* if so, and we have not had an explicit coding set for the form */
212
213                 /* use multipart/form-data encoding */
214                 Input oneInput = null;
215
216                 for (Enumeration JavaDoc ei = inputs.elements(); ei.hasMoreElements();) {
217                     oneInput = (Input) ei.nextElement();
218
219                     String JavaDoc s = oneInput.getType();
220
221                     if (s != null && s.equals("file")) {
222                         if (encoding.equals("")) {
223                             f.setEncoding("multipart/form-data");
224                         }
225                     }
226                 }
227
228                 Table inputTable = new Table("Input Table");
229                 add(con, inputs, inputTable);
230
231                 if (inputTable.getContentCount() > 0) {
232                     f.add(inputTable);
233                 }
234             } /* if inputs not null */
235
236             if (transitions != null) {
237                 Table transitionTable = new Table("Transitions Table");
238                 Row transitionRow = new Row();
239                 transitionTable.add(transitionRow);
240                 addTransitions(transitions, transitionRow, f, con);
241
242                 if (transitionRow.getContentCount() > 0) {
243                     f.add(transitionTable);
244                 }
245             } /* if transitions not null */
246
247             if (f.getContentCount() > 0) {
248                 myPage.add(f);
249             }
250
251             Object JavaDoc o = GenericSession.getAttribute(req,
252                     com.jcorporate.expresso.ext.taglib.Back.BACK_KEY +
253                     ".URL");
254             Object JavaDoc ot = GenericSession.getAttribute(req,
255                     com.jcorporate.expresso.ext.taglib.Back.BACK_KEY +
256                     ".TITLE");
257
258             if (o != null) {
259                 String JavaDoc backUrl = (String JavaDoc) o;
260                 String JavaDoc title = (String JavaDoc) ot;
261                 String JavaDoc backImage = ConfigManager.getContext("default").getImages() +
262                         "/FingerLeft.gif";
263
264                 if (!backUrl.equals("NONE")) {
265                     myPage.add(new Text("<p align=\"center\"><a HREF=\"" + backUrl +
266                             "\" title=\"" + title +
267                             "\"><img SRC=\"" + backImage +
268                             "\" border=\"0\"></a></p>"));
269                 }
270             }
271
272             printDisplay(con, req, res, myPage);
273             if (log.isDebugEnabled()) {
274                 log.debug("Finished displaying default view");
275             }
276         } catch (NullPointerException JavaDoc npe) {
277             npe.printStackTrace();
278             log.warn("NullPointerException:", npe);
279             throw npe;
280         } catch (HtmlException he) {
281             log.warn("HtmlException:", he);
282             throw new ControllerException(he);
283         } catch (DBException de) {
284             log.warn("DBException:", de);
285             throw new ControllerException(de);
286         } catch (ConfigurationException ce) {
287             throw new ControllerException(ce);
288         }
289     } /* doController(HttpServletRequest, HttpServletResponse, Hashtable) */
290
291
292     /**
293      * Separate to allow overriding for different types of rendering.
294      *
295      * @param con The ControllerResponse object to render
296      * @param request the Servlet Request.
297      * @param response the Servlet response
298      * @param p the Page to display
299      * @throws HtmlException upon rendering error
300      * @throws ControllerException upon i18n error
301      */

302     protected void printDisplay(ControllerResponse con,
303                                 HttpServletRequest JavaDoc request,
304                                 HttpServletResponse JavaDoc response, Page p)
305             throws HtmlException, ControllerException {
306         p.display(request, response, con.getString("charset"));
307     }
308
309     /**
310      * Displays the errors
311      *
312      * @param myPage page to add the errors for
313      * @param errs the error collection to add to the page
314      * @throws HtmlException upon rendering error
315      */

316     private void displayErrors(Page myPage, ErrorCollection errs)
317             throws HtmlException {
318         Heading errorHeading = new Heading("Errors", 3);
319         errorHeading.setFontColor("red");
320         myPage.add(errorHeading);
321
322         Table errorTable = new Table("errors");
323         errorTable.setBorder(1);
324
325         Row oneRow = null;
326
327         for (Iterator JavaDoc i = errs.get(); i.hasNext();) {
328             ActionMessage oneError = (ActionMessage) i.next();
329             oneRow = new Row("one error");
330
331             Text t = new Text(oneError.getKey());
332             t.setFontColor("red");
333             oneRow.add(new Cell(t));
334             errorTable.add(oneRow);
335         }
336
337         myPage.add(errorTable);
338         myPage.add(new Break());
339     }
340
341     /**
342      * Add a Vector of Transition objects to the specified HtmlElement object.
343      * We are also passed the Form object in order to add the necessary hidden fields
344      * for parameters for each transition.
345      *
346      * @param h the list of transitions
347      * @param res the ControllerREsponse object
348      * @param r the table row to add to
349      * @param f the html element to add to
350      * @throws HtmlException upon rendering error
351      */

352     private void addTransitions(Vector JavaDoc h, Row r, HtmlElement f,
353                                 ControllerResponse res)
354             throws HtmlException {
355         Transition a = null;
356         Cell oneCell = null;
357         Button oneButton = null;
358         String JavaDoc paramString = null;
359         Hashtable JavaDoc params = null;
360         HiddenField hidden = null;
361
362         /* Add the parameters by creating a hidden "xxx_params" field for each transition */
363         for (Enumeration JavaDoc e = h.elements(); e.hasMoreElements();) {
364             a = (Transition) e.nextElement();
365
366             if (a != null) {
367                 params = a.getParams();
368
369                 if (params != null) {
370                     paramString = a.getParamString();
371                     hidden = new HiddenField(a.getName() + "_params",
372                             paramString);
373                 }
374                 if (f != null) {
375                     f.add(hidden);
376                 } else {
377                     r.add(hidden);
378                 }
379             }
380         }
381         /* Add an actual button for each of the specified transitions to the row we were given */
382         for (Enumeration JavaDoc e = h.elements(); e.hasMoreElements();) {
383             a = (Transition) e.nextElement();
384
385             if (a != null) {
386                 oneCell = new Cell();
387                 oneCell.setAlignment("center");
388                 oneCell.setVerticalAlignment("center");
389
390                 String JavaDoc labelString = null;
391
392                 try {
393                     labelString = res.getString(a.getLabel());
394                 } catch (Exception JavaDoc ee) {
395                     labelString = a.getLabel();
396                 }
397
398                 oneButton = new Button("button_" + a.getName(), labelString);
399                 oneCell.add(oneButton);
400                 r.add(oneCell);
401             }
402         }
403     } /* addTransitions(Vector, Row) */
404
405
406     /**
407      * Add an Input item to the specified row in a table
408      *
409      * @param i the input to render
410      * @param oneRow the row to render to
411      * @param con the controller response where we got the input
412      * @throws HtmlException upon rendering error
413      * @throws ConfigurationException upon error getting data contexts and setup values
414      * @throws ControllerException upon other exception
415      */

416     private void addInput(Input i, Row oneRow, ControllerResponse con)
417             throws HtmlException, ConfigurationException,
418             ControllerException {
419         String JavaDoc l = i.getLabel();
420         Cell oneCell;
421
422         if (l != null && l.length() != 0) {
423             oneCell = new Cell();
424             oneCell.add(new Text(l + ":"));
425             oneCell.setCSSClass("jc-label");
426             oneRow.add(oneCell);
427         }
428
429         oneCell = new Cell();
430         oneCell.setCSSClass("jc-formfield");
431         oneRow.add(oneCell);
432
433         if (StringUtil.notNull(i.getAttribute("readOnly")).equalsIgnoreCase("Y")) {
434             oneCell.add(new Text(i.getDefaultValue()));
435
436             return;
437         }
438
439         String JavaDoc defaultValue = i.getDefaultValue();
440
441         if (defaultValue == null) {
442             defaultValue = ("");
443         }
444
445         /* if the input is multi-valued, use a drop-down list */
446         Vector JavaDoc validValues = i.getValidValues();
447         ValidValue oneValue = null;
448
449         if (validValues != null && !validValues.isEmpty()) {
450             if (i.getAttributes().containsKey("radio") ||
451                     i.getAttributes().containsKey("radio-vertical")) {
452                 Radio radio = new Radio(i.getName(), defaultValue);
453
454                 for (Enumeration JavaDoc vv = validValues.elements();
455                      vv.hasMoreElements();) {
456                     ValidValue v = (ValidValue) vv.nextElement();
457                     radio.addOption(v.getValue(), v.getDescription());
458                 }
459                 if (i.getAttributes().containsKey("radio-vertical")) {
460                     radio.setVertical(true);
461                 }
462
463                 oneCell.add(radio);
464             } else if (i.getAttributes().containsKey("checkbox") ||
465                     i.getAttributes().containsKey("checkbox-vertical")) {
466                 CheckBox check = new CheckBox(i.getName(), defaultValue);
467
468                 for (Enumeration JavaDoc vv = validValues.elements();
469                      vv.hasMoreElements();) {
470                     ValidValue v = (ValidValue) vv.nextElement();
471                     check.addOption(v.getValue(), v.getDescription());
472                 }
473
474                 oneCell.add(check);
475
476                 if (i.getAttributes().containsKey("checkbox-vertical")) {
477                     check.setVertical(true);
478                 }
479             } else {
480                 DropDown d = new DropDown(i.getName());
481
482                 for (Enumeration JavaDoc vv = validValues.elements();
483                      vv.hasMoreElements();) {
484                     oneValue = (ValidValue) vv.nextElement();
485                     d.addOption(oneValue.getValue(), oneValue.getDescription());
486                 }
487
488                 d.setCurrentValue(defaultValue);
489                 oneCell.add(d);
490             }
491         } else if (i.getAttributes().containsKey("checkbox")) {
492
493             /* if it's boolean, use a checkbox */
494             if (defaultValue.equalsIgnoreCase("Y")) {
495                 oneCell.add(new CheckBox(i.getName(), "Y", true));
496             } else {
497                 oneCell.add(new CheckBox(i.getName(), "Y", false));
498             }
499
500             oneCell.setAlignment("center");
501         } else if (i.getAttributes().containsKey("textarea")) {
502             oneCell.add(new TextArea(i.getName(),
503                     StringUtil.notNull(defaultValue).trim(),
504                     i.getLines(), i.getDisplayLength()));
505         } else if (i.getAttributes().containsKey("password")) {
506             oneCell.add(new TextField(i.getName(), defaultValue,
507                     i.getMaxLength(), i.getDisplayLength()));
508         } else if (i.getAttributes().containsKey("submit")) {
509             oneCell.add(new Button(i.getName(), i.getName()));
510         } else if (i.getAttributes().containsKey("reset")) {
511         } else if (i.getAttributes().containsKey("file")) {
512             oneCell.add(new FileInput(i.getName(), defaultValue));
513         } else if (i.getAttributes().containsKey("hidden")) {
514             oneCell.add(new HiddenField(i.getName(), defaultValue));
515         } else {
516
517             /* else use a text field */
518             if (i.getLines() > 1) {
519
520                 /* use a text area */
521                 oneCell.add(new TextArea(i.getName(), defaultValue,
522                         i.getLines(), i.getDisplayLength()));
523             } else {
524                 oneCell.add(new TextField(i.getName(), defaultValue,
525                         i.getMaxLength(),
526                         i.getDisplayLength()));
527             }
528         }
529         if (i.getNested() != null) {
530             Vector JavaDoc v = i.getNested();
531             ControllerElement oneElement = null;
532
533             for (Enumeration JavaDoc ve = v.elements(); ve.hasMoreElements();) {
534                 oneElement = (ControllerElement) ve.nextElement();
535
536                 if (oneElement instanceof Input) {
537                     addInput((Input) oneElement, oneRow, con);
538                 } else if (oneElement instanceof Output) {
539                     addOutput((Output) oneElement, oneRow, con);
540                 } else if (oneElement instanceof Transition) {
541                     Transition nestedTransition = (Transition) oneElement;
542                     String JavaDoc newText = "<a HREF=\"" +
543                             ConfigManager.getContextPath() +
544                             con.getRequestPath() + "?" +
545                             nestedTransition.getParamString() +
546                             "\" target=\"_new\"><img SRC=\"" +
547                             ConfigManager.getContext(con.getDBName()).getImages() +
548                             "/search.png\" border=\"0\"></a>";
549                     Cell lookupCell = new Cell(new Text(newText));
550                     oneRow.add(lookupCell);
551                 }
552             } /* for each nested element */
553
554         } /* if any nested elements */
555
556
557         // if (!lookupAdded) {
558
// oneRow.add(new Cell(new Text("&nbsp;")));
559
// }
560
}
561
562     private void addInput(Input i, Table t, ControllerResponse con)
563             throws HtmlException, ConfigurationException,
564             ControllerException {
565         Row oneRow = new Row();
566         t.add(oneRow);
567         addInput(i, oneRow, con);
568     } /* addInput(Input, Table) */
569
570
571     private void addOutput(Output o, Row oneRow, ControllerResponse con)
572             throws HtmlException {
573         Cell oneCell = null;
574         String JavaDoc l = o.getLabel();
575
576         if (l != null && l.length() > 0) {
577             oneCell = new Cell();
578             oneCell.setCSSClass("jc-label");
579
580             if (o.getStyle() != null) {
581                 oneCell.add(new Text(l, o.getStyle()));
582             } else {
583                 oneCell.add(new Text(l));
584             }
585             if (oneRow.isCaptionRow()) {
586                 oneCell.setCSSClass("jc-tabletitle");
587                 oneCell.setHeaderCell(true);
588             } else {
589                 oneCell.setNoWrap(false);
590             }
591
592             oneRow.add(oneCell);
593         }
594
595         boolean anyNested = false;
596         Vector JavaDoc v = o.getNested();
597
598         if (v != null) {
599             if (v.size() > 0) {
600                 anyNested = true;
601             }
602         }
603
604         Transition nestedTransition = null;
605         ControllerElement ce = null;
606
607         if (anyNested) {
608             for (Enumeration JavaDoc tn = v.elements(); tn.hasMoreElements();) {
609                 ce = (ControllerElement) tn.nextElement();
610
611                 if (ce instanceof Transition) {
612                     nestedTransition = (Transition) ce;
613                 }
614             }
615         }
616
617         String JavaDoc c = o.getContent();
618
619         if (c != null) {
620             oneCell = new Cell();
621
622             if (oneRow.isCaptionRow()) {
623                 oneCell.setCSSClass("jc-tabletitle");
624                 oneCell.setHeaderCell(true);
625             } else {
626                 oneCell.setCSSClass("jc-default");
627             }
628             if (nestedTransition == null) {
629                 oneCell.add(new Text(c));
630             } else {
631                 String JavaDoc newText = "<a HREF=\"" +
632                         ConfigManager.getContextPath() +
633                         con.getRequestPath() + "?" +
634                         nestedTransition.getParamString() + "\">" + c +
635                         "</a>";
636                 oneCell.add(new Text(newText));
637             }
638
639             oneRow.add(oneCell);
640         }
641         if (anyNested) {
642             Cell c2 = new Cell();
643             c2.setNoWrap(false);
644             oneRow.add(c2);
645
646             Table t2 = new Table("Nested Output");
647             t2.setBorder(1);
648             c2.add(t2);
649
650             for (Enumeration JavaDoc ve = v.elements(); ve.hasMoreElements();) {
651                 ce = (ControllerElement) ve.nextElement();
652
653                 if (ce instanceof Output) {
654                     addOutput((Output) ce, t2, con);
655                 } else if (ce instanceof Transition) {
656                     nestedTransition = (Transition) ce;
657                 }
658             }
659         } /* if any nested elements */
660
661     }
662
663     /**
664      * Add the designated output to the given table
665      *
666      * @param o The Output object to be added to this table
667      * @param t The Table object that the output should be added to
668      * @param con the ControllerResponse object
669      * @throws HtmlException upon rendering error
670      */

671     private void addOutput(Output o, Table t, ControllerResponse con)
672             throws HtmlException {
673         Row oneRow = new Row();
674         t.add(oneRow);
675         addOutput(o, oneRow, con);
676     } /* addOutput(Output, Table) */
677
678
679     /**
680      * Type generic method to addOutput/addInput/addTransition
681      *
682      * @param con The controller response
683      * @param v the list of controller elements
684      * @param t the table to add to
685      */

686     private void add(ControllerResponse con, Vector JavaDoc v, Table t)
687             throws HtmlException, ConfigurationException,
688             ControllerException {
689         for (Enumeration JavaDoc e = v.elements(); e.hasMoreElements();) {
690             ControllerElement o = (ControllerElement) e.nextElement();
691
692             if (o instanceof Output) {
693                 addOutput((Output) o, t, con);
694             } else if (o instanceof Input) {
695                 addInput((Input) o, t, con);
696
697                 //} else if (o instanceof Block) {
698
// addBlock(con, (Block) o, t);
699
}
700         } /* for each element in the vector */
701
702     } /* add(Vector, Table) */
703
704
705     private void addBlockAsTable(Block b, HtmlElement myPage,
706                                  ControllerResponse con)
707             throws HtmlException, ConfigurationException,
708             ControllerException {
709         Table blockTable = new Table();
710         blockTable.setBorder(1);
711         myPage.add(blockTable);
712
713         if (b.getAttributes().containsKey("header-row")) {
714             Row oneRow = new Row();
715             oneRow.setTitle(b.getAttribute("header-row"));
716             blockTable.add(oneRow);
717         }
718
719         Block oneBlockRow = null;
720         ControllerElement ce = null;
721
722         for (Enumeration JavaDoc erows = b.getNested().elements();
723              erows.hasMoreElements();) {
724             ce = (ControllerElement) erows.nextElement();
725
726             if (ce instanceof Block) {
727                 oneBlockRow = (Block) ce;
728             } else {
729                 throw new HtmlException("Block '" + b.getName() +
730                         "' is specified as a table, but tables must " +
731                         "contain only blocks (one per row) - this block contained a '" +
732                         ce.getClass().getName() + "'");
733             }
734             if ("Y".equals(oneBlockRow.getAttribute("row"))) {
735                 ControllerElement oneCell = null;
736                 Row oneRow = new Row();
737                 blockTable.add(oneRow);
738
739                 for (Enumeration JavaDoc rowContents = oneBlockRow.getNested().elements();
740                      rowContents.hasMoreElements();) {
741                     oneCell = (ControllerElement) rowContents.nextElement();
742
743                     if (oneCell instanceof Output) {
744                         addOutput((Output) oneCell, oneRow, con);
745                     } else if (oneCell instanceof Input) {
746                         addInput((Input) oneCell, oneRow, con);
747                     }
748                 }
749             } else {
750                 throw new HtmlException("Block '" + b.getName() +
751                         "' is specified as a table, but tables must " +
752                         "contain only blocks designed as rows. This block contained a row '" +
753                         oneBlockRow.getName() +
754                         "' that was not specified as a row.");
755             }
756         }
757     } /* addBlockAsTable */
758
759
760     /**
761      * Add a single "block" object to the page. This is done by creating a table to hold the contents of the block,
762      * then adding all of the contents to that table.
763      *
764      * @param con the controller response object
765      * @param container the container element
766      * @param thisBlock the block to render
767      */

768     private void addBlock(ControllerResponse con, HtmlElement container,
769                           Block thisBlock)
770             throws HtmlException, ConfigurationException,
771             ControllerException {
772         if ("Y".equals(thisBlock.getAttribute("table"))) {
773             addBlockAsTable(thisBlock, container, con);
774         } else {
775             if (thisBlock.getNested() != null) {
776                 Vector JavaDoc transitions = new Vector JavaDoc();
777                 Vector JavaDoc v = thisBlock.getNested();
778
779                 if (v.size() > 0) {
780                     Table myBlockTable = new Table("Table for Block " +
781                             thisBlock.getName());
782                     container.add(myBlockTable);
783
784                     for (Enumeration JavaDoc ve = v.elements(); ve.hasMoreElements();) {
785                         ControllerElement ce = (ControllerElement) ve.nextElement();
786
787                         if (ce instanceof Output) {
788                             addOutput((Output) ce, myBlockTable, con);
789                         } else if (ce instanceof Input) {
790                             addInput((Input) ce, myBlockTable, con);
791                         } else if (ce instanceof Transition) {
792                             transitions.addElement(ce);
793                         } else if (ce instanceof Block) {
794                             addBlock(con, container, (Block) ce);
795                         }
796                     }
797                     if (transitions.size() > 0) {
798                         Row r = new Row("transitions for block");
799                         myBlockTable.add(r);
800                         addTransitions(transitions, r, null, con);
801                     }
802                 } /* if nested vector has contents */
803
804             } /* if any nested elements */
805
806         } /* if the block is marked as a table itself */
807
808     } /* addBlock(ControllerResponse, myPage, thisBlock) */
809
810
811 }
Popular Tags