throw new IllegalStateException("AutoCompleteTextField name"
+ " is not defined. Set the name before calling"
+ " getHeadElements().");
}
Page page = getPage();
if (page == null) {
throw new IllegalStateException("The AutoCompleteTextField, '"
+ fieldName + "', is not attached to the Page. Add"
+ " AutoCompleteTextField to a parent form or container and"
+ " attach the parent to the Page before calling"
+ " getHeadElements().");
}
Context context = getContext();
if (headElements == null) {
headElements = super.getHeadElements();
String versionIndicator = ClickUtils.getResourceVersionIndicator(context);
headElements.add(new CssImport("/click/extras-control.css",
versionIndicator));
headElements.add(new JsImport("/click/control.js", versionIndicator));
headElements.add(new JsImport("/click/prototype/prototype.js",
versionIndicator));
headElements.add(new JsImport("/click/prototype/effects.js",
versionIndicator));
headElements.add(new JsImport("/click/prototype/controls.js",
versionIndicator));
}
// Note: the setup script is recreated and checked if it is contained in
// the headElement. This check cater for when the field is used by another
// Control using the fly-weight pattern eg. FormTable.
String fieldId = getId();
JsScript script = new JsScript();
script.setId(fieldId + "-autocomplete");
if (!headElements.contains(script)) {
// Script must be executed as soon as browser dom is ready
script.setExecuteOnDomReady(true);
String contextPath = context.getRequest().getContextPath();
HtmlStringBuffer buffer = new HtmlStringBuffer(150);
buffer.append("new Ajax.Autocompleter(");
buffer.append("'").append(fieldId).append("'");
buffer.append(",'").append(fieldId).append("-auto-complete-div'");
buffer.append(",'").append(contextPath).append(page.getPath()).append("'");
String id = getId();
// Include the field id as a parameter
buffer.append(",{parameters: '").append(id).append("=1'");