| 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 org.eclipse.osbp.ui.api.themes.IThemeResourceService.ThemeResourceType; |
| |
| import com.vaadin.data.Container.Filter; |
| import com.vaadin.data.Validator; |
| import com.vaadin.data.util.BeanItemContainer; |
| 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.Compare; |
| import com.vaadin.data.util.filter.Not; |
| 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.server.Resource; |
| import com.vaadin.server.ThemeResource; |
| import com.vaadin.ui.ComboBox; |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.HorizontalLayout; |
| import com.vaadin.ui.Label; |
| import com.vaadin.ui.TextField; |
| |
| @SuppressWarnings("serial") |
| public class CompareDecimalComponent extends CompareComponent { |
| |
| private Label captionField; |
| |
| private ComboBox compareTypeField; |
| |
| private TextField valueField; |
| |
| private CompareType selectedCompareType; |
| |
| @Inject |
| public CompareDecimalComponent(@Named("propertyId") String propertyId, @Named("type") Class<? extends Number> type, |
| IViewContext viewContext) { |
| super(propertyId, type, viewContext); |
| } |
| |
| @Override |
| protected Component createMainLayout() { |
| HorizontalLayout mainLayout = new HorizontalLayout(); |
| captionField = new Label(); |
| captionField.setValue(propertyId); |
| compareTypeField = new ComboBox(); |
| valueField = new TextField(); |
| valueField.setNullRepresentation(""); |
| valueField.setConverter(toConverter()); |
| valueField.addValidator(toValidator()); |
| |
| mainLayout.addComponent(captionField); |
| mainLayout.addComponent(compareTypeField); |
| mainLayout.addComponent(valueField); |
| |
| captionField.setWidth("100%"); |
| compareTypeField.setWidth("100%"); |
| valueField.setWidth("100%"); |
| mainLayout.setExpandRatio(captionField, 1.0f); |
| mainLayout.setExpandRatio(compareTypeField, 0.5f); |
| mainLayout.setExpandRatio(valueField, 1.5f); |
| |
| updateContainer(); |
| compareTypeField.setItemCaptionPropertyId("caption"); |
| compareTypeField.setItemIconPropertyId("image"); |
| return mainLayout; |
| } |
| |
| protected void updateContainer() { |
| BeanItemContainer<CompareType> container = new BeanItemContainer<>(CompareType.class); |
| selectedCompareType = new CompareType(CompareTypeEnum.EQ, toLabel(CompareTypeEnum.EQ), toImage(CompareTypeEnum.EQ)); |
| container.addBean(selectedCompareType); |
| container |
| .addBean(new CompareType(CompareTypeEnum.NE, toLabel(CompareTypeEnum.NE), toImage(CompareTypeEnum.NE))); |
| container |
| .addBean(new CompareType(CompareTypeEnum.GT, toLabel(CompareTypeEnum.GT), toImage(CompareTypeEnum.GT))); |
| container |
| .addBean(new CompareType(CompareTypeEnum.GE, toLabel(CompareTypeEnum.GE), toImage(CompareTypeEnum.GE))); |
| container |
| .addBean(new CompareType(CompareTypeEnum.LT, toLabel(CompareTypeEnum.LT), toImage(CompareTypeEnum.LT))); |
| container |
| .addBean(new CompareType(CompareTypeEnum.LE, toLabel(CompareTypeEnum.LE), toImage(CompareTypeEnum.LE))); |
| |
| compareTypeField.setContainerDataSource(container); |
| compareTypeField.select(selectedCompareType); |
| } |
| |
| protected String toLabel(CompareTypeEnum enumx) { |
| return Util.getCaption(i18nService, enumx.name(), enumx.name(), getLocale()); |
| } |
| |
| protected Resource toImage(CompareTypeEnum enumx) { |
| return themeResourceService.getThemeResource(enumx.name(), ThemeResourceType.ICON); |
| } |
| |
| public Filter getFilter() { |
| if (!valueField.isValid() || !compareTypeField.isValid()) { |
| return null; |
| } |
| |
| CompareType compareType = (CompareType) compareTypeField.getValue(); |
| |
| if (compareType == null) { |
| return null; |
| } |
| |
| Number numericValue = (Number) valueField.getConvertedValue(); |
| // if (value == null || value.trim().equals("")) { |
| // return null; |
| // } |
| |
| // Comparable<?> numericValue = parser.apply(value); |
| Filter filter = null; |
| switch (compareType.typeEnum) { |
| case EQ: |
| filter = new Compare.Equal(propertyId, numericValue); |
| break; |
| case GE: |
| filter = new Compare.GreaterOrEqual(propertyId, numericValue); |
| break; |
| case GT: |
| filter = new Compare.Greater(propertyId, numericValue); |
| break; |
| case LE: |
| filter = new Compare.LessOrEqual(propertyId, numericValue); |
| break; |
| case LT: |
| filter = new Compare.Less(propertyId, numericValue); |
| break; |
| case NE: |
| filter = new Not(new Compare.Equal(propertyId, numericValue)); |
| break; |
| } |
| |
| return filter; |
| } |
| |
| @Override |
| public void resetAllFilters() { |
| compareTypeField.setValue(null); |
| valueField.setValue(null); |
| compareTypeField.select(selectedCompareType); |
| } |
| |
| protected void updateCaptions() { |
| captionField.setValue(getCaptionInternal()); |
| |
| updateContainer(); |
| } |
| |
| protected Converter<String, ? extends Number> toConverter() { |
| if (type == Integer.class || type == Integer.TYPE) { |
| return new StringToIntegerConverter(); |
| } else if (type == BigDecimal.class) { |
| return new StringToBigDecimalConverter(); |
| } else if (type == BigInteger.class) { |
| return new StringToBigIntegerConverter(); |
| } else if (type == Byte.class || type == Byte.TYPE) { |
| return new StringToByteConverter(); |
| } else if (type == Double.class || type == Double.TYPE) { |
| return new StringToDoubleConverter(); |
| } else if (type == Float.class || type == Float.TYPE) { |
| return new StringToFloatConverter(); |
| } else if (type == Long.class || type == Long.TYPE) { |
| return new StringToLongConverter(); |
| } else if (type == Short.class || type == Short.TYPE) { |
| return 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!"); |
| } |
| |
| public static class CompareType { |
| private CompareTypeEnum typeEnum; |
| private String caption; |
| private Resource image; |
| |
| public CompareType(CompareTypeEnum typeEnum, String caption, Resource image) { |
| super(); |
| this.typeEnum = typeEnum; |
| this.caption = caption; |
| this.image = image; |
| } |
| |
| public CompareTypeEnum getTypeEnum() { |
| return typeEnum; |
| } |
| |
| public void setTypeEnum(CompareTypeEnum typeEnum) { |
| this.typeEnum = typeEnum; |
| } |
| |
| public String getCaption() { |
| return caption; |
| } |
| |
| public void setCaption(String caption) { |
| this.caption = caption; |
| } |
| |
| public Resource getImage() { |
| return image; |
| } |
| |
| public void setImage(Resource image) { |
| this.image = image; |
| } |
| |
| } |
| |
| public enum CompareTypeEnum { |
| EQ, NE, GT, GE, LE, LT; |
| } |
| } |