Package com.storedobject.vaadin.util
Class NumericField<T extends Number>
java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.AbstractField<com.vaadin.flow.component.customfield.CustomField<T>,T>
com.vaadin.flow.component.customfield.CustomField<T>
com.storedobject.vaadin.CustomField<T>
com.storedobject.vaadin.CustomTextField<T>
com.storedobject.vaadin.util.NumericField<T>
- Type Parameters:
T
- The numeric type handled by the field, extendsNumber
.
- All Implemented Interfaces:
DisablePaste
,HasStyle
,com.vaadin.flow.component.AttachNotifier
,com.vaadin.flow.component.BlurNotifier<com.vaadin.flow.component.customfield.CustomField<T>>
,com.vaadin.flow.component.DetachNotifier
,com.vaadin.flow.component.Focusable<com.vaadin.flow.component.customfield.CustomField<T>>
,com.vaadin.flow.component.FocusNotifier<com.vaadin.flow.component.customfield.CustomField<T>>
,com.vaadin.flow.component.HasElement
,com.vaadin.flow.component.HasEnabled
,com.vaadin.flow.component.HasHelper
,com.vaadin.flow.component.HasLabel
,com.vaadin.flow.component.HasSize
,com.vaadin.flow.component.HasStyle
,com.vaadin.flow.component.HasTheme
,com.vaadin.flow.component.HasValidation
,com.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<T>,
,T>, T> com.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<T>,
,T>, T> com.vaadin.flow.component.shared.HasPrefix
,com.vaadin.flow.component.shared.HasSuffix
,com.vaadin.flow.component.shared.HasTooltip
,com.vaadin.flow.component.shared.HasValidationProperties
,com.vaadin.flow.component.shared.InputField<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<T>,
,T>, T> com.vaadin.flow.component.textfield.HasAutocomplete
,Serializable
- Direct Known Subclasses:
BigDecimalField
,DoubleField
,IntegerField
,LongField
An abstract class representing a numeric text field with support for various configurations,
such as grouping, allowance of negative values, formatting, and specified width constraints.
This class serves as a base class for creating type-specific numeric fields.
- Author:
- Syam
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.component.AbstractField
com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<C extends com.vaadin.flow.component.Component,
V> Nested classes/interfaces inherited from interface com.vaadin.flow.component.BlurNotifier
com.vaadin.flow.component.BlurNotifier.BlurEvent<C extends com.vaadin.flow.component.Component>
Nested classes/interfaces inherited from interface com.vaadin.flow.component.FocusNotifier
com.vaadin.flow.component.FocusNotifier.FocusEvent<C extends com.vaadin.flow.component.Component>
Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
com.vaadin.flow.component.HasValue.ValueChangeEvent<V>, com.vaadin.flow.component.HasValue.ValueChangeListener<E extends com.vaadin.flow.component.HasValue.ValueChangeEvent<?>>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
Indicates whether negative values are allowed for the numeric input field.protected boolean
Determines whether the numeric field should apply grouping of digits (e.g., thousands separators) when displaying numbers.protected int
Represents the width of the numeric field in terms of the number of characters allowed.Fields inherited from interface com.vaadin.flow.component.textfield.HasAutocomplete
AUTOCOMPLETE_ATTRIBUTE
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
NumericField
(T defaultValue) Constructs a NumericField with a specified default value. -
Method Summary
Modifier and TypeMethodDescriptionprotected String
Format the value to display it as a string.final boolean
Retrieves the current configuration indicating whether negative numbers are allowed in this numeric field.protected String
Retrieves the country code associated with the current application context.int
Retrieves the number of decimal places allowed for the numeric field.protected abstract int
Retrieves the default length for the numeric field.final int
Retrieves the length of the numeric field.final boolean
Checks whether grouping is enabled for the numeric field.protected void
onAttach
(com.vaadin.flow.component.AttachEvent attachEvent) void
setAllowNegative
(boolean allowNegative) Sets whether the numeric field allows negative values.void
setGrouping
(boolean grouping) Sets whether grouping is enabled for the numeric field.final void
setLength
(int width) Sets the maximum length of the numeric field.protected final void
Configures the formatting pattern for the numeric field based on several parameters such as grouping, decimal places, and whether negative values are allowed.void
Methods inherited from class com.storedobject.vaadin.CustomTextField
alignRight, createField, customizeTextField, disablePaste, generateModelValue, getAutocomplete, getField, getInternalElement, getModelValue, getPrefixComponent, getSuffixComponent, isAutoselect, setAutocomplete, setAutoselect, setEmptyDisplay, setEnabled, setHeight, setMaxHeight, setMaxWidth, setMinHeight, setMinWidth, setPlaceholder, setPrefixComponent, setPresentationValue, setReadOnly, setSuffixComponent, setWidth
Methods inherited from class com.storedobject.vaadin.CustomField
addField, addField, focus
Methods inherited from class com.vaadin.flow.component.customfield.CustomField
add, addThemeVariants, getLabel, remove, removeThemeVariants, setLabel, updateValue
Methods inherited from class com.vaadin.flow.component.AbstractField
addValueChangeListener, getEmptyValue, getValue, isEmpty, setModelValue, valueEquals
Methods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListener
Methods inherited from interface com.vaadin.flow.component.BlurNotifier
addBlurListener
Methods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListener
Methods inherited from interface com.vaadin.flow.component.Focusable
addFocusShortcut, blur, getTabIndex, setTabIndex
Methods inherited from interface com.vaadin.flow.component.FocusNotifier
addFocusListener
Methods inherited from interface com.vaadin.flow.component.HasElement
getElement
Methods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabled
Methods inherited from interface com.vaadin.flow.component.HasHelper
getHelperComponent, getHelperText, setHelperComponent, setHelperText
Methods inherited from interface com.vaadin.flow.component.HasSize
getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeightFull, setMaxHeight, setMaxWidth, setMinHeight, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidthFull
Methods inherited from interface com.storedobject.vaadin.HasStyle
getClassName, getClassNames, getStyle, setClassName
Methods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, hasClassName, removeClassName, removeClassNames, setClassName
Methods inherited from interface com.vaadin.flow.component.HasTheme
addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeName
Methods inherited from interface com.vaadin.flow.component.shared.HasTooltip
getTooltip, setTooltipText
Methods inherited from interface com.vaadin.flow.component.HasValidation
setManualValidation
Methods inherited from interface com.vaadin.flow.component.shared.HasValidationProperties
getErrorMessage, isInvalid, setErrorMessage, setInvalid
Methods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, clear, getEmptyValue, getOptionalValue, getValue, isEmpty
Methods inherited from interface com.vaadin.flow.component.HasValueAndElement
isReadOnly, isRequiredIndicatorVisible, setRequiredIndicatorVisible
-
Field Details
-
grouping
protected boolean groupingDetermines whether the numeric field should apply grouping of digits (e.g., thousands separators) when displaying numbers. -
allowNegative
protected boolean allowNegativeIndicates whether negative values are allowed for the numeric input field. This variable determines if the field should accept negative numeric input. -
width
protected int widthRepresents the width of the numeric field in terms of the number of characters allowed. This variable controls the maximum visible or editable width of the field.
-
-
Constructor Details
-
NumericField
Constructs a NumericField with a specified default value.- Parameters:
defaultValue
- the default value of the numeric field
-
-
Method Details
-
onAttach
protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent) - Overrides:
onAttach
in classCustomTextField<T extends Number>
-
isGrouping
public final boolean isGrouping()Checks whether grouping is enabled for the numeric field.- Returns:
- true if grouping is enabled, false otherwise
-
setGrouping
public void setGrouping(boolean grouping) Sets whether grouping is enabled for the numeric field. Grouping typically refers to the separation of digits into groups, such as by thousands, for easier readability (e.g., 1,000,000 for one million).- Parameters:
grouping
- a boolean value wheretrue
enables grouping, andfalse
disables grouping
-
getAllowNegative
public final boolean getAllowNegative()Retrieves the current configuration indicating whether negative numbers are allowed in this numeric field.- Returns:
- true if negative numbers are allowed; false otherwise
-
setAllowNegative
public void setAllowNegative(boolean allowNegative) Sets whether the numeric field allows negative values.- Parameters:
allowNegative
- a boolean specifying if negative values are allowed. If true, negative values are permitted; otherwise, they are not.
-
setValue
- Specified by:
setValue
in interfacecom.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<com.vaadin.flow.component.customfield.CustomField<T extends Number>,
T extends Number>, T extends Number> - Overrides:
setValue
in classcom.vaadin.flow.component.AbstractField<com.vaadin.flow.component.customfield.CustomField<T extends Number>,
T extends Number>
-
getLength
public final int getLength()Retrieves the length of the numeric field.- Returns:
- the length of the numeric field as an integer.
-
getDecimals
public int getDecimals()Retrieves the number of decimal places allowed for the numeric field.- Returns:
- the number of decimals as an integer
-
format
Description copied from class:CustomTextField
Format the value to display it as a string.- Overrides:
format
in classCustomTextField<T extends Number>
- Parameters:
value
- Value to format.- Returns:
- Displayable string.
-
getDefaultLength
protected abstract int getDefaultLength()Retrieves the default length for the numeric field. The default length typically represents a predefined or initial configuration for the field's width.- Returns:
- the default length of the numeric field as an integer
-
setLength
public final void setLength(int width) Sets the maximum length of the numeric field. The length is adjusted based on certain criteria such as whether negative values are allowed and the number of decimal places. If the specified length is less than 1, a default value of 18 is used. The minimum length is dynamically calculated based on whether negative values are allowed and the number of decimal places. If the specified length is less than the calculated minimum length, the minimum length is used instead.- Parameters:
width
- the desired maximum length for the numeric field. If the value is less than the calculated minimum or a default value, it will be adjusted accordingly.
-
setPattern
protected final void setPattern()Configures the formatting pattern for the numeric field based on several parameters such as grouping, decimal places, and whether negative values are allowed. This method ensures that the internalformatter
instance is properly set up or removed if it already exists. It calculates the maximum width of the numeric input based on defined constraints such as whether negative numbers are allowed and the number of decimal places. Theformatter
uses parameters such as grouping characters, decimal separators, and the desired formatting style to ensure that the numeric field's input and display follow the specified rules. The method also applies the configured formatter to the underlying text field and updates the displayed value to match the current formatting rules. Preconditions: - The numeric field's configuration parameters (e.g., grouping, decimal precision) must be set. - ThegetField()
method provides the internal text field for the numeric field. Postconditions: - The numeric field'sformatter
is updated and applied to the text field. - The displayed presentation value reflects the new formatting pattern. -
getCountry
Retrieves the country code associated with the current application context. If no application context is available, the default country code "US" is returned.- Returns:
- the country code as a string, defaulting to "US" if the application context is unavailable.
-