KickJava   Java API By Example, From Geeks To Geeks.

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


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.i18n.CmsMessages;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.main.CmsLog;
37 import org.opencms.widgets.A_CmsWidget;
38 import org.opencms.widgets.CmsCheckboxWidget;
39 import org.opencms.widgets.CmsComboWidget;
40 import org.opencms.widgets.CmsDownloadGalleryWidget;
41 import org.opencms.widgets.CmsHtmlGalleryWidget;
42 import org.opencms.widgets.CmsImageGalleryWidget;
43 import org.opencms.widgets.CmsInputWidget;
44 import org.opencms.widgets.CmsLinkGalleryWidget;
45 import org.opencms.widgets.CmsSelectWidgetOption;
46 import org.opencms.widgets.CmsTableGalleryWidget;
47 import org.opencms.widgets.CmsTextareaWidget;
48 import org.opencms.widgets.CmsVfsFileWidget;
49 import org.opencms.widgets.I_CmsWidget;
50 import org.opencms.workplace.CmsWidgetDialog;
51 import org.opencms.workplace.CmsWidgetDialogParameter;
52 import org.opencms.workplace.CmsWorkplaceSettings;
53
54 import java.util.ArrayList JavaDoc;
55 import java.util.Iterator JavaDoc;
56 import java.util.List JavaDoc;
57
58 import javax.servlet.http.HttpServletRequest JavaDoc;
59 import javax.servlet.http.HttpServletResponse JavaDoc;
60 import javax.servlet.jsp.PageContext JavaDoc;
61
62 import org.apache.commons.logging.Log;
63
64 /**
65  * A basic example and proof-of-concept on how to use OpenCms widgets within a custom build form
66  * without XML contents.<p>
67  *
68  * @author Alexander Kandzior
69  *
70  * @version $Revision$
71  *
72  * @since 6.0.0
73  */

74 public class CmsAdminWidgetDemo4 extends CmsWidgetDialog {
75
76     /** The dialog type. */
77     public static final String JavaDoc DIALOG_TYPE = "widgetdemo4";
78
79     /** The log object for this class. */
80     private static final Log LOG = CmsLog.getLog(CmsAdminWidgetDemo3.class);
81
82     /**
83      * Public constructor with JSP action element.<p>
84      *
85      * @param jsp an initialized JSP action element
86      */

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

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

107     public void actionCommit() {
108
109         // not implemented for this demo
110
}
111
112     /**
113      * Builds the HTML for the demo4 form.<p>
114      *
115      * @return the HTML for the demo4 form
116      */

117     public String JavaDoc buildDemo4Form() {
118
119         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
120
121         try {
122
123             // create table
124
result.append("<table class=\"xmlTable\">\n");
125
126             Iterator JavaDoc i = getWidgets().iterator();
127             // iterate the type sequence
128
while (i.hasNext()) {
129
130                 // get the current widget base definition
131
CmsWidgetDialogParameter base = (CmsWidgetDialogParameter)i.next();
132                 List JavaDoc sequence = (List JavaDoc)getParameters().get(base.getName());
133                 int count = sequence.size();
134
135                 // check if value is optional or multiple
136
boolean addValue = false;
137                 if (count < base.getMaxOccurs()) {
138                     addValue = true;
139                 }
140                 boolean removeValue = false;
141                 if (count > base.getMinOccurs()) {
142                     removeValue = true;
143                 }
144
145                 // check if value is present
146
boolean disabledElement = false;
147                 if (count < 1) {
148                     // no parameter with the value present, but also not optional: use base as parameter
149
sequence = new ArrayList JavaDoc();
150                     sequence.add(base);
151                     count = 1;
152                     if (base.getMinOccurs() == 0) {
153                         disabledElement = true;
154                     }
155                 }
156
157                 // loop through multiple elements
158
for (int j = 0; j < count; j++) {
159
160                     // get the parameter and the widget
161
CmsWidgetDialogParameter p = (CmsWidgetDialogParameter)sequence.get(j);
162                     I_CmsWidget widget = p.getWidget();
163
164                     // create label and help bubble cells
165
result.append("<tr>");
166                     result.append("<td class=\"xmlLabel");
167                     if (disabledElement) {
168                         // element is disabled, mark it with css
169
result.append("Disabled");
170                     }
171                     result.append("\">");
172                     result.append(keyDefault(A_CmsWidget.getLabelKey(p), p.getName()));
173                     if (count > 1) {
174                         result.append(" [").append(p.getIndex() + 1).append("]");
175                     }
176                     result.append(": </td>");
177                     if (p.getIndex() == 0) {
178                         // show help bubble only on first element of each content definition
179
result.append(p.getWidget().getHelpBubble(getCms(), this, p));
180                     } else {
181                         // create empty cell for all following elements
182
result.append(dialogHorizontalSpacer(16));
183                     }
184
185                     // append individual widget html cell if element is enabled
186
if (!disabledElement) {
187                         // this is a simple type, display widget
188
result.append(widget.getDialogWidget(getCms(), this, p));
189                     } else {
190                         // disabled element, show message for optional element
191
result.append("<td class=\"xmlTdDisabled maxwidth\">");
192                         result.append(Messages.get().getBundle(getLocale()).key(
193                             Messages.GUI_EDITOR_XMLCONTENT_OPTIONALELEMENT_0));
194                         result.append("</td>");
195                     }
196
197                     // append add and remove element buttons if required
198
result.append(dialogHorizontalSpacer(5));
199                     result.append("<td>");
200                     if (addValue || removeValue) {
201                         result.append("<table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
202                         result.append(buildAddElement(base.getName(), p.getIndex(), addValue));
203                         result.append(buildRemoveElement(base.getName(), p.getIndex(), removeValue));
204                         result.append("</tr></table>");
205                     }
206                     result.append("</td>");
207                     // close row
208
result.append("</tr>\n");
209
210                 }
211             }
212             // close table
213
result.append("</table>\n");
214         } catch (Throwable JavaDoc t) {
215             LOG.error(org.opencms.workplace.editors.Messages.get().getBundle().key(
216                 org.opencms.workplace.editors.Messages.ERR_XML_EDITOR_0), t);
217         }
218         return result.toString();
219     }
220
221     /**
222      * Creates the list of widgets for this dialog.<p>
223      */

224     protected void defineWidgets() {
225
226         addWidget(new CmsWidgetDialogParameter("stringwidget", new CmsInputWidget(), 0, 5));
227         addWidget(new CmsWidgetDialogParameter("textwidget", new CmsTextareaWidget(), 0, 5));
228         // Please note: Boolean widget sequences are currently not supported
229
addWidget(new CmsWidgetDialogParameter("boolwidget", new CmsCheckboxWidget()));
230         addWidget(new CmsWidgetDialogParameter("vfsfilewidget", new CmsVfsFileWidget(), 0, 5));
231         addWidget(new CmsWidgetDialogParameter("imagegalwidget", new CmsImageGalleryWidget(), 0, 5));
232         addWidget(new CmsWidgetDialogParameter("downgalwidget", new CmsDownloadGalleryWidget(), 0, 5));
233         addWidget(new CmsWidgetDialogParameter("htmlgalwidget", new CmsHtmlGalleryWidget(), 0, 5));
234         addWidget(new CmsWidgetDialogParameter("tablegalwidget", new CmsTableGalleryWidget(), 0, 5));
235         addWidget(new CmsWidgetDialogParameter("extgalwidget", new CmsLinkGalleryWidget(), 0, 5));
236
237         CmsMessages messages = Messages.get().getBundle(getLocale());
238
239         String JavaDoc val1 = messages.key(Messages.GUI_WIDGETDEMO_DEMOVALUE_1, new Integer JavaDoc(1));
240         List JavaDoc widgetOptions = new ArrayList JavaDoc();
241         widgetOptions.add(new CmsSelectWidgetOption(val1, false, null, messages.key(
242             Messages.GUI_WIDGETDEMO_DEMOHELP_1,
243             val1)));
244         widgetOptions.add(new CmsSelectWidgetOption(
245             messages.key(Messages.GUI_WIDGETDEMO_DEMOVALUE_1, new Integer JavaDoc(2)),
246             true,
247             null,
248             messages.key(Messages.GUI_WIDGETDEMO_DEMOHELP_VAL2_0)));
249         widgetOptions.add(new CmsSelectWidgetOption(messages.key(Messages.GUI_WIDGETDEMO_DEMOVALUE_1, new Integer JavaDoc(2))));
250         addWidget(new CmsWidgetDialogParameter("combowidget", new CmsComboWidget(widgetOptions), 0, 2));
251     }
252
253     /**
254      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
255      */

256     protected String JavaDoc[] getPageArray() {
257
258         return new String JavaDoc[] {"page1"};
259     }
260
261     /**
262      * @see org.opencms.workplace.CmsWidgetDialog#initMessages()
263      */

264     protected void initMessages() {
265
266         // add specific dialog resource bundle
267
addMessages(Messages.get().getBundleName());
268         // add default resource bundles
269
addMessages(org.opencms.workplace.demos.Messages.get().getBundleName());
270         super.initMessages();
271     }
272
273     /**
274      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
275      */

276     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
277
278         // set the dialog type
279
setParamDialogtype(DIALOG_TYPE);
280
281         // fill the parameter values in the get/set methods
282
fillParamValues(request);
283
284         // fill the widget map
285
defineWidgets();
286         fillWidgetValues(request);
287
288         // set the action for the JSP switch
289
if (DIALOG_SAVE.equals(getParamAction())) {
290             // ok button pressed
291
setAction(ACTION_SAVE);
292         } else if (DIALOG_OK.equals(getParamAction())) {
293             // ok button pressed
294
setAction(ACTION_CANCEL);
295         } else if (DIALOG_CANCEL.equals(getParamAction())) {
296             // cancel button pressed
297
setAction(ACTION_CANCEL);
298         } else if (EDITOR_ACTION_ELEMENT_ADD.equals(getParamAction())) {
299             setAction(ACTION_ELEMENT_ADD);
300             actionToggleElement();
301             setAction(ACTION_DEFAULT);
302         } else if (EDITOR_ACTION_ELEMENT_REMOVE.equals(getParamAction())) {
303             setAction(ACTION_ELEMENT_REMOVE);
304             actionToggleElement();
305             setAction(ACTION_DEFAULT);
306         } else {
307             // set the default action
308
setAction(ACTION_DEFAULT);
309         }
310     }
311 }
312
Popular Tags