KickJava   Java API By Example, From Geeks To Geeks.

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


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

63 public class CmsAdminWidgetDemo8 extends CmsWidgetDialog {
64
65     /** Value for the action: display dialog page 1. */
66     public static final int ACTION_DISPLAY_PAGE_1 = 301;
67
68     /** Value for the action: display dialog page 2. */
69     public static final int ACTION_DISPLAY_PAGE_2 = 302;
70
71     /** The dialog type. */
72     public static final String JavaDoc DIALOG_TYPE = "widgetdemo8";
73
74     /** Defines which pages are valid for this dialog. */
75     public static final String JavaDoc[] PAGES = {"page1", "page2"};
76
77     /** The allowed pages for this dialog in a List. */
78     public static final List JavaDoc PAGE_LIST = Arrays.asList(PAGES);
79
80     /** The job info object that is edited on this dialog. */
81     protected CmsScheduledJobInfo m_jobInfo;
82
83     /**
84      * Public constructor with JSP action element.<p>
85      *
86      * @param jsp an initialized JSP action element
87      */

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

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

108     public void actionCommit() {
109
110         // not implemented for this demo
111

112     }
113
114     /**
115      * Builds the HTML for the dialog form.<p>
116      *
117      * @return the HTML for the dialog form
118      */

119     public String JavaDoc buildDialogForm() {
120
121         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
122
123         try {
124
125             // create the dialog HTML
126
result.append(createDialogHtml(getParamPage()));
127
128         } catch (Throwable JavaDoc t) {
129             // TODO: Error handling
130
}
131         return result.toString();
132     }
133
134     /**
135      * Creats the HTML for the buttons on the dialog.<p>
136      *
137      * @return the HTML for the buttons on the dialog.<p>
138      */

139     public String JavaDoc dialogButtonsCustom() {
140
141         if (PAGES[1].equals(getParamPage())) {
142             // this is the second dialog page
143
return dialogButtons(new int[] {BUTTON_OK, BUTTON_BACK, BUTTON_CANCEL}, new String JavaDoc[3]);
144         } else {
145             return dialogButtons(new int[] {BUTTON_CONTINUE, BUTTON_CANCEL}, new String JavaDoc[2]);
146         }
147     }
148
149     /**
150      * @see org.opencms.workplace.CmsDialog#getCancelAction()
151      */

152     public String JavaDoc getCancelAction() {
153
154         // set the default action
155
setParamPage(PAGES[0]);
156
157         return DIALOG_SET;
158     }
159
160     /**
161      * Creates the list of widgets for this dialog.<p>
162      */

163     protected void defineWidgets() {
164
165         Object JavaDoc o = getDialogObject();
166
167         // required to read the default values for the optional context parameters
168
CmsContextInfo dC = new CmsContextInfo();
169
170         if (!(o instanceof CmsScheduledJobInfo)) {
171             // create a new job info
172
m_jobInfo = new CmsScheduledJobInfo();
173             m_jobInfo.setContextInfo(dC);
174
175             // add some parameters to check issues with pre-filled maps
176
m_jobInfo.getParameters().put("key1", "value1");
177             m_jobInfo.getParameters().put("key2", "value2");
178
179         } else {
180             // reuse job info object stored in session
181
m_jobInfo = (CmsScheduledJobInfo)o;
182         }
183
184         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "jobName", PAGES[0], new CmsInputWidget()));
185         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "className", PAGES[0], new CmsInputWidget()));
186         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "cronExpression", PAGES[0], new CmsInputWidget()));
187
188         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "contextInfo.userName", PAGES[0], new CmsInputWidget()));
189         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "contextInfo.projectName", PAGES[0], new CmsInputWidget()));
190         addWidget(new CmsWidgetDialogParameter(
191             m_jobInfo,
192             "contextInfo.siteRoot",
193             dC.getSiteRoot(),
194             PAGES[0],
195             new CmsVfsFileWidget(),
196             0,
197             1));
198         addWidget(new CmsWidgetDialogParameter(
199             m_jobInfo,
200             "contextInfo.requestedUri",
201             dC.getRequestedUri(),
202             PAGES[0],
203             new CmsVfsFileWidget(),
204             0,
205             1));
206         addWidget(new CmsWidgetDialogParameter(
207             m_jobInfo,
208             "contextInfo.localeName",
209             dC.getLocaleName(),
210             PAGES[0],
211             new CmsInputWidget(),
212             0,
213             1));
214         addWidget(new CmsWidgetDialogParameter(
215             m_jobInfo,
216             "contextInfo.encoding",
217             dC.getEncoding(),
218             PAGES[0],
219             new CmsInputWidget(),
220             0,
221             1));
222         addWidget(new CmsWidgetDialogParameter(
223             m_jobInfo,
224             "contextInfo.remoteAddr",
225             dC.getRemoteAddr(),
226             PAGES[0],
227             new CmsInputWidget(),
228             0,
229             1));
230
231         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "reuseInstance", PAGES[0], new CmsCheckboxWidget()));
232         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "active", PAGES[0], new CmsCheckboxWidget()));
233
234         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "parameters", PAGES[1], new CmsInputWidget()));
235     }
236
237     /**
238      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
239      */

240     protected String JavaDoc[] getPageArray() {
241
242         return PAGES;
243     }
244
245     /**
246      * @see org.opencms.workplace.CmsWidgetDialog#initMessages()
247      */

248     protected void initMessages() {
249
250         // add specific dialog resource bundle
251
addMessages(Messages.get().getBundleName());
252         // add default resource bundles
253
addMessages(org.opencms.workplace.demos.Messages.get().getBundleName());
254         super.initMessages();
255     }
256
257     /**
258      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
259      */

260     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
261
262         // set the dialog type
263
setParamDialogtype(DIALOG_TYPE);
264
265         // fill the parameter values in the get/set methods
266
fillParamValues(request);
267
268         if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamPage()) || !PAGE_LIST.contains(getParamPage())) {
269             // ensure a valid page is set
270
setParamPage(PAGES[0]);
271         }
272
273         // fill the widget map
274
defineWidgets();
275         fillWidgetValues(request);
276
277         // set the action for the JSP switch
278
if (DIALOG_SAVE.equals(getParamAction())) {
279             // ok button pressed
280
setAction(ACTION_SAVE);
281             List JavaDoc errors = commitWidgetValues();
282             if (errors.size() > 0) {
283                 Iterator JavaDoc i = errors.iterator();
284                 while (i.hasNext()) {
285                     Exception JavaDoc e = (Exception JavaDoc)i.next();
286                     System.err.println(e.getMessage());
287                     if (e.getCause() != null) {
288                         System.err.println("Cause: " + e.getCause().getMessage());
289                     }
290                 }
291                 setAction(ACTION_DEFAULT);
292             }
293         } else if (DIALOG_OK.equals(getParamAction())) {
294             // ok button pressed
295
setAction(ACTION_CANCEL);
296         } else if (DIALOG_CANCEL.equals(getParamAction())) {
297             // cancel button pressed
298
setAction(ACTION_CANCEL);
299         } else if (EDITOR_ACTION_ELEMENT_ADD.equals(getParamAction())) {
300             setAction(ACTION_ELEMENT_ADD);
301             actionToggleElement();
302             setAction(ACTION_DEFAULT);
303         } else if (EDITOR_ACTION_ELEMENT_REMOVE.equals(getParamAction())) {
304             setAction(ACTION_ELEMENT_REMOVE);
305             actionToggleElement();
306             setAction(ACTION_DEFAULT);
307         } else if (DIALOG_BACK.equals(getParamAction())) {
308
309             setAction(ACTION_DEFAULT);
310             List JavaDoc errors = commitWidgetValues(PAGES[1]);
311             if (errors.size() > 0) {
312                 return;
313             }
314
315             setParamPage(PAGES[0]);
316
317         } else if (DIALOG_CONTINUE.equals(getParamAction())) {
318
319             setAction(ACTION_DEFAULT);
320             List JavaDoc errors = commitWidgetValues(PAGES[0]);
321             if (errors.size() > 0) {
322                 return;
323             }
324
325             setParamPage(PAGES[1]);
326
327         } else {
328             // set the default action
329
setAction(ACTION_DEFAULT);
330         }
331
332         // save the current state of the job (may be changed because of the widget values)
333
setDialogObject(m_jobInfo);
334     }
335
336 }
337
Popular Tags