use of org.apache.ofbiz.widget.model.ModelFormField in project ofbiz-framework by apache.
the class MacroFormRenderer method renderLookupField.
public void renderLookupField(Appendable writer, Map<String, Object> context, LookupField lookupField) throws IOException {
ModelFormField modelFormField = lookupField.getModelFormField();
String lookupFieldFormName = lookupField.getFormName(context);
String conditionGroup = modelFormField.getConditionGroup();
String className = "";
String alert = "false";
if (UtilValidate.isNotEmpty(modelFormField.getWidgetStyle())) {
className = modelFormField.getWidgetStyle();
if (modelFormField.shouldBeRed(context)) {
alert = "true";
}
}
// check for required field style on single forms
if ("single".equals(modelFormField.getModelForm().getType()) && modelFormField.getRequiredField()) {
String requiredStyle = modelFormField.getRequiredFieldStyle();
if (UtilValidate.isEmpty(requiredStyle)) {
requiredStyle = "required";
}
if (UtilValidate.isEmpty(className)) {
className = requiredStyle;
} else {
className = requiredStyle + " " + className;
}
}
String name = modelFormField.getParameterName(context);
String value = modelFormField.getEntry(context, lookupField.getDefaultValue(context));
if (value == null) {
value = "";
}
String size = Integer.toString(lookupField.getSize());
Integer maxlength = lookupField.getMaxlength();
String id = modelFormField.getCurrentContainerId(context);
List<ModelForm.UpdateArea> updateAreas = modelFormField.getOnChangeUpdateAreas();
// add default ajax auto completer to all lookup fields
if (UtilValidate.isEmpty(updateAreas) && UtilValidate.isNotEmpty(lookupFieldFormName)) {
String autoCompleterTarget = null;
if (lookupFieldFormName.indexOf('?') == -1) {
autoCompleterTarget = lookupFieldFormName + "?";
} else {
autoCompleterTarget = lookupFieldFormName + "&amp;";
}
autoCompleterTarget = autoCompleterTarget + "ajaxLookup=Y";
updateAreas = new LinkedList<>();
updateAreas.add(new ModelForm.UpdateArea("change", id, autoCompleterTarget));
}
boolean ajaxEnabled = UtilValidate.isNotEmpty(updateAreas) && this.javaScriptEnabled;
String autocomplete = "";
if (!lookupField.getClientAutocompleteField() || !ajaxEnabled) {
autocomplete = "off";
}
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
boolean readonly = lookupField.getReadonly();
// add lookup pop-up button
String descriptionFieldName = lookupField.getDescriptionFieldName();
ModelForm modelForm = modelFormField.getModelForm();
String formName = modelFormField.getParentFormName();
if (UtilValidate.isEmpty(formName)) {
formName = FormRenderer.getCurrentFormName(modelForm, context);
}
StringBuilder targetParameterIter = new StringBuilder();
StringBuilder imgSrc = new StringBuilder();
// FIXME: refactor using the StringUtils methods
List<String> targetParameterList = lookupField.getTargetParameterList();
targetParameterIter.append("[");
for (String targetParameter : targetParameterList) {
if (targetParameterIter.length() > 1) {
targetParameterIter.append(",");
}
targetParameterIter.append("'");
targetParameterIter.append(targetParameter);
targetParameterIter.append("'");
}
targetParameterIter.append("]");
this.appendContentUrl(imgSrc, "/images/fieldlookup.gif");
String ajaxUrl = "";
if (ajaxEnabled) {
ajaxUrl = createAjaxParamsFromUpdateAreas(updateAreas, "", context);
}
String lookupPresentation = lookupField.getLookupPresentation();
if (UtilValidate.isEmpty(lookupPresentation)) {
lookupPresentation = "";
}
String lookupHeight = lookupField.getLookupHeight();
String lookupWidth = lookupField.getLookupWidth();
String lookupPosition = lookupField.getLookupPosition();
String fadeBackground = lookupField.getFadeBackground();
if (UtilValidate.isEmpty(fadeBackground)) {
fadeBackground = "false";
}
Boolean isInitiallyCollapsed = lookupField.getInitiallyCollapsed();
String clearText = "";
Map<String, Object> uiLabelMap = UtilGenerics.checkMap(context.get("uiLabelMap"));
if (uiLabelMap != null) {
clearText = (String) uiLabelMap.get("CommonClear");
} else {
Debug.logWarning("Could not find uiLabelMap in context", module);
}
Boolean showDescription = lookupField.getShowDescription();
if (showDescription == null) {
showDescription = "Y".equals(visualTheme.getModelTheme().getLookupShowDescription());
}
// lastViewName, used by lookup to remember the real last view name
// Try to get it from parameters firstly
String lastViewName = request.getParameter("_LAST_VIEW_NAME_");
if (UtilValidate.isEmpty(lastViewName)) {
// get from session
lastViewName = (String) request.getSession().getAttribute("_LAST_VIEW_NAME_");
}
if (UtilValidate.isEmpty(lastViewName)) {
lastViewName = "";
}
String tabindex = modelFormField.getTabindex();
StringWriter sr = new StringWriter();
sr.append("<@renderLookupField ");
sr.append(" className=\"");
sr.append(className);
sr.append("\" alert=\"");
sr.append(alert);
sr.append("\" name=\"");
sr.append(name);
sr.append("\" value=\"");
sr.append(value);
sr.append("\" size=\"");
sr.append(size);
sr.append("\" maxlength=\"");
sr.append((maxlength != null ? Integer.toString(maxlength) : ""));
sr.append("\" id=\"");
sr.append(id);
sr.append("\" event=\"");
if (event != null) {
sr.append(event);
}
sr.append("\" action=\"");
if (action != null) {
sr.append(action);
}
sr.append("\" readonly=");
sr.append(Boolean.toString(readonly));
sr.append(" autocomplete=\"");
sr.append(autocomplete);
sr.append("\" descriptionFieldName=\"");
sr.append(descriptionFieldName);
sr.append("\" formName=\"");
sr.append(formName);
sr.append("\" fieldFormName=\"");
sr.append(lookupFieldFormName);
sr.append("\" targetParameterIter=");
sr.append(targetParameterIter.toString());
sr.append(" imgSrc=\"");
sr.append(imgSrc.toString());
sr.append("\" ajaxUrl=\"");
sr.append(ajaxUrl);
sr.append("\" ajaxEnabled=");
sr.append(Boolean.toString(ajaxEnabled));
sr.append(" presentation=\"");
sr.append(lookupPresentation);
if (UtilValidate.isNotEmpty(lookupHeight)) {
sr.append("\" height=\"");
sr.append(lookupHeight);
}
if (UtilValidate.isNotEmpty(lookupWidth)) {
sr.append("\" width=\"");
sr.append(lookupWidth);
}
if (UtilValidate.isNotEmpty(lookupPosition)) {
sr.append("\" position=\"");
sr.append(lookupPosition);
}
sr.append("\" fadeBackground=\"");
sr.append(fadeBackground);
sr.append("\" clearText=\"");
sr.append(clearText);
sr.append("\" showDescription=\"");
sr.append(Boolean.toString(showDescription));
sr.append("\" initiallyCollapsed=\"");
sr.append(Boolean.toString(isInitiallyCollapsed));
sr.append("\" lastViewName=\"");
sr.append(lastViewName);
sr.append("\" conditionGroup=\"");
sr.append(conditionGroup);
sr.append("\" tabindex=\"");
sr.append(tabindex);
sr.append("\" delegatorName=\"");
sr.append(((HttpSession) context.get("session")).getAttribute("delegatorName").toString());
sr.append("\" />");
executeMacro(writer, sr.toString());
this.addAsterisks(writer, context, modelFormField);
this.makeHyperlinkString(writer, lookupField.getSubHyperlink(), context);
this.appendTooltip(writer, context, modelFormField);
}
use of org.apache.ofbiz.widget.model.ModelFormField in project ofbiz-framework by apache.
the class MacroFormRenderer method renderImageField.
public void renderImageField(Appendable writer, Map<String, Object> context, ImageField imageField) throws IOException {
ModelFormField modelFormField = imageField.getModelFormField();
String value = modelFormField.getEntry(context, imageField.getValue(context));
String description = imageField.getDescription(context);
String alternate = imageField.getAlternate(context);
String style = imageField.getStyle(context);
if (UtilValidate.isEmpty(description)) {
description = imageField.getModelFormField().getTitle(context);
}
if (UtilValidate.isEmpty(alternate)) {
alternate = description;
}
if (UtilValidate.isNotEmpty(value)) {
if (!value.startsWith("http")) {
StringBuilder buffer = new StringBuilder();
ContentUrlTag.appendContentPrefix(request, buffer);
buffer.append(value);
value = buffer.toString();
}
} else if (value == null) {
value = "";
}
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
StringWriter sr = new StringWriter();
sr.append("<@renderImageField ");
sr.append(" value=\"");
sr.append(value);
sr.append("\" description=\"");
sr.append(encode(description, modelFormField, context));
sr.append("\" alternate=\"");
sr.append(encode(alternate, modelFormField, context));
sr.append("\" style=\"");
sr.append(style);
sr.append("\" event=\"");
sr.append(event == null ? "" : event);
sr.append("\" action=\"");
sr.append(action == null ? "" : action);
sr.append("\" />");
executeMacro(writer, sr.toString());
this.makeHyperlinkString(writer, imageField.getSubHyperlink(), context);
this.appendTooltip(writer, context, modelFormField);
}
use of org.apache.ofbiz.widget.model.ModelFormField in project ofbiz-framework by apache.
the class FormRenderer method renderItemRow.
// The fields in the three lists, usually created in the preprocessing phase
// of the renderItemRows method are rendered: this will create a visual representation
// of one row (corresponding to one position).
private void renderItemRow(Appendable writer, Map<String, Object> localContext, FormStringRenderer formStringRenderer, boolean formPerItem, List<ModelFormField> hiddenIgnoredFieldList, List<ModelFormField> innerDisplayHyperlinkFieldsBegin, List<ModelFormField> innerFormFields, List<ModelFormField> innerDisplayHyperlinkFieldsEnd, List<ModelFormField> mainFieldList, int position, int numOfColumns) throws IOException {
int numOfCells = innerDisplayHyperlinkFieldsBegin.size() + innerDisplayHyperlinkFieldsEnd.size() + (innerFormFields.size() > 0 ? 1 : 0);
int numOfColumnsToSpan = numOfColumns - numOfCells + 1;
if (numOfColumnsToSpan < 1) {
numOfColumnsToSpan = 1;
}
// render row formatting open
formStringRenderer.renderFormatItemRowOpen(writer, localContext, modelForm);
Iterator<ModelFormField> innerDisplayHyperlinkFieldsBeginIter = innerDisplayHyperlinkFieldsBegin.iterator();
Map<String, Integer> fieldCount = new HashMap<>();
while (innerDisplayHyperlinkFieldsBeginIter.hasNext()) {
ModelFormField modelFormField = innerDisplayHyperlinkFieldsBeginIter.next();
if (fieldCount.containsKey(modelFormField.getFieldName())) {
fieldCount.put(modelFormField.getFieldName(), fieldCount.get(modelFormField.getFieldName()) + 1);
} else {
fieldCount.put(modelFormField.getFieldName(), 1);
}
}
if (modelForm.getGroupColumns()) {
// do the first part of display and hyperlink fields
Iterator<ModelFormField> innerDisplayHyperlinkFieldIter = innerDisplayHyperlinkFieldsBegin.iterator();
while (innerDisplayHyperlinkFieldIter.hasNext()) {
boolean cellOpen = false;
ModelFormField modelFormField = innerDisplayHyperlinkFieldIter.next();
if (modelFormField.shouldIgnore(localContext)) {
continue;
}
// span columns only if this is the last column in the row (not just in this first list)
if (fieldCount.get(modelFormField.getName()) < 2) {
if ((innerDisplayHyperlinkFieldIter.hasNext() || numOfCells > innerDisplayHyperlinkFieldsBegin.size())) {
formStringRenderer.renderFormatItemRowCellOpen(writer, localContext, modelForm, modelFormField, 1);
} else {
formStringRenderer.renderFormatItemRowCellOpen(writer, localContext, modelForm, modelFormField, numOfColumnsToSpan);
}
cellOpen = true;
}
if ((!"list".equals(modelForm.getType()) && !"multi".equals(modelForm.getType())) || modelFormField.shouldUse(localContext)) {
if ((fieldCount.get(modelFormField.getName()) > 1)) {
if ((innerDisplayHyperlinkFieldIter.hasNext() || numOfCells > innerDisplayHyperlinkFieldsBegin.size())) {
formStringRenderer.renderFormatItemRowCellOpen(writer, localContext, modelForm, modelFormField, 1);
} else {
formStringRenderer.renderFormatItemRowCellOpen(writer, localContext, modelForm, modelFormField, numOfColumnsToSpan);
}
cellOpen = true;
}
modelFormField.renderFieldString(writer, localContext, formStringRenderer);
}
if (cellOpen) {
formStringRenderer.renderFormatItemRowCellClose(writer, localContext, modelForm, modelFormField);
}
}
// The form cell is rendered only if there is at least an input field
if (innerFormFields.size() > 0) {
// render the "form" cell
// TODO: colspan
formStringRenderer.renderFormatItemRowFormCellOpen(writer, localContext, modelForm);
if (formPerItem) {
formStringRenderer.renderFormOpen(writer, localContext, modelForm);
}
// do all of the hidden fields...
this.renderHiddenIgnoredFields(writer, localContext, formStringRenderer, hiddenIgnoredFieldList);
Iterator<ModelFormField> innerFormFieldIter = innerFormFields.iterator();
while (innerFormFieldIter.hasNext()) {
ModelFormField modelFormField = innerFormFieldIter.next();
if (modelForm.getSeparateColumns() || modelFormField.getSeparateColumn()) {
formStringRenderer.renderFormatItemRowCellOpen(writer, localContext, modelForm, modelFormField, 1);
}
// render field widget
if ((!"list".equals(modelForm.getType()) && !"multi".equals(modelForm.getType())) || modelFormField.shouldUse(localContext)) {
modelFormField.renderFieldString(writer, localContext, formStringRenderer);
}
if (modelForm.getSeparateColumns() || modelFormField.getSeparateColumn()) {
formStringRenderer.renderFormatItemRowCellClose(writer, localContext, modelForm, modelFormField);
}
}
if (formPerItem) {
formStringRenderer.renderFormClose(writer, localContext, modelForm);
}
formStringRenderer.renderFormatItemRowFormCellClose(writer, localContext, modelForm);
}
// render the rest of the display/hyperlink fields
innerDisplayHyperlinkFieldIter = innerDisplayHyperlinkFieldsEnd.iterator();
while (innerDisplayHyperlinkFieldIter.hasNext()) {
ModelFormField modelFormField = innerDisplayHyperlinkFieldIter.next();
// span columns only if this is the last column in the row
if (innerDisplayHyperlinkFieldIter.hasNext()) {
formStringRenderer.renderFormatItemRowCellOpen(writer, localContext, modelForm, modelFormField, 1);
} else {
formStringRenderer.renderFormatItemRowCellOpen(writer, localContext, modelForm, modelFormField, numOfColumnsToSpan);
}
if ((!"list".equals(modelForm.getType()) && !"multi".equals(modelForm.getType())) || modelFormField.shouldUse(localContext)) {
modelFormField.renderFieldString(writer, localContext, formStringRenderer);
}
formStringRenderer.renderFormatItemRowCellClose(writer, localContext, modelForm, modelFormField);
}
} else {
// do all of the hidden fields...
this.renderHiddenIgnoredFields(writer, localContext, formStringRenderer, hiddenIgnoredFieldList);
Iterator<ModelFormField> mainFieldIter = mainFieldList.iterator();
while (mainFieldIter.hasNext()) {
ModelFormField modelFormField = mainFieldIter.next();
// don't do any header for hidden or ignored fields inside this loop
FieldInfo fieldInfo = modelFormField.getFieldInfo();
if (fieldInfo.getFieldType() == FieldInfo.HIDDEN || fieldInfo.getFieldType() == FieldInfo.IGNORED) {
continue;
}
// span columns only if this is the last column in the row
if (mainFieldIter.hasNext()) {
formStringRenderer.renderFormatItemRowCellOpen(writer, localContext, modelForm, modelFormField, 1);
} else {
formStringRenderer.renderFormatItemRowCellOpen(writer, localContext, modelForm, modelFormField, numOfColumnsToSpan);
}
if ((!"list".equals(modelForm.getType()) && !"multi".equals(modelForm.getType())) || modelFormField.shouldUse(localContext)) {
modelFormField.renderFieldString(writer, localContext, formStringRenderer);
}
formStringRenderer.renderFormatItemRowCellClose(writer, localContext, modelForm, modelFormField);
}
}
// render row formatting close
formStringRenderer.renderFormatItemRowClose(writer, localContext, modelForm);
}
use of org.apache.ofbiz.widget.model.ModelFormField in project ofbiz-framework by apache.
the class FormRenderer method getFieldListsByPosition.
private Collection<List<ModelFormField>> getFieldListsByPosition(List<ModelFormField> modelFormFieldList) {
Map<Integer, List<ModelFormField>> fieldsByPosition = new TreeMap<>();
for (ModelFormField modelFormField : modelFormFieldList) {
Integer position = Integer.valueOf(modelFormField.getPosition());
List<ModelFormField> fieldListByPosition = fieldsByPosition.get(position);
if (fieldListByPosition == null) {
fieldListByPosition = new LinkedList<>();
fieldsByPosition.put(position, fieldListByPosition);
}
fieldListByPosition.add(modelFormField);
}
return fieldsByPosition.values();
}
use of org.apache.ofbiz.widget.model.ModelFormField in project ofbiz-framework by apache.
the class FormRenderer method render.
/**
* Renders this form to a writer, as defined with the
* FormStringRenderer implementation.
*
* @param writer The Writer that the form text will be written to
* @param context Map containing the form context; the following are
* reserved words in this context: parameters (Map), isError (Boolean),
* itemIndex (Integer, for lists only, otherwise null), formName
* (String, optional alternate name for form, defaults to the
* value of the name attribute)
*/
public void render(Appendable writer, Map<String, Object> context) throws Exception {
// increment the paginator, only for list and multi forms
if (modelForm instanceof ModelGrid) {
WidgetWorker.incrementPaginatorNumber(context);
}
// Populate the viewSize and viewIndex so they are available for use during form actions
context.put("viewIndex", Paginator.getViewIndex(modelForm, context));
context.put("viewSize", Paginator.getViewSize(modelForm, context));
modelForm.runFormActions(context);
// if this is a list form, don't use Request Parameters
if (modelForm instanceof ModelGrid) {
context.put("useRequestParameters", Boolean.FALSE);
}
// find the highest position number to get the max positions used
int positions = 1;
for (ModelFormField modelFormField : modelForm.getFieldList()) {
int curPos = modelFormField.getPosition();
if (curPos > positions) {
positions = curPos;
}
FieldInfo currentFieldInfo = modelFormField.getFieldInfo();
if (currentFieldInfo == null) {
throw new IllegalArgumentException("Error rendering form, a field has no FieldInfo, ie no sub-element for the type of field for field named: " + modelFormField.getName());
}
}
if ("single".equals(modelForm.getType())) {
this.renderSingleFormString(writer, context, positions);
} else if ("list".equals(modelForm.getType())) {
this.renderListFormString(writer, context, positions);
} else if ("multi".equals(modelForm.getType())) {
this.renderMultiFormString(writer, context, positions);
} else if ("upload".equals(modelForm.getType())) {
this.renderSingleFormString(writer, context, positions);
} else {
if (UtilValidate.isEmpty(modelForm.getType())) {
throw new IllegalArgumentException("The form 'type' tag is missing or empty on the form with the name " + modelForm.getName());
}
throw new IllegalArgumentException("The form type " + modelForm.getType() + " is not supported for form with name " + modelForm.getName());
}
}
Aggregations