| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.runtime.web.vaadin.components.fields.search; |
| |
| import java.util.List; |
| |
| import org.eclipse.core.databinding.Binding; |
| import org.eclipse.core.databinding.DataBindingContext; |
| import org.eclipse.core.databinding.UpdateValueStrategy; |
| import org.eclipse.core.databinding.beans.BeanProperties; |
| import org.eclipse.core.databinding.beans.PojoObservables; |
| import org.eclipse.core.databinding.observable.value.IObservableValue; |
| import org.eclipse.osbp.runtime.web.vaadin.common.data.IBeanSearchService; |
| import org.eclipse.osbp.runtime.web.vaadin.components.fields.search.filter.IFilterProperty; |
| import org.eclipse.osbp.runtime.web.vaadin.components.fields.search.filter.TextFilterProperty; |
| import org.eclipse.osbp.runtime.web.vaadin.databinding.VaadinObservables; |
| |
| import com.vaadin.data.Container.Filter; |
| import com.vaadin.data.util.ObjectProperty; |
| import com.vaadin.event.FieldEvents; |
| import com.vaadin.event.FieldEvents.TextChangeEvent; |
| import com.vaadin.ui.AbstractTextField.TextChangeEventMode; |
| import com.vaadin.ui.Button; |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.HorizontalLayout; |
| import com.vaadin.ui.TextField; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * The Class EntityTextSearchField. |
| * |
| * @param <BEAN> |
| * the generic type |
| */ |
| @SuppressWarnings("serial") |
| public class EntityTextSearchField<BEAN> extends SearchField<Object> { |
| |
| /** The value binding. */ |
| private Binding valueBinding; |
| |
| /** The filter property. */ |
| private TextFilterProperty filterProperty; |
| |
| /** The search service. */ |
| @SuppressWarnings("unused") |
| private IBeanSearchService<BEAN> searchService; |
| |
| /** |
| * Instantiates a new entity text search field. |
| * |
| * @param id |
| * the id |
| * @param propertyId |
| * the property id |
| * @param dbContext |
| * the db context |
| */ |
| public EntityTextSearchField(String id, Object propertyId, |
| DataBindingContext dbContext) { |
| super(id, propertyId, dbContext); |
| |
| filterProperty = new TextFilterProperty(getPropertyId(), getLocale()); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.web.vaadin.components.fields.search.ISearchField#bindFilterChanged(org.eclipse.core.databinding.observable.value.IObservableValue) |
| */ |
| public void bindFilterChanged(IObservableValue targetObservableValue) { |
| Binding binding = getDbContext().bindValue(targetObservableValue, |
| BeanProperties.value("filter").observe(filterProperty), |
| new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), |
| new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE)); |
| filterBindings.add(binding); |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.ui.CustomField#initContent() |
| */ |
| @Override |
| protected Component initContent() { |
| |
| HorizontalLayout root = new HorizontalLayout(); |
| TextField textField = new TextField(); |
| textField.setImmediate(true); |
| textField.setNullRepresentation(""); |
| textField.setInvalidAllowed(false); |
| textField.setTextChangeEventMode(TextChangeEventMode.LAZY); |
| textField.setTextChangeTimeout(250); |
| textField.addTextChangeListener(new FieldEvents.TextChangeListener() { |
| @Override |
| public void textChange(TextChangeEvent event) { |
| // List<BEAN> proposal = |
| // searchService.findProposal(event.getText()); |
| // updateProposal(proposal); |
| } |
| }); |
| |
| Button searchButton = new Button(); |
| |
| root.addComponent(textField); |
| root.addComponent(searchButton); |
| |
| // Create the property |
| ObjectProperty<Object> property = new ObjectProperty<Object>("", |
| Object.class, false); |
| textField.setPropertyDataSource(property); |
| |
| // Create the bindings |
| DataBindingContext dbContext = getDbContext(); |
| valueBinding = dbContext.bindValue(VaadinObservables |
| .observeValue(textField), PojoObservables.observeValue( |
| filterProperty, IFilterProperty.PROP_FILTER_VALUE)); |
| |
| return root; |
| } |
| |
| /** |
| * Update proposal. |
| * |
| * @param proposal |
| * the proposal |
| */ |
| protected void updateProposal(List<BEAN> proposal) { |
| |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.ui.AbstractField#getType() |
| */ |
| @Override |
| public Class<? extends String> getType() { |
| return String.class; |
| } |
| |
| /** |
| * See {@link TextField#setNullRepresentation(String)}. |
| * |
| * @param value |
| * the new null representation |
| */ |
| public void setNullRepresentation(String value) { |
| ((TextField) getContent()).setNullRepresentation(value); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.web.vaadin.components.fields.search.ISearchField#getFilter() |
| */ |
| @Override |
| public Filter getFilter() { |
| return filterProperty.getFilter(); |
| } |
| |
| /** |
| * Dispose the field. |
| */ |
| public void dispose() { |
| |
| filterProperty = null; |
| |
| if (valueBinding != null) { |
| valueBinding.dispose(); |
| valueBinding = null; |
| } |
| |
| // dispose the bindings |
| for (Binding b : filterBindings) { |
| b.dispose(); |
| } |
| filterBindings.clear(); |
| } |
| } |