summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorPekka Hyvönen <pekka@vaadin.com>2016-10-27 16:13:44 +0300
committerVaadin Code Review <review@vaadin.com>2016-11-01 10:26:33 +0000
commit48c249a13ecca8c4c8bd68814850e5e3cdd37d81 (patch)
treeb1f066b2bdd48b09b38b27bd677abba1b0d5d5fc /server
parent92d697b9886896f1dad41026ba3519563838f93e (diff)
downloadvaadin-framework-48c249a13ecca8c4c8bd68814850e5e3cdd37d81.tar.gz
vaadin-framework-48c249a13ecca8c4c8bd68814850e5e3cdd37d81.zip
Replace setRequired & HasRequired with setRequiredIndicator
Vaadin 7 compatiblity fields still use setRequired via AbstractField (legacy). Public setRequiredIndicator is added to AbstractField, AbstractMultiSelect and AbstractSingleSelect. Internally it is still handled on AbstractComponent & -Connector level. Changes the declarative syntax, required -> required-indicator-visible. Fixes vaadin/framework8-issue#419 Change-Id: I940dc66944d27584bd78e5452aee627ee3abd03a
Diffstat (limited to 'server')
-rw-r--r--server/src/main/java/com/vaadin/data/HasRequired.java48
-rw-r--r--server/src/main/java/com/vaadin/data/HasValue.java20
-rw-r--r--server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java11
-rw-r--r--server/src/main/java/com/vaadin/ui/AbstractComponent.java55
-rw-r--r--server/src/main/java/com/vaadin/ui/AbstractField.java11
-rw-r--r--server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java21
-rw-r--r--server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java14
-rw-r--r--server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPopup.java10
-rw-r--r--server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java10
-rw-r--r--server/src/test/java/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java7
10 files changed, 141 insertions, 66 deletions
diff --git a/server/src/main/java/com/vaadin/data/HasRequired.java b/server/src/main/java/com/vaadin/data/HasRequired.java
deleted file mode 100644
index cca6fcd699..0000000000
--- a/server/src/main/java/com/vaadin/data/HasRequired.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright 2000-2016 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.data;
-
-import com.vaadin.ui.Component;
-
-/**
- * Interface implemented by field which can be marked as required. A required
- * status is handled by the parent layout.
- *
- * @since 8.0
- * @author Vaadin Ltd
- */
-public interface HasRequired extends Component {
-
- /**
- * Sets whether the field is required or not.
- *
- * If the field is required, it is visually indicated in the user interface.
- *
- * @param required
- * <code>true</code> to make the field required,
- * <code>false</code> otherwise
- */
- public void setRequired(boolean required);
-
- /**
- * Checks whether the field is required.
- *
- * @return <code>true</code> if the field is required, <code>false</code>
- * otherwise
- */
- public boolean isRequired();
-
-}
diff --git a/server/src/main/java/com/vaadin/data/HasValue.java b/server/src/main/java/com/vaadin/data/HasValue.java
index aa130344b2..e02297b7f4 100644
--- a/server/src/main/java/com/vaadin/data/HasValue.java
+++ b/server/src/main/java/com/vaadin/data/HasValue.java
@@ -94,7 +94,7 @@ public interface HasValue<V> extends Serializable {
* This a shorthand method for {@link HasValue#getValue()} for the event
* source {@link #getSource()}. Thus the value is always the most recent
* one, even if has been changed after the firing of this event.
- *
+ *
* @see HasValue#getValue()
*
* @return the new value
@@ -217,4 +217,22 @@ public interface HasValue<V> extends Serializable {
public default boolean isEmpty() {
return Objects.equals(getValue(), getEmptyValue());
}
+
+ /**
+ * Sets the required indicator visible or not.
+ * <p>
+ * If set visible, it is visually indicated in the user interface.
+ *
+ * @param requiredIndicatorVisible
+ * <code>true</code> to make the required indicator visible,
+ * <code>false</code> if not
+ */
+ public void setRequiredIndicatorVisible(boolean requiredIndicatorVisible);
+
+ /**
+ * Checks whether the required indicator is visible.
+ *
+ * @return <code>true</code> if visible, <code>false</code> if not
+ */
+ public boolean isRequiredIndicatorVisible();
}
diff --git a/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java b/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java
index 99532c34d1..b033538949 100644
--- a/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java
+++ b/server/src/main/java/com/vaadin/data/validator/NotEmptyValidator.java
@@ -17,7 +17,7 @@ package com.vaadin.data.validator;
import java.util.Objects;
-import com.vaadin.data.HasRequired;
+import com.vaadin.data.HasValue;
import com.vaadin.data.Result;
import com.vaadin.data.Validator;
import com.vaadin.data.util.converter.ValueContext;
@@ -34,21 +34,22 @@ import com.vaadin.data.util.converter.ValueContext;
* <p>
* If the field is required, it is visually indicated in the user interface.
* Furthermore, required fields requires "non-empty" validator. So in addition
- * to call {@link HasRequired#setRequired(boolean)} method one should add an
- * instance of this validator explicitly so the code looks like this:
+ * to call {@link HasRequired#setRequiredIndicatorVisible(boolean)} method one
+ * should add an instance of this validator explicitly so the code looks like
+ * this:
*
* <pre>
* <code>
* Binder<Bean,String, String> binder = new Binder<>();
* TextField name = new TextField();
- * name.setRequired(true);
+ * name.setRequiredIndicatorVisible(true);
* binder.forField(name).withValidator(
* new NonEmptyValidator("Name cannot be empty"))
* .bind(Bean::getName, Bean::setName);
* </code>
* </pre>
*
- * @see HasRequired
+ * @see HasValue#setRequiredIndicatorVisible(boolean)
* @author Vaadin Ltd
* @since 8.0
*
diff --git a/server/src/main/java/com/vaadin/ui/AbstractComponent.java b/server/src/main/java/com/vaadin/ui/AbstractComponent.java
index 6cbf745851..8711b601e6 100644
--- a/server/src/main/java/com/vaadin/ui/AbstractComponent.java
+++ b/server/src/main/java/com/vaadin/ui/AbstractComponent.java
@@ -60,6 +60,7 @@ import com.vaadin.shared.EventId;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.Registration;
import com.vaadin.shared.ui.ComponentStateUtil;
+import com.vaadin.shared.ui.RequiredIndicatorState;
import com.vaadin.shared.util.SharedUtil;
import com.vaadin.ui.declarative.DesignAttributeHandler;
import com.vaadin.ui.declarative.DesignContext;
@@ -1045,7 +1046,7 @@ public abstract class AbstractComponent extends AbstractClientConnector
}
/**
- * Returns true if the component is responsive
+ * Returns true if the component is responsive.
*
* @since 7.5.0
* @return true if the component is responsive
@@ -1370,6 +1371,58 @@ public abstract class AbstractComponent extends AbstractClientConnector
listener);
}
+ /**
+ * Sets the visibility of the required indicator. <strong>NOTE: Does not
+ * apply for all components!</strong>.
+ * <p>
+ * If the component supports the required indicator (state extends
+ * {@link RequiredIndicatorState}), then expose this method and
+ * {@link #isRequiredIndicatorVisible()} as {@code public} in the component
+ * and call this method.
+ * <p>
+ * This method will throw a {@link IllegalStateException} if the component
+ * state (returned by {@link #getState()}) does not inherit
+ * {@link RequiredIndicatorState}.
+ *
+ * @param visible
+ * <code>true</code> to make the required indicator visible,
+ * <code>false</code> if not
+ */
+ protected void setRequiredIndicatorVisible(boolean visible) {
+ if (getState(false) instanceof RequiredIndicatorState) {
+ ((RequiredIndicatorState) getState()).required = visible;
+ } else {
+ throw new IllegalStateException(
+ "This component does not support the required indicator, since state is of type "
+ + getStateType().getSimpleName()
+ + " and does not inherit "
+ + RequiredIndicatorState.class.getSimpleName());
+ }
+ }
+
+ /**
+ * Checks whether the required indicator is visible or not. <strong>NOTE:
+ * Does not apply for all components!</strong>.
+ * <p>
+ * This method will throw a {@link IllegalStateException} if the component
+ * state (returned by {@link #getState()}) does not inherit
+ * {@link RequiredIndicatorState}.
+ *
+ * @return <code>true</code> if visible, <code>false</code> if not
+ * @see #setRequiredIndicatorVisible(boolean)
+ */
+ protected boolean isRequiredIndicatorVisible() {
+ if (getState(false) instanceof RequiredIndicatorState) {
+ return ((RequiredIndicatorState) getState(false)).required;
+ } else {
+ throw new IllegalStateException(
+ "This component does not support the required indicator, since state is of type "
+ + getStateType().getSimpleName()
+ + " and does not inherit "
+ + RequiredIndicatorState.class.getSimpleName());
+ }
+ }
+
private static final Logger getLogger() {
return Logger.getLogger(AbstractComponent.class.getName());
}
diff --git a/server/src/main/java/com/vaadin/ui/AbstractField.java b/server/src/main/java/com/vaadin/ui/AbstractField.java
index 4add30183e..065825b5ad 100644
--- a/server/src/main/java/com/vaadin/ui/AbstractField.java
+++ b/server/src/main/java/com/vaadin/ui/AbstractField.java
@@ -23,7 +23,6 @@ import java.util.Objects;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;
-import com.vaadin.data.HasRequired;
import com.vaadin.data.HasValue;
import com.vaadin.shared.AbstractFieldState;
import com.vaadin.shared.Registration;
@@ -49,7 +48,7 @@ import com.vaadin.util.ReflectTools;
* the input value type
*/
public abstract class AbstractField<T> extends AbstractComponent
- implements HasValue<T>, HasRequired, Focusable {
+ implements HasValue<T>, Focusable {
@Deprecated
private static final Method VALUE_CHANGE_METHOD = ReflectTools.findMethod(
@@ -214,12 +213,12 @@ public abstract class AbstractField<T> extends AbstractComponent
}
@Override
- public void setRequired(boolean required) {
- getState().required = required;
+ public void setRequiredIndicatorVisible(boolean visible) {
+ super.setRequiredIndicatorVisible(visible);
}
@Override
- public boolean isRequired() {
- return getState(false).required;
+ public boolean isRequiredIndicatorVisible() {
+ return super.isRequiredIndicatorVisible();
}
}
diff --git a/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java b/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java
index 5ab6951f8d..99c1682536 100644
--- a/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java
+++ b/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java
@@ -37,6 +37,7 @@ import com.vaadin.shared.data.selection.MultiSelectServerRpc;
import com.vaadin.shared.data.selection.SelectionModel;
import com.vaadin.shared.data.selection.SelectionModel.Multi;
import com.vaadin.shared.ui.ListingJsonConstants;
+import com.vaadin.shared.ui.RequiredIndicatorState;
import com.vaadin.util.ReflectTools;
import elemental.json.JsonObject;
@@ -472,4 +473,24 @@ public abstract class AbstractMultiSelect<T>
this.itemEnabledProvider = itemEnabledProvider;
}
+ @Override
+ public void setRequiredIndicatorVisible(boolean visible) {
+ super.setRequiredIndicatorVisible(visible);
+ }
+
+ @Override
+ public boolean isRequiredIndicatorVisible() {
+ return super.isRequiredIndicatorVisible();
+ }
+
+ @Override
+ protected RequiredIndicatorState getState() {
+ return (RequiredIndicatorState) super.getState();
+ }
+
+ @Override
+ protected RequiredIndicatorState getState(boolean markAsDirty) {
+ return (RequiredIndicatorState) super.getState(markAsDirty);
+ };
+
}
diff --git a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java
index c4405f06b1..272acf3b09 100644
--- a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java
+++ b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java
@@ -306,7 +306,7 @@ public abstract class AbstractSingleSelect<T> extends
* The call is delegated to {@link #getSelectedItem()}
*
* @return the current selection, may be {@code null}
- *
+ *
* @see #getSelectedItem()
* @see Single#getSelectedItem
*/
@@ -321,7 +321,7 @@ public abstract class AbstractSingleSelect<T> extends
* value is {@code null} then it deselects currently selected item.
* <p>
* The call is delegated to {@link #setSelectedItem(Object)}.
- *
+ *
* @see #setSelectedItem(Object)
* @see Single#setSelectedItem(Object)
*
@@ -349,4 +349,14 @@ public abstract class AbstractSingleSelect<T> extends
protected AbstractSingleSelectState getState(boolean markAsDirty) {
return (AbstractSingleSelectState) super.getState(markAsDirty);
}
+
+ @Override
+ public void setRequiredIndicatorVisible(boolean visible) {
+ super.setRequiredIndicatorVisible(visible);
+ }
+
+ @Override
+ public boolean isRequiredIndicatorVisible() {
+ return super.isRequiredIndicatorVisible();
+ }
}
diff --git a/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPopup.java b/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPopup.java
index ba00773fec..f474e51e99 100644
--- a/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPopup.java
+++ b/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPopup.java
@@ -710,6 +710,16 @@ public class ColorPickerPopup extends Window implements HasValue<Color> {
}
};
+ @Override
+ public void setRequiredIndicatorVisible(boolean visible) {
+ super.setRequiredIndicatorVisible(visible);
+ }
+
+ @Override
+ public boolean isRequiredIndicatorVisible() {
+ return super.isRequiredIndicatorVisible();
+ }
+
private static Logger getLogger() {
return Logger.getLogger(ColorPickerPopup.class.getName());
}
diff --git a/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java b/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java
index 67724d134b..27e5690e3c 100644
--- a/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java
+++ b/server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java
@@ -185,4 +185,14 @@ public class ColorPickerPreview extends CssLayout implements HasValue<Color> {
protected String getCss(Component c) {
return "background: " + color.getCSS();
}
+
+ @Override
+ public void setRequiredIndicatorVisible(boolean visible) {
+ super.setRequiredIndicatorVisible(visible);
+ }
+
+ @Override
+ public boolean isRequiredIndicatorVisible() {
+ return super.isRequiredIndicatorVisible();
+ }
}
diff --git a/server/src/test/java/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java b/server/src/test/java/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java
index df7b64b671..e22905c9f3 100644
--- a/server/src/test/java/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java
+++ b/server/src/test/java/com/vaadin/tests/server/component/abstractfield/AbstractFieldDeclarativeTest.java
@@ -28,7 +28,7 @@ import com.vaadin.ui.AbstractField;
* declarative test for a real component should extend it and implement abstract
* methods to be able to test the common properties. Components specific
* properties should be tested additionally in the subclasses implementations.
- *
+ *
* @author Vaadin Ltd
*
*/
@@ -39,10 +39,11 @@ public abstract class AbstractFieldDeclarativeTest<T extends AbstractField<V>, V
public void requiredDeserialization()
throws InstantiationException, IllegalAccessException {
boolean isRequired = true;
- String design = String.format("<%s required/>", getComponentTag());
+ String design = String.format("<%s required-indicator-visible/>",
+ getComponentTag());
T component = getComponentClass().newInstance();
- component.setRequired(isRequired);
+ component.setRequiredIndicatorVisible(isRequired);
testRead(design, component);
testWrite(design, component);
}