KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > content > EditTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.content;
17
18 import com.blandware.atleap.webapp.struts.ContentTilesRequestProcessor;
19 import com.blandware.atleap.webapp.taglib.core.util.ContextMenuItem;
20 import com.blandware.atleap.webapp.taglib.core.util.JavaScriptUtil;
21 import com.blandware.atleap.webapp.util.core.ApplicationResources;
22 import com.blandware.atleap.webapp.util.core.RequestUtil;
23 import com.blandware.atleap.webapp.util.core.WebappConstants;
24 import com.blandware.atleap.webapp.util.core.WebappUtil;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.commons.validator.GenericValidator;
28 import org.apache.struts.Globals;
29 import org.apache.struts.taglib.TagUtils;
30
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.jsp.JspException JavaDoc;
34 import javax.servlet.jsp.JspTagException JavaDoc;
35 import javax.servlet.jsp.PageContext JavaDoc;
36 import javax.servlet.jsp.tagext.JspFragment JavaDoc;
37 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.StringWriter JavaDoc;
40 import java.net.MalformedURLException JavaDoc;
41 import java.util.Arrays JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /**
45  * <p>While in edit mode this tag wraps body with &lt;div&gt;&lt;/div&gt; tags to mark it editable.
46  * When system is in edit mode and user double-clicks on editable area, action with rewritten URI is called.
47  * Also generated DIV is supplied with context menu; user may do following using
48  * it: edit for current content locale (the same as if user double-clicked it),
49  * edit for all content locales, or edit for selected content locales.
50  * Technically, the only difference between URIs, corresponding to those three
51  * context menu items, is that for first case (editing for current locale)
52  * <code>localesMode=current</code> is added to URI, and for second case (editing
53  * for all locales) <code>localesMode=all</code> is added.
54  * </p>
55  * <p>
56  * If nothing is supplied by tag body, or result is empty string, then message
57  * like "The field value is empty, please insert the text" is treated as result.
58  * </p>
59  * <p>
60  * Allowed parameters are:
61  * <ul>
62  * <li>
63  * <b>action</b> - context-relative URI of Struts Action to call to update
64  * content which is placed inside this tag
65  * </li>
66  * <li>
67  * <b>forward</b> - name of global Action Forward to use to update content which
68  * is placed inside this tag
69  * </li>
70  * <li>
71  * <b>uri</b> - context-relative URI of action to call to update content which
72  * is placed inside this tag
73  * </li>
74  * <li>
75  * <b>contextMenuJsp</b> - path to JSP which will render a context menu. If not
76  * given, <code>WebappConstants.DEFAULT_CONTEXT_MENU_JSP</code> is used.
77  * </li>
78  * </ul>
79  * </p>
80  * <p>
81  * At least one of <b>action</b>, <b>forward</b> or <b>uri</b> must be
82  * specified. When calculating URI, firstly <b>action</b> is considered, then
83  * (if it's <code>null</code>), <b>forward</b> is taken into account. If it's
84  * <code>null</code> too, <b>uri</b> is used.
85  * </p>
86  * <p><a HREF="EditTag.java.htm"><i>View Source</i></a></p>
87  * <p/>
88  *
89  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
90  * @version $Revision: 1.19 $ $Date: 2005/10/24 15:06:21 $
91  * @jsp.tag name="edit"
92  * body-content="scriptless"
93  */

94 public class EditTag extends SimpleTagSupport JavaDoc {
95
96     protected transient final Log log = LogFactory.getLog(EditTag.class);
97     protected static final String JavaDoc NUMBER_KEY = "com.blandware.atleap.taglib.content.EDIT_TAG_NUMBER";
98
99     /**
100      * Context-relative URI of Struts Action to call to update content which is placed inside this tag
101      */

102     protected String JavaDoc action = null;
103
104     /**
105      * Name of global ActionForward to use to update content which is placed inside this tag
106      */

107     protected String JavaDoc forward = null;
108
109     /**
110      * Context-relative URI of action to call to update content which is placed inside this tag
111      */

112     protected String JavaDoc uri = null;
113
114     /**
115      * Path to JSP which will render a context menu
116      */

117     protected String JavaDoc contextMenuJsp = null;
118
119
120     /**
121      * Returns Struts Action
122      *
123      * @return Struts Action
124      * @see #action
125      * @jsp.attribute required="false"
126      * rtexprvalue="true"
127      * type="java.lang.String"
128      * description="Context-relative URI of Struts Action to call to update content which is placed inside this tag"
129      */

130     public String JavaDoc getAction() {
131         return action;
132     }
133
134     /**
135      * Sets Struts Action
136      *
137      * @param action Struts Action to set
138      * @see #action
139      */

140     public void setAction(String JavaDoc action) {
141         this.action = action;
142     }
143
144     /**
145      * Returns ActionForward
146      *
147      * @return ActionForward
148      * @see #forward
149      * @jsp.attribute required="false"
150      * rtexprvalue="true"
151      * type="java.lang.String"
152      * description="Name of global ActionForward to use to update content which is placed inside this tag"
153      */

154     public String JavaDoc getForward() {
155         return forward;
156     }
157
158     /**
159      * Sets ActionForward
160      *
161      * @param forward ActionForward to set
162      * @see #forward
163      */

164     public void setForward(String JavaDoc forward) {
165         this.forward = forward;
166     }
167
168     /**
169      * Returns URI
170      *
171      * @return URI
172      * @see #uri
173      * @jsp.attribute required="false"
174      * rtexprvalue="true"
175      * type="java.lang.String"
176      * description="Context-relative URI to call to update content which is placed inside this tag"
177      */

178     public String JavaDoc getUri() {
179         return uri;
180     }
181
182     /**
183      * Sets URI
184      *
185      * @param uri URI to set
186      * @see #uri
187      */

188     public void setUri(String JavaDoc uri) {
189         this.uri = uri;
190     }
191
192     /**
193      * Returns address of JSP that will be used to render a context menu
194      *
195      * @return address of JSP for context menu
196      * @jsp.attribute required="false"
197      * rtexprvalue="true"
198      * type="java.lang.String"
199      * description="Path to JSP which will render a context menu"
200      */

201     public String JavaDoc getContextMenuJsp() {
202         return contextMenuJsp;
203     }
204
205     /**
206      * Sets address of JSP that will be used to render a context menu
207      *
208      * @param contextMenuJsp address of JSP for context menu to set
209      */

210     public void setContextMenuJsp(String JavaDoc contextMenuJsp) {
211         this.contextMenuJsp = contextMenuJsp;
212     }
213
214     /**
215      * Processes the tag
216      *
217      * @throws JspException
218      * @throws IOException
219      */

220     public void doTag() throws JspException JavaDoc, IOException JavaDoc {
221
222         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
223         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
224         ApplicationResources applicationResources = ApplicationResources.getInstance(pageContext.getServletContext());
225
226         if ( uri == null && action == null && forward == null ) {
227             JspTagException JavaDoc e = new JspTagException JavaDoc("One of the attributes 'action', 'forward' or 'uri' must be specified");
228             throw e;
229         }
230
231         if ( action != null ) {
232             uri = WebappUtil.getActionMappingURL(action, null, request, WebappConstants.URL_TYPE_DOMAIN_RELATIVE);
233         } else if ( forward != null ) {
234             try {
235                 uri = WebappUtil.getActionForwardURL(forward, null, request, WebappConstants.URL_TYPE_DOMAIN_RELATIVE);
236             } catch ( MalformedURLException JavaDoc e ) {
237                 JspTagException JavaDoc ex = new JspTagException JavaDoc(e);
238                 throw ex;
239             }
240         }
241
242         // calculate tag number
243
Integer JavaDoc tagNumber = (Integer JavaDoc) pageContext.getAttribute(NUMBER_KEY, PageContext.REQUEST_SCOPE);
244         if ( tagNumber == null ) {
245             tagNumber = new Integer JavaDoc(0);
246         } else {
247             tagNumber = new Integer JavaDoc(tagNumber.intValue() + 1);
248         }
249
250         pageContext.setAttribute(NUMBER_KEY, tagNumber, PageContext.REQUEST_SCOPE);
251
252
253         String JavaDoc content = null;
254         boolean contentExists = true;
255         StringWriter JavaDoc sw = new StringWriter JavaDoc();
256         JspFragment JavaDoc body = getJspBody();
257         if ( body != null ) {
258             getJspBody().invoke(sw);
259             content = sw.toString();
260             contentExists = content.trim().length() > 0;
261         } else {
262             contentExists = false;
263         }
264
265         TagUtils tagUtils = TagUtils.getInstance();
266         if ( !contentExists ) {
267             content = applicationResources.getMessage(request, "core.commons.contentTag.insertText");
268         }
269
270         if ( GenericValidator.isBlankOrNull(contextMenuJsp) ) {
271             contextMenuJsp = WebappConstants.DEFAULT_CONTEXT_MENU_JSP;
272         }
273
274         Object JavaDoc editModeEnabled = request.getSession().getAttribute(WebappConstants.SITE_EDIT_MODE_ENABLED_KEY);
275         boolean createWrapper = editModeEnabled != null && Boolean.TRUE.equals(editModeEnabled);
276         if ( createWrapper ) {
277             StringBuffer JavaDoc divId = new StringBuffer JavaDoc("__editable__content__").append(tagNumber).append("__wrapper__");
278             String JavaDoc requestUrl = (String JavaDoc) request.getAttribute(ContentTilesRequestProcessor.PROCESSED_URL);
279
280             // make context menu ID
281
StringBuffer JavaDoc contextMenuId = new StringBuffer JavaDoc("__contextMenu__edit__").append(tagNumber.toString());
282             // make URIs for context menu
283
Map JavaDoc params = RequestUtil.getRequestParametersFromUri(uri);
284             String JavaDoc base = RequestUtil.getBaseFromUri(uri);
285             params.put("localesMode", "current");
286             String JavaDoc uriForCurrentLocale = RequestUtil.appendParams(base, params);
287             params.put("localesMode", "all");
288             String JavaDoc uriForAllLocales = RequestUtil.appendParams(base, params);
289             String JavaDoc uriForChosenLocales = uri;
290
291             /*
292                That's a name of JS variable that will hold this
293                context menu (that variable is used to refer to this context
294                menu from showContextMenu(), for instance)
295             */

296             StringBuffer JavaDoc contextMenuLayerVar = new StringBuffer JavaDoc(contextMenuId.toString());
297
298             StringBuffer JavaDoc onmouseover = new StringBuffer JavaDoc("doSelectLayer(this.id);");
299             StringBuffer JavaDoc onmouseout = new StringBuffer JavaDoc("doUnselectLayer(this.id);");
300             StringBuffer JavaDoc ondblclick = new StringBuffer JavaDoc("doCallContentUpdate('").append(uriForCurrentLocale).append("', '").append(requestUrl).append("');");
301             StringBuffer JavaDoc oncontextmenu = new StringBuffer JavaDoc("return showContextMenu(").append(contextMenuLayerVar).append(", event);");
302
303             String JavaDoc divDisplay = "block";
304
305             // append content to wrapper and close DIV
306
StringBuffer JavaDoc wrappedContent = new StringBuffer JavaDoc("<div id=\"").append(divId)
307                     .append("\" name=\"").append(divId)
308                     .append("\" class=\"fieldValueWrapper\" style=\"display: ")
309                     .append(divDisplay).append(";\" ").append("onmouseover=\"")
310                     .append(onmouseover).append("\" onmouseout=\"").append(onmouseout)
311                     .append("\" ondblclick=\"").append(ondblclick)
312                     .append("\" oncontextmenu=\"").append(oncontextmenu).append("\">")
313                     .append(content).append("</div>");
314
315             tagUtils.write(pageContext, wrappedContent.toString());
316
317             // Insert context menu
318
// Obtain titles
319
String JavaDoc editInCurrentLocaleTitle = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY, "core.editMode.menu.context.editInCurrentLocale");
320             String JavaDoc editInAllLocalesTitle = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY, "core.editMode.menu.context.editInAllLocales");
321             String JavaDoc editInChosenLocalesTitle = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY, "core.editMode.menu.context.editInChosenLocales");
322
323             // Create and populate list of items
324
ContextMenuItem menu = new ContextMenuItem();
325             menu.setId(contextMenuId.toString());
326             menu.addChildItem(new ContextMenuItem(JavaScriptUtil.createLinkAsLayer("javascript:doCallContentUpdate('" + uriForCurrentLocale + "', '" + requestUrl + "');",
327                     editInCurrentLocaleTitle, WebappConstants.CONTEXT_MENU_ITEM_STYLE_CLASS, WebappConstants.HIGHLIGHTED_CONTEXT_MENU_ITEM_STYLE_CLASS)));
328             menu.addChildItem(new ContextMenuItem(JavaScriptUtil.createLinkAsLayer("javascript:doCallContentUpdate('" + uriForAllLocales + "', '" + requestUrl + "');",
329                     editInAllLocalesTitle, WebappConstants.CONTEXT_MENU_ITEM_STYLE_CLASS, WebappConstants.HIGHLIGHTED_CONTEXT_MENU_ITEM_STYLE_CLASS)));
330             menu.addChildItem(new ContextMenuItem(JavaScriptUtil.createLinkAsLayer("javascript:doCallContentUpdate('" + uriForChosenLocales + "', '" + requestUrl + "');",
331                     editInChosenLocalesTitle, WebappConstants.CONTEXT_MENU_ITEM_STYLE_CLASS, WebappConstants.HIGHLIGHTED_CONTEXT_MENU_ITEM_STYLE_CLASS)));
332
333             // Store menu in request
334
pageContext.setAttribute(WebappConstants.CONTEXT_MENUS_KEY, Arrays.asList(new ContextMenuItem[]{menu}), PageContext.REQUEST_SCOPE);
335
336             // Do include
337
try {
338                 pageContext.include(contextMenuJsp);
339             } catch ( ServletException JavaDoc ex ) {
340                 JspTagException JavaDoc e = new JspTagException JavaDoc(ex);
341                 TagUtils.getInstance().saveException(pageContext, e);
342                 throw e;
343             }
344
345
346         } else {
347             tagUtils.write(pageContext, content);
348         }
349
350     }
351
352 }
353
Popular Tags