1 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 ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.jsp.JspException ; 34 import javax.servlet.jsp.JspTagException ; 35 import javax.servlet.jsp.PageContext ; 36 import javax.servlet.jsp.tagext.JspFragment ; 37 import javax.servlet.jsp.tagext.SimpleTagSupport ; 38 import java.io.IOException ; 39 import java.io.StringWriter ; 40 import java.net.MalformedURLException ; 41 import java.util.Arrays ; 42 import java.util.Map ; 43 44 94 public class EditTag extends SimpleTagSupport { 95 96 protected transient final Log log = LogFactory.getLog(EditTag.class); 97 protected static final String NUMBER_KEY = "com.blandware.atleap.taglib.content.EDIT_TAG_NUMBER"; 98 99 102 protected String action = null; 103 104 107 protected String forward = null; 108 109 112 protected String uri = null; 113 114 117 protected String contextMenuJsp = null; 118 119 120 130 public String getAction() { 131 return action; 132 } 133 134 140 public void setAction(String action) { 141 this.action = action; 142 } 143 144 154 public String getForward() { 155 return forward; 156 } 157 158 164 public void setForward(String forward) { 165 this.forward = forward; 166 } 167 168 178 public String getUri() { 179 return uri; 180 } 181 182 188 public void setUri(String uri) { 189 this.uri = uri; 190 } 191 192 201 public String getContextMenuJsp() { 202 return contextMenuJsp; 203 } 204 205 210 public void setContextMenuJsp(String contextMenuJsp) { 211 this.contextMenuJsp = contextMenuJsp; 212 } 213 214 220 public void doTag() throws JspException , IOException { 221 222 PageContext pageContext = (PageContext ) getJspContext(); 223 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 224 ApplicationResources applicationResources = ApplicationResources.getInstance(pageContext.getServletContext()); 225 226 if ( uri == null && action == null && forward == null ) { 227 JspTagException e = new JspTagException ("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 e ) { 237 JspTagException ex = new JspTagException (e); 238 throw ex; 239 } 240 } 241 242 Integer tagNumber = (Integer ) pageContext.getAttribute(NUMBER_KEY, PageContext.REQUEST_SCOPE); 244 if ( tagNumber == null ) { 245 tagNumber = new Integer (0); 246 } else { 247 tagNumber = new Integer (tagNumber.intValue() + 1); 248 } 249 250 pageContext.setAttribute(NUMBER_KEY, tagNumber, PageContext.REQUEST_SCOPE); 251 252 253 String content = null; 254 boolean contentExists = true; 255 StringWriter sw = new StringWriter (); 256 JspFragment 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 editModeEnabled = request.getSession().getAttribute(WebappConstants.SITE_EDIT_MODE_ENABLED_KEY); 275 boolean createWrapper = editModeEnabled != null && Boolean.TRUE.equals(editModeEnabled); 276 if ( createWrapper ) { 277 StringBuffer divId = new StringBuffer ("__editable__content__").append(tagNumber).append("__wrapper__"); 278 String requestUrl = (String ) request.getAttribute(ContentTilesRequestProcessor.PROCESSED_URL); 279 280 StringBuffer contextMenuId = new StringBuffer ("__contextMenu__edit__").append(tagNumber.toString()); 282 Map params = RequestUtil.getRequestParametersFromUri(uri); 284 String base = RequestUtil.getBaseFromUri(uri); 285 params.put("localesMode", "current"); 286 String uriForCurrentLocale = RequestUtil.appendParams(base, params); 287 params.put("localesMode", "all"); 288 String uriForAllLocales = RequestUtil.appendParams(base, params); 289 String uriForChosenLocales = uri; 290 291 296 StringBuffer contextMenuLayerVar = new StringBuffer (contextMenuId.toString()); 297 298 StringBuffer onmouseover = new StringBuffer ("doSelectLayer(this.id);"); 299 StringBuffer onmouseout = new StringBuffer ("doUnselectLayer(this.id);"); 300 StringBuffer ondblclick = new StringBuffer ("doCallContentUpdate('").append(uriForCurrentLocale).append("', '").append(requestUrl).append("');"); 301 StringBuffer oncontextmenu = new StringBuffer ("return showContextMenu(").append(contextMenuLayerVar).append(", event);"); 302 303 String divDisplay = "block"; 304 305 StringBuffer wrappedContent = new StringBuffer ("<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 String editInCurrentLocaleTitle = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY, "core.editMode.menu.context.editInCurrentLocale"); 320 String editInAllLocalesTitle = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY, "core.editMode.menu.context.editInAllLocales"); 321 String editInChosenLocalesTitle = TagUtils.getInstance().message(pageContext, Globals.MESSAGES_KEY, Globals.LOCALE_KEY, "core.editMode.menu.context.editInChosenLocales"); 322 323 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 pageContext.setAttribute(WebappConstants.CONTEXT_MENUS_KEY, Arrays.asList(new ContextMenuItem[]{menu}), PageContext.REQUEST_SCOPE); 335 336 try { 338 pageContext.include(contextMenuJsp); 339 } catch ( ServletException ex ) { 340 JspTagException e = new JspTagException (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 |