IModel<List<? extends String>> choicesModel = new LoadableDetachableModel<List<? extends String>>() {
@Override
protected List<? extends String> load() {
List<String> result = new ArrayList<String>();
AbstractContainer node = (AbstractContainer) VariablesPanel.this.getModelObject();
result.addAll(node.getVariableKeys());
return result;
}
};
final TextField<String> tf;
add(tf = new TextField<String>("key", new PropertyModel<String>(this, "key")));
tf.setRequired(true);
tf.setOutputMarkupId(true);
final DropDownChoice<String> keySuggestions;
add(keySuggestions = new DropDownChoice<String>("keySuggestions", new Model<String>(), choicesModel) {
@Override
public boolean isVisible() {
return VariablesPanel.this.getModelObject() instanceof GlobalContainerNode == false;
}
});
keySuggestions.setNullValid(true);
keySuggestions.add(new AjaxFormComponentUpdatingBehavior("onchange") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
tf.setModelObject(keySuggestions.getModelObject());
keySuggestions.setModelObject(null);
target.addComponent(tf);
target.addComponent(keySuggestions);
target.focusComponent(tf);
}
});
add(new TextField<String>("value", new PropertyModel<String>(this, "value")).setRequired(true));
add(new AjaxButton("submit") {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
AbstractContainer node = (AbstractContainer) VariablesPanel.this.getModelObject();
node.setVariableValue(key, value);
node.save();
onItemAdded();
key = null;
value = null;
target.addComponent(form);
target.addChildren(findParent(VariablesPanel.class), FeedbackPanel.class);
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
target.addChildren(findParent(VariablesPanel.class), FeedbackPanel.class);
}
});
tf.add(new IValidator<String>() {
public void validate(IValidatable validatable) {
String key = (String) validatable.getValue();
AbstractContainer node = (AbstractContainer) VariablesPanel.this.getModelObject();
if (key.contains("/") || key.contains(":")) {
report(validatable, "keyValidator.invalidKey", key);
} else if (node.getVariableValue(key, false) != null) {
report(validatable, "keyValidator.duplicateKey", key);
}
}
private void report(IValidatable validatable, String messageKey, String key) {