blob: eee64a25e9b6762114ba04f508144d31cc35bf90 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - 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 v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Florian Pirchner - Initial implementation
*
*/
package org.eclipse.osbp.ecview.extension.grid.presentation;
import java.lang.reflect.Field;
import org.eclipse.osbp.runtime.common.annotations.DtoUtils;
import org.eclipse.osbp.runtime.web.vaadin.common.data.filter.JoinFilter;
import org.vaadin.gridutil.cell.CellFilterComponent;
import com.vaadin.data.util.filter.Or;
import com.vaadin.data.util.filter.SimpleStringFilter;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.Grid;
import com.vaadin.ui.TextField;
import com.vaadin.ui.themes.ValoTheme;
/**
* The Class GridCellFilter.
*/
@SuppressWarnings("serial")
public class GridCellFilter extends org.vaadin.gridutil.cell.GridCellFilter {
/** The grid. */
private Grid grid;
/**
* Instantiates a new grid cell filter.
*
* @param grid
* the grid
*/
public GridCellFilter(Grid grid) {
super(grid);
this.grid = grid;
}
/**
* Sets the dto filter.
*
* @param columnId
* the column id
* @param dtoClass
* the dto class
* @param inputPrompt
* the input prompt
* @return the text field
*/
public TextField setDtoFilter(final Object columnId,
final Class<?> dtoClass, final String inputPrompt) {
CellFilterComponent<TextField> filter = new CellFilterComponent<TextField>() {
private static final long serialVersionUID = 1L;
private String domainKeyPropertyId;
private String domainDescriptionPropertyId;
TextField textField = new TextField();
@Override
public TextField layoutComponent() {
Field domainKeyField = DtoUtils.getDomainKeyField(dtoClass);
if (domainKeyField != null) {
domainKeyPropertyId = domainKeyField.getName();
}
Field domainDescriptionField = DtoUtils
.getDomainDescriptionField(dtoClass);
if (domainDescriptionField != null) {
domainDescriptionPropertyId = domainDescriptionField
.getName();
}
this.textField.setImmediate(true);
this.textField.setInputPrompt(inputPrompt);
this.textField.addStyleName(ValoTheme.TEXTFIELD_TINY);
this.textField.addTextChangeListener(new TextChangeListener() {
private static final long serialVersionUID = -3567212620627878001L;
@Override
public void textChange(final TextChangeEvent event) {
if (event.getText() != null
&& event.getText().length() > 0) {
if (isValid(domainKeyPropertyId)
&& isValid(domainDescriptionPropertyId)) {
// filter for key or description
SimpleStringFilter numberFilter = new SimpleStringFilter(
domainKeyPropertyId, event.getText(),
true, false);
SimpleStringFilter descriptionFilter = new SimpleStringFilter(
domainDescriptionPropertyId, event
.getText(), true, false);
Or or = new Or(numberFilter, descriptionFilter);
replaceFilter(new JoinFilter((String) columnId,
or), columnId);
} else if (isValid(domainKeyPropertyId)) {
// filter for key
SimpleStringFilter numberFilter = new SimpleStringFilter(
domainKeyPropertyId, event.getText(),
true, false);
replaceFilter(new JoinFilter((String) columnId,
numberFilter), columnId);
}
} else {
removeFilter(columnId);
}
}
private boolean isValid(String value) {
return value != null && !value.equals("");
}
});
return this.textField;
}
@Override
public void clearFilter() {
this.textField.clear();
}
};
setCustomFilter(columnId, filter);
return filter.getComponent();
}
/**
* Destroys the filter.
*/
public void destroy() {
grid.removeHeaderRow(getFilterRow());
}
}