import com.vaadin.data.Validator.InvalidValueException;
import com.vaadin.data.fieldgroup.DefaultFieldGroupFieldFactory;
import com.vaadin.data.fieldgroup.FieldGroup;
+import com.vaadin.data.fieldgroup.FieldGroup.BindException;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.fieldgroup.FieldGroupFieldFactory;
import com.vaadin.data.sort.Sort;
/**
* Sets the field component used to edit the properties in this column
- * when the item editor is active. Please refer to
- * {@link Grid#setEditorField(Object, Field)} for more information.
+ * when the item editor is active. If an item has not been set, then the
+ * binding is postponed until the item is set using
+ * {@link #editItem(Object)}.
+ * <p>
+ * Setting the field to <code>null</code> clears any previously set
+ * field, causing a new field to be created the next time the item
+ * editor is opened.
*
* @param editor
- * the editor field, cannot be null
+ * the editor field
* @return this column
*/
public Column setEditorField(Field<?> editor) {
/**
* Returns the editor field used to edit the properties in this column
- * when the item editor is active. Please refer to
- * {@link Grid#getEditorField(Object)} for more information.
+ * when the item editor is active. Returns null if the column is not
+ * {@link Column#isEditable() editable}.
+ * <p>
+ * When {@link #editItem(Object) editItem} is called, fields are
+ * automatically created and bound for any unbound properties.
+ * <p>
+ * Getting a field before the editor has been opened depends on special
+ * support from the {@link FieldGroup} in use. Using this method with a
+ * user-provided <code>FieldGroup</code> might cause
+ * {@link BindException} to be thrown.
*
- * @return the editor field or null if this column is not editable
+ * @return the bound field; or <code>null</code> if the respective
+ * column is not editable
+ *
+ * @throws IllegalArgumentException
+ * if there is no column for the provided property id
+ * @throws BindException
+ * if no field has been configured and there is a problem
+ * building or binding
*/
public Field<?> getEditorField() {
return grid.getEditorField(getPropertyId());
}
}
- /**
- * Gets the field component that represents a property in the item editor.
- * Returns null if the corresponding column is not
- * {@link Column#isEditable() editable}.
- * <p>
- * When {@link #editItem(Object) editItem} is called, fields are
- * automatically created and bound for any unbound properties.
- * <p>
- * Getting a field before the editor has been opened depends on special
- * support from the {@link FieldGroup} in use. Using this method with a
- * user-provided <code>FieldGroup</code> might cause {@link BindException}
- * to be thrown.
- *
- * @param propertyId
- * the property id of the property for which to find the field
- * @return the bound field or null if the respective column is not editable
- *
- * @throws IllegalArgumentException
- * if there is no column for the provided property id
- * @throws BindException
- * if no field has been configured and there is a problem
- * building or binding
- */
- public Field<?> getEditorField(Object propertyId) {
+ private Field<?> getEditorField(Object propertyId) {
checkColumnExists(propertyId);
if (!getColumn(propertyId).isEditable()) {
}
}
- /**
- * Binds the field to the given propertyId. If an item has not been set,
- * then the binding is postponed until the item is set using
- * {@link #editItem(Object)}.
- * <p>
- * Setting the field to <code>null</code> clears any previously set field,
- * causing a new field to be created the next time the item editor is
- * opened.
- *
- * @param field
- * The field to bind
- * @param propertyId
- * The propertyId to bind the field to
- */
- public void setEditorField(Object propertyId, Field<?> field) {
+ private void setEditorField(Object propertyId, Field<?> field) {
checkColumnExists(propertyId);
Field<?> oldField = editorFieldGroup.getField(propertyId);
@Test
public void testPropertyAndColumnEditorFieldsMatch() {
- grid.setEditorField("column1", new TextField());
- assertSame(grid.getEditorField("column1"), grid.getColumn("column1")
+ Column column1 = grid.getColumn("column1");
+ column1.setEditorField(new TextField());
+ assertSame(column1.getEditorField(), grid.getColumn("column1")
.getEditorField());
- grid.getColumn("column2").setEditorField(new TextField());
- assertSame(grid.getEditorField("column2"), grid.getColumn("column2")
- .getEditorField());
+ Column column2 = grid.getColumn("column2");
+ column2.setEditorField(new TextField());
+ assertSame(column2.getEditorField(), column2.getEditorField());
}
@Test
assertEquals(getEditedItem(), grid.getEditorFieldGroup()
.getItemDataSource());
- assertEquals(DEFAULT_NAME, grid.getEditorField(PROPERTY_NAME)
- .getValue());
- assertEquals(String.valueOf(DEFAULT_AGE),
- grid.getEditorField(PROPERTY_AGE).getValue());
+ assertEquals(DEFAULT_NAME, grid.getColumn(PROPERTY_NAME)
+ .getEditorField().getValue());
+ assertEquals(String.valueOf(DEFAULT_AGE), grid.getColumn(PROPERTY_AGE)
+ .getEditorField().getValue());
}
@Test
public void testSaveEditor() throws Exception {
startEdit();
- TextField field = (TextField) grid.getEditorField(PROPERTY_NAME);
+ TextField field = (TextField) grid.getColumn(PROPERTY_NAME)
+ .getEditorField();
field.setValue("New Name");
assertEquals(DEFAULT_NAME, field.getPropertyDataSource().getValue());
public void testSaveEditorCommitFail() throws Exception {
startEdit();
- ((TextField) grid.getEditorField(PROPERTY_AGE)).setValue("Invalid");
+ ((TextField) grid.getColumn(PROPERTY_AGE).getEditorField())
+ .setValue("Invalid");
try {
// Manual fail instead of @Test(expected=...) to check it is
// saveEditor that fails and not setValue
@Test
public void testCancelEditor() throws Exception {
startEdit();
- TextField field = (TextField) grid.getEditorField(PROPERTY_NAME);
+ TextField field = (TextField) grid.getColumn(PROPERTY_NAME)
+ .getEditorField();
field.setValue("New Name");
grid.cancelEditor();
public void testGetField() throws Exception {
startEdit();
- assertNotNull(grid.getEditorField(PROPERTY_NAME));
+ assertNotNull(grid.getColumn(PROPERTY_NAME).getEditorField());
}
@Test
public void testGetFieldWithoutItem() throws Exception {
grid.setEditorEnabled(true);
- assertNotNull(grid.getEditorField(PROPERTY_NAME));
+ assertNotNull(grid.getColumn(PROPERTY_NAME).getEditorField());
}
@Test
public void testCustomBinding() {
TextField textField = new TextField();
- grid.setEditorField(PROPERTY_NAME, textField);
+ grid.getColumn(PROPERTY_NAME).setEditorField(textField);
startEdit();
- assertSame(textField, grid.getEditorField(PROPERTY_NAME));
+ assertSame(textField, grid.getColumn(PROPERTY_NAME).getEditorField());
}
@Test(expected = IllegalStateException.class)
public void testFieldIsNotReadonly() {
startEdit();
- Field<?> field = grid.getEditorField(PROPERTY_NAME);
+ Field<?> field = grid.getColumn(PROPERTY_NAME).getEditorField();
assertFalse(field.isReadOnly());
}
startEdit();
grid.getEditorFieldGroup().setReadOnly(true);
- Field<?> field = grid.getEditorField(PROPERTY_NAME);
+ Field<?> field = grid.getColumn(PROPERTY_NAME).getEditorField();
assertTrue(field.isReadOnly());
}
@Test
public void testColumnRemoved() {
- Field<?> field = grid.getEditorField(PROPERTY_NAME);
+ Field<?> field = grid.getColumn(PROPERTY_NAME).getEditorField();
assertSame("field should be attached to ", grid, field.getParent());
@Test
public void testSetFieldAgain() {
TextField field = new TextField();
- grid.setEditorField(PROPERTY_NAME, field);
+ grid.getColumn(PROPERTY_NAME).setEditorField(field);
field = new TextField();
- grid.setEditorField(PROPERTY_NAME, field);
+ grid.getColumn(PROPERTY_NAME).setEditorField(field);
assertSame("new field should be used.", field,
- grid.getEditorField(PROPERTY_NAME));
+ grid.getColumn(PROPERTY_NAME).getEditorField());
}
private void startEdit() {
grid.setEditorEnabled(true);
- grid.setEditorField("firstName", new PasswordField());
+ grid.getColumn("firstName").setEditorField(new PasswordField());
- TextField lastNameField = (TextField) grid
- .getEditorField("lastName");
+ TextField lastNameField = (TextField) grid.getColumn("lastName")
+ .getEditorField();
lastNameField.setMaxLength(50);
- grid.getEditorField("phoneNumber").setReadOnly(true);
+ grid.getColumn("phoneNumber").getEditorField().setReadOnly(true);
addComponent(grid);
}
grid.setSelectionMode(SelectionMode.NONE);
- grid.getEditorField(getColumnProperty(2)).setReadOnly(true);
+ grid.getColumn(getColumnProperty(2)).getEditorField().setReadOnly(true);
grid.getColumn(getColumnProperty(3)).setEditable(false);
createGridActions();
});
grid.setEditorEnabled(true);
grid.setSizeFull();
- grid.getEditorField("age").addValidator(
- new IntegerRangeValidator("Must be between 0 and 100", 0, 100));
+ grid.getColumn("age")
+ .getEditorField()
+ .addValidator(
+ new IntegerRangeValidator("Must be between 0 and 100",
+ 0, 100));
addComponent(grid);
getLayout().setExpandRatio(grid, 1);
}