I have a TreeTable class that I need to be able to filter via the contents of a TextField. (I can worry about styling said TextField later so it has a magnifying glass icon and an “X” button for quickling clearing it.)
I’ve attempted to follow along with the Vaadin Book example, modifying it slightly so it should work better in my use case. For example, I keep them all in one object, since the methods “changeDayTo” and “filterTable” seem like they should be internal to this TreeTable. (Keeping in mind that whole ‘objects should only contain properties and methods that are specific to themselves’ idea.)
So here’s my semi-custom TreeTable class:
[code]
/**
*
*/
package info.chrismcgee.sky.scheduling.ui.components;
import java.sql.SQLException;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.vaadin.data.Container.Filterable;
import com.vaadin.data.util.filter.SimpleStringFilter;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TreeTable;
import com.vaadin.ui.Notification.Type;
import info.chrismcgee.sky.scheduling.beans.Job;
import info.chrismcgee.sky.scheduling.beans.OrderDetail;
import info.chrismcgee.sky.tables.JobManager;
/**
- @author Marketing
*/
public class ProductionTreeTable extends TreeTable {
/**
* Serialization!
*/
private static final long serialVersionUID = -8456241351283529486L;
private final static Logger logger = Logger.getLogger(ProductionTreeTable.class.getName()); // Logging!
private SimpleStringFilter filter = null;
private Filterable f = null;
/**
* Constructor
*/
public ProductionTreeTable() {
super();
// Set the columns.
addContainerProperty("Name / Product", HorizontalLayout.class, "");
addContainerProperty("Job # / Detail", String.class, "");
addContainerProperty("Print Type", String.class, "");
addContainerProperty("Colors", Long.class, 0L);
addContainerProperty("Quantity", Long.class, 0L);
addContainerProperty("Total", Long.class, 0L);
}
HorizontalLayout createNode (String name, boolean isChecked) {
HorizontalLayout layout = new HorizontalLayout();
layout.addComponent(new CheckBox(null, isChecked));
layout.addComponent(new Label(name));
return layout;
}
public void changeDayTo (final Date date) {
// Get a list of jobs on a particular date.
filter = null; // Reset the filter.
List<Job> jobsList = null;
try {
jobsList = JobManager.getJobsByDate(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (jobsList != null && jobsList.size() > 0) {
removeAllItems(); // Clear out the TreeTable first.
for (final Job j : jobsList) {
// Go through each item in the Jobs list and add it
// (along with all of its OrderDetail items) to the TreeTable.
final Object jobId = addItem(new Object[] {createNode(j.getCustomerName(), j.getJobCompleted() != null), j.getJobId(),
"", null, null, null}, null);
for (final OrderDetail od : j.getOrderDetailList()) {
final Object odId = addItem(new Object[] {createNode(od.getProductId(), od.getItemCompleted() != null), od.getProductDetail(),
od.getPrintType().getValue(), od.getNumColors(), od.getQuantity(), (od.getNumColors() * od.getQuantity())}, null);
boolean parentSet = setParent(odId, jobId);
setChildrenAllowed(odId, false);
setCollapsed(odId, false);
}
setCollapsed(jobId, false);
}
filterTable("");
}
}
public void filterTable (String searchText) {
// TODO Auto-generated method stub
f = (Filterable) getContainerDataSource();
// Remove old filter
if (filter != null) {
f.removeContainerFilter(filter);
}
// Set new filter for the "Name / Product" column
filter = new SimpleStringFilter("Name / Product", searchText, true, false);
f.addContainerFilter(filter);
Notification.show("Results narrowed.", Type.TRAY_NOTIFICATION);
}
}
[/code]Unfortunately, anything typed in the text field (which gets passed to the filterTable() method) causes the TreeTable to be completely emptied. I’m sure it has something to do with the Filterable property and/or the SimpleStringFilter object I’m trying to use here, but I don’t know what it is. I’m completely new to filtering. All I do know is that, once the TreeTable has been populated with data in the changeDayTo() method, it is using a HierarchicalContainer, which does implement the Container.Filterable interface, from what I understand. Now I just need to know how to take advantage of this.
Please help?