KickJava   Java API By Example, From Geeks To Geeks.

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


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

68 public class CmsAdminWidgetDemo3 extends CmsWidgetDialog {
69
70     /** The dialog type. */
71     public static final String JavaDoc DIALOG_TYPE = "widgetdemo3";
72
73     /** The log object for this class. */
74     private static final Log LOG = CmsLog.getLog(CmsAdminWidgetDemo3.class);
75
76     /**
77      * Public constructor with JSP action element.<p>
78      *
79      * @param jsp an initialized JSP action element
80      */

81     public CmsAdminWidgetDemo3(CmsJspActionElement jsp) {
82
83         super(jsp);
84     }
85
86     /**
87      * Public constructor with JSP variables.<p>
88      *
89      * @param context the JSP page context
90      * @param req the JSP request
91      * @param res the JSP response
92      */

93     public CmsAdminWidgetDemo3(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
94
95         this(new CmsJspActionElement(context, req, res));
96     }
97
98     /**
99      * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
100      */

101     public void actionCommit() {
102
103         // not implemented for this demo
104

105     }
106
107     /**
108      * Builds the HTML for the demo3 form.<p>
109      *
110      * @return the HTML for the demo3 form
111      */

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

211     protected void defineWidgets() {
212
213         addWidget(new CmsWidgetDialogParameter("stringwidget", new CmsInputWidget()));
214         addWidget(new CmsWidgetDialogParameter("textwidget", new CmsTextareaWidget()));
215         addWidget(new CmsWidgetDialogParameter("boolwidget", new CmsCheckboxWidget()));
216         addWidget(new CmsWidgetDialogParameter("vfsfilewidget", new CmsVfsFileWidget()));
217         addWidget(new CmsWidgetDialogParameter("imagegalwidget", new CmsImageGalleryWidget()));
218     }
219
220     /**
221      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
222      */

223     protected String JavaDoc[] getPageArray() {
224
225         return new String JavaDoc[] {"page1"};
226     }
227
228     /**
229      * @see org.opencms.workplace.CmsWidgetDialog#initMessages()
230      */

231     protected void initMessages() {
232
233         // add specific dialog resource bundle
234
addMessages(Messages.get().getBundleName());
235         // add default resource bundles
236
addMessages(org.opencms.workplace.demos.Messages.get().getBundleName());
237         super.initMessages();
238     }
239
240     /**
241      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
242      */

243     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
244
245         // set the dialog type
246
setParamDialogtype(DIALOG_TYPE);
247
248         // fill the parameter values in the get/set methods
249
fillParamValues(request);
250
251         // fill the widget map
252
defineWidgets();
253         fillWidgetValues(request);
254
255         // set the action for the JSP switch
256
if (DIALOG_SAVE.equals(getParamAction())) {
257             // ok button pressed
258
setAction(ACTION_SAVE);
259         } else if (DIALOG_OK.equals(getParamAction())) {
260             // ok button pressed
261
setAction(ACTION_CANCEL);
262         } else if (DIALOG_CANCEL.equals(getParamAction())) {
263             // cancel button pressed
264
setAction(ACTION_CANCEL);
265         } else {
266             // set the default action
267
setAction(ACTION_DEFAULT);
268         }
269     }
270 }
271
Popular Tags