final IButton deselectAllButton = new EnhancedIButton(MSG.view_autoDiscoveryQ_deselectAll());
deselectAllButton.setDisabled(true);
footer.addMember(deselectAllButton);
IButton refreshButton = new EnhancedIButton(MSG.common_button_refresh());
refreshButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent clickEvent) {
refresh();
}
});
footer.addMember(refreshButton);
treeGrid.addSelectionChangedHandler(new SelectionChangedHandler() {
public void onSelectionChanged(SelectionEvent selectionEvent) {
if (selectionChangedHandlerDisabled || selectionEvent.isRightButtonDown()) {
return;
}
selectionChangedHandlerDisabled = true;
final TreeNode selectedNode = treeGrid.getTree()
.findById(selectionEvent.getRecord().getAttribute("id"));
TreeNode parentNode = treeGrid.getTree().getParent(selectedNode);
boolean isPlatform = treeGrid.getTree().isRoot(parentNode);
boolean isCheckboxMarked = treeGrid.isSelected(selectedNode);
if (isPlatform) {
if (isCheckboxMarked) {
SC.ask(MSG.view_autoDiscoveryQ_confirmSelect(), new BooleanCallback() {
public void execute(Boolean confirmed) {
if (confirmed && !treeGrid.getTree().hasChildren(selectedNode)) {
selectedNode.setAttribute("selectChildOnArrival", "true");
treeGrid.getTree().loadChildren(selectedNode);
} else {
if (confirmed) {
selectAllPlatformChildren(selectedNode);
}
updateButtonEnablement(selectAllButton, deselectAllButton, importButton,
ignoreButton, unignoreButton);
selectionChangedHandlerDisabled = false;
}
}
});
} else {
selectedNode.setAttribute("autoSelectChildren", "false");
treeGrid.deselectRecords(treeGrid.getTree().getChildren(selectedNode));
// the immediate redraw below should not be necessary, but without it the deselected
// platform checkbox remained checked.
// treeGrid.redraw();
updateButtonEnablement(selectAllButton, deselectAllButton, importButton, ignoreButton,
unignoreButton);
selectionChangedHandlerDisabled = false;
}
} else {
if (isCheckboxMarked) {
if (!treeGrid.isSelected(parentNode)) {
treeGrid.selectRecord(parentNode);
}
}
updateButtonEnablement(selectAllButton, deselectAllButton, importButton, ignoreButton,
unignoreButton);
selectionChangedHandlerDisabled = false;
}
}
});
treeGrid.addDataArrivedHandler(new DataArrivedHandler() {
public void onDataArrived(DataArrivedEvent dataArrivedEvent) {
if (treeGrid != null) {
TreeNode parent = dataArrivedEvent.getParentNode();
if (!treeGrid.getTree().isRoot(parent)) {
// This flag can be set when we select a platform or as part of selectAll button handling. It
// means that we want to immediately select the child server nodes.
boolean selectChildOnArrival = Boolean.valueOf(parent.getAttribute("selectChildOnArrival"));
if (selectChildOnArrival) {
// data includes the platform, just get the descendant servers
treeGrid.selectRecords(treeGrid.getData().getDescendantLeaves());
}
}
// This logic is relevant to what we do in the selectAll button
boolean endDisable = true;
if (loadAllPlatforms) {
TreeNode rootNode = treeGrid.getTree().getRoot();
TreeNode[] platformNodes = treeGrid.getTree().getChildren(rootNode);
for (TreeNode platformNode : platformNodes) {
if (!treeGrid.getTree().isLoaded(platformNode)) {
endDisable = false;
break;
}
}
}
if (endDisable) {
updateButtonEnablement(selectAllButton, deselectAllButton, importButton, ignoreButton,
unignoreButton);
selectionChangedHandlerDisabled = false;
}
// NOTE: Due to a SmartGWT bug, the TreeGrid is not automatically redrawn upon data arrival, and
// calling treeGrid.markForRedraw() doesn't redraw it either. The user can mouse over the grid
// to cause it to redraw, but it is obviously not reasonable to expect that. So we must
// explicitly call redraw() here.
treeGrid.redraw();
}
}
});
dataSource.addFailedFetchListener(new AsyncCallback() {
// just in case we have a failure while fetching, try to make sure we don't lock up the view.
public void onFailure(Throwable caught) {
loadAllPlatforms = false;
updateButtonEnablement(selectAllButton, deselectAllButton, importButton, ignoreButton, unignoreButton);
selectionChangedHandlerDisabled = false;
}
public void onSuccess(Object result) {
// never called
}
});
selectAllButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent clickEvent) {
SC.ask(MSG.view_autoDiscoveryQ_confirmSelectAll(), new BooleanCallback() {
public void execute(Boolean selectChildServers) {
selectionChangedHandlerDisabled = true;
TreeNode rootNode = treeGrid.getTree().getRoot();
TreeNode[] platformNodes = treeGrid.getTree().getChildren(rootNode);
if (selectChildServers) {
disableButtons(selectAllButton, deselectAllButton, importButton, ignoreButton,
unignoreButton);
// we may need to fetch the child servers in order to select them.
ArrayList<TreeNode> platformsToLoad = new ArrayList<TreeNode>();
for (TreeNode platformNode : platformNodes) {
if (treeGrid.getTree().isLoaded(platformNode)) {
// if loaded then just select the nodes
if (!treeGrid.isSelected(platformNode)) {
treeGrid.selectRecord(platformNode);
}
treeGrid.selectRecords(treeGrid.getTree().getChildren(platformNode));
} else {
platformsToLoad.add(platformNode);
}
}
if (platformsToLoad.isEmpty()) {
// we're done, everything is already loaded
treeGrid.markForRedraw();
updateButtonEnablement(selectAllButton, deselectAllButton, importButton, ignoreButton,
unignoreButton);
selectionChangedHandlerDisabled = false;
} else {
// this plays with the dataArrivedHandler which is responsible for
// doing the child selection and checking to see if all platforms have been loaded
// as the child data comes in.
loadAllPlatforms = true;
for (TreeNode platformToLoad : platformsToLoad) {
// load and select
if (!treeGrid.isSelected(platformToLoad)) {
treeGrid.selectRecord(platformToLoad);
}
platformToLoad.setAttribute("selectChildOnArrival", "true");
treeGrid.getTree().loadChildren(platformToLoad);
}
}
} else {
treeGrid.selectRecords(platformNodes);
treeGrid.markForRedraw();
updateButtonEnablement(selectAllButton, deselectAllButton, importButton, ignoreButton,
unignoreButton);
selectionChangedHandlerDisabled = false;
}
}
});
}
});
deselectAllButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent clickEvent) {
selectionChangedHandlerDisabled = true;
treeGrid.deselectAllRecords();
treeGrid.markForRedraw();
updateButtonEnablement(selectAllButton, deselectAllButton, importButton, ignoreButton, unignoreButton);
selectionChangedHandlerDisabled = false;
}
});
importButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent clickEvent) {
disableButtons(selectAllButton, deselectAllButton, importButton, ignoreButton, unignoreButton);
// TODO (ips): Make the below message sticky, but add a new ClearSticky Message option that the
// below callback methods can use to clear it once the importResources() call has
// completed.
CoreGUI.getMessageCenter().notify(
new Message(MSG.view_autoDiscoveryQ_importInProgress(), Message.Severity.Info, EnumSet
.of(Message.Option.Transient)));
importResources(getSelectedPlatforms());
}
});
ignoreButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent clickEvent) {
disableButtons(selectAllButton, deselectAllButton, importButton, ignoreButton, unignoreButton);
CoreGUI.getMessageCenter().notify(
new Message(MSG.view_autoDiscoveryQ_ignoreInProgress(), Message.Severity.Info, EnumSet
.of(Message.Option.Transient)));
// assuming here that the ignore list will not be massive and that we can do it all in one go,
// otherwise re-impl this like import.
resourceService.ignoreResources(getAllSelectedIds(), new AsyncCallback<Void>() {
public void onFailure(Throwable caught) {
CoreGUI.getErrorHandler().handleError(MSG.view_autoDiscoveryQ_ignoreFailure(), caught);
}
public void onSuccess(Void result) {
CoreGUI.getMessageCenter().notify(
new Message(MSG.view_autoDiscoveryQ_ignoreSuccessful(), Message.Severity.Info));
refresh();
}
});
}
});
unignoreButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent clickEvent) {
disableButtons(selectAllButton, deselectAllButton, importButton, ignoreButton, unignoreButton);
CoreGUI.getMessageCenter().notify(
new Message(MSG.view_autoDiscoveryQ_unignoreInProgress(), Message.Severity.Info, EnumSet
.of(Message.Option.Transient)));