blob: a02fa893fe2908b342e8bbdec9e4af1c6acff125 [file] [log] [blame]
package org.eclipse.osbp.runtime.web.vaadin.components.fields.filter2;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.inject.Inject;
import javax.inject.Named;
import org.eclipse.osbp.ecview.core.common.context.IViewContext;
import com.vaadin.data.Container.Filter;
import com.vaadin.data.Validator;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.StringToBigDecimalConverter;
import com.vaadin.data.util.converter.StringToBigIntegerConverter;
import com.vaadin.data.util.converter.StringToByteConverter;
import com.vaadin.data.util.converter.StringToDoubleConverter;
import com.vaadin.data.util.converter.StringToFloatConverter;
import com.vaadin.data.util.converter.StringToIntegerConverter;
import com.vaadin.data.util.converter.StringToLongConverter;
import com.vaadin.data.util.converter.StringToShortConverter;
import com.vaadin.data.util.filter.Between;
import com.vaadin.data.validator.ByteRangeValidator;
import com.vaadin.data.validator.DoubleRangeValidator;
import com.vaadin.data.validator.FloatRangeValidator;
import com.vaadin.data.validator.IntegerRangeValidator;
import com.vaadin.data.validator.LongRangeValidator;
import com.vaadin.data.validator.ShortRangeValidator;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
@SuppressWarnings("serial")
public class BetweenDecimalComponent<T extends Number & Comparable<?>> extends BetweenComponent {
private Label captionField;
private TextField fromValueField;
private TextField untilValueField;
@Inject
public BetweenDecimalComponent(@Named("propertyId") String propertyId, @Named("type") Class<T> type,
IViewContext viewContext) {
super(propertyId, type, viewContext);
}
@Override
protected Component createMainLayout() {
HorizontalLayout mainLayout = new HorizontalLayout();
captionField = new Label();
captionField.addStyleName("caption");
captionField.setValue(propertyId);
fromValueField = new TextField();
fromValueField.setImmediate(true);
fromValueField.setNullRepresentation("");
fromValueField.addStyleName("from");
fromValueField.setConverter(toConverter());
fromValueField.addValidator(toValidator());
untilValueField = new TextField();
untilValueField.setImmediate(true);
untilValueField.setNullRepresentation("");
untilValueField.addStyleName("until");
untilValueField.setConverter(toConverter());
untilValueField.addValidator(toValidator());
mainLayout.addComponent(captionField);
mainLayout.addComponent(fromValueField);
mainLayout.addComponent(untilValueField);
captionField.setWidth("100%");
fromValueField.setWidth("100%");
untilValueField.setWidth("100%");
mainLayout.setExpandRatio(captionField, 1.0f);
mainLayout.setExpandRatio(fromValueField, 1.0f);
mainLayout.setExpandRatio(untilValueField, 1.0f);
return mainLayout;
}
@SuppressWarnings("unchecked")
protected Converter<String, T> toConverter() {
if (type == Integer.class || type == Integer.TYPE) {
return (Converter<String, T>) new StringToIntegerConverter();
} else if (type == BigDecimal.class) {
return (Converter<String, T>) new StringToBigDecimalConverter();
} else if (type == BigInteger.class) {
return (Converter<String, T>) new StringToBigIntegerConverter();
} else if (type == Byte.class || type == Byte.TYPE) {
return (Converter<String, T>) new StringToByteConverter();
} else if (type == Double.class || type == Double.TYPE) {
return (Converter<String, T>) new StringToDoubleConverter();
} else if (type == Float.class || type == Float.TYPE) {
return (Converter<String, T>) new StringToFloatConverter();
} else if (type == Long.class || type == Long.TYPE) {
return (Converter<String, T>) new StringToLongConverter();
} else if (type == Short.class || type == Short.TYPE) {
return (Converter<String, T>) new StringToShortConverter();
}
throw new IllegalStateException("Number type is not valid!");
}
protected Validator toValidator() {
if (type == Integer.class || type == Integer.TYPE) {
return new IntegerRangeValidator("not valid", Integer.MIN_VALUE, Integer.MAX_VALUE);
} else if (type == Byte.class || type == Byte.TYPE) {
return new ByteRangeValidator("not valid", Byte.MIN_VALUE, Byte.MAX_VALUE);
} else if (type == Double.class || type == Double.TYPE) {
return new DoubleRangeValidator("not valid", Double.NEGATIVE_INFINITY, Double.MAX_VALUE);
} else if (type == Float.class || type == Float.TYPE) {
return new FloatRangeValidator("not valid", Float.MIN_VALUE, Float.MAX_VALUE);
} else if (type == Long.class || type == Long.TYPE) {
return new LongRangeValidator("not valid", Long.MIN_VALUE, Long.MAX_VALUE);
} else if (type == Short.class || type == Short.TYPE) {
return new ShortRangeValidator("not valid", Short.MIN_VALUE, Short.MAX_VALUE);
}
throw new IllegalStateException("Number type is not valid!");
}
@Override
public void resetAllFilters() {
fromValueField.setValue(null);
untilValueField.setValue(null);
}
public Filter getFilter() {
if (!fromValueField.isValid() || !untilValueField.isValid()) {
return null;
}
if (fromValueField.getValue() == null || fromValueField.getValue().trim().equals("")
|| untilValueField.getValue() == null || untilValueField.getValue().trim().equals("")) {
return null;
}
Between between = new Between(propertyId, (Comparable<?>) fromValueField.getConvertedValue(),
(Comparable<?>) untilValueField.getConvertedValue());
return between;
}
protected void updateCaptions() {
captionField.setValue(getCaptionInternal());
}
}