Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
book-of-vaadin.pdf
Скачиваний:
88
Добавлен:
24.03.2015
Размер:
13.43 Mб
Скачать

Binding Components to Data

9.5.7. Filterable Containers

Containers that implement the Container.Filterable interface can be filtered. For example, the built-in IndexedContainer and the bean item containers implement it. Filtering is typically used for filtering the content of a Table.

Filters implement the Filter interface and you add them to a filterable container with the addContainerFilter() method. Container items that pass the filter condition are kept and shown in the filterable component.

Filter filter = new SimpleStringFilter("name", "Douglas", true, false);

table.addContainerFilter(filter);

If multiple filters are added to a container, they are evaluated using the logical AND operator so that only items that are passed by all the filters are kept.

Atomic and Composite Filters

Filters can be classified as atomic and composite. Atomic filters, such as SimpleStringFilter, define a single condition, usually for a specific container property. Composite filters make filtering decisions based on the result of one or more other filters. The built-in composite filters implement the logical operators AND, OR, or NOT.

For example, the following composite filter would filter out items where the name property contains the name "Douglas" somewhere and where the age property has value less than 42. The properties must have String and Integer types, respectively.

filter = new Or(new SimpleStringFilter("name", "Douglas", true, false),

new Compare.Less("age", 42));

Built-In Filter Types

The built-in filter types are the following:

SimpleStringFilter

Passes items where the specified property, that must be of String type, contains the given filterString as a substring. If ignoreCase is true, the search is case insensitive. If the onlyMatchPrefix is true, the substring may only be in the beginning of the string, otherwise it may be elsewhere as well.

IsNull

Passes items where the specified property has null value. For in-memory filtering, a simple == check is performed. For other containers, the comparison implementation is container dependent, but should correspond to the in-memory null check.

Equal, Greater, Less, GreaterOrEqual, and LessOrEqual

The comparison filter implementations compare the specified property value to the given constant and pass items for which the comparison result is true.The comparison operators are included in the abstract Compare class.

Filterable Containers

261

Binding Components to Data

For the Equal filter, the equals() method for the property is used in built-in in-memory containers. In other types of containers, the comparison is container dependent and may use, for example, database comparison operations.

For the other filters, the property value type must implement the Comparable interface to work with the built-in in-memory containers. Again for the other types of containers, the comparison is container dependent.

And and Or

These logical operator filters are composite filters that combine multiple other filters.

Not

The logical unary operator filter negates which items are passed by the filter given as the parameter.

Implementing Custom Filters

A custom filter needs to implement the Container.Filter interface.

A filter can use a single or multiple properties for the filtering logic. The properties used by the filter must be returned with the appliesToProperty() method. If the filter applies to a userdefined property or properties, it is customary to give the properties as the first argument for the constructor of the filter.

class MyCustomFilter implements Container.Filter { protected String propertyId;

protected String regex;

public MyCustomFilter(String propertyId, String regex) {

this.propertyId

=

propertyId;

this.regex

=

regex;

}

 

 

/** Tells if this filter works on the given property. */ @Override

public boolean appliesToProperty(Object propertyId) { return propertyId != null &&

propertyId.equals(this.propertyId);

}

The actual filtering logic is done in the passesFilter() method, which simply returns true if the item should pass the filter and false if it should be filtered out.

/** Apply the filter on an item to check if it passes. */ @Override

public boolean passesFilter(Object itemId, Item item) throws UnsupportedOperationException {

//Acquire the relevant property from the item object Property p = item.getItemProperty(propertyId);

//Should always check validity

if (p == null || !p.getType().equals(String.class)) return false;

String value = (String) p.getValue();

// The actual filter logic return value.matches(regex);

}

}

262

Filterable Containers

Binding Components to Data

You can use such a custom filter just like any other:

c.addContainerFilter(

new MyCustomFilter("Name", (String) tf.getValue()));

Filterable Containers

263

264

Chapter 10

Vaadin

SQLContainer

10.1. Architecture .........................................................................................

266

10.2. Getting Started with SQLContainer .....................................................

266

10.3. Filtering and Sorting ............................................................................

267

10.4. Editing ..................................................................................................

268

10.5. Caching, Paging and Refreshing .........................................................

270

10.6. Referencing Another SQLContainer ...................................................

271

10.7. Using FreeformQuery and FreeformStatementDelegate .................

272

10.8. Non-implemented methods of Vaadin container interfaces .................

273

10.9. Known Issues and Limitations .............................................................

274

Vaadin SQLContainer is a container implementation that allows easy and customizable access to data stored in various SQL-speaking databases.

SQLContainer supports two types of database access. Using TableQuery, the pre-made query generators will enable fetching, updating, and inserting data directly from the container into a database table - automatically, whereas FreeformQuery allows the developer to use their own, probably more complex query for fetching data and their own optional implementations for writing, filtering and sorting support - item and property handling as well as lazy loading will still be handled automatically.

In addition to the customizable database connection options, SQLContainer also extends the Vaadin Container interface to implement more advanced and more database-oriented filtering

Book of Vaadin

265

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]