/* (non-Javadoc)
* @see org.ofbiz.widget.form.FormStringRenderer#renderDropDownField(java.io.Writer, java.util.Map, org.ofbiz.widget.form.ModelFormField.DropDownField)
*/
public void renderDropDownField(Appendable writer, Map<String, Object> context, DropDownField dropDownField) throws IOException {
ModelFormField modelFormField = dropDownField.getModelFormField();
ModelForm modelForm = modelFormField.getModelForm();
ModelFormField.AutoComplete autoComplete = dropDownField.getAutoComplete();
boolean ajaxEnabled = autoComplete != null && this.javaScriptEnabled;
List<ModelFormField.OptionValue> allOptionValues = dropDownField.getAllOptionValues(context, WidgetWorker.getDelegator(context));
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
String currentValue = modelFormField.getEntry(context);
// Get the current value's description from the option value. If there
// is a localized version it will be there.
String currentDescription = null;
if (UtilValidate.isNotEmpty(currentValue)) {
for (ModelFormField.OptionValue optionValue : allOptionValues) {
if (encode(optionValue.getKey(), modelFormField, context).equals(currentValue)) {
currentDescription = optionValue.getDescription();
break;
}
}
}
if (ajaxEnabled) {
writer.append("<input type=\"text\"");
} else {
writer.append("<select");
}
appendClassNames(writer, context, modelFormField);
writer.append(" name=\"");
writer.append(modelFormField.getParameterName(context));
String idName = modelFormField.getCurrentContainerId(context);
if (ajaxEnabled) {
writer.append("_description\"");
String textFieldIdName = idName;
if (UtilValidate.isNotEmpty(textFieldIdName)) {
textFieldIdName += "_description";
writer.append(" id=\"");
writer.append(textFieldIdName);
writer.append('"');
}
if (UtilValidate.isNotEmpty(currentValue)) {
writer.append(" value=\"");
String explicitDescription = null;
if (currentDescription != null) {
explicitDescription = currentDescription;
} else {
explicitDescription = dropDownField.getCurrentDescription(context);
}
if (UtilValidate.isEmpty(explicitDescription)) {
explicitDescription = ModelFormField.FieldInfoWithOptions.getDescriptionForOptionKey(currentValue, allOptionValues);
}
explicitDescription = encode(explicitDescription, modelFormField, context);
writer.append(explicitDescription);
writer.append('"');
}
writer.append("/>");
appendWhitespace(writer);
writer.append("<input type=\"hidden\" name=\"");
writer.append(modelFormField.getParameterName(context));
writer.append('"');
if (UtilValidate.isNotEmpty(idName)) {
writer.append(" id=\"");
writer.append(idName);
writer.append('"');
}
if (UtilValidate.isNotEmpty(currentValue)) {
writer.append(" value=\"");
//String explicitDescription = dropDownField.getCurrentDescription(context);
writer.append(currentValue);
writer.append('"');
}
writer.append("/>");
appendWhitespace(writer);
writer.append("<script language=\"JavaScript\" type=\"text/javascript\">");
appendWhitespace(writer);
writer.append("var data = {");
int count = 0;
for (ModelFormField.OptionValue optionValue: allOptionValues) {
count++;
writer.append(optionValue.getKey()).append(": ");
writer.append(" '").append(optionValue.getDescription()).append("'");
if (count != allOptionValues.size()) {
writer.append(", ");
}
}
writer.append("};");
appendWhitespace(writer);
writer.append("ajaxAutoCompleteDropDown('").append(textFieldIdName).append("', '").append(idName).append("', data, {autoSelect: ").append(
autoComplete.getAutoSelect()).append(", frequency: ").append(autoComplete.getFrequency()).append(", minChars: ").append(autoComplete.getMinChars()).append(
", choices: ").append(autoComplete.getChoices()).append(", partialSearch: ").append(autoComplete.getPartialSearch()).append(
", partialChars: ").append(autoComplete.getPartialChars()).append(", ignoreCase: ").append(autoComplete.getIgnoreCase()).append(
", fullSearch: ").append(autoComplete.getFullSearch()).append("});");
appendWhitespace(writer);
writer.append("</script>");
} else {
writer.append('"');
if (UtilValidate.isNotEmpty(idName)) {
writer.append(" id=\"");
writer.append(idName);
writer.append('"');
}
if (dropDownField.isAllowMultiple()) {
writer.append(" multiple=\"multiple\"");
}
int otherFieldSize = dropDownField.getOtherFieldSize();
String otherFieldName = dropDownField.getParameterNameOther(context);
if (otherFieldSize > 0) {
//writer.append(" onchange=\"alert('ONCHANGE, process_choice:' + process_choice)\"");
//writer.append(" onchange='test_js()' ");
writer.append(" onchange=\"process_choice(this,document.");
writer.append(modelForm.getName());
writer.append(".");
writer.append(otherFieldName);
writer.append(")\" ");
}
if (UtilValidate.isNotEmpty(event) && UtilValidate.isNotEmpty(action)) {
writer.append(" ");
writer.append(event);
writer.append("=\"");
writer.append(action);
writer.append('"');
}
writer.append(" size=\"").append(dropDownField.getSize()).append("\">");
// if the current value should go first, stick it in
if (UtilValidate.isNotEmpty(currentValue) && "first-in-list".equals(dropDownField.getCurrent())) {
writer.append("<option");
writer.append(" selected=\"selected\"");
writer.append(" value=\"");
writer.append(currentValue);
writer.append("\">");
String explicitDescription = (currentDescription != null ? currentDescription : dropDownField.getCurrentDescription(context));
if (UtilValidate.isNotEmpty(explicitDescription)) {
writer.append(encode(explicitDescription, modelFormField, context));
} else {
String description = ModelFormField.FieldInfoWithOptions.getDescriptionForOptionKey(currentValue, allOptionValues);
writer.append(encode(description, modelFormField, context));
}
writer.append("</option>");
// add a "separator" option
writer.append("<option value=\"");
writer.append(currentValue);
writer.append("\">---</option>");
}
// if allow empty is true, add an empty option
if (dropDownField.isAllowEmpty()) {
writer.append("<option value=\"\"> </option>");
}
// list out all options according to the option list
for (ModelFormField.OptionValue optionValue: allOptionValues) {
String noCurrentSelectedKey = dropDownField.getNoCurrentSelectedKey(context);
writer.append("<option");
// if current value should be selected in the list, select it
if (UtilValidate.isNotEmpty(currentValue) && currentValue.equals(optionValue.getKey()) && "selected".equals(dropDownField.getCurrent())) {
writer.append(" selected=\"selected\"");
} else if (UtilValidate.isEmpty(currentValue) && noCurrentSelectedKey != null && noCurrentSelectedKey.equals(optionValue.getKey())) {
writer.append(" selected=\"selected\"");
}
writer.append(" value=\"");
writer.append(encode(optionValue.getKey(), modelFormField, context));
writer.append("\">");
writer.append(encode(optionValue.getDescription(), modelFormField, context));
writer.append("</option>");
}
writer.append("</select>");
// Adapted from work by Yucca Korpela
// http://www.cs.tut.fi/~jkorpela/forms/combo.html
if (otherFieldSize > 0) {
String fieldName = modelFormField.getParameterName(context);
Map<String, Object> dataMap = UtilGenerics.checkMap(modelFormField.getMap(context));
if (dataMap == null) {
dataMap = context;
}
Object otherValueObj = dataMap.get(otherFieldName);
String otherValue = (otherValueObj == null) ? "" : otherValueObj.toString();
writer.append("<noscript>");
writer.append("<input type='text' name='");
writer.append(otherFieldName);
writer.append("'/> ");
writer.append("</noscript>");
writer.append("\n<script type='text/javascript' language='JavaScript'><!--");
writer.append("\ndisa = ' disabled';");
writer.append("\nif (other_choice(document.");
writer.append(modelForm.getName());
writer.append(".");
writer.append(fieldName);
writer.append(")) disa = '';");
writer.append("\ndocument.write(\"<input type=");
writer.append("'text' name='");
writer.append(otherFieldName);
writer.append("' value='");
writer.append(otherValue);
writer.append("' size='");
writer.append(Integer.toString(otherFieldSize));
writer.append("' ");
writer.append("\" +disa+ \" onfocus='check_choice(document.");
writer.append(modelForm.getName());
writer.append(".");
writer.append(fieldName);
writer.append(")'/>\");");
writer.append("\nif (disa && document.styleSheets)");
writer.append(" document.");
writer.append(modelForm.getName());
writer.append(".");
writer.append(otherFieldName);
writer.append(".style.visibility = 'hidden';");
writer.append("\n//--></script>");
}