KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > widget > html > HtmlMenuRenderer


1 /*
2  * $Id: HtmlMenuRenderer.java 5864 2005-09-30 14:27:12Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.widget.html;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.ServletContext JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import javax.servlet.http.HttpSession JavaDoc;
34
35 import org.ofbiz.base.util.StringUtil;
36 import org.ofbiz.base.util.UtilValidate;
37 import org.ofbiz.entity.GenericDelegator;
38 import org.ofbiz.entity.GenericValue;
39 import org.ofbiz.webapp.control.RequestHandler;
40 import org.ofbiz.webapp.taglib.ContentUrlTag;
41 import org.ofbiz.widget.menu.MenuStringRenderer;
42 import org.ofbiz.widget.menu.ModelMenu;
43 import org.ofbiz.widget.menu.ModelMenuItem;
44 import org.ofbiz.widget.menu.ModelMenuItem.Image;
45 import org.ofbiz.widget.menu.ModelMenuItem.Link;
46
47 /**
48  * Widget Library - HTML Menu Renderer implementation
49  *
50  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
51  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
52  * @version $Rev: 5864 $
53  * @since 2.2
54  */

55 public class HtmlMenuRenderer implements MenuStringRenderer {
56
57     HttpServletRequest JavaDoc request;
58     HttpServletResponse JavaDoc response;
59     protected String JavaDoc userLoginIdAtPermGrant;
60     protected boolean userLoginIdHasChanged = true;
61     protected String JavaDoc permissionErrorMessage = "";
62
63     public static final String JavaDoc module = HtmlMenuRenderer.class.getName();
64
65     protected HtmlMenuRenderer() {}
66
67     public HtmlMenuRenderer(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
68         this.request = request;
69         this.response = response;
70     }
71
72     public void appendWhitespace(StringBuffer JavaDoc buffer) {
73         // appending line ends for now, but this could be replaced with a simple space or something
74
buffer.append("\r\n");
75         //buffer.append(' ');
76
}
77
78     public void appendOfbizUrl(StringBuffer JavaDoc buffer, String JavaDoc location) {
79         ServletContext JavaDoc ctx = (ServletContext JavaDoc) request.getAttribute("servletContext");
80         if (ctx == null) {
81             //if (Debug.infoOn()) Debug.logInfo("in appendOfbizUrl, ctx is null(0): buffer=" + buffer.toString() + " location:" + location, "");
82
HttpSession JavaDoc session = request.getSession();
83             if (session != null) {
84                 ctx = session.getServletContext();
85             } else {
86                 //if (Debug.infoOn()) Debug.logInfo("in appendOfbizUrl, session is null(1)", "");
87
}
88             if (ctx == null) {
89                 throw new RuntimeException JavaDoc("ctx is null. buffer=" + buffer.toString() + " location:" + location);
90             }
91                 //if (Debug.infoOn()) Debug.logInfo("in appendOfbizUrl, ctx is NOT null(2)", "");
92
}
93         GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
94         if (delegator == null) {
95                 //if (Debug.infoOn()) Debug.logInfo("in appendOfbizUrl, delegator is null(5)", "");
96
}
97         RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
98         // make and append the link
99
String JavaDoc s = rh.makeLink(this.request, this.response, location);
100             if (s.indexOf("null") >= 0) {
101                 //if (Debug.infoOn()) Debug.logInfo("in appendOfbizUrl(3), url: " + s, "");
102
}
103         buffer.append(s);
104     }
105
106     public void appendContentUrl(StringBuffer JavaDoc buffer, String JavaDoc location) {
107         ServletContext JavaDoc ctx = (ServletContext JavaDoc) this.request.getAttribute("servletContext");
108         if (ctx == null) {
109             //if (Debug.infoOn()) Debug.logInfo("in appendContentUrl, ctx is null(0): buffer=" + buffer.toString() + " location:" + location, "");
110
HttpSession JavaDoc session = request.getSession();
111             if (session != null) {
112                 ctx = session.getServletContext();
113             } else {
114                 //if (Debug.infoOn()) Debug.logInfo("in appendContentUrl, session is null(1)", "");
115
}
116             if (ctx == null) {
117                 throw new RuntimeException JavaDoc("ctx is null. buffer=" + buffer.toString() + " location:" + location);
118             }
119                 //if (Debug.infoOn()) Debug.logInfo("in appendContentUrl, ctx is NOT null(2)", "");
120
this.request.setAttribute("servletContext", ctx);
121         }
122         GenericDelegator delegator = (GenericDelegator)request.getAttribute("delegator");
123         if (delegator == null) {
124                 //if (Debug.infoOn()) Debug.logInfo("in appendContentUrl, delegator is null(6)", "");
125
}
126         ContentUrlTag.appendContentPrefix(this.request, buffer);
127         buffer.append(location);
128     }
129
130     public void appendTooltip(StringBuffer JavaDoc buffer, Map JavaDoc context, ModelMenuItem modelMenuItem) {
131         // render the tooltip, in other methods too
132
String JavaDoc tooltip = modelMenuItem.getTooltip(context);
133         if (UtilValidate.isNotEmpty(tooltip)) {
134             buffer.append("<span");
135             String JavaDoc tooltipStyle = modelMenuItem.getTooltipStyle();
136             if (UtilValidate.isNotEmpty(tooltipStyle)) {
137                 buffer.append(" class=\"");
138                 buffer.append(tooltipStyle);
139                 buffer.append("\"");
140             }
141             buffer.append("> -[");
142             buffer.append(tooltip);
143             buffer.append("]- </span>");
144         }
145     }
146
147     public void renderFormatSimpleWrapperRows(StringBuffer JavaDoc buffer, Map JavaDoc context, Object JavaDoc menuObj) {
148
149         List JavaDoc menuItemList = ((ModelMenu)menuObj).getMenuItemList();
150         Iterator JavaDoc menuItemIter = menuItemList.iterator();
151         ModelMenuItem currentMenuItem = null;
152
153         while (menuItemIter.hasNext()) {
154             currentMenuItem = (ModelMenuItem)menuItemIter.next();
155             renderMenuItem(buffer, context, currentMenuItem);
156         }
157         return;
158     }
159
160     public void renderMenuItem(StringBuffer JavaDoc buffer, Map JavaDoc context, ModelMenuItem menuItem) {
161         
162         //Debug.logInfo("in renderMenuItem, menuItem:" + menuItem.getName() + " context:" + context ,"");
163
boolean hideThisItem = isHideIfSelected(menuItem);
164         //if (Debug.infoOn()) Debug.logInfo("in HtmlMenuRendererImage, hideThisItem:" + hideThisItem,"");
165
if (hideThisItem)
166             return;
167
168
169         String JavaDoc style = menuItem.getAlignStyle();
170         if (UtilValidate.isNotEmpty(style)) {
171             String JavaDoc orientation = menuItem.getModelMenu().getOrientation();
172             if (orientation.equalsIgnoreCase("vertical")) style += "-vert";
173             String JavaDoc align = menuItem.getAlign();
174             if (align.equalsIgnoreCase("right")) style += "-right";
175             
176             buffer.append("<div class=\"" + style + "\">");
177         }
178         
179         
180         Link link = menuItem.getLink();
181         //if (Debug.infoOn()) Debug.logInfo("in HtmlMenuRendererImage, link(0):" + link,"");
182
if (link != null) {
183             renderLink(buffer, context, link);
184         }
185
186         if (UtilValidate.isNotEmpty(style)) {
187             // only render the close tag if we rendered the open
188
buffer.append("</div>");
189         }
190         
191         this.appendWhitespace(buffer);
192         return;
193     }
194
195     public boolean isDisableIfEmpty(ModelMenuItem menuItem, Map JavaDoc context) {
196
197         boolean disabled = false;
198         String JavaDoc disableIfEmpty = menuItem.getDisableIfEmpty();
199         if (UtilValidate.isNotEmpty(disableIfEmpty)) {
200             List JavaDoc keys = StringUtil.split(disableIfEmpty, "|");
201             Iterator JavaDoc iter = keys.iterator();
202             while (iter.hasNext()) {
203                 Object JavaDoc obj = context.get(disableIfEmpty);
204                 if (obj == null) {
205                     disabled = true;
206                     break;
207                 }
208             }
209         }
210         return disabled;
211     }
212
213
214     public String JavaDoc buildDivStr(ModelMenuItem menuItem, Map JavaDoc context) {
215         String JavaDoc divStr = "";
216         divStr = menuItem.getTitle(context);
217         return divStr;
218     }
219
220     public void renderMenuOpen(StringBuffer JavaDoc buffer, Map JavaDoc context, ModelMenu modelMenu) {
221
222         if (!userLoginIdHasChanged) {
223             userLoginIdHasChanged = userLoginIdHasChanged();
224         }
225
226             //Debug.logInfo("in HtmlMenuRenderer, userLoginIdHasChanged:" + userLoginIdHasChanged,"");
227
String JavaDoc menuWidth = modelMenu.getMenuWidth();
228         String JavaDoc menuContainerStyle = modelMenu.getMenuContainerStyle(context);
229         String JavaDoc widthStr = "";
230         if (UtilValidate.isNotEmpty(menuWidth)) {
231             widthStr = " style=\"width:" + menuWidth + ";\"";
232         }
233         buffer.append("<div class=\"" + menuContainerStyle + "\"" + widthStr + ">");
234         
235         this.appendWhitespace(buffer);
236     }
237
238     /* (non-Javadoc)
239      * @see org.ofbiz.widget.menu.MenuStringRenderer#renderMenuClose(java.lang.StringBuffer, java.util.Map, org.ofbiz.widget.menu.ModelMenu)
240      */

241     public void renderMenuClose(StringBuffer JavaDoc buffer, Map JavaDoc context, ModelMenu modelMenu) {
242         
243         String JavaDoc fillStyle = modelMenu.getFillStyle();
244         if (UtilValidate.isNotEmpty(fillStyle)) {
245             buffer.append("<div class=\"" + fillStyle + "\">&nbsp;</div>");
246         }
247         //String menuContainerStyle = modelMenu.getMenuContainerStyle(context);
248
buffer.append("</div>");
249         this.appendWhitespace(buffer);
250         
251         userLoginIdHasChanged = userLoginIdHasChanged();
252         GenericValue userLogin = (GenericValue)request.getSession().getAttribute("userLogin");
253         if (userLogin != null) {
254             String JavaDoc userLoginId = userLogin.getString("userLoginId");
255             //request.getSession().setAttribute("userLoginIdAtPermGrant", userLoginId);
256
setUserLoginIdAtPermGrant(userLoginId);
257             //Debug.logInfo("in HtmlMenuRenderer, userLoginId(Close):" + userLoginId + " userLoginIdAtPermGrant:" + request.getSession().getAttribute("userLoginIdAtPermGrant"),"");
258
} else {
259             request.getSession().setAttribute("userLoginIdAtPermGrant", null);
260         }
261     }
262
263     public void renderFormatSimpleWrapperOpen(StringBuffer JavaDoc buffer, Map JavaDoc context, ModelMenu modelMenu) {
264         //this.appendWhitespace(buffer);
265
}
266
267     public void renderFormatSimpleWrapperClose(StringBuffer JavaDoc buffer, Map JavaDoc context, ModelMenu modelMenu) {
268         //this.appendWhitespace(buffer);
269
}
270
271     public void setRequest(HttpServletRequest JavaDoc request) {
272         this.request = request;
273     }
274
275     public void setResponse(HttpServletResponse JavaDoc response) {
276         this.response = response;
277     }
278
279
280     /**
281      * @param string
282      */

283     public void setUserLoginIdAtPermGrant(String JavaDoc string) {
284             //Debug.logInfo("in HtmlMenuRenderer, userLoginIdAtPermGrant(setUserLoginIdAtPermGrant):" + string,"");
285
this.userLoginIdAtPermGrant = string;
286     }
287
288     /**
289      * @return
290      */

291     public String JavaDoc getUserLoginIdAtPermGrant() {
292         return this.userLoginIdAtPermGrant;
293     }
294
295     public boolean isHideIfSelected( ModelMenuItem menuItem) {
296
297         ModelMenu menu = menuItem.getModelMenu();
298         String JavaDoc currentMenuItemName = menu.getCurrentMenuItemName();
299         String JavaDoc currentItemName = menuItem.getName();
300         Boolean JavaDoc hideIfSelected = menuItem.getHideIfSelected();
301             //Debug.logInfo("in HtmlMenuRenderer, currentMenuItemName:" + currentMenuItemName + " currentItemName:" + currentItemName + " hideIfSelected:" + hideIfSelected,"");
302
if (hideIfSelected != null && hideIfSelected.booleanValue() && currentMenuItemName != null && currentMenuItemName.equals(currentItemName))
303             return true;
304         else
305             return false;
306     }
307
308
309     public boolean userLoginIdHasChanged() {
310
311         boolean hasChanged = false;
312         GenericValue userLogin = (GenericValue)request.getSession().getAttribute("userLogin");
313         userLoginIdAtPermGrant = getUserLoginIdAtPermGrant();
314         //userLoginIdAtPermGrant = (String)request.getSession().getAttribute("userLoginIdAtPermGrant");
315
String JavaDoc userLoginId = null;
316         if (userLogin != null)
317             userLoginId = userLogin.getString("userLoginId");
318             //Debug.logInfo("in HtmlMenuRenderer, userLoginId:" + userLoginId + " userLoginIdAtPermGrant:" + userLoginIdAtPermGrant ,"");
319
if ((userLoginId == null && userLoginIdAtPermGrant != null)
320            || (userLoginId != null && userLoginIdAtPermGrant == null)
321            || ((userLoginId != null && userLoginIdAtPermGrant != null)
322               && !userLoginId.equals(userLoginIdAtPermGrant))) {
323             hasChanged = true;
324         } else {
325             if (userLoginIdAtPermGrant != null)
326                hasChanged = true;
327             else
328                hasChanged = false;
329
330             userLoginIdAtPermGrant = null;
331         }
332         return hasChanged;
333     }
334
335
336     public void setUserLoginIdHasChanged(boolean b) {
337         userLoginIdHasChanged = b;
338     }
339
340
341     public String JavaDoc getTitle(ModelMenuItem menuItem, Map JavaDoc context) {
342
343         String JavaDoc title = null;
344         title = menuItem.getTitle(context);
345         return title;
346     }
347
348     public void renderLink(StringBuffer JavaDoc buffer, Map JavaDoc context, ModelMenuItem.Link link) {
349         // open tag
350
buffer.append("<a");
351         String JavaDoc id = link.getId(context);
352         if (UtilValidate.isNotEmpty(id)) {
353             buffer.append(" id=\"");
354             buffer.append(id);
355             buffer.append("\"");
356         }
357         
358         ModelMenuItem menuItem = link.getLinkMenuItem();
359         boolean isSelected = menuItem.isSelected(context);
360         
361         String JavaDoc style = null;
362         
363         if (isSelected) {
364             style = menuItem.getSelectedStyle();
365         } else {
366             style = link.getStyle(context);
367             if (UtilValidate.isEmpty(style))
368                 style = menuItem.getTitleStyle();
369             if (UtilValidate.isEmpty(style))
370                 style = menuItem.getWidgetStyle();
371         }
372         
373         if (menuItem.getDisabled()) {
374             style = menuItem.getDisabledTitleStyle();
375         }
376         
377         if (UtilValidate.isNotEmpty(style)) {
378             buffer.append(" class=\"");
379             buffer.append(style);
380             buffer.append("\"");
381         }
382         String JavaDoc name = link.getName(context);
383         if (UtilValidate.isNotEmpty(name)) {
384             buffer.append(" name=\"");
385             buffer.append(name);
386             buffer.append("\"");
387         }
388         String JavaDoc targetWindow = link.getTargetWindow(context);
389         if (UtilValidate.isNotEmpty(targetWindow)) {
390             buffer.append(" target=\"");
391             buffer.append(targetWindow);
392             buffer.append("\"");
393         }
394         String JavaDoc target = link.getTarget(context);
395         if (menuItem.getDisabled()) {
396             target = null;
397         }
398         if (UtilValidate.isNotEmpty(target)) {
399             buffer.append(" HREF=\"");
400             String JavaDoc urlMode = link.getUrlMode();
401             String JavaDoc prefix = link.getPrefix(context);
402             boolean fullPath = link.getFullPath();
403             boolean secure = link.getSecure();
404             boolean encode = link.getEncode();
405             HttpServletResponse JavaDoc res = (HttpServletResponse JavaDoc) context.get("response");
406             HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) context.get("request");
407             if (urlMode != null && urlMode.equalsIgnoreCase("intra-app")) {
408                 if (req != null && res != null) {
409                     ServletContext JavaDoc ctx = (ServletContext JavaDoc) req.getAttribute("servletContext");
410                     RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
411                     String JavaDoc urlString = rh.makeLink(req, res, target, fullPath, secure, encode);
412                     buffer.append(urlString);
413                 } else if (prefix != null) {
414                     buffer.append(prefix + target);
415                 } else {
416                     buffer.append(target);
417                 }
418             } else if (urlMode != null && urlMode.equalsIgnoreCase("content")) {
419                 StringBuffer JavaDoc newURL = new StringBuffer JavaDoc();
420                 ContentUrlTag.appendContentPrefix(req, newURL);
421                 newURL.append(target);
422                 buffer.append(newURL.toString());
423             } else {
424                 buffer.append(target);
425             }
426
427             buffer.append("\"");
428         }
429         buffer.append(">");
430         
431         // the text
432
Image img = link.getImage();
433         if (img == null)
434             buffer.append(link.getText(context));
435         else
436             renderImage(buffer, context, img);
437         
438         // close tag
439
buffer.append("</a>");
440     }
441
442     public void renderImage(StringBuffer JavaDoc buffer, Map JavaDoc context, ModelMenuItem.Image image) {
443         // open tag
444
buffer.append("<img ");
445         String JavaDoc id = image.getId(context);
446         if (UtilValidate.isNotEmpty(id)) {
447             buffer.append(" id=\"");
448             buffer.append(id);
449             buffer.append("\"");
450         }
451         String JavaDoc style = image.getStyle(context);
452         if (UtilValidate.isNotEmpty(style)) {
453             buffer.append(" class=\"");
454             buffer.append(style);
455             buffer.append("\"");
456         }
457         String JavaDoc wid = image.getWidth(context);
458         if (UtilValidate.isNotEmpty(wid)) {
459             buffer.append(" width=\"");
460             buffer.append(wid);
461             buffer.append("\"");
462         }
463         String JavaDoc hgt = image.getHeight(context);
464         if (UtilValidate.isNotEmpty(hgt)) {
465             buffer.append(" height=\"");
466             buffer.append(hgt);
467             buffer.append("\"");
468         }
469         String JavaDoc border = image.getBorder(context);
470         if (UtilValidate.isNotEmpty(border)) {
471             buffer.append(" border=\"");
472             buffer.append(border);
473             buffer.append("\"");
474         }
475         String JavaDoc src = image.getSrc(context);
476         if (UtilValidate.isNotEmpty(src)) {
477             buffer.append(" SRC=\"");
478             String JavaDoc urlMode = image.getUrlMode();
479             boolean fullPath = false;
480             boolean secure = false;
481             boolean encode = false;
482             HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) context.get("response");
483             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) context.get("request");
484             if (urlMode != null && urlMode.equalsIgnoreCase("ofbiz")) {
485                 if (request != null && response != null) {
486                     ServletContext JavaDoc ctx = (ServletContext JavaDoc) request.getAttribute("servletContext");
487                     RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
488                     String JavaDoc urlString = rh.makeLink(request, response, src, fullPath, secure, encode);
489                     buffer.append(urlString);
490                 } else {
491                     buffer.append(src);
492                 }
493             } else if (urlMode != null && urlMode.equalsIgnoreCase("content")) {
494                 if (request != null && response != null) {
495                     StringBuffer JavaDoc newURL = new StringBuffer JavaDoc();
496                     ContentUrlTag.appendContentPrefix(request, newURL);
497                     newURL.append(src);
498                     buffer.append(newURL.toString());
499                 }
500             } else {
501                 buffer.append(src);
502             }
503
504             buffer.append("\"");
505         }
506         buffer.append("/>");
507     }
508 }
509
510
Popular Tags