Author: adrianc
Date: Fri Jan 27 17:30:42 2012 New Revision: 1236773 URL: http://svn.apache.org/viewvc?rev=1236773&view=rev Log: Screen Widget Redesign - added new menu renderer based on the MenuWidgetVisitor interface. Added: ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWidgetVisitor.java (with props) Modified: ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWrapper.java ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/menu/ModelMenu.java ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java Added: ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWidgetVisitor.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWidgetVisitor.java?rev=1236773&view=auto ============================================================================== --- ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWidgetVisitor.java (added) +++ ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWidgetVisitor.java Fri Jan 27 17:30:42 2012 @@ -0,0 +1,399 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.widget.html; + +import java.io.IOException; +import java.math.BigDecimal; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.ofbiz.base.util.Assert; +import org.ofbiz.base.util.GeneralException; +import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilMisc; +import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.webapp.control.RequestHandler; +import org.ofbiz.webapp.taglib.ContentUrlTag; +import org.ofbiz.widget.ModelWidget; +import org.ofbiz.widget.WidgetWorker; +import org.ofbiz.widget.menu.MenuWidgetVisitor; +import org.ofbiz.widget.menu.ModelMenu; +import org.ofbiz.widget.menu.ModelMenuAction; +import org.ofbiz.widget.menu.ModelMenuItem; +import org.ofbiz.widget.menu.ModelMenuItem.Image; +import org.ofbiz.widget.menu.ModelMenuItem.Link; + +/** + * HTML menu widget renderer. + */ +public final class HtmlMenuWidgetVisitor extends HtmlWidgetRenderer implements MenuWidgetVisitor { + + /** + * Renders a <code>ModelMenu</code> as HTML. + * @param modelMenu + * @param writer + * @param context + * @throws IllegalArgumentException if any arguments are <code>null</code> or if <code>modelMenu</code> + * is an unsupported type + * @throws IOException + * @throws GeneralException + */ + public static void render(ModelMenu modelMenu, Appendable writer, Map<String, Object> context) throws IOException, GeneralException { + Assert.notNull("modelMenu", modelMenu); + if (!"simple".equals(modelMenu.getType())) { + throw new IllegalArgumentException("The type " + modelMenu.getType() + " is not supported for menu with name " + modelMenu.getName()); + } + modelMenu.accept(new HtmlMenuWidgetVisitor(writer, context)); + } + + private final Appendable writer; + private final Map<String, Object> context; + + public HtmlMenuWidgetVisitor(Appendable writer, Map<String, Object> context) { + Assert.notNull("writer", writer, "context", context); + this.writer = writer; + this.context = context; + } + + private boolean isDisableIfEmpty(ModelMenuItem menuItem) { + boolean disabled = false; + String disableIfEmpty = menuItem.getDisableIfEmpty(); + if (UtilValidate.isNotEmpty(disableIfEmpty)) { + List<String> keys = StringUtil.split(disableIfEmpty, "|"); + for (String key : keys) { + Object obj = context.get(key); + if (obj == null) { + disabled = true; + break; + } + } + } + return disabled; + } + + private boolean isHideIfSelected(ModelMenuItem menuItem) { + ModelMenu menu = menuItem.getModelMenu(); + String currentMenuItemName = menu.getSelectedMenuItemContextFieldName(context); + String currentItemName = menuItem.getName(); + Boolean hideIfSelected = menuItem.getHideIfSelected(); + return (hideIfSelected != null && hideIfSelected.booleanValue() && currentMenuItemName != null && currentMenuItemName.equals(currentItemName)); + } + + private void renderMenuClose(Appendable writer, Map<String, Object> context, ModelMenu modelMenu) throws IOException { + // TODO: div can't be directly inside an UL + String fillStyle = modelMenu.getFillStyle(); + if (UtilValidate.isNotEmpty(fillStyle)) { + writer.append("<div class=\"").append(fillStyle).append("\"> </div>"); + } + if (modelMenu.renderedMenuItemCount(context) > 0) { + writer.append(" </ul>"); + appendWhitespace(writer); + writer.append("</li>"); + appendWhitespace(writer); + writer.append("</ul>"); + appendWhitespace(writer); + } + writer.append(" <br class=\"clear\"/>"); + appendWhitespace(writer); + writer.append("</div>"); + appendWhitespace(writer); + if (ModelWidget.widgetBoundaryCommentsEnabled(context)) { + renderEndingBoundaryComment(writer, "Menu Widget", modelMenu); + } + } + + private void renderMenuOpen(Appendable writer, Map<String, Object> context, ModelMenu modelMenu) throws IOException { + if (ModelWidget.widgetBoundaryCommentsEnabled(context)) { + renderBeginningBoundaryComment(writer, "Menu Widget", modelMenu); + } + writer.append("<div"); + String menuId = modelMenu.getId(); + if (UtilValidate.isNotEmpty(menuId)) { + writer.append(" id=\"").append(menuId).append("\""); + } else { + // TODO: Remove else after UI refactor - allow both id and style + String menuContainerStyle = modelMenu.getMenuContainerStyle(context); + if (UtilValidate.isNotEmpty(menuContainerStyle)) { + writer.append(" class=\"").append(menuContainerStyle).append("\""); + } + } + String menuWidth = modelMenu.getMenuWidth(); + // TODO: Eliminate embedded styling after refactor + if (UtilValidate.isNotEmpty(menuWidth)) { + writer.append(" style=\"width:").append(menuWidth).append(";\""); + } + writer.append(">"); + appendWhitespace(writer); + String menuTitle = modelMenu.getTitle(context); + if (UtilValidate.isNotEmpty(menuTitle)) { + writer.append("<h2>").append(menuTitle).append("</h2>"); + appendWhitespace(writer); + } + if (modelMenu.renderedMenuItemCount(context) > 0) { + writer.append("<ul>"); + appendWhitespace(writer); + writer.append("<li>"); + appendWhitespace(writer); + writer.append(" <ul>"); + appendWhitespace(writer); + } + } + + public void visit(Image image) throws IOException, GeneralException { + writer.append("<img "); + String id = image.getId(context); + if (UtilValidate.isNotEmpty(id)) { + writer.append(" id=\""); + writer.append(id); + writer.append("\""); + } + String style = image.getStyle(context); + if (UtilValidate.isNotEmpty(style)) { + writer.append(" class=\""); + writer.append(style); + writer.append("\""); + } + String wid = image.getWidth(context); + if (UtilValidate.isNotEmpty(wid)) { + writer.append(" width=\""); + writer.append(wid); + writer.append("\""); + } + String hgt = image.getHeight(context); + if (UtilValidate.isNotEmpty(hgt)) { + writer.append(" height=\""); + writer.append(hgt); + writer.append("\""); + } + String border = image.getBorder(context); + if (UtilValidate.isNotEmpty(border)) { + writer.append(" border=\""); + writer.append(border); + writer.append("\""); + } + String src = image.getSrc(context); + if (UtilValidate.isNotEmpty(src)) { + writer.append(" src=\""); + String urlMode = image.getUrlMode(); + boolean fullPath = false; + boolean secure = false; + boolean encode = false; + HttpServletResponse response = (HttpServletResponse) context.get("response"); + HttpServletRequest request = (HttpServletRequest) context.get("request"); + if (urlMode != null && urlMode.equalsIgnoreCase("ofbiz")) { + if (request != null && response != null) { + ServletContext ctx = (ServletContext) request.getAttribute("servletContext"); + RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); + String urlString = rh.makeLink(request, response, src, fullPath, secure, encode); + writer.append(urlString); + } else { + writer.append(src); + } + } else if (urlMode != null && urlMode.equalsIgnoreCase("content")) { + if (request != null && response != null) { + StringBuilder newURL = new StringBuilder(); + ContentUrlTag.appendContentPrefix(request, newURL); + newURL.append(src); + writer.append(newURL.toString()); + } + } else { + writer.append(src); + } + writer.append("\""); + } + writer.append("/>"); + } + + public void visit(Link link) throws IOException, GeneralException { + String target = link.getTarget(context); + ModelMenuItem menuItem = link.getLinkMenuItem(); + if (menuItem.getDisabled() || isDisableIfEmpty(menuItem)) { + target = null; + } + if (UtilValidate.isNotEmpty(target)) { + HttpServletResponse response = (HttpServletResponse) context.get("response"); + HttpServletRequest request = (HttpServletRequest) context.get("request"); + String targetWindow = link.getTargetWindow(context); + String uniqueItemName = menuItem.getModelMenu().getName() + "_" + menuItem.getName() + "_LF_" + UtilMisc.<String>addToBigDecimalInMap(context, "menuUniqueItemIndex", BigDecimal.ONE); + String linkType = WidgetWorker.determineAutoLinkType(link.getLinkType(), target, link.getUrlMode(), request); + if ("hidden-form".equals(linkType)) { + writer.append("<form method=\"post\""); + writer.append(" action=\""); + // note that this passes null for the parameterList on purpose so they won't be put into the URL + WidgetWorker.buildHyperlinkUrl(writer, target, link.getUrlMode(), null, link.getPrefix(context), + link.getFullPath(), link.getSecure(), link.getEncode(), request, response, context); + writer.append("\""); + if (UtilValidate.isNotEmpty(targetWindow)) { + writer.append(" target=\""); + writer.append(targetWindow); + writer.append("\""); + } + writer.append(" name=\""); + writer.append(uniqueItemName); + writer.append("\">"); + StringUtil.SimpleEncoder simpleEncoder = (StringUtil.SimpleEncoder) context.get("simpleEncoder"); + for (Map.Entry<String, String> parameter: link.getParameterMap(context).entrySet()) { + writer.append("<input name=\""); + writer.append(parameter.getKey()); + writer.append("\" value=\""); + if (simpleEncoder != null) { + writer.append(simpleEncoder.encode(parameter.getValue())); + } else { + writer.append(parameter.getValue()); + } + writer.append("\" type=\"hidden\"/>"); + } + + writer.append("</form>"); + } + writer.append("<a"); + String id = link.getId(context); + if (UtilValidate.isNotEmpty(id)) { + writer.append(" id=\""); + writer.append(id); + writer.append("\""); + } + String style = link.getStyle(context); + if (UtilValidate.isNotEmpty(style)) { + writer.append(" class=\""); + writer.append(style); + writer.append("\""); + } + String name = link.getName(context); + if (UtilValidate.isNotEmpty(name)) { + writer.append(" name=\""); + writer.append(name); + writer.append("\""); + } + if (UtilValidate.isNotEmpty(targetWindow)) { + writer.append(" target=\""); + writer.append(targetWindow); + writer.append("\""); + } + writer.append(" href=\""); + String confirmationMsg = link.getConfirmation(context); + if ("hidden-form".equals(linkType)) { + if (UtilValidate.isNotEmpty(confirmationMsg)) { + writer.append("javascript:confirmActionFormLink('"); + writer.append(confirmationMsg); + writer.append("', '"); + writer.append(uniqueItemName); + writer.append("')"); + } else { + writer.append("javascript:document."); + writer.append(uniqueItemName); + writer.append(".submit()"); + } + } else { + if (UtilValidate.isNotEmpty(confirmationMsg)) { + writer.append("javascript:confirmActionLink('"); + writer.append(confirmationMsg); + writer.append("', '"); + WidgetWorker.buildHyperlinkUrl(writer, target, link.getUrlMode(), link.getParameterMap(context), link.getPrefix(context), + link.getFullPath(), link.getSecure(), link.getEncode(), request, response, context); + writer.append("')"); + } else { + WidgetWorker.buildHyperlinkUrl(writer, target, link.getUrlMode(), link.getParameterMap(context), link.getPrefix(context), + link.getFullPath(), link.getSecure(), link.getEncode(), request, response, context); + } + } + writer.append("\">"); + } + Image img = link.getImage(); + if (img != null) { + img.accept(this); + writer.append(" " + link.getText(context)); + } else { + writer.append(link.getText(context)); + } + if (UtilValidate.isNotEmpty(target)) { + writer.append("</a>"); + } + } + + public void visit(ModelMenu modelMenu) throws IOException, GeneralException { + ModelMenuAction.runSubActions(modelMenu.getActions(), this.context); + renderMenuOpen(writer, context, modelMenu); + for (ModelMenuItem item : modelMenu.getMenuItemList()) { + item.accept(this); + } + renderMenuClose(writer, context, modelMenu); + } + + public void visit(ModelMenuItem modelMenuItem) throws IOException, GeneralException { + if (isHideIfSelected(modelMenuItem)) { + return; + } + String style = modelMenuItem.getWidgetStyle(); + if (modelMenuItem.isSelected(context)) { + style = modelMenuItem.getSelectedStyle(); + if (UtilValidate.isEmpty(style)) { + style = "selected"; + } + } + if (modelMenuItem.getDisabled() || isDisableIfEmpty(modelMenuItem)) { + style = modelMenuItem.getDisabledTitleStyle(); + } + writer.append(" <li"); + String alignStyle = modelMenuItem.getAlignStyle(); + if (UtilValidate.isNotEmpty(style) || UtilValidate.isNotEmpty(alignStyle)) { + writer.append(" class=\""); + if (UtilValidate.isNotEmpty(style)) { + writer.append(style).append(" "); + } + if (UtilValidate.isNotEmpty(alignStyle)) { + writer.append(alignStyle); + } + writer.append("\""); + } + String toolTip = modelMenuItem.getTooltip(context); + if (UtilValidate.isNotEmpty(toolTip)) { + writer.append(" title=\"").append(toolTip).append("\""); + } + writer.append(">"); + Link link = modelMenuItem.getLink(); + if (link != null) { + link.accept(this); + } else { + String txt = modelMenuItem.getTitle(context); + StringUtil.SimpleEncoder simpleEncoder = (StringUtil.SimpleEncoder) context.get("simpleEncoder"); + if (simpleEncoder != null) { + txt = simpleEncoder.encode(txt); + } + writer.append(txt); + } + if (!modelMenuItem.getMenuItemList().isEmpty()) { + appendWhitespace(writer); + writer.append(" <ul>"); + appendWhitespace(writer); + for (ModelMenuItem childMenuItem : modelMenuItem.getMenuItemList()) { + childMenuItem.accept(this); + } + writer.append(" </ul>"); + appendWhitespace(writer); + } + writer.append("</li>"); + appendWhitespace(writer); + } + +} Propchange: ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWidgetVisitor.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWidgetVisitor.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Rev URL Modified: ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWrapper.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWrapper.java?rev=1236773&r1=1236772&r2=1236773&view=diff ============================================================================== --- ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWrapper.java (original) +++ ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/html/HtmlMenuWrapper.java Fri Jan 27 17:30:42 2012 @@ -31,6 +31,7 @@ import javax.servlet.http.HttpSession; import javax.xml.parsers.ParserConfigurationException; import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilHttp; import org.ofbiz.base.util.UtilValidate; @@ -103,21 +104,13 @@ public class HtmlMenuWrapper { } public String renderMenuString() throws IOException { - HttpServletRequest req = ((HtmlMenuRenderer)renderer).request; - ServletContext ctx = (ServletContext) req.getAttribute("servletContext"); - if (ctx == null) { - if (Debug.infoOn()) Debug.logInfo("in renderMenuString, ctx is null(0)" , ""); - } - Writer writer = new StringWriter(); - modelMenu.renderMenuString(writer, context, renderer); - - HttpServletRequest req2 = ((HtmlMenuRenderer)renderer).request; - ServletContext ctx2 = (ServletContext) req2.getAttribute("servletContext"); - if (ctx2 == null) { - if (Debug.infoOn()) Debug.logInfo("in renderMenuString, ctx is null(2)" , ""); + try { + HtmlMenuWidgetVisitor.render(modelMenu, writer, context); + } catch (GeneralException e) { + throw new IOException(e); } - + modelMenu.renderMenuString(writer, context, renderer); return writer.toString(); } Modified: ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/menu/ModelMenu.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/menu/ModelMenu.java?rev=1236773&r1=1236772&r2=1236773&view=diff ============================================================================== --- ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/menu/ModelMenu.java (original) +++ ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/menu/ModelMenu.java Fri Jan 27 17:30:42 2012 @@ -94,8 +94,7 @@ public class ModelMenu extends ModelWidg protected List<ModelMenuAction> actions; - - // ===== CONSTRUCTORS ===== + // ===== CONSTRUCTORS ===== /** Default Constructor */ public ModelMenu() {} @@ -666,4 +665,7 @@ public class ModelMenu extends ModelWidg return menuItemList; } + public List<ModelMenuAction> getActions() { + return actions; + } } Modified: ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java?rev=1236773&r1=1236772&r2=1236773&view=diff ============================================================================== --- ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java (original) +++ ofbiz/branches/20111115ScreenWidgetRedesign/framework/widget/src/org/ofbiz/widget/screen/ModelScreenWidget.java Fri Jan 27 17:30:42 2012 @@ -22,8 +22,8 @@ import java.io.IOException; import java.util.Collection; import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.ListIterator; +import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -45,16 +45,17 @@ import org.ofbiz.base.util.string.Flexib import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.condition.EntityCondition; import org.ofbiz.widget.ModelWidget; import org.ofbiz.widget.ModelWidgetAction; +import org.ofbiz.widget.PortalPageWorker; import org.ofbiz.widget.WidgetFactory; import org.ofbiz.widget.WidgetWorker; -import org.ofbiz.widget.PortalPageWorker; import org.ofbiz.widget.form.FormFactory; import org.ofbiz.widget.form.FormStringRenderer; import org.ofbiz.widget.form.ModelForm; import org.ofbiz.widget.html.HtmlFormRenderer; -import org.ofbiz.widget.html.HtmlMenuRenderer; +import org.ofbiz.widget.html.HtmlMenuWidgetVisitor; import org.ofbiz.widget.menu.MenuFactory; import org.ofbiz.widget.menu.MenuStringRenderer; import org.ofbiz.widget.menu.ModelMenu; @@ -63,7 +64,6 @@ import org.ofbiz.widget.tree.TreeFactory import org.ofbiz.widget.tree.TreeStringRenderer; import org.w3c.dom.Element; import org.xml.sax.SAXException; -import org.ofbiz.entity.condition.*; /** @@ -1312,23 +1312,19 @@ public abstract class ModelScreenWidget @Override public void renderWidgetString(Appendable writer, Map<String, Object> context, ScreenStringRenderer screenStringRenderer) throws IOException { + ModelMenu modelMenu = getModelMenu(context); // try finding the menuStringRenderer by name in the context in case one was prepared and put there MenuStringRenderer menuStringRenderer = (MenuStringRenderer) context.get("menuStringRenderer"); // if there was no menuStringRenderer put in place, now try finding the request/response in the context and creating a new one if (menuStringRenderer == null) { - HttpServletRequest request = (HttpServletRequest) context.get("request"); - HttpServletResponse response = (HttpServletResponse) context.get("response"); - if (request != null && response != null) { - menuStringRenderer = new HtmlMenuRenderer(request, response); + try { + modelMenu.accept(new HtmlMenuWidgetVisitor(writer, context)); + } catch (GeneralException e) { + throw new IOException(e); } + return; } - // still null, throw an error - if (menuStringRenderer == null) { - throw new IllegalArgumentException("Could not find a menuStringRenderer in the context, and could not find HTTP request/response objects need to create one."); - } - - ModelMenu modelMenu = getModelMenu(context); - modelMenu.renderMenuString(writer, context, menuStringRenderer); + throw new IllegalArgumentException("Could not find a menuStringRenderer in the context, and could not find HTTP request/response objects need to create one."); } public ModelMenu getModelMenu(Map<String, Object> context) { |
Free forum by Nabble | Edit this page |