瀏覽代碼

Add public Field.isEmpty() and clear() (#15354)

Change-Id: I6bda7ff2a66a9ad172c899d855ca868881600be4
tags/7.4.0.beta1
Artur Signell 9 年之前
父節點
當前提交
22b20bc9fc

+ 3
- 16
server/src/com/vaadin/ui/AbstractField.java 查看文件

@@ -1502,25 +1502,12 @@ public abstract class AbstractField<T> extends AbstractComponent implements
markAsDirty();
}

/**
* Is the field empty?
*
* In general, "empty" state is same as null. As an exception, TextField
* also treats empty string as "empty".
*/
protected boolean isEmpty() {
@Override
public boolean isEmpty() {
return (getFieldValue() == null);
}

/**
* Clear the value of the field.
* <p>
* The field value is typically reset to the initial value of the field but
* this is not mandatory. Calling {@link #isEmpty()} on a cleared field must
* always returns true.
*
* @since
*/
@Override
public void clear() {
setValue(null);
}

+ 1
- 1
server/src/com/vaadin/ui/AbstractSelect.java 查看文件

@@ -1782,7 +1782,7 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
* @see AbstractField#isEmpty().
*/
@Override
protected boolean isEmpty() {
public boolean isEmpty() {
if (!multiSelect) {
return super.isEmpty();
} else {

+ 1
- 1
server/src/com/vaadin/ui/AbstractTextField.java 查看文件

@@ -317,7 +317,7 @@ public abstract class AbstractTextField extends AbstractField<String> implements
}

@Override
protected boolean isEmpty() {
public boolean isEmpty() {
return super.isEmpty() || getValue().length() == 0;
}


+ 22
- 0
server/src/com/vaadin/ui/Field.java 查看文件

@@ -113,4 +113,26 @@ public interface Field<T> extends Component, BufferedValidatable, Property<T>,
return (Property) getSource();
}
}

/**
* Is the field empty?
*
* In general, "empty" state is same as null. As an exception, TextField
* also treats empty string as "empty".
*
* @since
* @return true if the field is empty, false otherwise
*/
public boolean isEmpty();

/**
* Clears the value of the field.
* <p>
* The field value is typically reset to the initial value of the field.
* Calling {@link #isEmpty()} on a cleared field must always returns true.
*
* @since
*/
public void clear();

}

+ 7
- 2
server/src/com/vaadin/ui/Form.java 查看文件

@@ -1186,9 +1186,14 @@ public class Form extends AbstractField<Object> implements Item.Editor,
}
}

/** Form is empty if all of its fields are empty. */
/**
* {@inheritDoc}
* <p>
* A Form is empty if all of its fields are empty.
*
*/
@Override
protected boolean isEmpty() {
public boolean isEmpty() {

for (Iterator<Field<?>> i = fields.values().iterator(); i.hasNext();) {
Field<?> f = i.next();

+ 1
- 1
server/src/com/vaadin/ui/RichTextArea.java 查看文件

@@ -285,7 +285,7 @@ public class RichTextArea extends AbstractField<String> implements
}

@Override
protected boolean isEmpty() {
public boolean isEmpty() {
return super.isEmpty() || getValue().length() == 0;
}


+ 71
- 0
server/tests/src/com/vaadin/ui/AbstractSelectTest.java 查看文件

@@ -16,10 +16,13 @@
package com.vaadin.ui;

import java.util.ArrayList;
import java.util.HashSet;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.data.util.ObjectProperty;

public class AbstractSelectTest {

@Test
@@ -73,4 +76,72 @@ public class AbstractSelectTest {
.toArray());

}

@Test
public void singleSelectInitiallyEmpty() {
AbstractSelect s = new ListSelect();
Assert.assertTrue(s.isEmpty());
}

@Test
public void singleSelectEmptyAfterClearUsingPDS() {
AbstractSelect s = new ListSelect();
s.addItem("foo");
s.addItem("bar");
s.setPropertyDataSource(new ObjectProperty<String>("foo"));

Assert.assertFalse(s.isEmpty());
s.clear();
Assert.assertTrue(s.isEmpty());
}

@Test
public void singleSelectEmptyAfterClear() {
AbstractSelect s = new ListSelect();
s.addItem("foo");
s.addItem("bar");
s.setValue("bar");

Assert.assertFalse(s.isEmpty());
s.clear();
Assert.assertTrue(s.isEmpty());
}

@Test
public void multiSelectInitiallyEmpty() {
AbstractSelect s = new ListSelect();
s.setMultiSelect(true);
Assert.assertTrue(s.isEmpty());
}

@Test
public void multiSelectEmptyAfterClearUsingPDS() {
AbstractSelect s = new ListSelect();
s.setMultiSelect(true);
s.addItem("foo");
s.addItem("bar");
HashSet<String> sel = new HashSet<String>();
sel.add("foo");
sel.add("bar");
s.setPropertyDataSource(new ObjectProperty<HashSet>(sel));

Assert.assertFalse(s.isEmpty());
s.clear();
Assert.assertTrue(s.isEmpty());
}

@Test
public void multiSelectEmptyAfterClear() {
AbstractSelect s = new ListSelect();
s.setMultiSelect(true);
s.addItem("foo");
s.addItem("bar");
s.select("foo");
s.select("bar");

Assert.assertFalse(s.isEmpty());
s.clear();
Assert.assertTrue(s.isEmpty());
}

}

+ 47
- 0
server/tests/src/com/vaadin/ui/RichTextAreaTest.java 查看文件

@@ -0,0 +1,47 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.ui;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.data.util.ObjectProperty;

public class RichTextAreaTest {
@Test
public void initiallyEmpty() {
RichTextArea tf = new RichTextArea();
Assert.assertTrue(tf.isEmpty());
}

@Test
public void emptyAfterClearUsingPDS() {
RichTextArea tf = new RichTextArea(new ObjectProperty<String>("foo"));
Assert.assertFalse(tf.isEmpty());
tf.clear();
Assert.assertTrue(tf.isEmpty());
}

@Test
public void emptyAfterClear() {
RichTextArea tf = new RichTextArea();
tf.setValue("foobar");
Assert.assertFalse(tf.isEmpty());
tf.clear();
Assert.assertTrue(tf.isEmpty());
}

}

+ 47
- 0
server/tests/src/com/vaadin/ui/TextAreaTest.java 查看文件

@@ -0,0 +1,47 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.ui;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.data.util.ObjectProperty;

public class TextAreaTest {
@Test
public void initiallyEmpty() {
TextArea tf = new TextArea();
Assert.assertTrue(tf.isEmpty());
}

@Test
public void emptyAfterClearUsingPDS() {
TextArea tf = new TextArea(new ObjectProperty<String>("foo"));
Assert.assertFalse(tf.isEmpty());
tf.clear();
Assert.assertTrue(tf.isEmpty());
}

@Test
public void emptyAfterClear() {
TextArea tf = new TextArea();
tf.setValue("foobar");
Assert.assertFalse(tf.isEmpty());
tf.clear();
Assert.assertTrue(tf.isEmpty());
}

}

Loading…
取消
儲存