(container.getItemIds());
}
if (!(container instanceof Filterable)
|| !(container instanceof Indexed)
|| getItemCaptionMode() != ITEM_CAPTION_MODE_PROPERTY) {
return null;
}
Filterable filterable = (Filterable) container;
Filter filter = buildFilter(filterstring, filteringMode);
// adding and removing filters leads to extraneous item set
// change events from the underlying container, but the ComboBox does
// not process or propagate them based on the flag filteringContainer
if (filter != null) {
filterable.addContainerFilter(filter);
}
// try-finally to ensure that the filter is removed from container even
// if a exception is thrown...
try {
Indexed indexed = (Indexed) container;
int indexToEnsureInView = -1;
// if not an option request (item list when user changes page), go
// to page with the selected item after filtering if accepted by
// filter
Object selection = getValue();
if (isScrollToSelectedItem() && !optionRequest
&& selection != null) {
// ensure proper page
indexToEnsureInView = indexed.indexOfId(selection);
}
filteredSize = container.size();
assert filteredSize >= 0;
currentPage = adjustCurrentPage(currentPage, needNullSelectOption,
indexToEnsureInView, filteredSize);
int first = getFirstItemIndexOnCurrentPage(needNullSelectOption);
int last = getLastItemIndexOnCurrentPage(needNullSelectOption,
filteredSize, first);
// Compute the number of items to fetch from the indexes given or
// based on the filtered size of the container
int lastItemToFetch = Math.min(last, filteredSize - 1);
int nrOfItemsToFetch = (lastItemToFetch + 1) - first;
List> options = indexed.getItemIds(first, nrOfItemsToFetch);
return options;
} finally {
// to the outside, filtering should not be visible
if (filter != null) {
filterable.removeContainerFilter(filter);
}
}
}
/**
* Constructs a filter instance to use when using a Filterable container in
* the ITEM_CAPTION_MODE_PROPERTY
mode.
*
* Note that the client side implementation expects the filter string to
* apply to the item caption string it sees, so changing the behavior of
* this method can cause problems.
*
* @param filterString
* @param filteringMode
* @return
*/
protected Filter buildFilter(String filterString,
FilteringMode filteringMode) {
Filter filter = null;
if (null != filterString && !"".equals(filterString)) {
switch (filteringMode) {
case OFF:
break;
case STARTSWITH:
filter = new SimpleStringFilter(getItemCaptionPropertyId(),
filterString, true, true);
break;
case CONTAINS:
filter = new SimpleStringFilter(getItemCaptionPropertyId(),
filterString, true, false);
break;
}
}
return filter;
}
@Override
public void containerItemSetChange(Container.ItemSetChangeEvent event) {
if (!isPainting) {
super.containerItemSetChange(event);
}
}
/**
* Makes correct sublist of given list of options.
*
* If paint is not an option request (affected by page or filter change),
* page will be the one where possible selection exists.
*
* Detects proper first and last item in list to return right page of
* options. Also, if the current page is beyond the end of the list, it will
* be adjusted.
*
* Package private only for testing purposes.
*
* @param options
* @param needNullSelectOption
* flag to indicate if nullselect option needs to be taken into
* consideration
*/
List> sanitizeList(List> options, boolean needNullSelectOption) {
int totalRows = options.size() + (needNullSelectOption ? 1 : 0);
if (pageLength != 0 && totalRows > pageLength) {
// options will not fit on one page
int indexToEnsureInView = -1;
// if not an option request (item list when user changes page), go
// to page with the selected item after filtering if accepted by
// filter
Object selection = getValue();
if (isScrollToSelectedItem() && !optionRequest
&& selection != null) {
// ensure proper page
indexToEnsureInView = options.indexOf(selection);
}
int size = options.size();
currentPage = adjustCurrentPage(currentPage, needNullSelectOption,
indexToEnsureInView, size);
int first = getFirstItemIndexOnCurrentPage(needNullSelectOption);
int last = getLastItemIndexOnCurrentPage(needNullSelectOption, size,
first);
return options.subList(first, last + 1);
} else {
return options;
}
}
/**
* Returns the index of the first item on the current page. The index is to
* the underlying (possibly filtered) contents. The null item, if any, does
* not have an index but takes up a slot on the first page.
*
* @param needNullSelectOption
* true if a null option should be shown before any other options
* (takes up the first slot on the first page, not counted in
* index)
* @return first item to show on the UI (index to the filtered list of
* options, not taking the null item into consideration if any)
*/
private int getFirstItemIndexOnCurrentPage(boolean needNullSelectOption) {
// Not all options are visible, find out which ones are on the
// current "page".
int first = currentPage * pageLength;
if (needNullSelectOption && currentPage > 0) {
first--;
}
return first;
}
/**
* Returns the index of the last item on the current page. The index is to
* the underlying (possibly filtered) contents. If needNullSelectOption is
* true, the null item takes up the first slot on the first page,
* effectively reducing the first page size by one.
*
* @param needNullSelectOption
* true if a null option should be shown before any other options
* (takes up the first slot on the first page, not counted in
* index)
* @param size
* number of items after filtering (not including the null item,
* if any)
* @param first
* index in the filtered view of the first item of the page
* @return index in the filtered view of the last item on the page
*/
private int getLastItemIndexOnCurrentPage(boolean needNullSelectOption,
int size, int first) {
// page length usable for non-null items
int effectivePageLength = pageLength
- (needNullSelectOption && (currentPage == 0) ? 1 : 0);
// zero pageLength implies infinite page size
return pageLength == 0 ? size - 1
: Math.min(size - 1, first + effectivePageLength - 1);
}
/**
* Adjusts the index of the current page if necessary: make sure the current
* page is not after the end of the contents, and optionally go to the page
* containg a specific item. There are no side effects but the adjusted page
* index is returned.
*
* @param page
* page number to use as the starting point
* @param needNullSelectOption
* true if a null option should be shown before any other options
* (takes up the first slot on the first page, not counted in
* index)
* @param indexToEnsureInView
* index of an item that should be included on the page (in the
* data set, not counting the null item if any), -1 for none
* @param size
* number of items after filtering (not including the null item,
* if any)
*/
private int adjustCurrentPage(int page, boolean needNullSelectOption,
int indexToEnsureInView, int size) {
if (indexToEnsureInView != -1) {
int newPage = (indexToEnsureInView + (needNullSelectOption ? 1 : 0))
/ pageLength;
page = newPage;
}
// adjust the current page if beyond the end of the list
if (page * pageLength > size) {
page = (size + (needNullSelectOption ? 1 : 0)) / pageLength;
}
return page;
}
/**
* Filters the options in memory and returns the full filtered list.
*
* This can be less efficient than using container filters, so use
* {@link #getOptionsWithFilter(boolean)} if possible (filterable container
* and suitable item caption mode etc.).
*
* @return
*/
protected List> getFilteredOptions() {
if (!isFilteringNeeded()) {
prevfilterstring = null;
filteredOptions = new LinkedList(getItemIds());
return filteredOptions;
}
if (filterstring.equals(prevfilterstring)) {
return filteredOptions;
}
Collection> items;
if (prevfilterstring != null
&& filterstring.startsWith(prevfilterstring)) {
items = filteredOptions;
} else {
items = getItemIds();
}
prevfilterstring = filterstring;
filteredOptions = new LinkedList();
for (final Object itemId : items) {
String caption = getItemCaption(itemId);
if (caption == null || caption.equals("")) {
continue;
} else {
caption = caption.toLowerCase(getLocale());
}
switch (filteringMode) {
case CONTAINS:
if (caption.indexOf(filterstring) > -1) {
filteredOptions.add(itemId);
}
break;
case STARTSWITH:
default:
if (caption.startsWith(filterstring)) {
filteredOptions.add(itemId);
}
break;
}
}
return filteredOptions;
}
/**
* Invoked when the value of a variable has changed.
*
* @see com.vaadin.ui.AbstractComponent#changeVariables(java.lang.Object,
* java.util.Map)
*/
@Override
public void changeVariables(Object source, Map variables) {
// Not calling super.changeVariables due the history of select
// component hierarchy
// Selection change
if (variables.containsKey("selected")) {
final String[] ka = (String[]) variables.get("selected");
// Single select mode
if (ka.length == 0) {
// Allows deselection only if the deselected item is visible
final Object current = getValue();
final Collection> visible = getVisibleItemIds();
if (visible != null && visible.contains(current)) {
setValue(null, true);
}
} else {
final Object id = itemIdMapper.get(ka[0]);
if (id != null && id.equals(getNullSelectionItemId())) {
setValue(null, true);
} else {
setValue(id, true);
}
}
}
String newFilter;
if ((newFilter = (String) variables.get("filter")) != null) {
// this is a filter request
currentPage = ((Integer) variables.get("page")).intValue();
filterstring = newFilter;
if (filterstring != null) {
filterstring = filterstring.toLowerCase(getLocale());
}
requestRepaint();
} else if (isNewItemsAllowed()) {
// New option entered (and it is allowed)
final String newitem = (String) variables.get("newitem");
if (newitem != null && !newitem.isEmpty()) {
getNewItemHandler().addNewItem(newitem);
// rebuild list
filterstring = null;
prevfilterstring = null;
}
}
if (variables.containsKey(FocusEvent.EVENT_ID)) {
fireEvent(new FocusEvent(this));
}
if (variables.containsKey(BlurEvent.EVENT_ID)) {
fireEvent(new BlurEvent(this));
}
}
@Override
public void setFilteringMode(FilteringMode filteringMode) {
this.filteringMode = filteringMode;
}
@Override
public FilteringMode getFilteringMode() {
return filteringMode;
}
@Override
public void addBlurListener(BlurListener listener) {
addListener(BlurEvent.EVENT_ID, BlurEvent.class, listener,
BlurListener.blurMethod);
}
@Override
public void removeBlurListener(BlurListener listener) {
removeListener(BlurEvent.EVENT_ID, BlurEvent.class, listener);
}
@Override
public void addFocusListener(FocusListener listener) {
addListener(FocusEvent.EVENT_ID, FocusEvent.class, listener,
FocusListener.focusMethod);
}
@Override
public void removeFocusListener(FocusListener listener) {
removeListener(FocusEvent.EVENT_ID, FocusEvent.class, listener);
}
/**
* ComboBox does not support multi select mode.
*
* @deprecated As of 7.0, use {@link ListSelect}, {@link OptionGroup} or
* {@link TwinColSelect} instead
* @see com.vaadin.ui.AbstractSelect#setMultiSelect(boolean)
* @throws UnsupportedOperationException
* if trying to activate multiselect mode
*/
@Deprecated
@Override
public void setMultiSelect(boolean multiSelect) {
if (multiSelect) {
throw new UnsupportedOperationException(
"Multiselect not supported");
}
}
/**
* ComboBox does not support multi select mode.
*
* @deprecated As of 7.0, use {@link ListSelect}, {@link OptionGroup} or
* {@link TwinColSelect} instead
*
* @see com.vaadin.ui.AbstractSelect#isMultiSelect()
*
* @return false
*/
@Deprecated
@Override
public boolean isMultiSelect() {
return false;
}
/**
* Returns the page length of the suggestion popup.
*
* @return the pageLength
*/
public int getPageLength() {
return pageLength;
}
/**
* Returns the suggestion pop-up's width as a CSS string.
*
* @see #setPopupWidth
* @since 7.7
*/
public String getPopupWidth() {
return suggestionPopupWidth;
}
/**
* Sets the page length for the suggestion popup. Setting the page length to
* 0 will disable suggestion popup paging (all items visible).
*
* @param pageLength
* the pageLength to set
*/
public void setPageLength(int pageLength) {
this.pageLength = pageLength;
markAsDirty();
}
/**
* Sets the suggestion pop-up's width as a CSS string. By using relative
* units (e.g. "50%") it's possible to set the popup's width relative to the
* ComboBox itself.
*
* @see #getPopupWidth()
* @since 7.7
* @param width
* the width
*/
public void setPopupWidth(String width) {
suggestionPopupWidth = width;
markAsDirty();
}
/**
* Sets whether to scroll the selected item visible (directly open the page
* on which it is) when opening the combo box popup or not. Only applies to
* single select mode.
*
* This requires finding the index of the item, which can be expensive in
* many large lazy loading containers.
*
* @param scrollToSelectedItem
* true to find the page with the selected item when opening the
* selection popup
*/
public void setScrollToSelectedItem(boolean scrollToSelectedItem) {
this.scrollToSelectedItem = scrollToSelectedItem;
}
/**
* Returns true if the select should find the page with the selected item
* when opening the popup (single select combo box only).
*
* @see #setScrollToSelectedItem(boolean)
*
* @return true if the page with the selected item will be shown when
* opening the popup
*/
public boolean isScrollToSelectedItem() {
return scrollToSelectedItem;
}
/**
* Sets the item style generator that is used to produce custom styles for
* showing items in the popup. The CSS class name that will be added to the
* item style names is v-filterselect-item-[style name] .
*
* @param itemStyleGenerator
* the item style generator to set, or null
to not
* use any custom item styles
* @since 7.5.6
*/
public void setItemStyleGenerator(ItemStyleGenerator itemStyleGenerator) {
this.itemStyleGenerator = itemStyleGenerator;
markAsDirty();
}
/**
* Gets the currently used item style generator.
*
* @return the itemStyleGenerator the currently used item style generator,
* or null
if no generator is used
* @since 7.5.6
*/
public ItemStyleGenerator getItemStyleGenerator() {
return itemStyleGenerator;
}
}