KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > demos > widget > CmsAdminWidgetDemo7


1 /*
2  * File : $Source$
3  * Date : $Date$
4  * Version: $Revision$
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.demos.widget;
33
34 import org.opencms.jsp.CmsJspActionElement;
35 import org.opencms.main.CmsContextInfo;
36 import org.opencms.scheduler.CmsScheduledJobInfo;
37 import org.opencms.widgets.A_CmsWidget;
38 import org.opencms.widgets.CmsCheckboxWidget;
39 import org.opencms.widgets.CmsDownloadGalleryWidget;
40 import org.opencms.widgets.CmsHtmlGalleryWidget;
41 import org.opencms.widgets.CmsImageGalleryWidget;
42 import org.opencms.widgets.CmsInputWidget;
43 import org.opencms.widgets.CmsLinkGalleryWidget;
44 import org.opencms.widgets.CmsTableGalleryWidget;
45 import org.opencms.widgets.CmsTextareaWidget;
46 import org.opencms.widgets.CmsVfsFileWidget;
47 import org.opencms.widgets.I_CmsWidget;
48 import org.opencms.workplace.CmsWidgetDialog;
49 import org.opencms.workplace.CmsWidgetDialogParameter;
50 import org.opencms.workplace.CmsWorkplaceSettings;
51
52 import java.util.ArrayList JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.List JavaDoc;
55
56 import javax.servlet.http.HttpServletRequest JavaDoc;
57 import javax.servlet.http.HttpServletResponse JavaDoc;
58 import javax.servlet.jsp.PageContext JavaDoc;
59
60 /**
61  * A basic example and proof-of-concept on how to use OpenCms widgets within a custom build form
62  * without XML contents.<p>
63  *
64  * @author Alexander Kandzior
65  *
66  * @version $Revision$
67  *
68  * @since 6.0.0
69  */

70 public class CmsAdminWidgetDemo7 extends CmsWidgetDialog {
71
72     /** The dialog type. */
73     public static final String JavaDoc DIALOG_TYPE = "widgetdemo4";
74
75     /** The OpenCms context info object used for the job info. */
76     CmsContextInfo m_contextInfo;
77
78     /** The job info object that is edited on this dialog. */
79     CmsScheduledJobInfo m_jobInfo;
80
81     /**
82      * Public constructor with JSP action element.<p>
83      *
84      * @param jsp an initialized JSP action element
85      */

86     public CmsAdminWidgetDemo7(CmsJspActionElement jsp) {
87
88         super(jsp);
89     }
90
91     /**
92      * Public constructor with JSP variables.<p>
93      *
94      * @param context the JSP page context
95      * @param req the JSP request
96      * @param res the JSP response
97      */

98     public CmsAdminWidgetDemo7(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
99
100         this(new CmsJspActionElement(context, req, res));
101     }
102
103     /**
104      * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
105      */

106     public void actionCommit() {
107
108         // not implemented for this demo
109

110     }
111
112     /**
113      * Builds the HTML for the demo7 form.<p>
114      *
115      * @return the HTML for the demo7 form
116      */

117     public String JavaDoc buildDemo7Form() {
118
119         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
120
121         try {
122
123             // create the dialog HTML
124
result.append(createDialogHtml());
125
126         } catch (Throwable JavaDoc t) {
127             // TODO: Error handling
128
}
129         return result.toString();
130     }
131
132     /**
133      * Creates the dialog HTML for all occurences of one widget parameter.<p>
134      *
135      * @param base the widget parameter base
136      * @return the dialog HTML for one widget parameter6
137      */

138     protected String JavaDoc createDialogRowHtml(CmsWidgetDialogParameter base) {
139
140         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
141
142         List JavaDoc sequence = (List JavaDoc)getParameters().get(base.getName());
143         int count = sequence.size();
144
145         // check if value is optional or multiple
146
boolean addValue = false;
147         if (count < base.getMaxOccurs()) {
148             addValue = true;
149         }
150         boolean removeValue = false;
151         if (count > base.getMinOccurs()) {
152             removeValue = true;
153         }
154
155         // check if value is present
156
boolean disabledElement = false;
157         if (count < 1) {
158             // no parameter with the value present, but also not optional: use base as parameter
159
sequence = new ArrayList JavaDoc();
160             sequence.add(base);
161             count = 1;
162             if (base.getMinOccurs() == 0) {
163                 disabledElement = true;
164             }
165         }
166
167         // loop through multiple elements
168
for (int j = 0; j < count; j++) {
169
170             // get the parameter and the widget
171
CmsWidgetDialogParameter p = (CmsWidgetDialogParameter)sequence.get(j);
172             I_CmsWidget widget = p.getWidget();
173
174             // create label and help bubble cells
175
result.append("<tr>");
176             result.append("<td class=\"xmlLabel");
177             if (disabledElement) {
178                 // element is disabled, mark it with css
179
result.append("Disabled");
180             }
181             result.append("\">");
182             result.append(keyDefault(A_CmsWidget.getLabelKey(p), p.getName()));
183             if (count > 1) {
184                 result.append(" [").append(p.getIndex() + 1).append("]");
185             }
186             result.append(": </td>");
187             if (p.getIndex() == 0) {
188                 // show help bubble only on first element of each content definition
189
result.append(widget.getHelpBubble(getCms(), this, p));
190             } else {
191                 // create empty cell for all following elements
192
result.append(dialogHorizontalSpacer(16));
193             }
194
195             // append individual widget html cell if element is enabled
196
if (!disabledElement) {
197                 // this is a simple type, display widget
198
result.append(widget.getDialogWidget(getCms(), this, p));
199             } else {
200                 // disabled element, show message for optional element
201
result.append("<td class=\"xmlTdDisabled maxwidth\">");
202                 result.append(Messages.get().getBundle(getLocale()).key(
203                     Messages.GUI_EDITOR_XMLCONTENT_OPTIONALELEMENT_0));
204                 result.append("</td>");
205             }
206
207             // append add and remove element buttons if required
208
result.append(dialogHorizontalSpacer(5));
209             result.append("<td>");
210             if (addValue || removeValue) {
211                 result.append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
212
213                 if (!addValue) {
214                     result.append(dialogHorizontalSpacer(24));
215                 } else {
216                     result.append("<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
217                     result.append(buildAddElement(base.getName(), p.getIndex(), addValue));
218                 }
219
220                 if (removeValue) {
221                     if (!addValue) {
222                         result.append("<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
223                     }
224                     result.append(buildRemoveElement(base.getName(), p.getIndex(), removeValue));
225                 }
226
227                 result.append("</tr></table></td>");
228
229                 result.append("</tr></table>");
230             }
231             result.append("</td>");
232             // close row
233
result.append("</tr>\n");
234         }
235
236         return result.toString();
237     }
238
239     /**
240      * Creates the list of widgets for this dialog.<p>
241      */

242     protected void defineWidgets() {
243
244         m_jobInfo = new CmsScheduledJobInfo();
245         m_contextInfo = new CmsContextInfo();
246
247         addWidget(new CmsWidgetDialogParameter("stringwidget", new CmsInputWidget(), 0, 5));
248         addWidget(new CmsWidgetDialogParameter("textwidget", new CmsTextareaWidget(), 0, 5));
249         // Please note: Boolean widget sequences are currently not supported
250
addWidget(new CmsWidgetDialogParameter("boolwidget", new CmsCheckboxWidget()));
251         addWidget(new CmsWidgetDialogParameter("vfsfilewidget", new CmsVfsFileWidget(), 0, 5));
252         addWidget(new CmsWidgetDialogParameter("imagegalwidget", new CmsImageGalleryWidget(), 0, 5));
253         addWidget(new CmsWidgetDialogParameter("downgalwidget", new CmsDownloadGalleryWidget(), 0, 5));
254         addWidget(new CmsWidgetDialogParameter("htmlgalwidget", new CmsHtmlGalleryWidget(), 0, 5));
255         addWidget(new CmsWidgetDialogParameter("tablegalwidget", new CmsTableGalleryWidget(), 0, 5));
256         addWidget(new CmsWidgetDialogParameter("extgalwidget", new CmsLinkGalleryWidget(), 0, 5));
257     }
258
259     /**
260      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
261      */

262     protected String JavaDoc[] getPageArray() {
263
264         return new String JavaDoc[] {"page1"};
265     }
266
267     /**
268      * @see org.opencms.workplace.CmsWidgetDialog#initMessages()
269      */

270     protected void initMessages() {
271
272         // add specific dialog resource bundle
273
addMessages(Messages.get().getBundleName());
274         // add default resource bundles
275
addMessages(org.opencms.workplace.demos.Messages.get().getBundleName());
276         super.initMessages();
277     }
278
279     /**
280      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
281      */

282     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
283
284         // set the dialog type
285
setParamDialogtype(DIALOG_TYPE);
286
287         // fill the parameter values in the get/set methods
288
fillParamValues(request);
289
290         // fill the widget map
291
defineWidgets();
292         fillWidgetValues(request);
293
294         // set the action for the JSP switch
295
if (DIALOG_SAVE.equals(getParamAction())) {
296             // ok button pressed
297
setAction(ACTION_SAVE);
298             List JavaDoc errors = commitWidgetValues();
299             if (errors.size() > 0) {
300                 Iterator JavaDoc i = errors.iterator();
301                 while (i.hasNext()) {
302                     Exception JavaDoc e = (Exception JavaDoc)i.next();
303                     System.err.println(e.getMessage());
304                     if (e.getCause() != null) {
305                         System.err.println("Cause: " + e.getCause().getMessage());
306                     }
307                 }
308                 setAction(ACTION_DEFAULT);
309             }
310         } else if (DIALOG_OK.equals(getParamAction())) {
311             // ok button pressed
312
setAction(ACTION_CANCEL);
313         } else if (DIALOG_CANCEL.equals(getParamAction())) {
314             // cancel button pressed
315
setAction(ACTION_CANCEL);
316         } else if (EDITOR_ACTION_ELEMENT_ADD.equals(getParamAction())) {
317             setAction(ACTION_ELEMENT_ADD);
318             actionToggleElement();
319             setAction(ACTION_DEFAULT);
320         } else if (EDITOR_ACTION_ELEMENT_REMOVE.equals(getParamAction())) {
321             setAction(ACTION_ELEMENT_REMOVE);
322             actionToggleElement();
323             setAction(ACTION_DEFAULT);
324         } else {
325             // set the default action
326
setAction(ACTION_DEFAULT);
327         }
328     }
329 }
330
Popular Tags