Browse Source

Makes it possible to change save/cancel captions in Grid editor (#16551)

Change-Id: I8cf8ad7d071f531091735dc411b85db08ad6652b
grid
Henrik Paul 9 years ago
parent
commit
aa5a9d1493

+ 107
- 23
client/src/com/vaadin/client/widgets/Grid.java View File

@@ -966,8 +966,8 @@ public class Grid<T> extends ResizeComposite implements

private HandlerRegistration scrollHandler;

private Button saveButton;
private Button cancelButton;
private final Button saveButton;
private final Button cancelButton;

private static final int SAVE_TIMEOUT_MS = 5000;
private final Timer saveTimeout = new Timer() {
@@ -1048,6 +1048,28 @@ public class Grid<T> extends ResizeComposite implements
}
};

public Editor() {
saveButton = new Button();
saveButton.setText(GridConstants.DEFAULT_SAVE_CAPTION);
saveButton.setStylePrimaryName("v-nativebutton");
saveButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
save();
}
});

cancelButton = new Button();
cancelButton.setText(GridConstants.DEFAULT_CANCEL_CAPTION);
cancelButton.setStylePrimaryName("v-nativebutton");
cancelButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
cancel();
}
});
}

public int getRow() {
return rowIndex;
}
@@ -1292,28 +1314,7 @@ public class Grid<T> extends ResizeComposite implements
}
}

saveButton = new Button();
saveButton.setText("Save");
saveButton.setStylePrimaryName("v-nativebutton");
saveButton.addStyleName(styleName + "-save");
saveButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
save();
}
});
attachWidget(saveButton, editorOverlay);

cancelButton = new Button();
cancelButton.setText("Cancel");
cancelButton.setStylePrimaryName("v-nativebutton");
cancelButton.addStyleName(styleName + "-cancel");
cancelButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
cancel();
}
});
attachWidget(cancelButton, editorOverlay);

/*
@@ -1357,6 +1358,9 @@ public class Grid<T> extends ResizeComposite implements
}
columnToWidget.clear();

detachWidget(saveButton);
detachWidget(cancelButton);

editorOverlay.removeAllChildren();
editorOverlay.removeFromParent();

@@ -1366,9 +1370,14 @@ public class Grid<T> extends ResizeComposite implements
protected void setStylePrimaryName(String primaryName) {
if (styleName != null) {
editorOverlay.removeClassName(styleName);
saveButton.removeStyleName(styleName + "-save");
cancelButton.removeStyleName(styleName + "-cancel");
}
styleName = primaryName + "-editor";
editorOverlay.addClassName(styleName);

saveButton.addStyleName(styleName + "-save");
cancelButton.addStyleName(styleName + "-cancel");
}

/**
@@ -1396,6 +1405,11 @@ public class Grid<T> extends ResizeComposite implements
setParent(w, grid);
}

private void detachWidget(Widget w) {
setParent(w, null);
w.getElement().removeFromParent();
}

private static void setBounds(Element e, double left, double top,
double width, double height) {
Style style = e.getStyle();
@@ -1419,6 +1433,32 @@ public class Grid<T> extends ResizeComposite implements
saveButton.setEnabled(enabled);
cancelButton.setEnabled(enabled);
}

public void setSaveCaption(String saveCaption)
throws IllegalArgumentException {
if (saveCaption == null) {
throw new IllegalArgumentException(
"Save caption cannot be null");
}
saveButton.setText(saveCaption);
}

public String getSaveCaption() {
return saveButton.getText();
}

public void setCancelCaption(String cancelCaption)
throws IllegalArgumentException {
if (cancelCaption == null) {
throw new IllegalArgumentException(
"Cancel caption cannot be null");
}
cancelButton.setText(cancelCaption);
}

public String getCancelCaption() {
return cancelButton.getText();
}
}

public static abstract class AbstractGridKeyEvent<HANDLER extends AbstractGridKeyEventHandler>
@@ -5793,6 +5833,50 @@ public class Grid<T> extends ResizeComposite implements
return editor.getWidget(column);
}

/**
* Sets the caption on the save button in the Grid editor.
*
* @param saveCaption
* the caption to set
* @throws IllegalArgumentException
* if {@code saveCaption} is {@code null}
*/
public void setEditorSaveCaption(String saveCaption)
throws IllegalArgumentException {
editor.setSaveCaption(saveCaption);
}

/**
* Gets the current caption on the save button in the Grid editor.
*
* @return the current caption on the save button
*/
public String getEditorSaveCaption() {
return editor.getSaveCaption();
}

/**
* Sets the caption on the cancel button in the Grid editor.
*
* @param cancelCaption
* the caption to set
* @throws IllegalArgumentException
* if {@code cancelCaption} is {@code null}
*/
public void setEditorCancelCaption(String cancelCaption)
throws IllegalArgumentException {
editor.setCancelCaption(cancelCaption);
}

/**
* Gets the caption on the cancel button in the Grid editor.
*
* @return the current caption on the cancel button
*/
public String getEditorCancelCaption() {
return editor.getCancelCaption();
}

@Override
protected void onAttach() {
super.onAttach();

+ 50
- 0
server/src/com/vaadin/ui/Grid.java View File

@@ -4664,6 +4664,56 @@ public class Grid extends AbstractComponent implements SelectionNotifier,
return editorFieldGroup.getFieldFactory();
}

/**
* Sets the caption on the save button in the Grid editor.
*
* @param saveCaption
* the caption to set
* @throws IllegalArgumentException
* if {@code saveCaption} is {@code null}
*/
public void setEditorSaveCaption(String saveCaption)
throws IllegalArgumentException {
if (saveCaption == null) {
throw new IllegalArgumentException("Save caption cannot be null");
}
getState().editorSaveCaption = saveCaption;
}

/**
* Gets the current caption of the save button in the Grid editor.
*
* @return the current caption of the save button
*/
public String getEditorSaveCaption() {
return getState(false).editorSaveCaption;
}

/**
* Sets the caption on the cancel button in the Grid editor.
*
* @param cancelCaption
* the caption to set
* @throws IllegalArgumentException
* if {@code cancelCaption} is {@code null}
*/
public void setEditorCancelCaption(String cancelCaption)
throws IllegalArgumentException {
if (cancelCaption == null) {
throw new IllegalArgumentException("Cancel caption cannot be null");
}
getState().editorCancelCaption = cancelCaption;
}

/**
* Gets the current caption of the cancel button in the Grid editor.
*
* @return the current caption of the cancel button
*/
public String getCancelCaption() {
return getState(false).editorCancelCaption;
}

@Override
public void addItemClickListener(ItemClickListener listener) {
addListener(GridConstants.ITEM_CLICK_EVENT_ID, ItemClickEvent.class,

+ 6
- 0
shared/src/com/vaadin/shared/ui/grid/GridConstants.java View File

@@ -68,4 +68,10 @@ public final class GridConstants implements Serializable {
* Event ID for item click events
*/
public static final String ITEM_CLICK_EVENT_ID = "itemClick";

/** The default save button caption in the editor */
public static final String DEFAULT_SAVE_CAPTION = "Save";

/** The default cancel button caption in the editor */
public static final String DEFAULT_CANCEL_CAPTION = "Cancel";
}

+ 7
- 0
shared/src/com/vaadin/shared/ui/grid/GridState.java View File

@@ -146,4 +146,11 @@ public class GridState extends AbstractComponentState {
/** Whether row data might contain generated cell styles */
public boolean hasCellStyleGenerator;

/** The caption for the save button in the editor */
@DelegateToWidget
public String editorSaveCaption = GridConstants.DEFAULT_SAVE_CAPTION;

/** The caption for the cancel button in the editor */
@DelegateToWidget
public String editorCancelCaption = GridConstants.DEFAULT_CANCEL_CAPTION;
}

+ 16
- 0
uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java View File

@@ -960,6 +960,22 @@ public class GridBasicFeatures extends AbstractComponentTest<Grid> {
c.cancelEditor();
}
}, null);

createClickAction("Change save caption", "Editor",
new Command<Grid, String>() {
@Override
public void execute(Grid c, String value, Object data) {
c.setEditorSaveCaption("ǝʌɐS");
}
}, null);

createClickAction("Change cancel caption", "Editor",
new Command<Grid, String>() {
@Override
public void execute(Grid c, String value, Object data) {
c.setEditorCancelCaption("ʃǝɔuɐↃ");
}
}, null);
}

@SuppressWarnings("boxing")

+ 49
- 7
uitest/src/com/vaadin/tests/components/grid/basicfeatures/client/GridEditorClientTest.java View File

@@ -17,6 +17,7 @@ package com.vaadin.tests.components.grid.basicfeatures.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -31,11 +32,17 @@ import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import com.vaadin.shared.ui.grid.GridConstants;
import com.vaadin.tests.components.grid.basicfeatures.GridBasicClientFeaturesTest;
import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeatures;

public class GridEditorClientTest extends GridBasicClientFeaturesTest {

private static final String[] EDIT_ROW_100 = new String[] { "Component",
"Editor", "Edit row 100" };
private static final String[] EDIT_ROW_5 = new String[] { "Component",
"Editor", "Edit row 5" };

@Before
public void setUp() {
openTestURL();
@@ -44,7 +51,7 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest {

@Test
public void testProgrammaticOpeningClosing() {
selectMenuPath("Component", "Editor", "Edit row 5");
selectMenuPath(EDIT_ROW_5);
assertNotNull(getEditor());

selectMenuPath("Component", "Editor", "Cancel edit");
@@ -55,13 +62,13 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest {

@Test
public void testProgrammaticOpeningWithScroll() {
selectMenuPath("Component", "Editor", "Edit row 100");
selectMenuPath(EDIT_ROW_100);
assertNotNull(getEditor());
}

@Test(expected = NoSuchElementException.class)
public void testVerticalScrollLocking() {
selectMenuPath("Component", "Editor", "Edit row 5");
selectMenuPath(EDIT_ROW_5);
getGridElement().getCell(200, 0);
}

@@ -89,7 +96,7 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest {

@Test
public void testWidgetBinding() throws Exception {
selectMenuPath("Component", "Editor", "Edit row 100");
selectMenuPath(EDIT_ROW_100);
WebElement editor = getEditor();

List<WebElement> widgets = editor.findElements(By
@@ -108,7 +115,7 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest {
@Test
public void testWithSelectionColumn() throws Exception {
selectMenuPath("Component", "State", "Selection mode", "multi");
selectMenuPath("Component", "State", "Editor", "Edit row 5");
selectMenuPath(EDIT_ROW_5);

WebElement editor = getEditor();
List<WebElement> selectorDivs = editor.findElements(By
@@ -122,7 +129,7 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest {

@Test
public void testSave() {
selectMenuPath("Component", "Editor", "Edit row 100");
selectMenuPath(EDIT_ROW_100);

WebElement textField = getEditor().findElements(
By.className("gwt-TextBox")).get(0);
@@ -140,7 +147,7 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest {

@Test
public void testProgrammaticSave() {
selectMenuPath("Component", "Editor", "Edit row 100");
selectMenuPath(EDIT_ROW_100);

WebElement textField = getEditor().findElements(
By.className("gwt-TextBox")).get(0);
@@ -152,4 +159,39 @@ public class GridEditorClientTest extends GridBasicClientFeaturesTest {

assertEquals("Changed", getGridElement().getCell(100, 0).getText());
}

@Test
public void testCaptionChange() {
selectMenuPath(EDIT_ROW_5);
assertEquals("Save button caption should've been \""
+ GridConstants.DEFAULT_SAVE_CAPTION + "\" to begin with",
GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
assertEquals("Cancel button caption should've been \""
+ GridConstants.DEFAULT_CANCEL_CAPTION + "\" to begin with",
GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton()
.getText());

selectMenuPath("Component", "Editor", "Change Save Caption");
assertNotEquals(
"Save button caption should've changed while editor is open",
GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());

getCancelButton().click();

selectMenuPath("Component", "Editor", "Change Cancel Caption");
selectMenuPath(EDIT_ROW_5);
assertNotEquals(
"Cancel button caption should've changed while editor is closed",
GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton()
.getText());
}

protected WebElement getSaveButton() {
return getEditor().findElement(By.className("v-grid-editor-save"));
}

protected WebElement getCancelButton() {
return getEditor().findElement(By.className("v-grid-editor-cancel"));
}

}

+ 36
- 0
uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorTest.java View File

@@ -17,6 +17,7 @@ package com.vaadin.tests.components.grid.basicfeatures.server;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -31,6 +32,7 @@ import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import com.vaadin.shared.ui.grid.GridConstants;
import com.vaadin.testbench.elements.GridElement.GridCellElement;
import com.vaadin.testbench.elements.GridElement.GridEditorElement;
import com.vaadin.testbench.elements.NotificationElement;
@@ -160,6 +162,32 @@ public class GridEditorTest extends GridBasicFeaturesTest {
.getText());
}

@Test
public void testCaptionChange() {
selectMenuPath(EDIT_ITEM_5);
assertEquals("Save button caption should've been \""
+ GridConstants.DEFAULT_SAVE_CAPTION + "\" to begin with",
GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());
assertEquals("Cancel button caption should've been \""
+ GridConstants.DEFAULT_CANCEL_CAPTION + "\" to begin with",
GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton()
.getText());

selectMenuPath("Component", "Editor", "Change save caption");
assertNotEquals(
"Save button caption should've changed while editor is open",
GridConstants.DEFAULT_SAVE_CAPTION, getSaveButton().getText());

getCancelButton().click();

selectMenuPath("Component", "Editor", "Change cancel caption");
selectMenuPath(EDIT_ITEM_5);
assertNotEquals(
"Cancel button caption should've changed while editor is closed",
GridConstants.DEFAULT_CANCEL_CAPTION, getCancelButton()
.getText());
}

private void assertEditorOpen() {
assertNotNull("Editor is supposed to be open", getEditor());
assertEquals("Unexpected number of widgets", GridBasicFeatures.COLUMNS,
@@ -256,4 +284,12 @@ public class GridEditorTest extends GridBasicFeaturesTest {
assertEquals("Grid shouldn't scroll vertically while editing",
originalScrollPos, getGridVerticalScrollPos());
}

private WebElement getSaveButton() {
return getDriver().findElement(By.className("v-grid-editor-save"));
}

private WebElement getCancelButton() {
return getDriver().findElement(By.className("v-grid-editor-cancel"));
}
}

+ 14
- 0
uitest/src/com/vaadin/tests/widgetset/client/grid/GridBasicClientFeaturesWidget.java View File

@@ -956,6 +956,20 @@ public class GridBasicClientFeaturesWidget extends
}
}, "Component", "Editor");

addMenuCommand("Change Save Caption", new ScheduledCommand() {
@Override
public void execute() {
grid.setEditorSaveCaption("ǝʌɐS");
}
}, "Component", "Editor");

addMenuCommand("Change Cancel Caption", new ScheduledCommand() {
@Override
public void execute() {
grid.setEditorCancelCaption("ʃǝɔuɐↃ");
}
}, "Component", "Editor");

}

private void configureFooterRow(final FooterRow row) {

Loading…
Cancel
Save