All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.holonplatform.vaadin.components.Input Maven / Gradle / Ivy

There is a newer version: 5.4.0
Show newest version
/*
 * Copyright 2000-2017 Holon TDCN.
 * 
 * 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.holonplatform.vaadin.components;

import com.holonplatform.core.internal.utils.ObjectUtils;
import com.holonplatform.core.property.Property;
import com.holonplatform.core.property.PropertyRenderer;
import com.holonplatform.core.property.PropertyValueConverter;
import com.holonplatform.vaadin.components.ValueHolder.MaySupportValueChangeMode;
import com.holonplatform.vaadin.internal.components.InputConverterAdapter;
import com.holonplatform.vaadin.internal.components.InputFieldWrapper;
import com.vaadin.data.Converter;
import com.vaadin.data.HasValue;
import com.vaadin.shared.ui.ValueChangeMode;
import com.vaadin.ui.Component;

/**
 * Input component representation, i.e. a UI component that has a user-editable value.
 * 

* Extends {@link ValueHolder} since handles a value, supporting {@link ValueChangeListener}s registration. *

*

* The actual UI {@link Component} which represents the input component can be obtained through {@link #getComponent()}. *

*

* Extends {@link MaySupportValueChangeMode} to allow value change mode and timeout configuration for input components * which support it. *

* * @param Value type * * @since 5.0.0 */ public interface Input extends ValueHolder, ValueComponent, MaySupportValueChangeMode { /** * Sets the read-only mode of this input component. The user can't change the value when in read-only mode. * @param readOnly the read-only mode of this input component */ void setReadOnly(boolean readOnly); /** * Returns whether this input component is in read-only mode or not. * @return false if the user can modify the value, true if not */ boolean isReadOnly(); /** * Gets whether the field is required, i.e. a required indicator symbol is visible. * @return true if the field as required, false otherwise */ public boolean isRequired(); /** * Sets whether the required indicator symbol is visible. * @param required true to set the field as required, false otherwise */ void setRequired(boolean required); /** * Sets the focus for this input component, if supported by concrete component implementation. */ void focus(); // By default, behave as value change mode is not supported @Override default boolean isValueChangeModeSupported() { return false; } @Override default void setValueChangeMode(ValueChangeMode valueChangeMode) { // not supported by default } @Override default ValueChangeMode getValueChangeMode() { return ValueChangeMode.BLUR; } @Override default void setValueChangeTimeout(int valueChangeTimeout) { // not supported by default } @Override default int getValueChangeTimeout() { return -1; } // Adapters /** * Create a {@link Input} component type from given {@link HasValue} component. * @param Value type * @param {@link HasValue} component type * @param field The field instance (not null) * @return A new {@link Input} component which wraps the given field */ static & Component, T> Input from(F field) { return new InputFieldWrapper<>(field); } // Converters /** * Create a new {@link Input} from another {@link Input} with a different value type, using given {@link Converter} * to perform value conversions. * @param New value type * @param Original value type * @param input Actual input (not null) * @param converter Value converter (not null) * @return A new {@link Input} of the converted value type */ static Input from(Input input, Converter converter) { return new InputConverterAdapter<>(input, converter); } /** * Create a new {@link Input} from given {@link HasValue} component with a different value type, using given * {@link Converter} to perform value conversions. * @param {@link HasValue} component type * @param New value type * @param Original value type * @param field The field (not null) * @param converter Value converter (not null) * @return A new {@link Input} of the converted value type */ static & Component, T, V> Input from(F field, Converter converter) { return from(from(field), converter); } /** * Create a new {@link Input} from another {@link Input} with a different value type, using given * {@link PropertyValueConverter} to perform value conversions. * @param New value type * @param Original value type * @param input Actual input (not null) * @param property Property to provide to the converter * @param converter Value converter (not null) * @return A new {@link Input} of the converted value type */ static Input from(Input input, Property property, PropertyValueConverter converter) { ObjectUtils.argumentNotNull(converter, "PropertyValueConverter must be not null"); return new InputConverterAdapter<>(input, Converter.from(value -> converter.toModel(value, property), value -> converter.fromModel(value, property), e -> e.getMessage())); } /** * Create a new {@link Input} from another {@link Input} with a different value type, using given * {@link PropertyValueConverter} to perform value conversions. * @param {@link HasValue} component type * @param New value type * @param Original value type * @param field The field (not null) * @param property Property to provide to the converter * @param converter Value converter (not null) * @return A new {@link Input} of the converted value type */ static & Component, T, V> Input from(F field, Property property, PropertyValueConverter converter) { return from(from(field), property, converter); } // Renderers /** * A convenience interface with a fixed {@link Input} rendering type to use a {@link Input} {@link PropertyRenderer} * as a functional interface. * @param Property type */ @FunctionalInterface public interface InputPropertyRenderer extends PropertyRenderer, T> { /* * (non-Javadoc) * @see com.holonplatform.core.property.PropertyRenderer#getRenderType() */ @SuppressWarnings("unchecked") @Override default Class> getRenderType() { return (Class>) (Class) Input.class; } } /** * A convenience interface to render a {@link Property} as a {@link Input} using a {@link HasValue} component. * @param Property type */ public interface InputFieldPropertyRenderer & Component> extends InputPropertyRenderer { /** * Render given property as consistent value type {@link HasValue} component to handle the property * value. * @param property Property to render * @return property {@link HasValue} component */ F renderField(Property property); /* * (non-Javadoc) * @see com.holonplatform.core.property.PropertyRenderer#render(com.holonplatform.core.property.Property) */ @Override default Input render(Property property) { return Input.from(renderField(property)); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy