KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > editors > CmsDialogElements


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsDialogElements.java,v $
3  * Date : $Date: 2006/03/27 14:52:49 $
4  * Version: $Revision: 1.17 $
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.editors;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsPropertyDefinition;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsResourceFilter;
39 import org.opencms.i18n.CmsLocaleManager;
40 import org.opencms.jsp.CmsJspActionElement;
41 import org.opencms.main.CmsException;
42 import org.opencms.main.CmsLog;
43 import org.opencms.util.CmsStringUtil;
44 import org.opencms.workplace.CmsDialog;
45 import org.opencms.workplace.CmsWorkplaceSettings;
46 import org.opencms.xml.page.CmsXmlPage;
47 import org.opencms.xml.page.CmsXmlPageFactory;
48
49 import java.util.ArrayList JavaDoc;
50 import java.util.Collections JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Locale JavaDoc;
54
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56 import javax.servlet.http.HttpServletResponse JavaDoc;
57 import javax.servlet.jsp.JspException JavaDoc;
58 import javax.servlet.jsp.PageContext JavaDoc;
59
60 import org.apache.commons.logging.Log;
61
62 /**
63  * Provides methods for the editor elements dialog.<p>
64  *
65  * The following files use this class:
66  * <ul>
67  * <li>/jsp/editors/dialogs/elements.html
68  * </ul>
69  * <p>
70  *
71  * @author Andreas Zahner
72  *
73  * @version $Revision: 1.17 $
74  *
75  * @since 6.0.0
76  */

77 public class CmsDialogElements extends CmsDialog {
78
79     /** Value for the action: update the elements of the page. */
80     public static final int ACTION_UPDATE_ELEMENTS = 210;
81
82     /** The dialog type. */
83     public static final String JavaDoc DIALOG_TYPE = "elementselector";
84
85     /** Request parameter value for the action: update the elements of the page. */
86     public static final String JavaDoc DIALOG_UPDATE_ELEMENTS = "updateelements";
87
88     /** Prefix for the html input field for the body. */
89     public static final String JavaDoc PREFIX_PARAM_BODY = "element-";
90
91     /** The log object for this class. */
92     private static final Log LOG = CmsLog.getLog(CmsDialogElements.class);
93
94     /** Stores the element to change to after an element update operation. */
95     private String JavaDoc m_changeElement;
96
97     /** List used to store information of all possible elements of the page. */
98     private List JavaDoc m_elementList;
99
100     /** The element locale. */
101     private Locale JavaDoc m_elementLocale;
102
103     // Special parameters used by this dialog
104
private String JavaDoc m_paramElementlanguage;
105     private String JavaDoc m_paramElementname;
106     private String JavaDoc m_paramTempFile;
107
108     /**
109      * Public constructor.<p>
110      *
111      * @param jsp an initialized JSP action element
112      */

113     public CmsDialogElements(CmsJspActionElement jsp) {
114
115         super(jsp);
116         m_changeElement = "";
117     }
118
119     /**
120      * Public constructor with JSP variables.<p>
121      *
122      * @param context the JSP page context
123      * @param req the JSP request
124      * @param res the JSP response
125      */

126     public CmsDialogElements(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
127
128         this(new CmsJspActionElement(context, req, res));
129         m_changeElement = "";
130     }
131
132     /**
133      * Creates a list of possible elements of a template from the template property "template-elements"
134      * and the elements available in the provided xmlPage.<p>
135      *
136      * @param cms the CmsObject
137      * @param xmlPage the resource to read the elements from
138      * @param xmlPageUri the URI of the resouirce to read the template property from
139      * @param locale the current element locale
140      * @return the list of elements in a String array with element name, nice name (if present) and mandatory flag
141      */

142     public static List JavaDoc computeElements(CmsObject cms, CmsXmlPage xmlPage, String JavaDoc xmlPageUri, Locale JavaDoc locale) {
143
144         List JavaDoc result = new ArrayList JavaDoc();
145
146         if (xmlPage != null) {
147             List JavaDoc elementNames = xmlPage.getNames(locale);
148
149             Iterator JavaDoc i = elementNames.iterator();
150             while (i.hasNext()) {
151                 String JavaDoc name = (String JavaDoc)i.next();
152                 CmsDialogElement element = new CmsDialogElement(name, null, false, false, true);
153                 result.add(element);
154             }
155         }
156
157         String JavaDoc currentTemplate = null;
158         try {
159             currentTemplate = cms.readPropertyObject(xmlPageUri, CmsPropertyDefinition.PROPERTY_TEMPLATE, true).getValue();
160         } catch (CmsException e) {
161             if (LOG.isWarnEnabled()) {
162                 LOG.warn(e.getLocalizedMessage(), e);
163             }
164         }
165         if (currentTemplate != null && currentTemplate.length() > 0) {
166             // template found, check template-elements property
167
String JavaDoc elements = null;
168             try {
169                 // read the property from the template file
170
elements = cms.readPropertyObject(
171                     currentTemplate,
172                     CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS,
173                     false).getValue(null);
174             } catch (CmsException e) {
175                 if (LOG.isWarnEnabled()) {
176                     LOG.warn(e.getLocalizedMessage(), e);
177                 }
178             }
179             if (elements != null) {
180                 // elements are defined on template file, merge with available elements
181
List JavaDoc tokens = CmsStringUtil.splitAsList(elements, ',');
182                 Iterator JavaDoc it = tokens.iterator();
183                 while (it.hasNext()) {
184                     String JavaDoc currentElement = (String JavaDoc)it.next();
185                     String JavaDoc niceName = null;
186                     boolean mandatory = false;
187                     int sepIndex = currentElement.indexOf("|");
188                     if (sepIndex != -1) {
189                         // nice name found for current element, extract it
190
niceName = currentElement.substring(sepIndex + 1);
191                         currentElement = currentElement.substring(0, sepIndex);
192                     }
193                     if (currentElement.endsWith("*")) {
194                         // element is mandatory
195
mandatory = true;
196                         currentElement = currentElement.substring(0, currentElement.length() - 1);
197                     }
198
199                     CmsDialogElement element = new CmsDialogElement(currentElement, niceName, mandatory, true, false);
200                     if (result.contains(element)) {
201                         element.setExisting(true);
202                         result.remove(element);
203                     }
204                     result.add(element);
205                 }
206             }
207         }
208
209         Collections.sort(result);
210         return result;
211     }
212
213     /**
214      * Creates a list of possible elements of a template from the template property "template-elements"
215      * and the elements available in the provided resource file.<p>
216      *
217      * @param cms the CmsObject
218      * @param xmlPageUri the resource to read the elements from
219      * @param locale the current element locale
220      * @return the list of elements in a String array with element name, nice name (if present) and mandatory flag
221      */

222     public static List JavaDoc computeElements(CmsObject cms, String JavaDoc xmlPageUri, Locale JavaDoc locale) {
223
224         CmsXmlPage page = null;
225         try {
226             // read the xmlpage file
227
CmsFile pageFile = cms.readFile(xmlPageUri, CmsResourceFilter.IGNORE_EXPIRATION);
228             page = CmsXmlPageFactory.unmarshal(cms, pageFile);
229         } catch (CmsException e) {
230             LOG.warn(Messages.get().getBundle().key(Messages.LOG_READ_XMLPAGE_FAILED_1, xmlPageUri), e);
231             // xmlpage will be null, only "template-elements" property on template will be checked
232
}
233         return computeElements(cms, page, xmlPageUri, locale);
234     }
235
236     /**
237      * Updates the enabled/diabled status of all elements of the current page.<p>
238      *
239      * @throws JspException if there is an error including the error page
240      */

241     public void actionUpdateElements() throws JspException JavaDoc {
242
243         try {
244             List JavaDoc elementList = computeElements();
245             CmsFile file = getCms().readFile(getParamTempfile(), CmsResourceFilter.IGNORE_EXPIRATION);
246             CmsXmlPage page = CmsXmlPageFactory.unmarshal(getCms(), file);
247             boolean foundMandatory = false;
248             m_changeElement = "";
249             Iterator JavaDoc i = elementList.iterator();
250             while (i.hasNext()) {
251                 // get the current list element
252
CmsDialogElement element = (CmsDialogElement)i.next();
253                 if (element.isMandantory()
254                     || element.getName().equals(getParamElementname())
255                     || Boolean.valueOf(getJsp().getRequest().getParameter(PREFIX_PARAM_BODY + element.getName())).booleanValue()) {
256                     if (!element.isExisting()) {
257                         // create element in order to enable it properly
258
page.addValue(element.getName(), getElementLocale());
259                     }
260                     page.setEnabled(element.getName(), getElementLocale(), true);
261                     if (element.isMandantory() && !foundMandatory) {
262                         m_changeElement = element.getName();
263                         foundMandatory = true;
264                     }
265                 } else {
266                     if (element.isExisting()) {
267                         // remove element if it is already existing
268
page.removeValue(element.getName(), getElementLocale());
269                     }
270                 }
271             }
272             // write the temporary file
273
file.setContents(page.marshal());
274             getCms().writeFile(file);
275             // set the javascript functions which should be executed
276
if (page.isEnabled(getParamElementname(), getElementLocale())) {
277                 m_changeElement = getParamElementname();
278             } else if (!foundMandatory) {
279                 if (elementList.size() > 0) {
280                     m_changeElement = ((CmsDialogElement)elementList.get(0)).getName();
281                 }
282             }
283         } catch (Throwable JavaDoc e) {
284             // show error dialog
285
setParamMessage(Messages.get().getBundle(getLocale()).key(Messages.ERR_UPDATE_ELEMENTS_0));
286             includeErrorpage(this, e);
287         }
288     }
289
290     /**
291      * Builds the html String for a form list of all possible page elements.<p>
292      *
293      * @return the html String for a form list
294      */

295     public String JavaDoc buildElementList() {
296
297         StringBuffer JavaDoc retValue = new StringBuffer JavaDoc(512);
298         retValue.append("<table border=\"0\">\n");
299         retValue.append("<tr>\n");
300         retValue.append("\t<td class=\"textbold\" unselectable=\"on\">"
301             + key(Messages.GUI_EDITOR_DIALOG_ELEMENTS_PAGEELEMENT_0)
302             + "</td>\n");
303         retValue.append("\t<td class=\"textbold\" unselectable=\"on\">&nbsp;&nbsp;"
304             + key(Messages.GUI_EDITOR_DIALOG_ELEMENTS_ENABLED_0)
305             + "&nbsp;&nbsp;</td>\n");
306         retValue.append("</tr>\n");
307         retValue.append("<tr><td colspan=\"2\"><span style=\"height: 6px;\"></span></td></tr>\n");
308
309         try {
310
311             // get the list of all possible elements
312
List JavaDoc elementList = computeElements();
313
314             // get all present bodies from the temporary file
315
CmsFile file = getCms().readFile(this.getParamTempfile(), CmsResourceFilter.IGNORE_EXPIRATION);
316             CmsXmlPage page = CmsXmlPageFactory.unmarshal(getCms(), file);
317
318             // show all possible elements
319
Iterator JavaDoc i = elementList.iterator();
320             while (i.hasNext()) {
321                 // get the current list element
322
CmsDialogElement element = (CmsDialogElement)i.next();
323                 // build an element row
324
retValue.append("<tr>\n");
325                 retValue.append("\t<td style=\"white-space: nowrap;\" unselectable=\"on\">" + element.getNiceName());
326                 retValue.append("</td>\n");
327                 retValue.append("\t<td class=\"textcenter\" unselectable=\"on\"><input type=\"checkbox\" name=\"");
328                 retValue.append(PREFIX_PARAM_BODY);
329                 retValue.append(element.getName());
330                 retValue.append("\" value=\"true\"");
331
332                 if ((!page.hasValue(element.getName(), getElementLocale()) && element.isMandantory())
333                     || page.isEnabled(element.getName(), getElementLocale())) {
334                     retValue.append(" checked=\"checked\"");
335                 }
336                 if (element.isMandantory() || element.getName().equals(getParamElementname())) {
337                     retValue.append(" disabled=\"disabled\"");
338                 }
339                 retValue.append(">");
340                 retValue.append("<script type=\"text/javascript\">registerElement(\"");
341
342                 retValue.append(element.getName());
343                 retValue.append("\", ");
344                 retValue.append(page.isEnabled(element.getName(), getElementLocale()));
345                 retValue.append(");</script>");
346
347                 retValue.append("</td>\n");
348                 retValue.append("</tr>\n");
349             }
350
351         } catch (CmsException e) {
352             // should usually never happen
353
if (LOG.isInfoEnabled()) {
354                 LOG.info(e);
355             }
356         }
357
358         retValue.append("</table>\n");
359         return retValue.toString();
360     }
361
362     /**
363      * Creates a list of possible elements of a template from the template property "template-elements".<p>
364      *
365      * @return the list of elements in a String array with element name, nice name (if present) and mandatory flag
366      */

367     public List JavaDoc computeElements() {
368
369         if (m_elementList == null) {
370             m_elementList = computeElements(getCms(), getParamTempfile(), getElementLocale());
371         }
372         return m_elementList;
373     }
374
375     /**
376      * Returns the element name that has to be changed.<p>
377      *
378      * @return the element name that has to be changed
379      */

380     public String JavaDoc getChangeElement() {
381
382         return m_changeElement;
383     }
384
385     /**
386      * Returns the current element locale.<p>
387      *
388      * @return the current element locale
389      */

390     public Locale JavaDoc getElementLocale() {
391
392         if (m_elementLocale == null) {
393             m_elementLocale = CmsLocaleManager.getLocale(getParamElementlanguage());
394         }
395         return m_elementLocale;
396     }
397
398     /**
399      * Returns the current element language.<p>
400      *
401      * @return the current element language
402      */

403     public String JavaDoc getParamElementlanguage() {
404
405         return m_paramElementlanguage;
406     }
407
408     /**
409      * Returns the current element name.<p>
410      *
411      * @return the current element name
412      */

413     public String JavaDoc getParamElementname() {
414
415         return m_paramElementname;
416     }
417
418     /**
419      * Returns the name of the temporary file.<p>
420      *
421      * @return the name of the temporary file
422      */

423     public String JavaDoc getParamTempfile() {
424
425         return m_paramTempFile;
426     }
427
428     /**
429      * Sets the current element language.<p>
430      *
431      * @param elementLanguage the current element language
432      */

433     public void setParamElementlanguage(String JavaDoc elementLanguage) {
434
435         m_paramElementlanguage = elementLanguage;
436     }
437
438     /**
439      * Sets the current element name.<p>
440      *
441      * @param elementName the current element name
442      */

443     public void setParamElementname(String JavaDoc elementName) {
444
445         m_paramElementname = elementName;
446     }
447
448     /**
449      * Sets the name of the temporary file.<p>
450      *
451      * @param fileName the name of the temporary file
452      */

453     public void setParamTempfile(String JavaDoc fileName) {
454
455         m_paramTempFile = fileName;
456     }
457
458     /**
459      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
460      */

461     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
462
463         // fill the parameter values in the get/set methods
464
fillParamValues(request);
465         // set the dialog type
466
setParamDialogtype(DIALOG_TYPE);
467         // set the action for the JSP switch
468
if (DIALOG_UPDATE_ELEMENTS.equals(getParamAction())) {
469             setAction(ACTION_UPDATE_ELEMENTS);
470         } else {
471             setAction(ACTION_DEFAULT);
472             // build title for delete dialog
473
setParamTitle(key(
474                 Messages.GUI_EDITOR_DIALOG_ELEMENTS_TITLE_1,
475                 new Object JavaDoc[] {CmsResource.getName(getParamResource())}));
476         }
477     }
478 }
Popular Tags