KickJava   Java API By Example, From Geeks To Geeks.

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


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.JspException JavaDoc;
52 import javax.servlet.jsp.PageContext JavaDoc;
53
54 /**
55  * A basic example and proof-of-concept on how to use OpenCms widgets within a custom build form
56  * without XML contents.<p>
57  *
58  * @author Alexander Kandzior
59  *
60  * @version $Revision$
61  *
62  * @since 6.0.0
63  */

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

92     public CmsAdminWidgetDemo6(CmsJspActionElement jsp) {
93
94         super(jsp);
95     }
96
97     /**
98      * Public constructor with JSP variables.<p>
99      *
100      * @param context the JSP page context
101      * @param req the JSP request
102      * @param res the JSP response
103      */

104     public CmsAdminWidgetDemo6(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
105
106         this(new CmsJspActionElement(context, req, res));
107     }
108
109     /**
110      * @see org.opencms.workplace.CmsWidgetDialog#actionCommit()
111      */

112     public void actionCommit() {
113
114         // not implemented for this demo
115

116     }
117
118     /**
119      * Builds the HTML for the dialog form.<p>
120      *
121      * @return the HTML for the dialog form
122      */

123     public String JavaDoc buildDialogForm() {
124
125         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
126
127         try {
128             // create the dialog HTML
129
result.append(createDialogHtml());
130         } catch (Throwable JavaDoc t) {
131             // since this is just a simple example...
132
t.printStackTrace();
133         }
134         return result.toString();
135     }
136
137     /**
138      * Creats the HTML for the buttons on the dialog.<p>
139      *
140      * @return the HTML for the buttons on the dialog.<p>
141      */

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

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

166     protected void defineWidgets() {
167
168         Object JavaDoc o = getSettings().getDialogObject();
169         if (!(o instanceof CmsScheduledJobInfo)) {
170             // create a new job info
171
m_jobInfo = new CmsScheduledJobInfo();
172             m_contextInfo = new CmsContextInfo();
173             m_jobInfo.setContextInfo(m_contextInfo);
174         } else {
175             // reuse job info object stored in session
176
m_jobInfo = (CmsScheduledJobInfo)o;
177             m_contextInfo = m_jobInfo.getContextInfo();
178         }
179
180         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "jobName", PAGE_ARRAY[0], new CmsInputWidget()));
181         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "className", PAGE_ARRAY[0], new CmsInputWidget()));
182         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "cronExpression", PAGE_ARRAY[0], new CmsInputWidget()));
183
184         addWidget(new CmsWidgetDialogParameter(m_contextInfo, "userName", PAGE_ARRAY[0], new CmsInputWidget()));
185         addWidget(new CmsWidgetDialogParameter(m_contextInfo, "projectName", PAGE_ARRAY[0], new CmsInputWidget()));
186         addWidget(new CmsWidgetDialogParameter(m_contextInfo, "siteRoot", PAGE_ARRAY[0], new CmsVfsFileWidget()));
187         addWidget(new CmsWidgetDialogParameter(m_contextInfo, "requestedUri", PAGE_ARRAY[0], new CmsVfsFileWidget()));
188         addWidget(new CmsWidgetDialogParameter(m_contextInfo, "localeName", PAGE_ARRAY[1], new CmsInputWidget()));
189         addWidget(new CmsWidgetDialogParameter(m_contextInfo, "encoding", PAGE_ARRAY[1], new CmsInputWidget()));
190         addWidget(new CmsWidgetDialogParameter(m_contextInfo, "remoteAddr", PAGE_ARRAY[1], new CmsInputWidget()));
191
192         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "reuseInstance", PAGE_ARRAY[1], new CmsCheckboxWidget()));
193         addWidget(new CmsWidgetDialogParameter(m_jobInfo, "active", PAGE_ARRAY[1], new CmsCheckboxWidget()));
194     }
195
196     /**
197      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
198      */

199     protected String JavaDoc[] getPageArray() {
200
201         return PAGE_ARRAY;
202     }
203
204     /**
205      * @see org.opencms.workplace.CmsWidgetDialog#initMessages()
206      */

207     protected void initMessages() {
208
209         // add specific dialog resource bundle
210
addMessages(Messages.get().getBundleName());
211         // add default resource bundles
212
addMessages(org.opencms.workplace.demos.Messages.get().getBundleName());
213         super.initMessages();
214     }
215
216     /**
217      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
218      */

219     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
220
221         // set the dialog type
222
setParamDialogtype(DIALOG_TYPE);
223
224         // fill the parameter values in the get/set methods
225
fillParamValues(request);
226
227         if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamPage()) || !PAGE_LIST.contains(getParamPage())) {
228             // ensure a valid page is set
229
setParamPage(PAGE_ARRAY[0]);
230         }
231
232         // fill the widget map
233
defineWidgets();
234         fillWidgetValues(request);
235
236         // set the action for the JSP switch
237
if (DIALOG_SAVE.equals(getParamAction())) {
238             // ok button pressed
239
setAction(ACTION_SAVE);
240             List JavaDoc errors = commitWidgetValues();
241             if (errors.size() > 0) {
242                 Iterator JavaDoc i = errors.iterator();
243                 while (i.hasNext()) {
244                     Exception JavaDoc e = (Exception JavaDoc)i.next();
245                     System.err.println(e.getMessage());
246                     if (e.getCause() != null) {
247                         System.err.println("Cause: " + e.getCause().getMessage());
248                     }
249                 }
250                 setAction(ACTION_DEFAULT);
251             }
252         } else if (DIALOG_OK.equals(getParamAction())) {
253             // ok button pressed
254
setAction(ACTION_CANCEL);
255         } else if (DIALOG_CANCEL.equals(getParamAction())) {
256             // cancel button pressed
257
setAction(ACTION_CANCEL);
258         } else if (EDITOR_ACTION_ELEMENT_ADD.equals(getParamAction())) {
259             setAction(ACTION_ELEMENT_ADD);
260             actionToggleElement();
261             setAction(ACTION_DEFAULT);
262         } else if (EDITOR_ACTION_ELEMENT_REMOVE.equals(getParamAction())) {
263             setAction(ACTION_ELEMENT_REMOVE);
264             actionToggleElement();
265             setAction(ACTION_DEFAULT);
266         } else if (DIALOG_BACK.equals(getParamAction())) {
267
268             setAction(ACTION_DEFAULT);
269             List JavaDoc errors = commitWidgetValues(PAGE_ARRAY[1]);
270
271             if (errors.size() > 0) {
272                 setAction(ACTION_ERROR);
273                 try {
274                     includeErrorpage(this, (Throwable JavaDoc)errors.get(0));
275                 } catch (JspException JavaDoc e) {
276                     // ignore
277
}
278                 return;
279             }
280
281             setParamPage(PAGE_ARRAY[0]);
282
283         } else if (DIALOG_CONTINUE.equals(getParamAction())) {
284
285             setAction(ACTION_DEFAULT);
286             List JavaDoc errors = commitWidgetValues(PAGE_ARRAY[0]);
287
288             if (errors.size() > 0) {
289                 setAction(ACTION_ERROR);
290                 try {
291                     includeErrorpage(this, (Throwable JavaDoc)errors.get(0));
292                 } catch (JspException JavaDoc e) {
293                     // ignore
294
}
295                 return;
296             }
297
298             setParamPage(PAGE_ARRAY[1]);
299
300         } else {
301             // set the default action
302
setAction(ACTION_DEFAULT);
303         }
304
305         // save the current state of the job (may be changed because of the widget values)
306
getSettings().setDialogObject(m_jobInfo);
307     }
308 }
309
Popular Tags