com.vaadin.ui.ListSelect Maven / Gradle / Ivy
/*
* Copyright (C) 2000-2024 Vaadin Ltd
*
* This program is available under Vaadin Commercial License and Service Terms.
*
* See for the full
* license.
*/
package com.vaadin.ui;
import java.util.Collection;
import com.vaadin.data.HasDataProvider;
import com.vaadin.data.provider.DataProvider;
import com.vaadin.shared.ui.listselect.ListSelectState;
/**
* This is a simple list select without, for instance, support for new items,
* lazyloading, and other advanced features.
*
* @author Vaadin Ltd
*
* @param
* item type
*/
public class ListSelect extends AbstractMultiSelect
implements HasDataProvider {
/** Default number of rows visible for select. */
// protected to allow javadoc linking
protected static final int DEFAULT_ROWS = 10;
/**
* Constructs a new ListSelect.
*/
public ListSelect() {
setRows(DEFAULT_ROWS);
}
/**
* Constructs a new ListSelect with the given caption.
*
* @param caption
* the caption to set, can be {@code null}
*/
public ListSelect(String caption) {
this();
setCaption(caption);
}
/**
* Constructs a new ListSelect with caption and data provider for options.
*
* @param caption
* the caption to set, can be {@code null}
* @param dataProvider
* the data provider, not {@code null}
* @since 8.0
*/
public ListSelect(String caption, DataProvider dataProvider) {
this(caption);
setDataProvider(dataProvider);
}
/**
* Constructs a new ListSelect with caption and the given options.
*
* @param caption
* the caption to set, can be {@code null}
* @param options
* the options, cannot be {@code null}
*/
public ListSelect(String caption, Collection options) {
this(caption, DataProvider.ofCollection(options));
}
/**
* Returns the number of rows in the select.
*
* Default value is {@link #DEFAULT_ROWS}
*
* @return the number of rows visible
*/
public int getRows() {
return getState(false).rows;
}
/**
* Sets the number of rows in the select. If the number of rows is set to 0,
* the actual number of displayed rows is determined implicitly by the
* select.
*
* If a height if set (using {@link #setHeight(String)} or
* {@link #setHeight(float, Unit)}) it overrides the number of rows. Leave
* the height undefined to use this method.
*
* Default value is {@link #DEFAULT_ROWS}
*
* @param rows
* the number of rows to set.
*/
public void setRows(int rows) {
if (rows < 0) {
rows = 0;
}
if (getState(false).rows != rows) {
getState().rows = rows;
}
}
@Override
protected ListSelectState getState() {
return (ListSelectState) super.getState();
}
@Override
protected ListSelectState getState(boolean markAsDirty) {
return (ListSelectState) super.getState(markAsDirty);
}
@Override
public DataProvider getDataProvider() {
return internalGetDataProvider();
}
@Override
public void setDataProvider(DataProvider dataProvider) {
internalSetDataProvider(dataProvider);
}
}