KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > CmsJspTagEditable


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagEditable.java,v $
3  * Date : $Date: 2006/10/26 12:25:35 $
4  * Version: $Revision: 1.24 $
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.jsp;
33
34 import org.opencms.file.CmsBackupResourceHandler;
35 import org.opencms.file.CmsObject;
36 import org.opencms.flex.CmsFlexController;
37 import org.opencms.main.CmsLog;
38 import org.opencms.main.OpenCms;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.workplace.editors.directedit.CmsDirectEditJspIncludeProvider;
41 import org.opencms.workplace.editors.directedit.CmsDirectEditMode;
42 import org.opencms.workplace.editors.directedit.CmsDirectEditParams;
43 import org.opencms.workplace.editors.directedit.I_CmsDirectEditProvider;
44
45 import javax.servlet.ServletRequest JavaDoc;
46 import javax.servlet.jsp.JspException JavaDoc;
47 import javax.servlet.jsp.PageContext JavaDoc;
48 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
49
50 import org.apache.commons.logging.Log;
51
52 /**
53  * Implementation of the <code>&lt;cms:editable/&gt;</code> tag.<p>
54  *
55  * This class is also used to generate the direct edit buttons for the
56  * <code>&lt;cms:include editable="..." /&gt;</code> and <code>&lt;cms:contentload editable="..." /&gt;</code> tags.<p>
57  *
58  * Since OpenCms version 6.2.3, the direct edit button HTML generated is controlled by an instance of {@link I_CmsDirectEditProvider}.
59  * The default direct edit provider used can be configured in <code>opencms-workplace.xml</code> in the
60  * <code>&lt;directeditprovider class="..." /&gt;</code> node. The standard provider is
61  * {@link org.opencms.workplace.editors.directedit.CmsDirectEditDefaultProvider}.
62  * It's possible to override the default provider onm a page-by-page basis by initializing direct edit with
63  * <code>&lt;cms:editable provider="...." /&gt;</code> on top of the page.<p>
64  *
65  * Since OpenCms version 6.2.3, it is also possible to place the HTML of the direct edit buttons manually.
66  * This is intended for pages where the template HTML is not compatible with the direct edit HTML,
67  * which usually results in a funny placement of the direct edit buttons in a totally wrong position.
68  * To do manual placement of the direct edit buttons, you need to place <code>&lt;cms:editable mode="manual"&gt;</code> and
69  * <code>&lt;/cms:editable&gt;</code> around your HTML. Both tags (start and end) will insert HTML according to
70  * the used {@link I_CmsDirectEditProvider}. The direct edit provider used must also support manual
71  * placing, or the manual tags will be ignored and the HTML will be inserted at the automatic position.
72  * A provider which support manual placing is the {@link org.opencms.workplace.editors.directedit.CmsDirectEditTextButtonProvider}.<p>
73  *
74  * @version $Revision: 1.24 $
75  *
76  * @since 6.0.0
77  */

78 public class CmsJspTagEditable extends BodyTagSupport JavaDoc {
79
80     /** The log object for this class. */
81     private static final Log LOG = CmsLog.getLog(CmsJspTagEditable.class);
82
83     /** Serial version UID required for safe serialization. */
84     private static final long serialVersionUID = 4137789622146499225L;
85
86     /** File with editable elements. */
87     protected String JavaDoc m_file;
88
89     /** Indicates which direct edit mode is active. */
90     protected CmsDirectEditMode m_mode;
91
92     /** Class name of the direct edit provider. */
93     protected String JavaDoc m_provider;
94
95     /** Indicates if the tag is the first on the page, this mean the header file must be included. */
96     private boolean m_firstOnPage;
97
98     /** Indicates if the direct edit HTML is to be placed manually. */
99     private boolean m_manualPlacement;
100
101     /**
102      * Editable action method.<p>
103      *
104      * @param context the current JSP page context
105      * @param provider the class name of the direct edit privider to use (may be <code>null</code>, which means use the default)
106      * @param mode the direct edit mode to use (may be <code>null</code>, which means current use mode on page)
107      * @param fileName optional filename parameter for the direct edit provider (may be <code>null</code>, which means use the default)
108      *
109      * @throws JspException in case something goes wrong
110      */

111     public static void editableTagAction(PageContext JavaDoc context, String JavaDoc provider, CmsDirectEditMode mode, String JavaDoc fileName)
112     throws JspException JavaDoc {
113
114         if (mode == CmsDirectEditMode.FALSE) {
115             // direct edit is turned off
116
return;
117         }
118
119         ServletRequest JavaDoc req = context.getRequest();
120         if (CmsBackupResourceHandler.isBackupRequest(req)) {
121             // don't display direct edit buttons on a backup resource
122
return;
123         }
124
125         CmsFlexController controller = CmsFlexController.getController(req);
126         CmsObject cms = controller.getCmsObject();
127
128         if (!cms.getRequestContext().currentProject().isOnlineProject()) {
129             // direct edit is never enabled in the online project
130
I_CmsDirectEditProvider eb = getDirectEditProvider(context);
131             if (eb == null) {
132                 if (CmsStringUtil.isNotEmpty(fileName) && CmsStringUtil.isEmpty(provider)) {
133                     // if only a filename but no provider class is given, use JSP includes for backward compatibility
134
provider = CmsDirectEditJspIncludeProvider.class.getName();
135                 }
136                 // no provider available in page context
137
if (CmsStringUtil.isNotEmpty(provider)) {
138                     try {
139                         // create a new instance of the selected provider
140
eb = (I_CmsDirectEditProvider)Class.forName(provider).newInstance();
141                     } catch (Exception JavaDoc e) {
142                         // log error
143
LOG.error(Messages.get().getBundle().key(Messages.ERR_DIRECT_EDIT_PROVIDER_1, provider), e);
144                     }
145                 }
146                 if (eb == null) {
147                     // use configured direct edit provider as a fallback
148
eb = OpenCms.getWorkplaceManager().getDirectEditProvider();
149                 }
150                 if (mode == null) {
151                     // use automatic placement by default
152
mode = CmsDirectEditMode.AUTO;
153                 }
154                 eb.init(cms, mode, fileName);
155                 // store the provider in the page context
156
setDirectEditProvider(context, eb);
157             }
158             if (eb.isManual(mode)) {
159                 // manual mode, insert required HTML
160
CmsDirectEditParams params = getDirectEditProviderParams(context);
161                 if (params != null) {
162                     // insert direct edit start HTML
163
eb.insertDirectEditStart(context, params);
164                 } else {
165                     // insert direct edit end HTML
166
eb.insertDirectEditEnd(context);
167                 }
168             } else {
169                 // insert direct edit header HTML
170
eb.insertDirectEditIncludes(context, new CmsDirectEditParams(cms.getRequestContext().getUri()));
171             }
172         }
173     }
174
175     /**
176      * Closes the current direct edit element.<p>
177      *
178      * @param context the current JSP page context
179      *
180      * @throws JspException in case something goes wrong
181      */

182     public static void endDirectEdit(PageContext JavaDoc context) throws JspException JavaDoc {
183
184         // get the direct edit bean from the context
185
I_CmsDirectEditProvider eb = getDirectEditProvider(context);
186
187         if (eb != null) {
188             // the direct edit bean must be available
189
eb.insertDirectEditEnd(context);
190         }
191     }
192
193     /**
194      * Returns the current initialized instance of the direct edit provider.<p>
195      *
196      * @param context the current JSP page context
197      *
198      * @return the current initialized instance of the direct edit provider
199      */

200     public static I_CmsDirectEditProvider getDirectEditProvider(PageContext JavaDoc context) {
201
202         // get the direct edit provider from the request attributes
203
return (I_CmsDirectEditProvider)context.getRequest().getAttribute(
204             I_CmsDirectEditProvider.ATTRIBUTE_DIRECT_EDIT_PROVIDER);
205     }
206
207     /**
208      * Includes the "direct edit" start element that adds HTML for the editable area to
209      * the output page.<p>
210      *
211      * @param context the current JSP page context
212      * @param params the direct edit parameters
213      *
214      * @return <code>true</code> in case a direct edit element has been opened
215      *
216      * @throws JspException in case something goes wrong
217      */

218     public static boolean startDirectEdit(PageContext JavaDoc context, CmsDirectEditParams params) throws JspException JavaDoc {
219
220         // get the direct edit bean from the context
221
I_CmsDirectEditProvider eb = getDirectEditProvider(context);
222
223         boolean result = false;
224         if (eb != null) {
225             // the direct edit bean must be available
226
if (eb.isManual(params.getMode())) {
227                 // store the given parameters for the next manual call
228
setDirectEditProviderParams(context, params);
229             } else {
230                 // automatic mode, insert direct edit HTML
231
result = eb.insertDirectEditStart(context, params);
232             }
233         }
234
235         return result;
236     }
237
238     /**
239      * Returns the current initialized instance of the direct edit provider parameters from the given page context.<p>
240      *
241      * Also removes the parameters from the given page context.<p>
242      *
243      * @param context the current JSP page context
244      *
245      * @return the current initialized instance of the direct edit provider parameters
246      */

247     protected static CmsDirectEditParams getDirectEditProviderParams(PageContext JavaDoc context) {
248
249         // get the current request
250
ServletRequest JavaDoc req = context.getRequest();
251         // get the direct edit params from the request attributes
252
CmsDirectEditParams result = (CmsDirectEditParams)req.getAttribute(I_CmsDirectEditProvider.ATTRIBUTE_DIRECT_EDIT_PROVIDER_PARAMS);
253         if (result != null) {
254             req.removeAttribute(I_CmsDirectEditProvider.ATTRIBUTE_DIRECT_EDIT_PROVIDER_PARAMS);
255         }
256         return result;
257     }
258
259     /**
260      * Sets the current initialized instance of the direct edit provider.<p>
261      *
262      * @param context the current JSP page context
263      *
264      * @param provider the current initialized instance of the direct edit provider to set
265      */

266     protected static void setDirectEditProvider(PageContext JavaDoc context, I_CmsDirectEditProvider provider) {
267
268         // set the direct edit provider as attribute to the request
269
context.getRequest().setAttribute(I_CmsDirectEditProvider.ATTRIBUTE_DIRECT_EDIT_PROVIDER, provider);
270     }
271
272     /**
273      * Sets the current initialized instance of the direct edit provider parameters to the page context.<p>
274      *
275      * @param context the current JSP page context
276      * @param params the current initialized instance of the direct edit provider parameters to set
277      */

278     protected static void setDirectEditProviderParams(PageContext JavaDoc context, CmsDirectEditParams params) {
279
280         // set the direct edit params as attribute to the request
281
context.getRequest().setAttribute(I_CmsDirectEditProvider.ATTRIBUTE_DIRECT_EDIT_PROVIDER_PARAMS, params);
282     }
283
284     /**
285      * Close the direct edit tag, also prints the direct edit HTML to the current page.<p>
286      *
287      * @return {@link #EVAL_PAGE}
288      *
289      * @throws JspException in case something goes wrong
290      */

291     public int doEndTag() throws JspException JavaDoc {
292
293         if (m_firstOnPage || m_manualPlacement) {
294             // only execute action for the first "editable" tag on the page (include file), or in manual mode
295
editableTagAction(pageContext, m_provider, m_mode, m_file);
296         }
297         release();
298
299         return EVAL_PAGE;
300     }
301
302     /**
303      * Opens the direct edit tag, if manual mode is set then the next
304      * start HTML for the direct edit buttons is printed to the page.<p>
305      *
306      * @return {@link #EVAL_BODY_INCLUDE}
307      *
308      * @throws JspException in case something goes wrong
309      */

310     public int doStartTag() throws JspException JavaDoc {
311
312         if (!CmsFlexController.isCmsOnlineRequest(pageContext.getRequest())) {
313             // all this does NOT apply to the "online" project
314
I_CmsDirectEditProvider eb = getDirectEditProvider(pageContext);
315             // if no provider is available this is the first "editable" tag on the page
316
m_firstOnPage = (eb == null);
317             m_manualPlacement = false;
318             if (m_mode == CmsDirectEditMode.MANUAL) {
319                 // manual mode requested, we may need to insert HTML
320
if (!m_firstOnPage && (eb != null)) {
321                     // first tag on a page is only for insertion of header HTML
322
if (eb.isManual(m_mode)) {
323                         // the provider supports manual placement of buttons
324
m_manualPlacement = true;
325                         editableTagAction(pageContext, m_provider, m_mode, m_file);
326                     }
327                 }
328             }
329         } else {
330             // this will ensure the "end" tag is also ignored in the online project
331
m_firstOnPage = false;
332             m_manualPlacement = false;
333         }
334         return EVAL_BODY_INCLUDE;
335     }
336
337     /**
338      * Gets the file with elements for direct editing.<p>
339      *
340      * @return the file
341      */

342     public String JavaDoc getFile() {
343
344         return m_file != null ? m_file : "";
345     }
346
347     /**
348      * Returns the direct edit mode.<p>
349      *
350      * @return the direct edit mode
351      */

352     public String JavaDoc getMode() {
353
354         return m_mode != null ? m_mode.toString() : "";
355     }
356
357     /**
358      * Returns the class name of the direct edit provider.<p>
359      *
360      * @return the class name of the direct edit provider
361      */

362     public String JavaDoc getProvider() {
363
364         return m_provider != null ? m_provider : "";
365     }
366
367     /**
368      * Releases any resources we may have (or inherit).<p>
369      */

370     public void release() {
371
372         super.release();
373         m_file = null;
374         m_provider = null;
375         m_mode = null;
376         m_firstOnPage = false;
377         m_manualPlacement = true;
378     }
379
380     /**
381      * Sets the file with elements for direct editing.<p>
382      *
383      * @param file the file to set
384      */

385     public void setFile(String JavaDoc file) {
386
387         if (file != null) {
388             m_file = file;
389         }
390     }
391
392     /**
393      * Sets the direct edit mode.<p>
394      *
395      * @param mode the direct edit mode to set
396      */

397     public void setMode(String JavaDoc mode) {
398
399         m_mode = CmsDirectEditMode.valueOf(mode);
400     }
401
402     /**
403      * Sets the class name of the direct edit provider.<p>
404      *
405      * @param provider the class name of the direct edit provider to set
406      */

407     public void setProvider(String JavaDoc provider) {
408
409         if (provider != null) {
410             m_provider = provider;
411         }
412     }
413 }
Popular Tags