* @param attributes Extra attributes for the HTML tag.
*/
public void writeObjectSelect(ObjectField field, Object value, Object... attributes) throws IOException {
ErrorUtils.errorIfNull(field, "field");
ToolUi ui = field.as(ToolUi.class);
String placeholder = ObjectUtils.firstNonNull(ui.getPlaceholder(), "");
if (field.isRequired()) {
placeholder += " (Required)";
}
if (isObjectSelectDropDown(field)) {
Search dropDownSearch = new Search(field);
dropDownSearch.setParentId(param(UUID.class, OBJECT_ID_PARAMETER));
dropDownSearch.setParentTypeId(param(UUID.class, TYPE_ID_PARAMETER));
List<?> items;
if (field.getTypes().contains(ObjectType.getInstance(ObjectType.class))) {
List<ObjectType> types = new ArrayList<ObjectType>();
Predicate predicate = dropDownSearch.toQuery(getSite()).getPredicate();
for (ObjectType t : Database.Static.getDefault().getEnvironment().getTypes()) {
if (t.is(predicate)) {
types.add(t);
}
}
items = new ArrayList<Object>(types);
} else {
items = dropDownSearch.toQuery(getSite()).selectAll();
}
Collections.sort(items, new ObjectFieldComparator("_label", false));
writeStart("select",
"data-searchable", "true",
attributes);
writeStart("option", "value", "");
writeHtml(placeholder);
writeEnd();
for (Object item : items) {
State itemState = State.getInstance(item);
writeStart("option",
"selected", item.equals(value) ? "selected" : null,
"value", itemState.getId());
writeObjectLabel(item);
writeEnd();
}
writeEnd();
} else {
State state = State.getInstance(value);
StringBuilder typeIds = new StringBuilder();
for (ObjectType type : field.getTypes()) {
typeIds.append(type.getId());
typeIds.append(',');
}
if (typeIds.length() > 0) {
typeIds.setLength(typeIds.length() - 1);
}
writeElement("input",
"type", "text",
"class", "objectId",
"data-additional-query", field.getPredicate(),
"data-generic-argument-index", field.getGenericArgumentIndex(),
"data-dynamic-placeholder", ui.getPlaceholderDynamicText(),
"data-label", value != null ? getObjectLabel(value) : null,
"data-pathed", ToolUi.isOnlyPathed(field),
"data-preview", getPreviewThumbnailUrl(value),
"data-searcher-path", ui.getInputSearcherPath(),
"data-suggestions", ui.isEffectivelySuggestions(),
"data-typeIds", typeIds,
"data-visibility", value != null ? state.getVisibilityLabel() : null,
"value", value != null ? state.getId() : null,
"placeholder", placeholder,
attributes);