KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/editors/CmsSimplePageEditor.java,v $
3  * Date : $Date: 2006/10/19 14:16:40 $
4  * Version: $Revision: 1.18 $
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.CmsResourceFilter;
35 import org.opencms.file.CmsVfsResourceNotFoundException;
36 import org.opencms.i18n.CmsEncoder;
37 import org.opencms.jsp.CmsJspActionElement;
38 import org.opencms.lock.CmsLock;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.CmsLog;
41 import org.opencms.main.OpenCms;
42 import org.opencms.util.CmsStringUtil;
43 import org.opencms.workplace.CmsWorkplaceSettings;
44 import org.opencms.workplace.galleries.A_CmsGallery;
45 import org.opencms.xml.page.CmsXmlPageFactory;
46
47 import java.util.ArrayList JavaDoc;
48 import java.util.Collections JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52 import java.util.Map JavaDoc;
53 import java.util.Properties JavaDoc;
54
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56 import javax.servlet.jsp.JspException JavaDoc;
57
58 import org.apache.commons.logging.Log;
59
60 /**
61  * Creates the output for editing a CmsDefaultPage with the simple textarea editor.<p>
62  *
63  * The following editor uses this class:
64  * <ul>
65  * <li>/editors/simplehtml/editor.jsp
66  * </ul>
67  * <p>
68  *
69  * @author Andreas Zahner
70  *
71  * @version $Revision: 1.18 $
72  *
73  * @since 6.0.0
74  */

75 public class CmsSimplePageEditor extends CmsDefaultPageEditor {
76
77     /** Constant for the editor type, must be the same as the editors subfolder name in the VFS. */
78     private static final String JavaDoc EDITOR_TYPE = "simplehtml";
79
80     /** The log object for this class. */
81     private static final Log LOG = CmsLog.getLog(CmsSimplePageEditor.class);
82
83     /**
84      * Public constructor.<p>
85      *
86      * @param jsp an initialized JSP action element
87      */

88     public CmsSimplePageEditor(CmsJspActionElement jsp) {
89
90         super(jsp);
91     }
92
93     /**
94      * @see org.opencms.workplace.editors.CmsDefaultPageEditor#buildGalleryButtons(CmsEditorDisplayOptions, int, Properties)
95      */

96     public String JavaDoc buildGalleryButtons(CmsEditorDisplayOptions options, int buttonStyle, Properties JavaDoc displayOptions) {
97
98         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
99         Map JavaDoc galleryMap = OpenCms.getWorkplaceManager().getGalleries();
100         List JavaDoc galleries = new ArrayList JavaDoc(galleryMap.size());
101         Map JavaDoc typeMap = new HashMap JavaDoc(galleryMap.size());
102
103         Iterator JavaDoc i = galleryMap.keySet().iterator();
104         while (i.hasNext()) {
105             String JavaDoc key = (String JavaDoc)i.next();
106             A_CmsGallery currGallery = (A_CmsGallery)galleryMap.get(key);
107             galleries.add(currGallery);
108             // put the type name to the type Map
109
typeMap.put(currGallery, key);
110         }
111
112         // sort the found galleries by their order
113
Collections.sort(galleries);
114
115         for (int k = 0; k < galleries.size(); k++) {
116             A_CmsGallery currGallery = (A_CmsGallery)galleries.get(k);
117             String JavaDoc galleryType = (String JavaDoc)typeMap.get(currGallery);
118             String JavaDoc galleryName = CmsStringUtil.substitute(galleryType, "gallery", "");
119             if (options.showElement("gallery." + galleryName, displayOptions)) {
120                 // gallery is shown, create button code
121
result.append(button(
122                     "javascript:openGallery(\'" + galleryType + "\');",
123                     null,
124                     galleryType,
125                     Messages.getGalleryKey(galleryName),
126                     buttonStyle));
127             }
128         }
129
130         return result.toString();
131     }
132
133     /**
134      * @see org.opencms.workplace.editors.CmsEditor#getEditorResourceUri()
135      */

136     public String JavaDoc getEditorResourceUri() {
137
138         return getSkinUri() + "editors/" + EDITOR_TYPE + "/";
139     }
140
141     /**
142      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
143      */

144     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
145
146         // fill the parameter values in the get/set methods
147
fillParamValues(request);
148
149         // set the dialog type
150
setParamDialogtype(EDITOR_TYPE);
151
152         // Initialize a page object from the temporary file
153
if (getParamTempfile() != null && !"null".equals(getParamTempfile())) {
154             try {
155                 m_file = getCms().readFile(this.getParamTempfile(), CmsResourceFilter.ALL);
156                 m_page = CmsXmlPageFactory.unmarshal(getCms(), m_file);
157             } catch (CmsException e) {
158                 if (e instanceof CmsVfsResourceNotFoundException) {
159                     // the tempfile is missing, maybe someone else has deleted it
160
// try to create a new one and redo the initalization
161
try {
162                         setParamTempfile(createTempFile());
163                         m_file = getCms().readFile(this.getParamTempfile(), CmsResourceFilter.ALL);
164                         m_page = CmsXmlPageFactory.unmarshal(getCms(), m_file);
165                     } catch (CmsException e1) {
166                         // error during initialization
167
try {
168                             showErrorPage(this, e1);
169                         } catch (JspException JavaDoc exc) {
170                             // should usually never happen
171
if (LOG.isInfoEnabled()) {
172                                 LOG.info(exc);
173                             }
174                         }
175                     }
176                 } else {
177                     // error during initialization
178
try {
179                         showErrorPage(this, e);
180                     } catch (JspException JavaDoc exc) {
181                         // should usually never happen
182
if (LOG.isInfoEnabled()) {
183                             LOG.info(exc);
184                         }
185                     }
186                 }
187             }
188         }
189
190         // set the action for the JSP switch
191
if (EDITOR_SAVE.equals(getParamAction())) {
192             setAction(ACTION_SAVE);
193         } else if (EDITOR_SAVEEXIT.equals(getParamAction())) {
194             setAction(ACTION_SAVEEXIT);
195         } else if (EDITOR_SAVEACTION.equals(getParamAction())) {
196             setAction(ACTION_SAVEACTION);
197             try {
198                 actionDirectEdit();
199             } catch (Exception JavaDoc e) {
200                 // should usually never happen
201
if (LOG.isInfoEnabled()) {
202                     LOG.info(e);
203                 }
204             }
205             setAction(ACTION_EXIT);
206         } else if (EDITOR_EXIT.equals(getParamAction())) {
207             setAction(ACTION_EXIT);
208         } else if (EDITOR_CLOSEBROWSER.equals(getParamAction())) {
209             // closed browser window accidentally, unlock resource and delete temporary file
210
actionClear(true);
211             return;
212         } else if (EDITOR_DELETELOCALE.equals(getParamAction())) {
213             setAction(ACTION_DELETELOCALE);
214         } else if (EDITOR_CHANGE_ELEMENT.equals(getParamAction())) {
215             setAction(ACTION_SHOW);
216             actionChangeBodyElement();
217             // prepare the content String for the editor
218
prepareContent(false);
219         } else if (EDITOR_CLEANUP.equals(getParamAction())) {
220             setAction(ACTION_SHOW);
221             actionCleanupBodyElement();
222             // prepare the content String for the editor
223
prepareContent(false);
224         } else if (EDITOR_SHOW.equals(getParamAction())) {
225             setAction(ACTION_SHOW);
226             // prepare the content String for the editor
227
prepareContent(false);
228         } else if (EDITOR_PREVIEW.equals(getParamAction())) {
229             setAction(ACTION_PREVIEW);
230         } else {
231             // initial call of editor, initialize page and page parameters
232
setAction(ACTION_DEFAULT);
233             try {
234                 // lock resource if autolock is enabled in configuration
235
if (Boolean.valueOf(getParamDirectedit()).booleanValue()) {
236                     // set a temporary lock in direct edit mode
237
checkLock(getParamResource(), CmsLock.TEMPORARY);
238                 } else {
239                     // set common lock
240
checkLock(getParamResource());
241                 }
242                 // create the temporary file
243
setParamTempfile(createTempFile());
244                 // initialize a page object from the created temporary file
245
m_file = getCms().readFile(this.getParamTempfile(), CmsResourceFilter.ALL);
246                 m_page = CmsXmlPageFactory.unmarshal(getCms(), m_file);
247             } catch (CmsException e) {
248                 // error during initialization
249
try {
250                     showErrorPage(this, e);
251                     return;
252                 } catch (JspException JavaDoc exc) {
253                     // should usually never happen
254
if (LOG.isInfoEnabled()) {
255                         LOG.info(exc);
256                     }
257                 }
258             }
259             // set the initial body language & name if not given in request parameters
260
if (getParamElementlanguage() == null) {
261                 initBodyElementLanguage();
262             }
263             if (getParamElementname() == null) {
264                 initBodyElementName(null);
265             }
266             // initialize the editor content
267
initContent();
268             // prepare the content String for the editor
269
prepareContent(false);
270         }
271     }
272
273     /**
274      * Manipulates the content String and removes leading and trailing white spaces.<p>
275      *
276      * @param save if set to true, the content parameter is not updated
277      * @return the prepared content String
278      */

279     protected String JavaDoc prepareContent(boolean save) {
280
281         String JavaDoc content = getParamContent().trim();
282         // ensure all chars in the content are valid for the selected encoding
283
content = CmsEncoder.adjustHtmlEncoding(content, getFileEncoding());
284         if (!save) {
285             setParamContent(content);
286         }
287         return content;
288     }
289
290 }
291
Popular Tags