KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > CmsWidgetDialog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/CmsWidgetDialog.java,v $
3  * Date : $Date: 2006/11/07 10:58:26 $
4  * Version: $Revision: 1.62 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace;
33
34 import org.opencms.i18n.CmsEncoder;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.main.I_CmsThrowable;
37 import org.opencms.main.OpenCms;
38 import org.opencms.util.CmsRequestUtil;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.widgets.A_CmsWidget;
41 import org.opencms.widgets.CmsDisplayWidget;
42 import org.opencms.widgets.I_CmsWidget;
43 import org.opencms.widgets.I_CmsWidgetDialog;
44
45 import java.io.IOException JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.Arrays JavaDoc;
48 import java.util.HashMap JavaDoc;
49 import java.util.HashSet JavaDoc;
50 import java.util.Hashtable JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Map JavaDoc;
54 import java.util.Set JavaDoc;
55
56 import javax.servlet.ServletException JavaDoc;
57 import javax.servlet.http.HttpServletRequest JavaDoc;
58 import javax.servlet.http.HttpServletResponse JavaDoc;
59 import javax.servlet.jsp.JspException JavaDoc;
60 import javax.servlet.jsp.JspWriter JavaDoc;
61 import javax.servlet.jsp.PageContext JavaDoc;
62
63 /**
64  * Base class for dialogs that use the OpenCms widgets without XML content.<p>
65  *
66  * @author Alexander Kandzior
67  *
68  * @version $Revision: 1.62 $
69  *
70  * @since 6.0.0
71  */

72 public abstract class CmsWidgetDialog extends CmsDialog implements I_CmsWidgetDialog {
73
74     /** Action for optional element creation. */
75     public static final int ACTION_ELEMENT_ADD = 152;
76
77     /** Action for optional element removal. */
78     public static final int ACTION_ELEMENT_REMOVE = 153;
79
80     /** Value for the action: error in the form validation. */
81     public static final int ACTION_ERROR = 303;
82
83     /** Value for the action: save the dialog. */
84     public static final int ACTION_SAVE = 300;
85
86     /** Request parameter value for the action: save the dialog. */
87     public static final String JavaDoc DIALOG_SAVE = "save";
88
89     /** Indicates an optional element should be created. */
90     public static final String JavaDoc EDITOR_ACTION_ELEMENT_ADD = "addelement";
91
92     /** Indicates an optional element should be removed. */
93     public static final String JavaDoc EDITOR_ACTION_ELEMENT_REMOVE = "removeelement";
94
95     /** Prefix for "hidden" parameters, required since these must be unescaped later. */
96     public static final String JavaDoc HIDDEN_PARAM_PREFIX = "hidden.";
97
98     /** The errors thrown by commit actions. */
99     protected List JavaDoc m_commitErrors;
100
101     /** The object edited with this widget dialog. */
102     protected Object JavaDoc m_dialogObject;
103
104     /** The allowed pages for this dialog in a List. */
105     protected List JavaDoc m_pages;
106
107     /** Controls which page is currently displayed in the dialog. */
108     protected String JavaDoc m_paramPage;
109
110     /** The validation errors for the input form. */
111     protected List JavaDoc m_validationErrorList;
112
113     /** Contains all parameter value of this dialog. */
114     protected Map JavaDoc m_widgetParamValues;
115
116     /** The list of widgets used on the dialog. */
117     protected List JavaDoc m_widgets;
118
119     /** The set of help message ids that have already been used. */
120     private Set JavaDoc m_helpMessageIds;
121
122     /**
123      * Parameter stores the index of the element to add or remove.<p>
124      *
125      * This must not be <code>null</code>, because it must be available
126      * when calling <code>{@link org.opencms.workplace.CmsWorkplace#paramsAsHidden()}</code>.<p>
127      */

128     private String JavaDoc m_paramElementIndex = "0";
129
130     /**
131      * Parameter stores the name of the element to add or remove.<p>
132      *
133      * This must not be <code>null</code>, because it must be available
134      * when calling <code>{@link org.opencms.workplace.CmsWorkplace#paramsAsHidden()}</code>.<p>
135      */

136     private String JavaDoc m_paramElementName = "undefined";
137
138     /** Optional localized key prefix identificator. */
139     private String JavaDoc m_prefix;
140
141     /**
142      * Public constructor with JSP action element.<p>
143      *
144      * @param jsp an initialized JSP action element
145      */

146     public CmsWidgetDialog(CmsJspActionElement jsp) {
147
148         super(jsp);
149     }
150
151     /**
152      * Public constructor with JSP variables.<p>
153      *
154      * @param context the JSP page context
155      * @param req the JSP request
156      * @param res the JSP response
157      */

158     public CmsWidgetDialog(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
159
160         this(new CmsJspActionElement(context, req, res));
161     }
162
163     /**
164      * Deletes the edited dialog object from the session.<p>
165      */

166     public void actionCancel() {
167
168         clearDialogObject();
169     }
170
171     /**
172      * Commits the edited object after pressing the "OK" button.<p>
173      *
174      * @throws IOException in case of errros forwarding to the required result page
175      * @throws ServletException in case of errros forwarding to the required result page
176      */

177     public abstract void actionCommit() throws IOException JavaDoc, ServletException JavaDoc;
178
179     /**
180      * Adds or removes an optional element.<p>
181      *
182      * Depends on the value stored in the <code>{@link CmsDialog#getAction()}</code> method.<p>
183      */

184     public void actionToggleElement() {
185
186         // get the necessary parameters to add/remove the element
187
int index = 0;
188         try {
189             index = Integer.parseInt(getParamElementIndex());
190         } catch (Exception JavaDoc e) {
191             // ignore, should not happen
192
}
193         String JavaDoc name = getParamElementName();
194         // get the base parameter definition
195
CmsWidgetDialogParameter base = getParameterDefinition(name);
196         if (base != null) {
197             // the requested parameter is valid for this dialog
198
List JavaDoc params = (List JavaDoc)getParameters().get(name);
199             if (getAction() == ACTION_ELEMENT_REMOVE) {
200                 // remove the value
201
params.remove(index);
202             } else {
203                 List JavaDoc sequence = (List JavaDoc)getParameters().get(base.getName());
204                 if (sequence.size() > 0) {
205                     // add the new value after the clicked element
206
index = index + 1;
207                 }
208                 CmsWidgetDialogParameter newParam = new CmsWidgetDialogParameter(base, index);
209                 params.add(index, newParam);
210             }
211             // reset all index value in the parameter list
212
for (int i = 0; i < params.size(); i++) {
213                 CmsWidgetDialogParameter param = (CmsWidgetDialogParameter)params.get(i);
214                 param.setindex(i);
215             }
216         }
217     }
218
219     /**
220      * Returns the html for a button to add an optional element.<p>
221      *
222      * @param elementName name of the element
223      * @param insertAfter the index of the element after which the new element should be created
224      * @param enabled if true, the button to add an element is shown, otherwise a spacer is returned
225      * @return the html for a button to add an optional element
226      */

227     public String JavaDoc buildAddElement(String JavaDoc elementName, int insertAfter, boolean enabled) {
228
229         if (enabled) {
230             StringBuffer JavaDoc href = new StringBuffer JavaDoc(4);
231             href.append("javascript:addElement('");
232             href.append(elementName);
233             href.append("', ");
234             href.append(insertAfter);
235             href.append(");");
236             return button(href.toString(), null, "new.png", Messages.GUI_DIALOG_BUTTON_ADDNEW_0, 0);
237         } else {
238             return "";
239         }
240     }
241
242     /**
243      * Builds the HTML for the dialog form.<p>
244      *
245      * @return the HTML for the dialog form
246      */

247     public String JavaDoc buildDialogForm() {
248
249         // create the dialog HTML
250
return createDialogHtml(getParamPage());
251     }
252
253     /**
254      * Returns the html for a button to remove an optional element.<p>
255      *
256      * @param elementName name of the element
257      * @param index the element index of the element to remove
258      * @param enabled if true, the button to remove an element is shown, otherwise a spacer is returned
259      * @return the html for a button to remove an optional element
260      */

261     public String JavaDoc buildRemoveElement(String JavaDoc elementName, int index, boolean enabled) {
262
263         if (enabled) {
264             StringBuffer JavaDoc href = new StringBuffer JavaDoc(4);
265             href.append("javascript:removeElement('");
266             href.append(elementName);
267             href.append("', ");
268             href.append(index);
269             href.append(");");
270             return button(href.toString(), null, "deletecontent.png", Messages.GUI_DIALOG_BUTTON_DELETE_0, 0);
271         } else {
272             return "";
273         }
274     }
275
276     /**
277      * Clears the "dialog object" for this widget dialog by removing it from the current users session.<p>
278      */

279     public void clearDialogObject() {
280
281         setDialogObject(null);
282     }
283
284     /**
285      * Builds the end HTML for a block with 3D border in the dialog content area.<p>
286      *
287      * @return 3D block start / end segment
288      */

289     public String JavaDoc dialogBlockEnd() {
290
291         StringBuffer JavaDoc result = new StringBuffer JavaDoc(8);
292         result.append(super.dialogBlockEnd());
293         result.append(dialogSpacer());
294         result.append("</td></tr>\n");
295         return result.toString();
296     }
297
298     /**
299      * Builds the start HTML for a block with 3D border and optional subheadline in the dialog content area.<p>
300      *
301      * @param headline the headline String for the block
302      * @return 3D block start / end segment
303      */

304     public String JavaDoc dialogBlockStart(String JavaDoc headline) {
305
306         StringBuffer JavaDoc result = new StringBuffer JavaDoc(8);
307         result.append("<tr><td colspan=\"5\">\n");
308         result.append(super.dialogBlockStart(headline));
309         return result.toString();
310     }
311
312     /**
313      * Creates the HTML for the buttons on the dialog.<p>
314      *
315      * @return the HTML for the buttons on the dialog.<p>
316      */

317     public String JavaDoc dialogButtonsCustom() {
318
319         if (getPages().size() > 1) {
320             // this is a multi page dialog, create buttons according to current page
321
int pageIndex = getPages().indexOf(getParamPage());
322             if (pageIndex == getPages().size() - 1) {
323                 // this is the last dialog page
324
return dialogButtons(new int[] {BUTTON_OK, BUTTON_BACK, BUTTON_CANCEL}, new String JavaDoc[3]);
325             } else if (pageIndex > 0) {
326                 // this is a dialog page between first and last page
327
return dialogButtons(new int[] {BUTTON_BACK, BUTTON_CONTINUE, BUTTON_CANCEL}, new String JavaDoc[3]);
328             } else {
329                 // this is the first dialog page
330
return dialogButtons(new int[] {BUTTON_CONTINUE, BUTTON_CANCEL}, new String JavaDoc[2]);
331             }
332         }
333         boolean onlyDisplay = true;
334         Iterator JavaDoc it = m_widgets.iterator();
335         while (it.hasNext()) {
336             CmsWidgetDialogParameter wdp = (CmsWidgetDialogParameter)it.next();
337             if (!(wdp.getWidget() instanceof CmsDisplayWidget)) {
338                 onlyDisplay = false;
339                 break;
340             }
341         }
342         if (!onlyDisplay) {
343             // this is a single page dialog, create common buttons
344
return dialogButtons(new int[] {BUTTON_OK, BUTTON_CANCEL}, new String JavaDoc[2]);
345         }
346         // this is a display only dialog
347
return "";
348     }
349
350     /**
351      * Performs the dialog actions depending on the initialized action and displays the dialog form.<p>
352      *
353      * @throws JspException if dialog actions fail
354      * @throws IOException if writing to the JSP out fails, or in case of errros forwarding to the required result page
355      * @throws ServletException in case of errros forwarding to the required result page
356      */

357     public void displayDialog() throws JspException JavaDoc, IOException JavaDoc, ServletException JavaDoc {
358
359         displayDialog(false);
360     }
361
362     /**
363      * Performs the dialog actions depending on the initialized action and displays the dialog form if needed.<p>
364      *
365      * @param writeLater if <code>true</code> no output is written,
366      * you have to call manually the <code>{@link #defaultActionHtml()}</code> method.
367      *
368      * @throws JspException if dialog actions fail
369      * @throws IOException if writing to the JSP out fails, or in case of errros forwarding to the required result page
370      * @throws ServletException in case of errros forwarding to the required result page
371      */

372     public void displayDialog(boolean writeLater) throws JspException JavaDoc, IOException JavaDoc, ServletException JavaDoc {
373
374         if (isForwarded()) {
375             return;
376         }
377         switch (getAction()) {
378
379             case ACTION_CANCEL:
380                 // ACTION: cancel button pressed
381
actionCancel();
382                 actionCloseDialog();
383                 break;
384
385             case ACTION_ERROR:
386                 // ACTION: an error occured (display nothing)
387
break;
388
389             case ACTION_SAVE:
390                 // ACTION: save edited values
391
setParamAction(DIALOG_OK);
392                 actionCommit();
393                 if (closeDialogOnCommit()) {
394                     setAction(ACTION_CANCEL);
395                     actionCloseDialog();
396                     break;
397                 }
398                 setAction(ACTION_DEFAULT);
399
400             case ACTION_DEFAULT:
401             default:
402                 // ACTION: show dialog (default)
403
if (!writeLater) {
404                     writeDialog();
405                 }
406         }
407     }
408
409     /**
410      * @see org.opencms.widgets.I_CmsWidgetDialog#getButtonStyle()
411      */

412     public int getButtonStyle() {
413
414         return getSettings().getUserSettings().getEditorButtonStyle();
415     }
416
417     /**
418      * Returns the errors that are thrown by save actions or form generation.<p>
419      *
420      * @return the errors that are thrown by save actions or form generation
421      */

422     public List JavaDoc getCommitErrors() {
423
424         return m_commitErrors;
425     }
426
427     /**
428      * Returns the dialog object for this widget dialog, or <code>null</code>
429      * if no dialog object has been set.<p>
430      *
431      * @return the dialog object for this widget dialog, or <code>null</code>
432      */

433     public Object JavaDoc getDialogObject() {
434
435         if (m_dialogObject == null) {
436             m_dialogObject = getDialogObjectMap().get(getClass().getName());
437         }
438         return m_dialogObject;
439     }
440
441     /**
442      * @see org.opencms.widgets.I_CmsWidgetDialog#getHelpMessageIds()
443      */

444     public Set JavaDoc getHelpMessageIds() {
445
446         if (m_helpMessageIds == null) {
447             m_helpMessageIds = new HashSet JavaDoc();
448         }
449         return m_helpMessageIds;
450     }
451
452     /**
453      * Returns the index of the element to add or remove.<p>
454      *
455      * @return the index of the element to add or remove
456      */

457     public String JavaDoc getParamElementIndex() {
458
459         return m_paramElementIndex;
460     }
461
462     /**
463      * Returns the name of the element to add or remove.<p>
464      *
465      * @return the name of the element to add or remove
466      */

467     public String JavaDoc getParamElementName() {
468
469         return m_paramElementName;
470     }
471
472     /**
473      * Returns the page parameter.<p>
474      *
475      * @return the page parameter
476      */

477     public String JavaDoc getParamPage() {
478
479         return m_paramPage;
480     }
481
482     /**
483      * Returns the value of the widget parameter with the given name, or <code>null</code>
484      * if no such widget parameter is available.<p>
485      *
486      * @param name the widget parameter name to get the value for
487      *
488      * @return the value of the widget parameter with the given name
489      */

490     public String JavaDoc getParamValue(String JavaDoc name) {
491
492         return getParamValue(name, 0);
493     }
494
495     /**
496      * Returns the value of the widget parameter with the given name and index, or <code>null</code>
497      * if no such widget parameter is available.<p>
498      *
499      * @param name the widget parameter name to get the value for
500      * @param index the widget parameter index
501      *
502      * @return the value of the widget parameter with the given name and index
503      */

504     public String JavaDoc getParamValue(String JavaDoc name, int index) {
505
506         List JavaDoc params = (List JavaDoc)m_widgetParamValues.get(name);
507         if (params != null) {
508             if ((index >= 0) && (index < params.size())) {
509                 CmsWidgetDialogParameter param = (CmsWidgetDialogParameter)params.get(index);
510                 if (param.getId().equals(CmsWidgetDialogParameter.createId(name, index))) {
511                     return param.getStringValue(getCms());
512                 }
513             }
514         }
515
516         return null;
517     }
518
519     /**
520      * @see org.opencms.widgets.I_CmsWidgetDialog#getUserAgent()
521      */

522     public String JavaDoc getUserAgent() {
523
524         return getJsp().getRequest().getHeader(CmsRequestUtil.HEADER_USER_AGENT);
525     }
526
527     /**
528      * Generates the HTML for the end of the widget dialog.<p>
529      *
530      * This HTML includes additional components, for example the &lt;div&gt;
531      * tags containing the help texts.<p>
532      *
533      * @return the HTML for the end of the widget dialog
534      */

535     public String JavaDoc getWidgetHtmlEnd() {
536
537         StringBuffer JavaDoc result = new StringBuffer JavaDoc(32);
538         // iterate over unique widgets from collector
539
Iterator JavaDoc i = getWidgets().iterator();
540         while (i.hasNext()) {
541             CmsWidgetDialogParameter param = (CmsWidgetDialogParameter)i.next();
542             result.append(param.getWidget().getDialogHtmlEnd(getCms(), this, param));
543         }
544         return result.toString();
545     }
546
547     /**
548      * Generates the HTML include tags for external JavaScripts files of the used widgets.<p>
549      *
550      * @return the HTML include tags for external JavaScripts files of the used widgets
551      *
552      * @throws JspException if an error occurs during JavaScript generation
553      */

554     public String JavaDoc getWidgetIncludes() throws JspException JavaDoc {
555
556         StringBuffer JavaDoc result = new StringBuffer JavaDoc(32);
557         try {
558             // iterate over unique widgets from collector
559
Iterator JavaDoc i = getWidgets().iterator();
560             Set JavaDoc set = new HashSet JavaDoc();
561             while (i.hasNext()) {
562                 I_CmsWidget widget = ((CmsWidgetDialogParameter)i.next()).getWidget();
563                 if (!set.contains(widget)) {
564                     result.append(widget.getDialogIncludes(getCms(), this));
565                     result.append('\n');
566                     set.add(widget);
567                 }
568             }
569         } catch (Throwable JavaDoc e) {
570
571             includeErrorpage(this, e);
572         }
573         return result.toString();
574     }
575
576     /**
577      * Generates the JavaScript init calls for the used widgets.<p>
578      *
579      * @return the JavaScript init calls for the used widgets
580      *
581      * @throws JspException the JavaScript init calls for the used widgets
582      */

583     public String JavaDoc getWidgetInitCalls() throws JspException JavaDoc {
584
585         StringBuffer JavaDoc result = new StringBuffer JavaDoc(32);
586         try {
587             // iterate over unique widgets from collector
588
Iterator JavaDoc i = getWidgets().iterator();
589             Set JavaDoc set = new HashSet JavaDoc();
590             while (i.hasNext()) {
591                 I_CmsWidget widget = ((CmsWidgetDialogParameter)i.next()).getWidget();
592                 if (!set.contains(widget)) {
593                     result.append(widget.getDialogInitCall(getCms(), this));
594                     set.add(widget);
595                 }
596             }
597         } catch (Throwable JavaDoc e) {
598             includeErrorpage(this, e);
599         }
600         return result.toString();
601     }
602
603     /**
604      * Generates the JavaScript initialization methods for the used widgets.<p>
605      *
606      * @return the JavaScript initialization methods for the used widgets
607      *
608      * @throws JspException if an error occurs during JavaScript generation
609      */

610     public String JavaDoc getWidgetInitMethods() throws JspException JavaDoc {
611
612         StringBuffer JavaDoc result = new StringBuffer JavaDoc(32);
613         try {
614             // iterate over unique widgets from collector
615
Iterator JavaDoc i = getWidgets().iterator();
616             Set JavaDoc set = new HashSet JavaDoc();
617             while (i.hasNext()) {
618                 I_CmsWidget widget = ((CmsWidgetDialogParameter)i.next()).getWidget();
619                 if (!set.contains(widget)) {
620                     result.append(widget.getDialogInitMethod(getCms(), this));
621                     set.add(widget);
622                 }
623             }
624         } catch (Throwable JavaDoc e) {
625             includeErrorpage(this, e);
626         }
627         return result.toString();
628     }
629
630     /**
631      * @see org.opencms.workplace.CmsWorkplace#paramsAsHidden()
632      */

633     public String JavaDoc paramsAsHidden() {
634
635         if (getAction() != ACTION_ERROR) {
636             return super.paramsAsHidden();
637         }
638         // on an error page, also output the widget parameters
639
StringBuffer JavaDoc result = new StringBuffer JavaDoc();
640         result.append(super.paramsAsHidden());
641         result.append('\n');
642         result.append(widgetParamsAsHidden());
643         return result.toString();
644     }
645
646     /**
647      * Stores the given object as "dialog object" for this widget dialog in the current users session.<p>
648      *
649      * @param dialogObject the object to store
650      */

651     public void setDialogObject(Object JavaDoc dialogObject) {
652
653         m_dialogObject = dialogObject;
654         if (dialogObject == null) {
655             // null object: remove the entry from the map
656
getDialogObjectMap().remove(getClass().getName());
657         } else {
658             getDialogObjectMap().put(getClass().getName(), dialogObject);
659         }
660     }
661
662     /**
663      * Sets the index of the element to add or remove.<p>
664      *
665      * @param elementIndex the index of the element to add or remove
666      */

667     public void setParamElementIndex(String JavaDoc elementIndex) {
668
669         m_paramElementIndex = elementIndex;
670     }
671
672     /**
673      * Sets the name of the element to add or remove.<p>
674      *
675      * @param elementName the name of the element to add or remove
676      */

677     public void setParamElementName(String JavaDoc elementName) {
678
679         m_paramElementName = elementName;
680     }
681
682     /**
683      * Sets the page parameter.<p>
684      *
685      * @param paramPage the page parameter to set
686      */

687     public void setParamPage(String JavaDoc paramPage) {
688
689         m_paramPage = paramPage;
690     }
691
692     /**
693      * Returns the values of all widget parameters of this dialog as HTML hidden fields.<p>
694      *
695      * @return the values of all widget parameters of this dialog as HTML hidden fields
696      *
697      * @see org.opencms.workplace.CmsWorkplace#paramsAsHidden()
698      */

699     public String JavaDoc widgetParamsAsHidden() {
700
701         return widgetParamsAsHidden(null);
702     }
703
704     /**
705      * Returns the values of all widget parameters of this dialog as HTML hidden fields,
706      * excluding the widget values that are on the given dialog page.<p>
707      *
708      * This can be used to create multi-page dialogs where the values are passed from
709      * one page to another before everyting is submitted. If a widget A is used on page X,
710      * there should be no "hidden" HTML field for A since otherwise A would have 2 values when
711      * submitting the dialog page: The one from the widget itself and the one from the hidden
712      * field. This may lead to undefined results when processing the submitted values.<p>
713      *
714      * @param excludeDialogPage the dialog page to exclude the values for
715      *
716      * @return the values of all widget parameters of this dialog as HTML hidden fields,
717      * excluding the widget values that are on the given dialog page
718      *
719      * @see org.opencms.workplace.CmsWorkplace#paramsAsHidden()
720      */

721     public String JavaDoc widgetParamsAsHidden(String JavaDoc excludeDialogPage) {
722
723         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
724         Iterator JavaDoc i = m_widgetParamValues.keySet().iterator();
725         while (i.hasNext()) {
726             List JavaDoc params = (List JavaDoc)m_widgetParamValues.get(i.next());
727             Iterator JavaDoc j = params.iterator();
728             while (j.hasNext()) {
729                 CmsWidgetDialogParameter param = (CmsWidgetDialogParameter)j.next();
730                 String JavaDoc value = param.getStringValue(getCms());
731                 if (CmsStringUtil.isNotEmpty(value)
732                     && ((excludeDialogPage == null) || (!param.getDialogPage().equals(excludeDialogPage)))) {
733                     result.append("<input type=\"hidden\" name=\"");
734                     result.append(HIDDEN_PARAM_PREFIX);
735                     result.append(param.getId());
736                     result.append("\" value=\"");
737                     String JavaDoc encoded = CmsEncoder.encode(value, getCms().getRequestContext().getEncoding());
738                     result.append(encoded);
739                     result.append("\">\n");
740                 }
741             }
742         }
743         return result.toString();
744     }
745
746     /**
747      * Writes the dialog html code, only if the <code>{@link #ACTION_DEFAULT}</code> is set.<p>
748      *
749      * @throws JspException if dialog actions fail
750      * @throws IOException if writing to the JSP out fails, or in case of errros forwarding to the required result page
751      */

752     public void writeDialog() throws IOException JavaDoc, JspException JavaDoc {
753
754         if (isForwarded()) {
755             return;
756         }
757         switch (getAction()) {
758             case ACTION_CANCEL:
759             case ACTION_ERROR:
760             case ACTION_SAVE:
761                 break;
762
763             case ACTION_DEFAULT:
764             default:
765                 // ACTION: show dialog (default)
766
setParamAction(DIALOG_SAVE);
767                 JspWriter JavaDoc out = getJsp().getJspContext().getOut();
768                 out.print(defaultActionHtml());
769         }
770     }
771
772     /**
773      * Adds the given error to the list of errors that are thrown by save actions or form generation.<p>
774      *
775      * If the error list has not been initilaized yet, this is done automatically.<p>
776      *
777      * @param error the errors to add
778      */

779     protected void addCommitError(Exception JavaDoc error) {
780
781         if (m_commitErrors == null) {
782             m_commitErrors = new ArrayList JavaDoc();
783         }
784         m_commitErrors.add(error);
785     }
786
787     /**
788      * Adds a new widget parameter definition to the list of all widgets of this dialog.<p>
789      *
790      * @param param the widget parameter definition to add
791      */

792     protected void addWidget(CmsWidgetDialogParameter param) {
793
794         if (m_widgets == null) {
795             m_widgets = new ArrayList JavaDoc();
796         }
797         param.setKeyPrefix(m_prefix);
798         m_widgets.add(param);
799     }
800
801     /**
802      * Returns <code>true</code> if the dialog should be closed after the values have been committed.<p>
803      *
804      * The default implementation returns <code>true</code> in case there are no
805      * commit errors.<p>
806      *
807      * @return <code>true</code> if the dialog should be closed after the values have been committed
808      */

809     protected boolean closeDialogOnCommit() {
810
811         return !hasCommitErrors();
812     }
813
814     /**
815      * Commits all values on the dialog.<p>
816      *
817      * @return a List of all Exceptions that occured when comitting the dialog.<p>
818      */

819     protected List JavaDoc commitWidgetValues() {
820
821         return commitWidgetValues(null);
822     }
823
824     /**
825      * Commits all values on the given dialog page.<p>
826      *
827      * @param dialogPage the dialog (page) to commit
828      *
829      * @return a List of all Exceptions that occured when comitting the dialog page.<p>
830      */

831     protected List JavaDoc commitWidgetValues(String JavaDoc dialogPage) {
832
833         List JavaDoc result = new ArrayList JavaDoc();
834         Iterator JavaDoc i = getWidgets().iterator();
835         while (i.hasNext()) {
836             // check for all widget parameters
837
CmsWidgetDialogParameter base = (CmsWidgetDialogParameter)i.next();
838             if ((dialogPage == null) || (base.getDialogPage() == null) || dialogPage.equals(base.getDialogPage())) {
839                 // the parameter is located on the requested dialog
840
base.prepareCommit();
841                 List JavaDoc params = (List JavaDoc)m_widgetParamValues.get(base.getName());
842                 Iterator JavaDoc j = params.iterator();
843                 while (j.hasNext()) {
844                     CmsWidgetDialogParameter param = (CmsWidgetDialogParameter)j.next();
845                     try {
846                         param.commitValue(this);
847                     } catch (Exception JavaDoc e) {
848                         result.add(e);
849                     }
850                 }
851             }
852         }
853         setValidationErrorList(result);
854         return result;
855     }
856
857     /**
858      * Creates the dialog HTML for all defined widgets of this dialog.<p>
859      *
860      * @return the dialog HTML for all defined widgets of this dialog
861      */

862     protected String JavaDoc createDialogHtml() {
863
864         return createDialogHtml(null);
865     }
866
867     /**
868      * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
869      *
870      * To get a more complex layout variation, you have to overwrite this method in your dialog class.<p>
871      *
872      * @param dialog the dialog (page) to get the HTML for
873      * @return the dialog HTML for all defined widgets of the named dialog (page)
874      */

875     protected String JavaDoc createDialogHtml(String JavaDoc dialog) {
876
877         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
878
879         // create table
880
result.append(createWidgetTableStart());
881
882         // show error header once if there were validation errors
883
result.append(createWidgetErrorHeader());
884
885         Iterator JavaDoc i = getWidgets().iterator();
886         // iterate the type sequence
887
while (i.hasNext()) {
888             // get the current widget base definition
889
CmsWidgetDialogParameter base = (CmsWidgetDialogParameter)i.next();
890             // check if the element is on the requested dialog page
891
if ((dialog == null) || dialog.equals(base.getDialogPage())) {
892                 // add the HTML for the dialog element
893
result.append(createDialogRowHtml(base));
894             }
895         }
896         // close table
897
result.append(createWidgetTableEnd());
898
899         return result.toString();
900     }
901
902     /**
903      * Creates the dialog HTML for all occurences of one widget parameter.<p>
904      *
905      * @param base the widget parameter base
906      * @return the dialog HTML for one widget parameter
907      */

908     protected String JavaDoc createDialogRowHtml(CmsWidgetDialogParameter base) {
909
910         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
911
912         List JavaDoc sequence = (List JavaDoc)getParameters().get(base.getName());
913         int count = sequence.size();
914
915         // check if value is optional or multiple
916
boolean addValue = false;
917         if (count < base.getMaxOccurs()) {
918             addValue = true;
919         }
920         boolean removeValue = false;
921         if (count > base.getMinOccurs()) {
922             removeValue = true;
923         }
924
925         // check if value is present
926
boolean disabledElement = false;
927         if (count < 1) {
928             // no parameter with the value present, but also not optional: use base as parameter
929
sequence = new ArrayList JavaDoc();
930             sequence.add(base);
931             count = 1;
932             if (base.getMinOccurs() == 0) {
933                 disabledElement = true;
934             }
935         }
936
937         // loop through multiple elements
938
for (int j = 0; j < count; j++) {
939
940             // get the parameter and the widget
941
CmsWidgetDialogParameter p = (CmsWidgetDialogParameter)sequence.get(j);
942             I_CmsWidget widget = p.getWidget();
943
944             // check for an error in this row
945
if (p.hasError()) {
946                 // show error message
947
result.append("<tr><td></td><td><img SRC=\"");
948                 result.append(getSkinUri()).append("editors/xmlcontent/");
949                 result.append("error.png");
950                 result.append("\" border=\"0\" alt=\"\"></td><td class=\"xmlTdError\">");
951                 Throwable JavaDoc t = p.getError();
952                 while (t != null) {
953                     if (t instanceof I_CmsThrowable) {
954                         result.append(((I_CmsThrowable)t).getLocalizedMessage(getLocale()));
955                     } else {
956                         result.append(t.getLocalizedMessage());
957                     }
958                     t = t.getCause();
959                     if (t != null) {
960                         result.append("<br>");
961                     }
962                 }
963                 result.append("</td><td colspan=\"2\"></td></tr>\n");
964             }
965
966             // create label and help bubble cells
967
result.append("<tr>");
968             result.append("<td class=\"xmlLabel");
969             if (disabledElement) {
970                 // element is disabled, mark it with css
971
result.append("Disabled");
972             }
973
974             result.append("\">");
975             result.append(keyDefault(A_CmsWidget.getLabelKey(p), p.getName()));
976             if (count > 1) {
977                 result.append(" [").append(p.getIndex() + 1).append("]");
978             }
979             result.append(": </td>");
980             if (p.getIndex() == 0) {
981                 // show help bubble only on first element of each content definition
982
result.append(p.getWidget().getHelpBubble(getCms(), this, p));
983             } else {
984                 // create empty cell for all following elements
985
result.append(dialogHorizontalSpacer(16));
986             }
987
988             // append individual widget html cell if element is enabled
989
if (!disabledElement) {
990                 // this is a simple type, display widget
991
result.append(widget.getDialogWidget(getCms(), this, p));
992             } else {
993                 // disabled element, show message for optional element
994
result.append("<td class=\"xmlTdDisabled maxwidth\">");
995                 result.append(key(Messages.GUI_EDITOR_WIDGET_OPTIONALELEMENT_0));
996                 result.append("</td>");
997             }
998
999             // append add and remove element buttons if required
1000
result.append(dialogHorizontalSpacer(5));
1001            result.append("<td>");
1002            if (addValue || removeValue) {
1003                result.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
1004
1005                if (!addValue) {
1006                    result.append(dialogHorizontalSpacer(25));
1007                } else {
1008                    result.append("<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
1009                    result.append(buildAddElement(base.getName(), p.getIndex(), addValue));
1010                }
1011
1012                if (removeValue) {
1013                    if (!addValue) {
1014                        result.append("<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
1015                    }
1016                    result.append(buildRemoveElement(base.getName(), p.getIndex(), removeValue));
1017                }
1018
1019                result.append("</tr></table></td>");
1020                result.append("</tr></table>");
1021            }
1022            result.append("</td>");
1023            // close row
1024
result.append("</tr>\n");
1025        }
1026
1027        return result.toString();
1028    }
1029
1030    /**
1031     * Creates the dialog widget rows HTML for the specified widget indices.<p>
1032     *
1033     * @param startIndex the widget index to start with
1034     * @param endIndex the widget index to stop at
1035     * @return the dialog widget rows HTML for the specified widget indices
1036     */

1037    protected String JavaDoc createDialogRowsHtml(int startIndex, int endIndex) {
1038
1039        StringBuffer JavaDoc result = new StringBuffer JavaDoc((endIndex - startIndex) * 8);
1040        for (int i = startIndex; i <= endIndex; i++) {
1041            CmsWidgetDialogParameter base = (CmsWidgetDialogParameter)getWidgets().get(i);
1042            result.append(createDialogRowHtml(base));
1043        }
1044        return result.toString();
1045    }
1046
1047    /**
1048     * Creates the complete widget dialog end block HTML that finishes a widget block.<p>
1049     *
1050     * @return the complete widget dialog end block HTML that finishes a widget block
1051     */

1052    protected String JavaDoc createWidgetBlockEnd() {
1053
1054        StringBuffer JavaDoc result = new StringBuffer JavaDoc(8);
1055        result.append(createWidgetTableEnd());
1056        result.append(dialogBlockEnd());
1057        return result.toString();
1058    }
1059
1060    /**
1061     * Create the complete widget dialog start block HTML that begins a widget block with optional headline.<p>
1062     * @param headline the headline String for the block
1063     * @return the complete widget dialog start block HTML that begins a widget block with optional headline
1064     */

1065    protected String JavaDoc createWidgetBlockStart(String JavaDoc headline) {
1066
1067        StringBuffer JavaDoc result = new StringBuffer JavaDoc(16);
1068        result.append(dialogBlockStart(headline));
1069        result.append(createWidgetTableStart());
1070        return result.toString();
1071    }
1072
1073    /**
1074     * Creates the HTML for the error message if validation errors were found.<p>
1075     *
1076     * @return the HTML for the error message if validation errors were found
1077     */

1078    protected String JavaDoc createWidgetErrorHeader() {
1079
1080        StringBuffer JavaDoc result = new StringBuffer JavaDoc(8);
1081        if (hasValidationErrors() || hasCommitErrors()) {
1082            result.append("<tr><td colspan=\"5\">&nbsp;</td></tr>\n");
1083            result.append("<tr><td colspan=\"2\">&nbsp;</td>");
1084            result.append("<td class=\"xmlTdErrorHeader\">");
1085            result.append(key(Messages.GUI_EDITOR_WIDGET_VALIDATION_ERROR_TITLE_0));
1086            result.append("</td><td colspan=\"2\">&nbsp;");
1087            result.append("</td></tr>\n");
1088            result.append("<tr><td colspan=\"5\">&nbsp;</td></tr>\n");
1089            if (hasCommitErrors()) {
1090                result.append(dialogBlockStart(""));
1091                result.append(createWidgetTableStart());
1092                Iterator JavaDoc i = getCommitErrors().iterator();
1093                while (i.hasNext()) {
1094                    Throwable JavaDoc t = (Throwable JavaDoc)i.next();
1095                    result.append("<tr><td><img SRC=\"");
1096                    result.append(getSkinUri()).append("editors/xmlcontent/");
1097                    result.append("error.png");
1098                    result.append("\" border=\"0\" alt=\"\"></td><td class=\"xmlTdError maxwidth\">");
1099                    while (t != null) {
1100                        String JavaDoc message = "";
1101                        if (t instanceof I_CmsThrowable) {
1102                            message = ((I_CmsThrowable)t).getLocalizedMessage(getLocale());
1103                        } else {
1104                            message = t.getLocalizedMessage();
1105                        }
1106                        result.append(CmsStringUtil.escapeHtml(message));
1107                        t = t.getCause();
1108                        if (t != null) {
1109                            result.append("<br>");
1110                        }
1111                    }
1112                    result.append("</td></tr>\n");
1113                }
1114                result.append(createWidgetTableEnd());
1115                result.append(dialogBlockEnd());
1116            }
1117        }
1118        return result.toString();
1119    }
1120
1121    /**
1122     * Creates the HTML for the table around the dialog widgets.<p>
1123     *
1124     * @return the HTML for the table around the dialog widgets
1125     */

1126    protected String JavaDoc createWidgetTableEnd() {
1127
1128        return "</table>\n";
1129    }
1130
1131    /**
1132     * Creates the HTML to close the table around the dialog widgets.<p>
1133     *
1134     * @return the HTML to close the table around the dialog widgets
1135     */

1136    protected String JavaDoc createWidgetTableStart() {
1137
1138        return "<table cellspacing='0' cellpadding='0' class='xmlTable'>\n";
1139    }
1140
1141    /**
1142     * Generates the dialog starting html code.<p>
1143     *
1144     * @return html code
1145     *
1146     * @throws JspException if something goes wrong
1147     */

1148    protected String JavaDoc defaultActionHtml() throws JspException JavaDoc {
1149
1150        StringBuffer JavaDoc result = new StringBuffer JavaDoc(2048);
1151        result.append(defaultActionHtmlStart());
1152        result.append(defaultActionHtmlContent());
1153        result.append(defaultActionHtmlEnd());
1154        return result.toString();
1155    }
1156
1157    /**
1158     * Returns the html code for the default action content.<p>
1159     *
1160     * @return html code
1161     */

1162    protected String JavaDoc defaultActionHtmlContent() {
1163
1164        StringBuffer JavaDoc result = new StringBuffer JavaDoc(2048);
1165        result.append("<form name=\"EDITOR\" id=\"EDITOR\" method=\"post\" action=\"").append(getDialogRealUri());
1166        result.append("\" class=\"nomargin\" onsubmit=\"return submitAction('").append(DIALOG_OK).append(
1167            "', null, 'EDITOR');\">\n");
1168        result.append(dialogContentStart(getDialogTitle()));
1169        result.append(buildDialogForm());
1170        result.append(dialogContentEnd());
1171        result.append(dialogButtonsCustom());
1172        result.append(paramsAsHidden());
1173        if (getParamFramename() == null) {
1174            result.append("\n<input type=\"hidden\" name=\"").append(PARAM_FRAMENAME).append("\" value=\"\">\n");
1175        }
1176        result.append("</form>\n");
1177        result.append(getWidgetHtmlEnd());
1178        return result.toString();
1179    }
1180
1181    /**
1182     * Generates the dialog ending html code.<p>
1183     *
1184     * @return html code
1185     */

1186    protected String JavaDoc defaultActionHtmlEnd() {
1187
1188        StringBuffer JavaDoc result = new StringBuffer JavaDoc(2048);
1189        result.append(dialogEnd());
1190        result.append(bodyEnd());
1191        result.append(htmlEnd());
1192        return result.toString();
1193    }
1194
1195    /**
1196     * Generates the dialog starting html code.<p>
1197     *
1198     * @return html code
1199     *
1200     * @throws JspException if something goes wrong
1201     */

1202    protected String JavaDoc defaultActionHtmlStart() throws JspException JavaDoc {
1203
1204        StringBuffer JavaDoc result = new StringBuffer JavaDoc(2048);
1205        result.append(htmlStart("administration/index.html"));
1206        result.append("<script type=\"text/javascript\" SRC=\"").append(getResourceUri()).append(
1207            "editors/xmlcontent/edit.js\"></script>\n");
1208        result.append("<script type=\"text/javascript\" SRC=\"").append(getResourceUri()).append(
1209            "editors/xmlcontent/help.js\"></script>\n");
1210        result.append(getWidgetIncludes());
1211        result.append("<script type=\"text/javascript\">\n<!--\n");
1212        result.append("// flag indicating if form initialization is finished\n");
1213        result.append("var initialized = false;\n");
1214        result.append("// the OpenCms context path\n");
1215        result.append("var contextPath = \"").append(OpenCms.getSystemInfo().getOpenCmsContext()).append("\";\n\n");
1216        result.append("// action parameters of the form\n");
1217        result.append("var actionAddElement = \"").append(EDITOR_ACTION_ELEMENT_ADD).append("\";\n");
1218        result.append("var actionRemoveElement = \"").append(EDITOR_ACTION_ELEMENT_REMOVE).append("\";\n");
1219        result.append("function init() {\n");
1220        result.append(getWidgetInitCalls());
1221        result.append("\tsetTimeout(\"scrollForm();\", 200);\n");
1222        result.append("\tinitialized = true;\n");
1223        result.append("}\n\n");
1224        result.append("function exitEditor() {\n");
1225        result.append("\ttry {\n");
1226        result.append("\t\t// close file selector popup if present\n");
1227        result.append("\t\tcloseTreeWin();\n");
1228        result.append("\t} catch (e) {}\n");
1229        result.append("}\n");
1230        result.append(getWidgetInitMethods());
1231        result.append("\n// -->\n</script>\n");
1232        result.append(bodyStart(null, "onload='init();' onunload='exitEditor();'"));
1233        result.append(dialogStart());
1234        return result.toString();
1235    }
1236
1237    /**
1238     * Defines the list of parameters for this dialog.<p>
1239     */

1240    protected abstract void defineWidgets();
1241
1242    /**
1243     * Fills all widgets of this widget dialog with the values from the request parameters.<p>
1244     *
1245     * @param request the current HTTP servlet request
1246     */

1247    protected void fillWidgetValues(HttpServletRequest JavaDoc request) {
1248
1249        Map JavaDoc parameters = request.getParameterMap();
1250        Map JavaDoc processedParamters = new HashMap JavaDoc();
1251        Iterator JavaDoc p = parameters.keySet().iterator();
1252        // make sure all "hidden" widget parameters are decoded
1253
while (p.hasNext()) {
1254            String JavaDoc key = (String JavaDoc)p.next();
1255            String JavaDoc[] values = (String JavaDoc[])parameters.get(key);
1256            if (key.startsWith(HIDDEN_PARAM_PREFIX)) {
1257                // this is an encoded hidden parameter
1258
key = key.substring(HIDDEN_PARAM_PREFIX.length());
1259                String JavaDoc[] newValues = new String JavaDoc[values.length];
1260                for (int l = 0; l < values.length; l++) {
1261                    newValues[l] = CmsEncoder.decode(values[l], getCms().getRequestContext().getEncoding());
1262                }
1263                values = newValues;
1264            }
1265            processedParamters.put(key, values);
1266        }
1267
1268        // now process the parameters
1269
m_widgetParamValues = new HashMap JavaDoc();
1270        Iterator JavaDoc i = getWidgets().iterator();
1271
1272        while (i.hasNext()) {
1273            // check for all widget base parameters
1274
CmsWidgetDialogParameter base = (CmsWidgetDialogParameter)i.next();
1275
1276            List JavaDoc params = new ArrayList JavaDoc();
1277            int maxOccurs = base.getMaxOccurs();
1278
1279            boolean onPage = false;
1280            if (base.isCollectionBase()) {
1281                // for a collection base, check if we are on the page where the collection base is shown
1282
if (CmsStringUtil.isNotEmpty(getParamAction()) && !DIALOG_INITIAL.equals(getParamAction())) {
1283                    // if no action set (usually for first display of dialog) make sure all values are shown
1284
// DIALOG_INITIAL is a special value for the first display and must be handled the same way
1285
String JavaDoc page = getParamPage();
1286                    // keep in mind that since the paramPage will be set AFTER the widget values are filled,
1287
// so the first time this page is called from another page the following will result to "false",
1288
// but for every "submit" on the page this will be "true"
1289
onPage = CmsStringUtil.isEmpty(page)
1290                        || CmsStringUtil.isEmpty(base.getDialogPage())
1291                        || base.getDialogPage().equals(page);
1292                }
1293            }
1294
1295            for (int j = 0; j < maxOccurs; j++) {
1296                // check for all possible values in the request parameters
1297
String JavaDoc id = CmsWidgetDialogParameter.createId(base.getName(), j);
1298
1299                boolean required = (params.size() < base.getMinOccurs())
1300                    || (processedParamters.get(id) != null)
1301                    || (!onPage && base.hasValue(j));
1302
1303                if (required) {
1304                    CmsWidgetDialogParameter param = new CmsWidgetDialogParameter(base, params.size(), j);
1305                    param.setKeyPrefix(m_prefix);
1306                    base.getWidget().setEditorValue(getCms(), processedParamters, this, param);
1307                    params.add(param);
1308                }
1309            }
1310            m_widgetParamValues.put(base.getName(), params);
1311        }
1312    }
1313
1314    /**
1315     * Returns the title for this Dialog.<p>
1316     *
1317     * In the default implementation this method returns <code>null</code>.
1318     * Override this if needed.<p>
1319     *
1320     * @return the title for this Dialog, or <code>null</code> if this dialog has no title
1321     */

1322    protected String JavaDoc getDialogTitle() {
1323
1324        return null;
1325    }
1326
1327    /**
1328     * Returns the allowed pages for this dialog.<p>
1329     *
1330     * @return the allowed pages for this dialog
1331     */

1332    protected abstract String JavaDoc[] getPageArray();
1333
1334    /**
1335     * Returns the allowed pages for this dialog.<p>
1336     *
1337     * @return the allowed pages for this dialog
1338     */

1339    protected List JavaDoc getPages() {
1340
1341        if (m_pages == null) {
1342            m_pages = Arrays.asList(getPageArray());
1343        }
1344        return m_pages;
1345    }
1346
1347    /**
1348     * Returns the parameter widget definition for the given parameter name.<p>
1349     *
1350     * @param name the parameter name to get the definition for
1351     *
1352     * @return the parameter widget definition for the given parameter name
1353     */

1354    protected CmsWidgetDialogParameter getParameterDefinition(String JavaDoc name) {
1355
1356        Iterator JavaDoc i = getWidgets().iterator();
1357        while (i.hasNext()) {
1358            // check for all widget parameters
1359
CmsWidgetDialogParameter base = (CmsWidgetDialogParameter)i.next();
1360            if (base.getName().equals(name)) {
1361                return base;
1362            }
1363        }
1364        return null;
1365    }
1366
1367    /**
1368     * Returns the map with the widget parameter values.<p>
1369     *
1370     * @return the map with the widget parameter values
1371     */

1372    protected Map JavaDoc getParameters() {
1373
1374        return m_widgetParamValues;
1375    }
1376
1377    /**
1378     * Returns the validation errors for the dialog.<p>
1379     *
1380     * The method (@link CmsWidgetDialog#commitWidgetValues(String)) has to set this list.<p>
1381     *
1382     * @return the validation errors for the dialog
1383     */

1384    protected List JavaDoc getValidationErrorList() {
1385
1386        return m_validationErrorList;
1387    }
1388
1389    /**
1390     * Returns the widget HTML code for the given parameter.<p>
1391     *
1392     * @param param the name (id) of the parameter to get the widget HTML for
1393     *
1394     * @return the widget HTML code for the given parameter
1395     */

1396    protected String JavaDoc getWidget(CmsWidgetDialogParameter param) {
1397
1398        if (param != null) {
1399            return param.getWidget().getDialogWidget(getCms(), this, param);
1400        }
1401        return null;
1402    }
1403
1404    /**
1405     * Returns the list of all widgets used on this widget dialog, the
1406     * List must contain Objects of type <code>{@link CmsWidgetDialogParameter}</code>.<p>
1407     *
1408     * @return the list of all widgets used on this widget dialog
1409     */

1410    protected List JavaDoc getWidgets() {
1411
1412        if (m_widgets == null) {
1413            m_widgets = new ArrayList JavaDoc();
1414        }
1415        return m_widgets;
1416    }
1417
1418    /**
1419     * Returns <code>true</code> if the current dialog (page) has commit errors.<p>
1420     *
1421     * @return <code>true</code> if the current dialog (page) has commit errors
1422     */

1423    protected boolean hasCommitErrors() {
1424
1425        return (m_commitErrors != null) && (m_commitErrors.size() > 0);
1426    }
1427
1428    /**
1429     * Returns <code>true</code> if the current dialog (page) has validation errors.<p>
1430     *
1431     * @return <code>true</code> if the current dialog (page) has validation errors
1432     */

1433    protected boolean hasValidationErrors() {
1434
1435        return (m_validationErrorList != null) && (m_validationErrorList.size() > 0);
1436    }
1437
1438    /**
1439     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
1440     */

1441    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
1442
1443        // set the dialog type
1444
setParamDialogtype(getClass().getName());
1445
1446        // fill the parameter values in the get/set methods
1447
fillParamValues(request);
1448
1449        if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamPage()) || !getPages().contains(getParamPage())) {
1450            // ensure a valid page is set
1451
setParamPage((String JavaDoc)getPages().get(0));
1452        }
1453
1454        // test the needed parameters
1455
try {
1456            validateParamaters();
1457        } catch (Exception JavaDoc e) {
1458            // close if parameters not available
1459
setAction(ACTION_CANCEL);
1460            try {
1461                actionCloseDialog();
1462            } catch (JspException JavaDoc e1) {
1463                // noop
1464
}
1465            return;
1466        }
1467
1468        // fill the widget map
1469
defineWidgets();
1470        fillWidgetValues(request);
1471
1472        // set the action for the JSP switch
1473
if (DIALOG_SAVE.equals(getParamAction())) {
1474            // ok button pressed, save
1475
List JavaDoc errors = commitWidgetValues();
1476            if (errors.size() > 0) {
1477                setAction(ACTION_DEFAULT);
1478                // found validation errors, redisplay page
1479
return;
1480            }
1481            setAction(ACTION_SAVE);
1482        } else if (DIALOG_OK.equals(getParamAction())) {
1483            // ok button pressed
1484
setAction(ACTION_CANCEL);
1485        } else if (DIALOG_CANCEL.equals(getParamAction())) {
1486            // cancel button pressed
1487
setAction(ACTION_CANCEL);
1488        } else if (EDITOR_ACTION_ELEMENT_ADD.equals(getParamAction())) {
1489            // add optional input element
1490
setAction(ACTION_ELEMENT_ADD);
1491            actionToggleElement();
1492            setAction(ACTION_DEFAULT);
1493        } else if (EDITOR_ACTION_ELEMENT_REMOVE.equals(getParamAction())) {
1494            // remove optional input element
1495
setAction(ACTION_ELEMENT_REMOVE);
1496            actionToggleElement();
1497            setAction(ACTION_DEFAULT);
1498        } else if (DIALOG_BACK.equals(getParamAction())) {
1499            // go back one page
1500
setAction(ACTION_DEFAULT);
1501            List JavaDoc errors = commitWidgetValues(getParamPage());
1502            if (errors.size() > 0) {
1503                // found validation errors, redisplay page
1504
return;
1505            }
1506            int pageIndex = getPages().indexOf(getParamPage()) - 1;
1507            setParamPage((String JavaDoc)getPages().get(pageIndex));
1508
1509        } else if (DIALOG_CONTINUE.equals(getParamAction())) {
1510            // go to next page
1511
setAction(ACTION_DEFAULT);
1512            List JavaDoc errors = commitWidgetValues(getParamPage());
1513            if (errors.size() > 0) {
1514                // found validation errors, redisplay page
1515
return;
1516            }
1517            int pageIndex = getPages().indexOf(getParamPage()) + 1;
1518            setParamPage((String JavaDoc)getPages().get(pageIndex));
1519
1520        } else {
1521            // first dialog call, set the default action
1522
setAction(ACTION_DEFAULT);
1523        }
1524    }
1525
1526    /**
1527     * Sets the errors that are thrown by save actions or form generation.<p>
1528     *
1529     * @param errors the errors that are thrown by save actions or form generation
1530     */

1531    protected void setCommitErrors(List JavaDoc errors) {
1532
1533        m_commitErrors = errors;
1534    }
1535
1536    /**
1537     * Sets an optional localized key prefix identificator for all widgets.<p>
1538     *
1539     * @param prefix the optional localized key prefix identificator for all widgets
1540     *
1541     * @see org.opencms.widgets.I_CmsWidgetParameter#setKeyPrefix(java.lang.String)
1542     */

1543    protected void setKeyPrefix(String JavaDoc prefix) {
1544
1545        m_prefix = prefix;
1546    }
1547
1548    /**
1549     * Sets the allowed pages for this dialog.<p>
1550     *
1551     * @param pages the allowed pages for this dialog
1552     */

1553    protected void setPages(List JavaDoc pages) {
1554
1555        m_pages = pages;
1556    }
1557
1558    /**
1559     * Sets the validation errors for the dialog.<p>
1560     *
1561     * Use this in the method (@link CmsWidgetDialog#commitWidgetValues(String)) to set the list.<p>
1562     *
1563     * @param errors the validation errors
1564     */

1565    protected void setValidationErrorList(List JavaDoc errors) {
1566
1567        m_validationErrorList = errors;
1568    }
1569
1570    /**
1571     * Should be overriden for parameter validation.<p>
1572     *
1573     * The exception is never seen by the user, so it can be just a <code>new Exception()</code>.<p>
1574     *
1575     * @throws Exception if the parameters are not valid
1576     */

1577    protected void validateParamaters() throws Exception JavaDoc {
1578
1579        // valid by default
1580
}
1581
1582    /**
1583     * Returns the (internal use only) map of dialog objects.<p>
1584     *
1585     * @return the (internal use only) map of dialog objects
1586     */

1587    private Map JavaDoc getDialogObjectMap() {
1588
1589        Map JavaDoc objects = (Map JavaDoc)getSettings().getDialogObject();
1590        if (objects == null) {
1591            // using hashtable as most efficient version of a synchronized map
1592
objects = new Hashtable JavaDoc();
1593            getSettings().setDialogObject(objects);
1594        }
1595        return objects;
1596    }
1597}
Popular Tags