use of org.jboss.hal.ballroom.form.SwitchItem in project console by hal.
the class ResetServerDialog method reset.
void reset(String messagingServer) {
LabelBuilder labelBuilder = new LabelBuilder();
String l1 = labelBuilder.label(RESET_ALL_MESSAGE_COUNTERS);
Property p1 = metadata.getDescription().findOperation(RESET_ALL_MESSAGE_COUNTERS);
if (p1 != null && p1.getValue().hasDefined(DESCRIPTION)) {
l1 = p1.getValue().get(DESCRIPTION).asString();
l1 = Strings.sanitize(l1);
}
String l2 = labelBuilder.label(RESET_ALL_MESSAGE_COUNTER_HISTORIES);
Property p2 = metadata.getDescription().findOperation(RESET_ALL_MESSAGE_COUNTER_HISTORIES);
if (p2 != null && p2.getValue().hasDefined(DESCRIPTION)) {
l2 = p2.getValue().get(DESCRIPTION).asString();
l2 = Strings.sanitize(l2);
}
Form<ModelNode> form = new ModelNodeForm.Builder<>(Ids.RESET_MESSAGE_COUNTERS, Metadata.empty()).unboundFormItem(new SwitchItem(RESET_ALL_MESSAGE_COUNTERS, l1)).unboundFormItem(new SwitchItem(RESET_ALL_MESSAGE_COUNTER_HISTORIES, l2)).onSave((f, changedValues) -> column.resetServer(messagingServer, !f.getFormItem(RESET_ALL_MESSAGE_COUNTERS).isEmpty(), !f.getFormItem(RESET_ALL_MESSAGE_COUNTER_HISTORIES).isEmpty())).build();
form.addFormValidation(new RequireAtLeastOneAttributeValidation<>(asList(RESET_ALL_MESSAGE_COUNTERS, RESET_ALL_MESSAGE_COUNTER_HISTORIES), resources));
// Make the long labels more readable
stream(form.element().querySelectorAll("." + halFormLabel + ", ." + halFormInput)).filter(htmlElements()).map(asHtmlElement()).forEach(element -> element.style.width = WidthUnionType.of("50%"));
Dialog dialog = new Dialog.Builder(resources.constants().reset()).add(form.element()).primary(resources.constants().reset(), form::save).size(Size.MEDIUM).closeIcon(true).closeOnEsc(true).cancel().build();
dialog.registerAttachable(form);
form.edit(new ModelNode());
dialog.show();
}
use of org.jboss.hal.ballroom.form.SwitchItem in project console by hal.
the class RoleColumn method editScopedRole.
private void editScopedRole(Role role, String type, AddressTemplate template, AddressTemplate typeaheadTemplate, String formId, String scopeAttribute) {
Metadata metadata = metadataRegistry.lookup(template);
Form<ModelNode> form = new ModelNodeForm.Builder<>(formId, metadata).include(BASE_ROLE, scopeAttribute).customFormItem(BASE_ROLE, attributeDescription -> {
SingleSelectBoxItem item = new SingleSelectBoxItem(BASE_ROLE, new LabelBuilder().label(BASE_ROLE), standardRoleNames, false);
item.setRequired(true);
return item;
}).unboundFormItem(new SwitchItem(INCLUDE_ALL, new LabelBuilder().label(INCLUDE_ALL)), 2, resources.messages().includeAllHelpText()).build();
form.getFormItem(scopeAttribute).setRequired(true);
form.getFormItem(scopeAttribute).registerSuggestHandler(new ReadChildrenAutoComplete(dispatcher, statementContext, typeaheadTemplate));
form.getFormItem(INCLUDE_ALL).setValue(role.isIncludeAll());
form.attach();
ModelNode modelNode = new ModelNode();
modelNode.get(BASE_ROLE).set(role.getBaseRole().getName());
role.getScope().forEach(scope -> modelNode.get(scopeAttribute).add(scope));
new ModifyResourceDialog(resources.messages().modifyResourceTitle(type), form, (frm, changedValues) -> {
boolean includeAll = frm.<Boolean>getFormItem(INCLUDE_ALL).getValue();
boolean includeAllChanged = includeAll != role.isIncludeAll();
List<Task<FlowContext>> tasks = new ArrayList<>();
if (!changedValues.isEmpty()) {
tasks.add(new ModifyScopedRole(dispatcher, role, changedValues, metadata));
}
if (includeAllChanged) {
tasks.add(new ModifyIncludeAll(dispatcher, role, includeAll));
}
series(new FlowContext(progress.get()), tasks).subscribe(new SuccessfulOutcome<FlowContext>(eventBus, resources) {
@Override
public void onSuccess(FlowContext context) {
MessageEvent.fire(eventBus, Message.success(resources.messages().modifyResourceSuccess(type, role.getName())));
accessControl.reload(() -> {
refresh(role.getId());
eventBus.fireEvent(new RolesChangedEvent());
});
}
});
}).show(modelNode);
}
use of org.jboss.hal.ballroom.form.SwitchItem in project console by hal.
the class StandaloneServerPresenter method disableSslForManagementInterface.
@Override
@SuppressWarnings("DuplicatedCode")
public void disableSslForManagementInterface() {
Constants constants = resources.constants();
String serverName = environment.isStandalone() ? Names.STANDALONE_SERVER : Names.DOMAIN_CONTROLLER;
String label = constants.reload() + " " + serverName;
SwitchItem reload = new SwitchItem(RELOAD, label);
reload.setExpressionAllowed(false);
Form<ModelNode> form = new ModelNodeForm.Builder<>(Ids.build(RELOAD, FORM), Metadata.empty()).unboundFormItem(reload).build();
form.attach();
HTMLElement formElement = form.element();
ModelNode model = new ModelNode();
model.setEmptyObject();
form.edit(model);
ResourceAddress httpAddress = HTTP_INTERFACE_TEMPLATE.resolve(statementContext);
DialogFactory.buildConfirmation(constants.disableSSL(), resources.messages().disableSSLManagementQuestion(serverName), formElement, Dialog.Size.MEDIUM, () -> {
List<Task<FlowContext>> tasks = new ArrayList<>();
// load the http-interface resource to get the port, there are differente attributes for
// standalone and domain mode.
Task<FlowContext> loadHttpInterface = flowContext -> {
Operation readHttpInterface = new Operation.Builder(httpAddress, READ_RESOURCE_OPERATION).build();
return dispatcher.execute(readHttpInterface).doOnSuccess(value -> {
if (value.hasDefined(SOCKET_BINDING)) {
// standalone mode uses a socket-binding for port
// store the socket-binding name in the flow context and on a later call
// read the socket-binding-group=<s-b-g>/socket-binding=<http-binding> to
// retrieve the port number
flowContext.set(SOCKET_BINDING, value.get(SOCKET_BINDING).asString());
}
}).toCompletable();
};
tasks.add(loadHttpInterface);
// if standalone mode, read the socket-binding-group=<s-b-g>/socket-binding=<http-binding>
// to retrieve the port number
Task<FlowContext> readHttpPortTask = flowContext -> {
Operation op = new Operation.Builder(ResourceAddress.root(), READ_CHILDREN_NAMES_OPERATION).param(CHILD_TYPE, SOCKET_BINDING_GROUP).build();
return dispatcher.execute(op).doOnSuccess(result -> {
String sbg = result.asList().get(0).asString();
String httpBinding = flowContext.get(SOCKET_BINDING);
ResourceAddress address = SOCKET_BINDING_GROUP_TEMPLATE.resolve(statementContext, sbg, httpBinding);
Operation readPort = new Operation.Builder(address, READ_ATTRIBUTE_OPERATION).param(NAME, PORT).param(RESOLVE_EXPRESSIONS, true).build();
dispatcher.execute(readPort, portResult -> flowContext.set(PORT, portResult.asString()));
}).toCompletable();
};
tasks.add(readHttpPortTask);
// as part of the disable ssl task, undefine the secure-socket-binding
// the attribute only exists in standalone mode
Task<FlowContext> undefSslTask = flowContext -> {
Operation op = new Operation.Builder(httpAddress, UNDEFINE_ATTRIBUTE_OPERATION).param(NAME, SECURE_SOCKET_BINDING).build();
return dispatcher.execute(op).toCompletable();
};
tasks.add(undefSslTask);
// as part of the disable ssl task, undefine the ssl-context
Task<FlowContext> undefineSslContextTask = flowContext -> {
Operation op = new Operation.Builder(httpAddress, UNDEFINE_ATTRIBUTE_OPERATION).param(NAME, SSL_CONTEXT).build();
return dispatcher.execute(op).toCompletable();
};
tasks.add(undefineSslContextTask);
series(new FlowContext(progress.get()), tasks).subscribe(new SuccessfulOutcome<FlowContext>(getEventBus(), resources) {
@Override
public void onSuccess(FlowContext flowContext) {
if (reload.getValue() != null && reload.getValue()) {
String port = flowContext.get(PORT).toString();
// extracts the url search path, so the reload shows the same view the use is on
String urlSuffix = window.location.getHref();
urlSuffix = urlSuffix.substring(urlSuffix.indexOf("//") + 2);
urlSuffix = urlSuffix.substring(urlSuffix.indexOf("/"));
// the location to redirect the browser to the unsecure URL
// TODO Replace hardcoded scheme
String location = "http://" + window.location.getHostname() + ":" + port + urlSuffix;
reloadServer(null, location);
} else {
reload();
MessageEvent.fire(getEventBus(), Message.success(resources.messages().disableSSLManagementSuccess()));
}
}
@Override
public void onError(FlowContext context, Throwable throwable) {
MessageEvent.fire(getEventBus(), Message.error(resources.messages().disableSSLManagementError(throwable.getMessage())));
}
});
}).show();
}
use of org.jboss.hal.ballroom.form.SwitchItem in project console by hal.
the class ExactlyOneAlternativeValidation method validate.
@Override
public ValidationResult validate(final Form<T> form) {
LabelBuilder labelBuilder = new LabelBuilder();
Set<String> emptyItems = requiredAlternatives.stream().filter(name -> {
FormItem formItem = form.getFormItem(name);
boolean empty = formItem != null && formItem.isEmpty();
// there is a special case for SwitchItem of Boolean type, the SwitchItem.isEmpty() tests if the value is
// null, but for this validation case we must ensure the value is set to false to allow the validation
// to work
boolean switchItemFalse = false;
if (formItem != null && formItem.getClass().equals(SwitchItem.class)) {
Object value = formItem.getValue();
switchItemFalse = value != null && !Boolean.parseBoolean(value.toString());
}
return empty || switchItemFalse;
}).collect(toSet());
if (requiredAlternatives.size() == emptyItems.size()) {
// show an error on each related form item
requiredAlternatives.forEach(alternative -> {
FormItem<Object> formItem = form.getFormItem(alternative);
if (formItem.isEmpty()) {
formItem.showError(messages.exactlyOneAlternativeError(labelBuilder.enumeration(requiredAlternatives, constants.or())));
}
});
// return overall result
return ValidationResult.invalid(messages.exactlyOneAlternativesError(labelBuilder.enumeration(requiredAlternatives, constants.or())));
} else {
return ValidationResult.OK;
}
}
use of org.jboss.hal.ballroom.form.SwitchItem in project console by hal.
the class DefaultFormItemProvider method createFrom.
@Override
public FormItem<?> createFrom(Property property) {
FormItem<?> formItem = null;
String name = property.getName();
String label = labelBuilder.label(property);
ModelNode attributeDescription = property.getValue();
// don't use 'required' here!
boolean required = attributeDescription.hasDefined(NILLABLE) && !attributeDescription.get(NILLABLE).asBoolean();
boolean expressionAllowed = attributeDescription.hasDefined(EXPRESSIONS_ALLOWED) && attributeDescription.get(EXPRESSIONS_ALLOWED).asBoolean();
boolean readOnly = attributeDescription.hasDefined(ACCESS_TYPE) && (READ_ONLY.equals(attributeDescription.get(ACCESS_TYPE).asString()) || METRIC.equals(attributeDescription.get(ACCESS_TYPE).asString()));
String unit = attributeDescription.hasDefined(UNIT) ? attributeDescription.get(UNIT).asString() : null;
Deprecation deprecation = attributeDescription.hasDefined(DEPRECATED) ? new Deprecation(attributeDescription.get(DEPRECATED)) : null;
if (attributeDescription.hasDefined(TYPE)) {
ModelType type = attributeDescription.get(TYPE).asType();
ModelType valueType = (attributeDescription.has(VALUE_TYPE) && attributeDescription.get(VALUE_TYPE).getType() != ModelType.OBJECT) ? ModelType.valueOf(attributeDescription.get(VALUE_TYPE).asString()) : null;
switch(type) {
case BOOLEAN:
{
SwitchItem switchItem = new SwitchItem(name, label);
if (attributeDescription.hasDefined(DEFAULT)) {
switchItem.assignDefaultValue(attributeDescription.get(DEFAULT).asBoolean());
}
formItem = switchItem;
break;
}
case BIG_INTEGER:
case INT:
case LONG:
{
long min, max;
if (type == ModelType.INT) {
min = attributeDescription.get(MIN).asLong(Integer.MIN_VALUE);
max = attributeDescription.get(MAX).asLong(Integer.MAX_VALUE);
} else {
min = attributeDescription.get(MIN).asLong(MIN_SAFE_LONG);
max = attributeDescription.get(MAX).asLong(MAX_SAFE_LONG);
}
NumberItem numberItem = new NumberItem(name, label, unit, min, max);
if (attributeDescription.hasDefined(DEFAULT)) {
long defaultValue = attributeDescription.get(DEFAULT).asLong();
numberItem.assignDefaultValue(defaultValue);
}
formItem = numberItem;
break;
}
case DOUBLE:
{
long min = attributeDescription.get(MIN).asLong(MIN_SAFE_LONG);
long max = attributeDescription.get(MAX).asLong(MAX_SAFE_LONG);
NumberDoubleItem numberItem = new NumberDoubleItem(name, label, unit, min, max);
if (attributeDescription.hasDefined(DEFAULT)) {
double defaultValue = attributeDescription.get(DEFAULT).asDouble();
numberItem.assignDefaultValue(defaultValue);
}
formItem = numberItem;
break;
}
case LIST:
{
if (valueType != null && ModelType.STRING == valueType) {
List<String> allowedValues = stringValues(attributeDescription, ALLOWED);
if (!allowedValues.isEmpty()) {
MultiSelectBoxItem multiSelectBoxItem = new MultiSelectBoxItem(name, label, allowedValues);
if (attributeDescription.hasDefined(DEFAULT)) {
List<String> defaultValues = stringValues(attributeDescription, DEFAULT);
if (!defaultValues.isEmpty()) {
multiSelectBoxItem.assignDefaultValue(defaultValues);
}
}
formItem = multiSelectBoxItem;
} else {
ListItem listItem = new ListItem(name, label);
if (attributeDescription.hasDefined(DEFAULT)) {
List<String> defaultValues = stringValues(attributeDescription, DEFAULT);
if (!defaultValues.isEmpty()) {
listItem.assignDefaultValue(defaultValues);
}
}
formItem = listItem;
checkCapabilityReference(attributeDescription, formItem);
}
} else if (isSimpleTuple(attributeDescription)) {
// process OBJECT type attribute if all of its subattributes are simple types
formItem = new TuplesListItem(name, label, metadata.forComplexAttribute(property.getName()));
} else {
logger.warn("Unsupported model type {} for attribute {} in metadata {}. Unable to create a form item. Attribute will be skipped.", type.name(), property.getName(), metadata.getTemplate());
break;
}
break;
}
case OBJECT:
{
if (valueType != null && ModelType.STRING == valueType) {
PropertiesItem propertiesItem = new PropertiesItem(name, label);
List<Property> properties = ModelNodeHelper.getOrDefault(attributeDescription, DEFAULT, () -> attributeDescription.get(DEFAULT).asPropertyList(), emptyList());
if (!properties.isEmpty()) {
Map<String, String> defaultValues = new HashMap<>();
for (Property p : properties) {
defaultValues.put(p.getName(), p.getValue().asString());
}
propertiesItem.assignDefaultValue(defaultValues);
}
formItem = propertiesItem;
}
break;
}
case STRING:
{
List<String> allowedValues = stringValues(attributeDescription, ALLOWED);
if (allowedValues.isEmpty()) {
FormItem<String> textBoxItem = new TextBoxItem(name, label, null);
boolean sensitive = failSafeGet(attributeDescription, ACCESS_CONSTRAINTS + "/" + SENSITIVE).isDefined();
if (PASSWORD.equals(name) || sensitive) {
textBoxItem.mask();
}
if (attributeDescription.hasDefined(DEFAULT)) {
textBoxItem.assignDefaultValue(attributeDescription.get(DEFAULT).asString());
}
formItem = textBoxItem;
checkCapabilityReference(attributeDescription, formItem);
} else {
SingleSelectBoxItem singleSelectBoxItem = new SingleSelectBoxItem(name, label, allowedValues, !required);
if (attributeDescription.hasDefined(DEFAULT)) {
singleSelectBoxItem.assignDefaultValue(attributeDescription.get(DEFAULT).asString());
}
formItem = singleSelectBoxItem;
}
break;
}
// unsupported types
case BIG_DECIMAL:
case BYTES:
case EXPRESSION:
case PROPERTY:
case TYPE:
case UNDEFINED:
logger.warn("Unsupported model type {} for attribute {} in metadata {}. Unable to create a form item. Attribute will be skipped.", type.name(), property.getName(), metadata.getTemplate());
break;
default:
break;
}
if (formItem != null) {
formItem.setRequired(required);
formItem.setDeprecated(deprecation);
if (formItem.supportsExpressions()) {
formItem.setExpressionAllowed(expressionAllowed);
formItem.addResolveExpressionHandler(event -> {
// resend as application event
Core.INSTANCE.eventBus().fireEvent(event);
});
}
if (readOnly) {
formItem.setEnabled(false);
// if the attribute is read-only and required, the form validation prevents to save the form
// remove the required constraint to allow the save operation.
formItem.setRequired(false);
}
}
}
return formItem;
}
Aggregations