/* (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, modelForm.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.getIdName();
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 = (currentDescription != null ? currentDescription : dropDownField.getCurrentDescription(context));
if (UtilValidate.isNotEmpty(explicitDescription)) {
writer.append(encode(explicitDescription, modelFormField, context));
} else {
writer.append(ModelFormField.FieldInfoWithOptions.getDescriptionForOptionKey(currentValue, allOptionValues));
}
writer.append('"');
}
writer.append("/>");
appendWhitespace(writer);
writer.append("<input type=\"hidden\"");
writer.append(" 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() + ": ");
writer.append(" '" + optionValue.getDescription() + "'");
if (count != allOptionValues.size()) {
writer.append(", ");
}
}
writer.append("};");
appendWhitespace(writer);
writer.append("ajaxAutoCompleteDropDown('" + textFieldIdName + "', '" + idName + "', data, {autoSelect: " +
autoComplete.getAutoSelect() + ", frequency: " + autoComplete.getFrequency() + ", minChars: " + autoComplete.getMinChars() +
", choices: " + autoComplete.getChoices() + ", partialSearch: " + autoComplete.getPartialSearch() +
", partialChars: " + autoComplete.getPartialChars() + ", ignoreCase: " + autoComplete.getIgnoreCase() +
", fullSearch: " + autoComplete.getFullSearch() + "});");
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=\"" + dropDownField.getSize() + "\">");
// 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 {
writer.append(ModelFormField.FieldInfoWithOptions.getDescriptionForOptionKey(currentValue, allOptionValues));
}
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
Iterator<ModelFormField.OptionValue> optionValueIter = allOptionValues.iterator();
while (optionValueIter.hasNext()) {
ModelFormField.OptionValue optionValue = (ModelFormField.OptionValue) optionValueIter.next();
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(encode(optionValue.getKey(), modelFormField, context)) && "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();