/* (non-Javadoc)
* @see org.ofbiz.widget.form.FormStringRenderer#renderDropDownField(java.lang.StringBuffer, java.util.Map, org.ofbiz.widget.form.ModelFormField.DropDownField)
*/
public void renderDropDownField(StringBuffer buffer, Map context, DropDownField dropDownField) {
ModelFormField modelFormField = dropDownField.getModelFormField();
ModelForm modelForm = modelFormField.getModelForm();
String event = modelFormField.getEvent();
String action = modelFormField.getAction(context);
buffer.append("<select");
appendClassNames(buffer, context, modelFormField);
buffer.append(" name=\"");
buffer.append(modelFormField.getParameterName(context));
buffer.append('"');
String idName = modelFormField.getIdName();
if (UtilValidate.isNotEmpty(idName)) {
buffer.append(" id=\"");
buffer.append(idName);
buffer.append('"');
}
if (dropDownField.isAllowMultiple()) {
buffer.append(" multiple=\"multiple\"");
}
int otherFieldSize = dropDownField.getOtherFieldSize();
String otherFieldName = dropDownField.getParameterNameOther(context);
if (otherFieldSize > 0) {
//buffer.append(" onchange=\"alert('ONCHANGE, process_choice:' + process_choice)\"");
//buffer.append(" onchange='test_js()' ");
buffer.append(" onchange=\"process_choice(this,document.");
buffer.append(modelForm.getName());
buffer.append(".");
buffer.append(otherFieldName);
buffer.append(")\" ");
}
if (UtilValidate.isNotEmpty(event) && UtilValidate.isNotEmpty(action)) {
buffer.append(" ");
buffer.append(event);
buffer.append("=\"");
buffer.append(action);
buffer.append('"');
}
buffer.append(" size=\"").append(dropDownField.getSize()).append("\">");
String currentValue = modelFormField.getEntry(context);
List allOptionValues = dropDownField.getAllOptionValues(context, modelForm.getDelegator());
// if the current value should go first, stick it in
if (UtilValidate.isNotEmpty(currentValue) && "first-in-list".equals(dropDownField.getCurrent())) {
buffer.append("<option");
buffer.append(" selected=\"selected\"");
buffer.append(" value=\"");
buffer.append(currentValue);
buffer.append("\">");
String explicitDescription = dropDownField.getCurrentDescription(context);
if (UtilValidate.isNotEmpty(explicitDescription)) {
buffer.append(explicitDescription);
} else {
buffer.append(ModelFormField.FieldInfoWithOptions.getDescriptionForOptionKey(currentValue, allOptionValues));
}
buffer.append("</option>");
// add a "separator" option
buffer.append("<option value=\"");
buffer.append(currentValue);
buffer.append("\">---</option>");
}
// if allow empty is true, add an empty option
if (dropDownField.isAllowEmpty()) {
buffer.append("<option value=\"\"> </option>");
}
// list out all options according to the option list
Iterator optionValueIter = allOptionValues.iterator();
while (optionValueIter.hasNext()) {
ModelFormField.OptionValue optionValue = (ModelFormField.OptionValue) optionValueIter.next();
String noCurrentSelectedKey = dropDownField.getNoCurrentSelectedKey(context);
buffer.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())) {
buffer.append(" selected=\"selected\"");
} else if (UtilValidate.isEmpty(currentValue) && noCurrentSelectedKey != null && noCurrentSelectedKey.equals(optionValue.getKey())) {
buffer.append(" selected=\"selected\"");
}
buffer.append(" value=\"");
buffer.append(optionValue.getKey());
buffer.append("\">");
buffer.append(optionValue.getDescription());
buffer.append("</option>");
}
buffer.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 dataMap = modelFormField.getMap(context);
if (dataMap == null) {
dataMap = context;
}
Object otherValueObj = dataMap.get(otherFieldName);
String otherValue = (otherValueObj == null) ? "" : otherValueObj.toString();
buffer.append("<noscript>");
buffer.append("<input type='text' name='");
buffer.append(otherFieldName);
buffer.append("'/> ");
buffer.append("</noscript>");
buffer.append("\n<script type='text/javascript' language='JavaScript'><!--");
buffer.append("\ndisa = ' disabled';");
buffer.append("\nif(other_choice(document.");
buffer.append(modelForm.getName());
buffer.append(".");
buffer.append(fieldName);
buffer.append(")) disa = '';");
buffer.append("\ndocument.write(\"<input type=");
buffer.append("'text' name='");
buffer.append(otherFieldName);
buffer.append("' value='");
buffer.append(otherValue);
buffer.append("' size='");
buffer.append(otherFieldSize);
buffer.append("' ");
buffer.append("\" +disa+ \" onfocus='check_choice(document.");
buffer.append(modelForm.getName());
buffer.append(".");
buffer.append(fieldName);
buffer.append(")'/>\");");
buffer.append("\nif(disa && document.styleSheets)");
buffer.append(" document.");
buffer.append(modelForm.getName());
buffer.append(".");
buffer.append(otherFieldName);
buffer.append(".style.visibility = 'hidden';");
buffer.append("\n//--></script>");
}