📄 htmlformrenderer.java
字号:
/*
* $Id: HtmlFormRenderer.java,v 1.6 2003/12/05 20:42:51 byersa Exp $
*
* Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ofbiz.content.widget.html;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.base.util.UtilProperties;
import org.ofbiz.content.webapp.control.RequestHandler;
import org.ofbiz.content.webapp.taglib.ContentUrlTag;
import org.ofbiz.content.widget.form.FormStringRenderer;
import org.ofbiz.content.widget.form.ModelForm;
import org.ofbiz.content.widget.form.ModelFormField;
import org.ofbiz.content.widget.form.ModelFormField.CheckField;
import org.ofbiz.content.widget.form.ModelFormField.DateFindField;
import org.ofbiz.content.widget.form.ModelFormField.DateTimeField;
import org.ofbiz.content.widget.form.ModelFormField.DisplayField;
import org.ofbiz.content.widget.form.ModelFormField.DropDownField;
import org.ofbiz.content.widget.form.ModelFormField.HiddenField;
import org.ofbiz.content.widget.form.ModelFormField.HyperlinkField;
import org.ofbiz.content.widget.form.ModelFormField.IgnoredField;
import org.ofbiz.content.widget.form.ModelFormField.LookupField;
import org.ofbiz.content.widget.form.ModelFormField.FileField;
import org.ofbiz.content.widget.form.ModelFormField.RadioField;
import org.ofbiz.content.widget.form.ModelFormField.RangeFindField;
import org.ofbiz.content.widget.form.ModelFormField.ResetField;
import org.ofbiz.content.widget.form.ModelFormField.SubmitField;
import org.ofbiz.content.widget.form.ModelFormField.TextField;
import org.ofbiz.content.widget.form.ModelFormField.TextFindField;
import org.ofbiz.content.widget.form.ModelFormField.TextareaField;
/**
* Widget Library - HTML Form Renderer implementation
*
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
* @version $Revision: 1.6 $
* @since 2.2
*/
public class HtmlFormRenderer implements FormStringRenderer {
HttpServletRequest request;
HttpServletResponse response;
protected HtmlFormRenderer() {}
public HtmlFormRenderer(HttpServletRequest request, HttpServletResponse response) {
this.request = request;
this.response = response;
}
public void appendWhitespace(StringBuffer buffer) {
// appending line ends for now, but this could be replaced with a simple space or something
buffer.append("\r\n");
//buffer.append(' ');
}
public void appendOfbizUrl(StringBuffer buffer, String location) {
ServletContext ctx = (ServletContext) this.request.getAttribute("servletContext");
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
// make and append the link
buffer.append(rh.makeLink(this.request, this.response, location));
}
public void appendContentUrl(StringBuffer buffer, String location) {
ContentUrlTag.appendContentPrefix(this.request, buffer);
buffer.append(location);
}
public void appendTooltip(StringBuffer buffer, Map context, ModelFormField modelFormField) {
// render the tooltip, in other methods too
String tooltip = modelFormField.getTooltip(context);
if (UtilValidate.isNotEmpty(tooltip)) {
buffer.append("<span");
String tooltipStyle = modelFormField.getTooltipStyle();
if (UtilValidate.isNotEmpty(tooltipStyle)) {
buffer.append(" class=\"");
buffer.append(tooltipStyle);
buffer.append("\"");
}
buffer.append("> -[");
buffer.append(tooltip);
buffer.append("]- </span>");
}
}
/* (non-Javadoc)
* @see org.ofbiz.content.widget.form.FormStringRenderer#renderDisplayField(java.lang.StringBuffer, java.util.Map, org.ofbiz.content.widget.form.ModelFormField.DisplayField)
*/
public void renderDisplayField(StringBuffer buffer, Map context, DisplayField displayField) {
ModelFormField modelFormField = displayField.getModelFormField();
buffer.append("<span");
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
buffer.append(" class=\"");
buffer.append(modelFormField.getWidgetStyle());
buffer.append("\"");
}
// add a style of red if this is a date/time field and redWhen is true
if (modelFormField.shouldBeRed(context)) {
buffer.append(" style=\"color: red;\"");
}
buffer.append(">");
buffer.append(displayField.getDescription(context));
buffer.append("</span>");
this.appendTooltip(buffer, context, modelFormField);
this.appendWhitespace(buffer);
}
/* (non-Javadoc)
* @see org.ofbiz.content.widget.form.FormStringRenderer#renderHyperlinkField(java.lang.StringBuffer, java.util.Map, org.ofbiz.content.widget.form.ModelFormField.HyperlinkField)
*/
public void renderHyperlinkField(StringBuffer buffer, Map context, HyperlinkField hyperlinkField) {
ModelFormField modelFormField = hyperlinkField.getModelFormField();
this.makeHyperlinkString(
buffer,
modelFormField.getWidgetStyle(),
hyperlinkField.getTargetType(),
hyperlinkField.getTarget(context),
hyperlinkField.getDescription(context));
this.appendTooltip(buffer, context, modelFormField);
this.appendWhitespace(buffer);
}
public void makeHyperlinkString(StringBuffer buffer, ModelFormField.SubHyperlink subHyperlink, Map context) {
if (subHyperlink == null) {
return;
}
if (subHyperlink.shouldUse(context)) {
buffer.append(' ');
this.makeHyperlinkString(
buffer,
subHyperlink.getLinkStyle(),
subHyperlink.getTargetType(),
subHyperlink.getTarget(context),
subHyperlink.getDescription(context));
}
}
public void makeHyperlinkString(StringBuffer buffer, String linkStyle, String targetType, String target, String description) {
buffer.append("<a");
if (UtilValidate.isNotEmpty(linkStyle)) {
buffer.append(" class=\"");
buffer.append(linkStyle);
buffer.append("\"");
}
buffer.append(" href=\"");
if ("intra-app".equals(targetType)) {
this.appendOfbizUrl(buffer, "/" + target);
} else if ("inter-app".equals(targetType)) {
String fullTarget = target;
buffer.append(fullTarget);
String externalLoginKey = (String) this.request.getAttribute("externalLoginKey");
if (UtilValidate.isNotEmpty(externalLoginKey)) {
if (fullTarget.indexOf('?') == -1) {
buffer.append('?');
} else {
buffer.append('&');
}
buffer.append("externalLoginKey=");
buffer.append(externalLoginKey);
}
} else if ("content".equals(targetType)) {
this.appendContentUrl(buffer, target);
} else if ("plain".equals(targetType)) {
buffer.append(target);
} else {
buffer.append(target);
}
buffer.append("\"");
buffer.append('>');
buffer.append(description);
buffer.append("</a>");
}
/* (non-Javadoc)
* @see org.ofbiz.content.widget.form.FormStringRenderer#renderTextField(java.lang.StringBuffer, java.util.Map, org.ofbiz.content.widget.form.ModelFormField.TextField)
*/
public void renderTextField(StringBuffer buffer, Map context, TextField textField) {
ModelFormField modelFormField = textField.getModelFormField();
buffer.append("<input type=\"text\"");
String className = modelFormField.getWidgetStyle();
if (UtilValidate.isNotEmpty(className)) {
buffer.append(" class=\"");
buffer.append(className);
buffer.append('"');
}
// add a style of red if this is a date/time field and redWhen is true
if (modelFormField.shouldBeRed(context)) {
buffer.append(" style=\"color: red;\"");
}
buffer.append(" name=\"");
buffer.append(modelFormField.getParameterName(context));
buffer.append('"');
String value = modelFormField.getEntry(context, textField.getDefaultValue(context));
if (UtilValidate.isNotEmpty(value)) {
buffer.append(" value=\"");
buffer.append(value);
buffer.append('"');
}
buffer.append(" size=\"");
buffer.append(textField.getSize());
buffer.append('"');
Integer maxlength = textField.getMaxlength();
if (maxlength != null) {
buffer.append(" maxlength=\"");
buffer.append(maxlength.intValue());
buffer.append('"');
}
String idName = modelFormField.getIdName();
if (UtilValidate.isNotEmpty(idName)) {
buffer.append(" id=\"");
buffer.append(idName);
buffer.append('"');
}
buffer.append("/>");
this.makeHyperlinkString(buffer, textField.getSubHyperlink(), context);
this.appendTooltip(buffer, context, modelFormField);
this.appendWhitespace(buffer);
}
/* (non-Javadoc)
* @see org.ofbiz.content.widget.form.FormStringRenderer#renderTextareaField(java.lang.StringBuffer, java.util.Map, org.ofbiz.content.widget.form.ModelFormField.TextareaField)
*/
public void renderTextareaField(StringBuffer buffer, Map context, TextareaField textareaField) {
ModelFormField modelFormField = textareaField.getModelFormField();
buffer.append("<textarea class=\"textAreaBox\"");
String className = modelFormField.getWidgetStyle();
if (UtilValidate.isNotEmpty(className)) {
buffer.append(" class=\"");
buffer.append(className);
buffer.append('"');
}
buffer.append(" name=\"");
buffer.append(modelFormField.getParameterName(context));
buffer.append('"');
buffer.append(" cols=\"");
buffer.append(textareaField.getCols());
buffer.append('"');
buffer.append(" rows=\"");
buffer.append(textareaField.getRows());
buffer.append('"');
String idName = modelFormField.getIdName();
if (UtilValidate.isNotEmpty(idName)) {
buffer.append(" id=\"");
buffer.append(idName);
buffer.append('"');
}
buffer.append('>');
String value = modelFormField.getEntry(context, textareaField.getDefaultValue(context));
if (UtilValidate.isNotEmpty(value)) {
buffer.append(value);
}
buffer.append("</textarea>");
this.appendTooltip(buffer, context, modelFormField);
this.appendWhitespace(buffer);
}
/* (non-Javadoc)
* @see org.ofbiz.content.widget.form.FormStringRenderer#renderDateTimeField(java.lang.StringBuffer, java.util.Map, org.ofbiz.content.widget.form.ModelFormField.DateTimeField)
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -