layout.setHeight100();
layout.setWidth100();
layout.setMargin(11);
// create the header strip - will contain "set all values to" form
EnhancedToolStrip headerStrip = null;
if (!propertyIsReadOnly) {
headerStrip = new EnhancedToolStrip();
headerStrip.setWidth100();
headerStrip.setPadding(5);
headerStrip.setMembersMargin(10);
layout.addMember(headerStrip);
}
// create the list grid that contains all the member resource names and values
final ListGridRecord[] data = getMemberValuesGridData(this.memberConfigurations, propertyDefinitionSimple,
aggregatePropertySimple, index);
final ListGrid memberValuesGrid = new ListGrid();
memberValuesGrid.setHeight100();
memberValuesGrid.setWidth100();
memberValuesGrid.setData(data);
memberValuesGrid.setEditEvent(ListGridEditEvent.CLICK);
memberValuesGrid.setEditByCell(false); // selecting any cell allows the user to edit the entire row
ListGridField idField = new ListGridField(FIELD_ID, MSG.common_title_id());
idField.setHidden(true);
idField.setCanEdit(false);
ListGridField memberField = new ListGridField(FIELD_MEMBER, MSG.view_groupConfigEdit_member());
memberField.setCanEdit(false);
final ListGridField valueField = new ListGridField(FIELD_VALUE, MSG.common_title_value());
valueField.setRequired(propertyDefinitionSimple.isRequired());
valueField.setCanEdit(true);
final FormItem editorItem = buildEditorFormItem(valueField, propertyDefinitionSimple);
valueField.setEditorType(editorItem);
valueField.setCellFormatter(new CellFormatter() {
public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
if (value == null) {
return "";
}
List<PropertyDefinitionEnumeration> enumeratedValues = propertyDefinitionSimple.getEnumeratedValues();
if (enumeratedValues != null && !enumeratedValues.isEmpty()) {
for (PropertyDefinitionEnumeration option : enumeratedValues) {
if (option.getValue().equals(value.toString())) {
return option.getName();
}
}
return value.toString();
} else {
switch (propertyDefinitionSimple.getType()) {
case BOOLEAN: {
return BOOLEAN_PROPERTY_ITEM_VALUE_MAP.get(value.toString());
}
case PASSWORD: {
return "******";
}
default:
return value.toString();
}
}
}
});
ListGridField unsetField = new ListGridField(FIELD_UNSET, MSG.view_groupConfigEdit_unset());
unsetField.setType(ListGridFieldType.BOOLEAN);
if (propertyDefinitionSimple.isRequired()) {
unsetField.setHidden(true);
unsetField.setCanEdit(false);
} else {
unsetField.setHidden(false);
unsetField.setCanEdit(true);
unsetField.setCanToggle(false); // otherwise, our handler won't get the form
// add handler to handle when the user flips the unset bit
unsetField.addChangedHandler(new com.smartgwt.client.widgets.grid.events.ChangedHandler() {
public void onChanged(com.smartgwt.client.widgets.grid.events.ChangedEvent event) {
Boolean isUnset = (Boolean) event.getValue();
FormItem valueItem = event.getForm().getField(FIELD_VALUE);
if (isUnset) {
int rowNum = event.getRowNum();
memberValuesGrid.setEditValue(rowNum, FIELD_VALUE, (String) null);
} else {
valueItem.focusInItem();
}
valueItem.redraw();
}
});
// add handler when user clears the value field - this should turn on the "unset" button
valueField.addChangeHandler(new com.smartgwt.client.widgets.grid.events.ChangeHandler() {
public void onChange(com.smartgwt.client.widgets.grid.events.ChangeEvent event) {
int rowNum = event.getRowNum();
if (event.getValue() == null || event.getValue().toString().length() == 0) {
memberValuesGrid.setEditValue(rowNum, FIELD_UNSET, true);
} else {
memberValuesGrid.setEditValue(rowNum, FIELD_UNSET, false);
}
}
});
}
idField.setWidth("75");
memberField.setWidth("*");
unsetField.setWidth("50");
valueField.setWidth("45%");
memberValuesGrid.setFields(idField, memberField, unsetField, valueField);
layout.addMember(memberValuesGrid);
// create the footer strip - will contain ok and cancel buttons
EnhancedToolStrip footerStrip = new EnhancedToolStrip();
footerStrip.setWidth100();
footerStrip.setPadding(5);
footerStrip.setMembersMargin(10);
layout.addMember(footerStrip);
// footer OK button
final IButton okButton = new EnhancedIButton(MSG.common_button_ok(), ButtonColor.BLUE);
if (!propertyIsReadOnly) {
// if its read-only, allow the ok button to be enabled to just let the user close the window
// otherwise, disable it now and we will enable it after we know things are ready to be saved
okButton.setDisabled(true);
}
okButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
public void onClick(ClickEvent clickEvent) {
if (!propertyIsReadOnly) {
boolean valuesHomogeneous = true;
boolean isValid = true;
String firstValue = data[0].getAttribute(FIELD_VALUE);
int i = 0;
for (ListGridRecord valueItem : data) {
String value = valueItem.getAttribute(FIELD_VALUE);
if (valuesHomogeneous) {
if ((value != null && !value.equals(firstValue)) || (value == null && firstValue != null)) {
valuesHomogeneous = false;
}
}
isValid = isValid && memberValuesGrid.validateRow(i);
Configuration config = (Configuration) valueItem.getAttributeAsObject(ATTR_CONFIG_OBJ);
PropertySimple memberPropertySimple = (PropertySimple) getProperty(config,
aggregatePropertySimple, index);
memberPropertySimple.setValue(value);
memberPropertySimple.setErrorMessage(null);
}
String aggregateStaticItemName = aggregateValueItem.getAttribute(RHQ_STATIC_ITEM_NAME_ATTRIBUTE);
FormItem aggregateStaticItem = aggregateValueItem.getForm().getField(aggregateStaticItemName);
String aggregateUnsetItemName = aggregateValueItem.getAttribute(RHQ_UNSET_ITEM_NAME_ATTRIBUTE);
FormItem aggregateUnsetItem = aggregateValueItem.getForm().getField(aggregateUnsetItemName);
aggregateUnsetItem.setDisabled(!valuesHomogeneous);
if (valuesHomogeneous) {
// Update the value of the aggregate property and set its override flag to true.
aggregatePropertySimple.setValue(firstValue);
aggregatePropertySimple.setOverride(true);
boolean isUnset = (firstValue == null);
aggregateUnsetItem.setValue(isUnset);
// Set the aggregate value item's value to the homogeneous value, unhide it, and enable it
// unless it's unset.
setValue(aggregateValueItem, firstValue);
aggregateValueItem.show();
aggregateValueItem.setDisabled(isUnset);
aggregateStaticItem.hide();
} else {
aggregatePropertySimple.setValue(null);
aggregatePropertySimple.setOverride(false);
aggregateValueItem.hide();
aggregateStaticItem.show();
}
CoreGUI.getMessageCenter().notify(
new Message(MSG.view_groupConfigEdit_saveReminder(), Severity.Info));
// this has the potential to send another message to the message center
firePropertyChangedEvent(aggregatePropertySimple, propertyDefinitionSimple, isValid);
}
popup.destroy();
}
});
footerStrip.addMember(okButton);
// track errors in grid - enable/disable OK button accordingly
// I tried many ways to get this to work right - this is what I came up with
memberValuesGrid.addRowEditorExitHandler(new RowEditorExitHandler() {
public void onRowEditorExit(RowEditorExitEvent event) {
memberValuesGrid.validateRow(event.getRowNum());
if (memberValuesGrid.hasErrors()) {
okButton.disable();
} else {
okButton.enable();
}
}
});
// if the data can be changed, add some additional widgets; if not, make the value grid read only
if (propertyIsReadOnly) {
memberValuesGrid.setCanEdit(false);
} else {
// put a cancel button in the footer strip to allow the user to exit without saving changes
final IButton cancelButton = new EnhancedIButton(MSG.common_button_cancel());
cancelButton.focus();
cancelButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
public void onClick(ClickEvent clickEvent) {
popup.destroy();
}
});
footerStrip.addMember(cancelButton);
// create a small form in the header strip to allow the user to apply a value to ALL rows
final DynamicForm setAllForm = new DynamicForm();
setAllForm.setNumCols(3);
setAllForm.setCellPadding(5);