refactored all dialogs so they use DataEntryDialog as their common base class

This commit is contained in:
Axel Uhl committed 2011-08-26 15:49:18 +02:00
1 parent d2a00020d2
commit eecd24ada2
6 files changed
+157 -109

No files matched your search

@@ -38,7 +38,7 @@ public abstract class DataEntryDialog<T> {
}
public DataEntryDialog(String title, String message, String okButtonName, String cancelButtonName,
String initialValue, Validator<T> validator, final AsyncCallback<T> callback) {
Validator<T> validator, final AsyncCallback<T> callback) {
dateEntryDialog = new DialogBox();
dateEntryDialog.setText(title);
dateEntryDialog.setAnimationEnabled(true);
@@ -77,8 +77,11 @@ public abstract class DataEntryDialog<T> {
String errorMessage = validator.getErrorMessage(getResult());
if (errorMessage == null) {
getStatusLabel().setText("");
getOkButton().setEnabled(true);
} else {
getStatusLabel().setText(errorMessage);
getStatusLabel().setStyleName("errorLabel");
getOkButton().setEnabled(false);
}
return errorMessage == null;
}
@@ -1,6 +1,7 @@
package com.sap.sailing.gwt.ui.client;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@@ -399,114 +400,32 @@ public class LeaderboardConfigPanel extends FormPanel implements EventDisplayer
}
private void addNewLeaderboard() {
final DialogBox leaderboardCreationDialogBox = new DialogBox();
leaderboardCreationDialogBox.setText(stringConstants.createNewLeaderboard());
leaderboardCreationDialogBox.setAnimationEnabled(true);
final Button okButton = new Button("OK");
okButton.setEnabled(false);
VerticalPanel dialogVPanel = new VerticalPanel();
final Label statusLabel = new Label(stringConstants.pleaseEnterNonEmptyName());
dialogVPanel.add(statusLabel);
final TextBox[] discardThresholdBoxes = new TextBox[MAX_NUMBER_OF_DISCARDED_RESULTS];
dialogVPanel.add(new Label(stringConstants.leaderboardName()));
final TextBox leaderboardNameField = new TextBox();
AbstractEntryPoint.addFocusUponKeyUpToggler(leaderboardNameField);
leaderboardNameField.addChangeHandler(new ChangeHandler() {
@Override
public void onChange(ChangeEvent event) {
enableOkButtonIfValid(okButton, leaderboardNameField, discardThresholdBoxes, statusLabel);
}
});
dialogVPanel.add(leaderboardNameField);
dialogVPanel.add(new Label(stringConstants.discardRacesFromHowManyStartedRacesOn()));
HorizontalPanel hp = new HorizontalPanel();
for (int i=0; i<discardThresholdBoxes.length; i++) {
hp.add(new Label(""+(i+1)+"."));
TextBox tb = new TextBox();
tb.setVisibleLength(2);
AbstractEntryPoint.addFocusUponKeyUpToggler(tb);
tb.addChangeHandler(new ChangeHandler() {
@Override
public void onChange(ChangeEvent event) {
enableOkButtonIfValid(okButton, leaderboardNameField, discardThresholdBoxes, statusLabel);
}
});
discardThresholdBoxes[i] = tb;
hp.add(tb);
}
dialogVPanel.add(hp);
dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
HorizontalPanel buttonPanel = new HorizontalPanel();
dialogVPanel.add(buttonPanel);
buttonPanel.add(okButton);
Button cancelButton = new Button(stringConstants.cancel());
buttonPanel.add(cancelButton);
cancelButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
leaderboardCreationDialogBox.hide();
}
});
leaderboardCreationDialogBox.setWidget(dialogVPanel);
okButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
leaderboardCreationDialogBox.hide();
List<Integer> discardThresholds = new ArrayList<Integer>();
for (int i=0; i<discardThresholdBoxes.length; i++) {
if (discardThresholdBoxes[i].getValue() != null && discardThresholdBoxes[i].getValue().trim().length() > 0) {
try {
discardThresholds.add(Integer.valueOf(discardThresholdBoxes[i].getValue().trim()));
} catch (NumberFormatException e) {
errorReporter.reportError("Internal error; NumberFormatException for "+discardThresholdBoxes[i].getValue()+
" which should have been caught by validation before");
LeaderboardCreationDialog dialog = new LeaderboardCreationDialog(Collections.unmodifiableCollection(leaderboardNames),
stringConstants, errorReporter, new AsyncCallback<Pair<String,String[]>>() {
@Override
public void onFailure(Throwable arg0) {}
@Override
public void onSuccess(Pair<String, String[]> result) {
List<Integer> discardThresholds = new ArrayList<Integer>();
for (int i=0; i<result.getB().length; i++) {
if (result.getB()[i] != null && result.getB()[i].trim().length() > 0) {
try {
discardThresholds.add(Integer.valueOf(result.getB()[i].trim()));
} catch (NumberFormatException e) {
errorReporter.reportError("Internal error; NumberFormatException for "+result.getB()[i]+
" which should have been caught by validation before");
}
}
}
int[] discanrdThresholdsAsIntArray = new int[discardThresholds.size()];
int i=0;
for (Integer integer : discardThresholds) {
discanrdThresholdsAsIntArray[i++] = integer;
}
createNewLeaderboard(result.getA(), discanrdThresholdsAsIntArray);
}
}
int[] discanrdThresholdsAsIntArray = new int[discardThresholds.size()];
int i=0;
for (Integer integer : discardThresholds) {
discanrdThresholdsAsIntArray[i++] = integer;
}
createNewLeaderboard(leaderboardNameField.getText(), discanrdThresholdsAsIntArray);
}
});
AbstractEntryPoint.linkEnterToButton(okButton, leaderboardNameField);
AbstractEntryPoint.linkEnterToButton(okButton, discardThresholdBoxes);
AbstractEntryPoint.linkEscapeToButton(cancelButton, leaderboardNameField);
AbstractEntryPoint.linkEscapeToButton(cancelButton, discardThresholdBoxes);
leaderboardCreationDialogBox.center();
leaderboardNameField.setFocus(true);
}
private void enableOkButtonIfValid(final Button okButton, final TextBox leaderboardNameField, TextBox[] discardThresholdBoxes, Label statusLabel) {
boolean nonEmpty = leaderboardNameField.getValue() != null && leaderboardNameField.getValue().trim().length() > 0;
boolean unique = !leaderboardNames.contains(leaderboardNameField.getValue());
boolean discardThresholdsAscending = true;
boolean discardThresholdsAreNumeric = discardThresholdBoxes[0].getValue() == null ||
discardThresholdBoxes[0].getValue().matches("[0-9]*");
for (int i=1; i<discardThresholdBoxes.length; i++) {
if (discardThresholdBoxes[i].getValue() != null && discardThresholdBoxes[i].getValue().trim().length() > 0) {
try {
discardThresholdsAscending = discardThresholdsAscending &&
discardThresholdBoxes[i-1].getValue() != null && discardThresholdBoxes[i-1].getValue().trim().length() > 0 &&
Integer.valueOf(discardThresholdBoxes[i-1].getValue().trim()) < Integer.valueOf(discardThresholdBoxes[i].getValue().trim());
} catch (NumberFormatException e) {
discardThresholdsAreNumeric = false;
}
}
}
if (!nonEmpty) {
statusLabel.setText(stringConstants.pleaseEnterNonEmptyName());
} else if (!unique) {
statusLabel.setText(stringConstants.leaderboardWithThisNameAlreadyExists());
} else if (!discardThresholdsAreNumeric) {
statusLabel.setText(stringConstants.discardThresholdsMustBeNumeric());
} else if (!discardThresholdsAscending) {
statusLabel.setText(stringConstants.discardThresholdsMustBeAscending());
} else {
statusLabel.setText(" ");
}
okButton.setEnabled(nonEmpty && unique && discardThresholdsAreNumeric && discardThresholdsAscending);
});
dialog.show();
}
private void createNewLeaderboard(final String leaderboardName, int[] discardThresholds) {
@@ -0,0 +1,122 @@
package com.sap.sailing.gwt.ui.client;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sap.sailing.gwt.ui.shared.Pair;
public class LeaderboardCreationDialog extends DataEntryDialog<Pair<String, String[]>> {
private static final int MAX_NUMBER_OF_DISCARDED_RESULTS = 4;
private final TextBox entryField;
private final TextBox[] discardThresholdBoxes;
private final ErrorReporter errorReporter;
private final StringConstants stringConstants;
private static class LeaderboardParameterValidator implements Validator<Pair<String, String[]>> {
private final StringConstants stringConstants;
private final Collection<String> existingLeaderboardNames;
public LeaderboardParameterValidator(Collection<String> existingLeaderboardNames, StringConstants stringConstants) {
super();
this.existingLeaderboardNames = existingLeaderboardNames;
this.stringConstants = stringConstants;
}
@Override
public String getErrorMessage(Pair<String, String[]> valueToValidate) {
String errorMessage;
boolean nonEmpty = valueToValidate.getA() != null && valueToValidate.getA().trim().length() > 0;
boolean unique = !existingLeaderboardNames.contains(valueToValidate.getA());
boolean discardThresholdsAscending = true;
boolean discardThresholdsAreNumeric = valueToValidate.getB().length == 0 || valueToValidate.getB()[0] == null ||
valueToValidate.getB()[0].matches("[0-9]*");
for (int i=1; i<valueToValidate.getB().length; i++) {
if (valueToValidate.getB()[i] != null && valueToValidate.getB()[i].trim().length() > 0) {
try {
discardThresholdsAscending = discardThresholdsAscending &&
valueToValidate.getB()[i-1] != null && valueToValidate.getB()[i-1].trim().length() > 0 &&
Integer.valueOf(valueToValidate.getB()[i-1].trim()) < Integer.valueOf(valueToValidate.getB()[i].trim());
} catch (NumberFormatException e) {
discardThresholdsAreNumeric = false;
}
}
}
if (!nonEmpty) {
errorMessage = stringConstants.pleaseEnterNonEmptyName();
} else if (!unique) {
errorMessage = stringConstants.leaderboardWithThisNameAlreadyExists();
} else if (!discardThresholdsAreNumeric) {
errorMessage = stringConstants.discardThresholdsMustBeNumeric();
} else if (!discardThresholdsAscending) {
errorMessage = stringConstants.discardThresholdsMustBeAscending();
} else {
errorMessage = null;
}
return errorMessage;
}
}
public LeaderboardCreationDialog(Collection<String> existingLeaderboardNames, StringConstants stringConstants,
ErrorReporter errorReporter, AsyncCallback<Pair<String, String[]>> callback) {
super(stringConstants.leaderboardName(), stringConstants.leaderboardName(), stringConstants.ok(),
stringConstants.cancel(), new LeaderboardParameterValidator(existingLeaderboardNames, stringConstants), callback);
this.errorReporter = errorReporter;
this.stringConstants = stringConstants;
entryField = createTextBox(/* initial value */null);
discardThresholdBoxes = new TextBox[MAX_NUMBER_OF_DISCARDED_RESULTS];
for (int i=0; i<discardThresholdBoxes.length; i++) {
discardThresholdBoxes[i] = createTextBox(/* initialValue */ null);
discardThresholdBoxes[i].setVisibleLength(2);
}
}
@Override
protected Pair<String, String[]> getResult() {
List<String> discardThresholds = new ArrayList<String>();
for (int i=0; i<discardThresholdBoxes.length; i++) {
if (discardThresholdBoxes[i].getValue() != null && discardThresholdBoxes[i].getValue().trim().length() > 0) {
try {
Integer.valueOf(discardThresholdBoxes[i].getValue().trim()); // ensure there is no exception
discardThresholds.add(discardThresholdBoxes[i].getValue().trim());
} catch (NumberFormatException e) {
errorReporter.reportError("Internal error; NumberFormatException for "+discardThresholdBoxes[i].getValue()+
" which should have been caught by validation before");
}
}
}
String[] discardThresholdBoxesContents = discardThresholds.toArray(new String[discardThresholds.size()]);
return new Pair<String, String[]>(entryField.getValue(), discardThresholdBoxesContents);
}
@Override
protected Widget getAdditionalWidget() {
VerticalPanel panel = new VerticalPanel();
Widget additionalWidget = super.getAdditionalWidget();
if (additionalWidget != null) {
panel.add(additionalWidget);
}
panel.add(entryField);
panel.add(new Label(stringConstants.discardRacesFromHowManyStartedRacesOn()));
HorizontalPanel hp = new HorizontalPanel();
for (int i=0; i<discardThresholdBoxes.length; i++) {
hp.add(new Label(""+(i+1)+"."));
hp.add(discardThresholdBoxes[i]);
}
panel.add(hp);
return panel;
}
@Override
public void show() {
super.show();
entryField.setFocus(true);
}
}
@@ -16,7 +16,7 @@ public class TextfieldEntryDialog extends DataEntryDialog<String> {
public TextfieldEntryDialog(String title, String message, String okButtonName, String cancelButtonName,
String initialValue, final Validator<String> validator, final AsyncCallback<String> callback) {
super(title, message, okButtonName, cancelButtonName, initialValue, validator, callback);
super(title, message, okButtonName, cancelButtonName, validator, callback);
entryField = createTextBox(initialValue);
}
@@ -13,7 +13,7 @@ public class TextfieldEntryDialogWithCheckbox extends DataEntryDialog<Pair<Strin
public TextfieldEntryDialogWithCheckbox(String title, String message, String okButtonName, String cancelButtonName,
String checkboxLabel, String initialValue, Validator<Pair<String, Boolean>> validator, AsyncCallback<Pair<String, Boolean>> callback) {
super(title, message, okButtonName, cancelButtonName, initialValue, validator, callback);
super(title, message, okButtonName, cancelButtonName, validator, callback);
checkbox = createCheckbox(checkboxLabel);
entryField = createTextBox(initialValue);
}
@@ -35,4 +35,8 @@ h1 {
.gwt-TabPanelBottom {
height: 100%;
}
.errorLabel {
color: #FF0000;
}