private GenericCallback<SnapshotInfo[]> createGenericCallback(final String snapshotName,
final String packageName,
final PackageServiceAsync serv) {
return new GenericCallback<SnapshotInfo[]>() {
public void onSuccess(final SnapshotInfo[] snaps) {
final FormStylePopup copy = new FormStylePopup(images.snapshot(),
constants.CopySnapshotText(snapshotName));
final List<RadioButton> options = new ArrayList<RadioButton>();
VerticalPanel vert = new VerticalPanel();
for (int i = 0; i < snaps.length; i++) {
// cant copy onto to itself...
if (!snaps[i].getName().equals(snapshotName)) {
RadioButton existing = new RadioButton("snapshotNameGroup",
snaps[i].getName()); // NON-NLS
options.add(existing);
vert.add(existing);
}
}
HorizontalPanel newNameHorizontalPanel = new HorizontalPanel();
final TextBox newNameTextBox = new TextBox();
final String newNameText = constants.NEW()
+ ": ";
final RadioButton newNameRadioButton = new RadioButton("snapshotNameGroup",
newNameText);
newNameHorizontalPanel.add(newNameRadioButton);
newNameTextBox.setEnabled(false);
newNameRadioButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
newNameTextBox.setEnabled(true);
}
});
newNameHorizontalPanel.add(newNameTextBox);
options.add(newNameRadioButton);
vert.add(newNameHorizontalPanel);
copy.addAttribute(constants.ExistingSnapshots(),
vert);
Button ok = new Button(constants.OK());
copy.addAttribute("",
ok);
ok.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
if (!isOneButtonSelected(options)) {
Window.alert(constants.YouHaveToEnterOrChoseALabelNameForTheSnapshot());
return;
}
if (newNameRadioButton.getValue()) {
if (checkUnique(snaps,
newNameTextBox.getText())) {
serv.copyOrRemoveSnapshot(packageName,
snapshotName,
false,
newNameTextBox.getText(),
new GenericCallback<java.lang.Void>() {
public void onSuccess(Void v) {
copy.hide();
Window.alert(constants.CreatedSnapshot0ForPackage1(
newNameTextBox.getText(),
packageName));
}
});
}
} else {
for (RadioButton rb : options) {
if (rb.getValue()) {
final String newName = rb.getText();
serv.copyOrRemoveSnapshot(packageName,
snapshotName,
false,
newName,
new GenericCallback<java.lang.Void>() {
public void onSuccess(Void v) {
copy.hide();
Window.alert(constants.Snapshot0ForPackage1WasCopiedFrom2(
newName,
packageName,
snapshotName));
}
});
}
}
}
}
private boolean isOneButtonSelected(final List<RadioButton> options) {
boolean oneButtonIsSelected = false;
for (RadioButton rb : options) {
if (rb.getValue()) {
oneButtonIsSelected = true;
break;
}
}
return oneButtonIsSelected;
}
private boolean checkUnique(SnapshotInfo[] snaps,
String name) {
for (SnapshotInfo sn : snaps) {
if (sn.getName().equals(name)) {
Window.alert(constants.PleaseEnterANonExistingSnapshotName());
return false;
}
}
return true;
}
});
copy.show();
}
};
}