123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821 |
- /*
- @ITMillApache2LicenseForJavaFiles@
- */
-
- package com.vaadin.ui;
-
- import java.io.Serializable;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedHashSet;
- import java.util.LinkedList;
- import java.util.Map;
- import java.util.Set;
-
- import com.vaadin.data.Container;
- import com.vaadin.data.Item;
- import com.vaadin.data.Property;
- import com.vaadin.data.util.IndexedContainer;
- import com.vaadin.event.DataBoundTransferable;
- import com.vaadin.event.dd.DragAndDropEvent;
- import com.vaadin.event.dd.DropTarget;
- import com.vaadin.event.dd.DropTargetDetailsImpl;
- import com.vaadin.event.dd.acceptcriteria.ClientCriterion;
- import com.vaadin.event.dd.acceptcriteria.ClientSideCriterion;
- import com.vaadin.terminal.KeyMapper;
- import com.vaadin.terminal.PaintException;
- import com.vaadin.terminal.PaintTarget;
- import com.vaadin.terminal.Resource;
- import com.vaadin.terminal.gwt.client.ui.dd.VIsOverId;
- import com.vaadin.terminal.gwt.client.ui.dd.VItemIdIs;
- import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation;
-
- /**
- * <p>
- * A class representing a selection of items the user has selected in a UI. The
- * set of choices is presented as a set of {@link com.vaadin.data.Item}s in a
- * {@link com.vaadin.data.Container}.
- * </p>
- *
- * <p>
- * A <code>Select</code> component may be in single- or multiselect mode.
- * Multiselect mode means that more than one item can be selected
- * simultaneously.
- * </p>
- *
- * @author IT Mill Ltd.
- * @version
- * @VERSION@
- * @since 5.0
- */
- @SuppressWarnings("serial")
- public abstract class AbstractSelect extends AbstractField implements
- Container, Container.Viewer, Container.PropertySetChangeListener,
- Container.PropertySetChangeNotifier, Container.ItemSetChangeNotifier,
- Container.ItemSetChangeListener {
-
- /**
- * Item caption mode: Item's ID's <code>String</code> representation is used
- * as caption.
- */
- public static final int ITEM_CAPTION_MODE_ID = 0;
- /**
- * Item caption mode: Item's <code>String</code> representation is used as
- * caption.
- */
- public static final int ITEM_CAPTION_MODE_ITEM = 1;
- /**
- * Item caption mode: Index of the item is used as caption. The index mode
- * can only be used with the containers implementing the
- * {@link com.vaadin.data.Container.Indexed} interface.
- */
- public static final int ITEM_CAPTION_MODE_INDEX = 2;
- /**
- * Item caption mode: If an Item has a caption it's used, if not, Item's
- * ID's <code>String</code> representation is used as caption. <b>This is
- * the default</b>.
- */
- public static final int ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID = 3;
- /**
- * Item caption mode: Captions must be explicitly specified.
- */
- public static final int ITEM_CAPTION_MODE_EXPLICIT = 4;
- /**
- * Item caption mode: Only icons are shown, captions are hidden.
- */
- public static final int ITEM_CAPTION_MODE_ICON_ONLY = 5;
- /**
- * Item caption mode: Item captions are read from property specified with
- * <code>setItemCaptionPropertyId</code>.
- */
- public static final int ITEM_CAPTION_MODE_PROPERTY = 6;
-
- /**
- * Interface for option filtering, used to filter options based on user
- * entered value. The value is matched to the item caption.
- * <code>FILTERINGMODE_OFF</code> (0) turns the filtering off.
- * <code>FILTERINGMODE_STARTSWITH</code> (1) matches from the start of the
- * caption. <code>FILTERINGMODE_CONTAINS</code> (1) matches anywhere in the
- * caption.
- */
- public interface Filtering extends Serializable {
- public static final int FILTERINGMODE_OFF = 0;
- public static final int FILTERINGMODE_STARTSWITH = 1;
- public static final int FILTERINGMODE_CONTAINS = 2;
-
- /**
- * Sets the option filtering mode.
- *
- * @param filteringMode
- * the filtering mode to use
- */
- public void setFilteringMode(int filteringMode);
-
- /**
- * Gets the current filtering mode.
- *
- * @return the filtering mode in use
- */
- public int getFilteringMode();
-
- }
-
- /**
- * Is the select in multiselect mode?
- */
- private boolean multiSelect = false;
-
- /**
- * Select options.
- */
- protected Container items;
-
- /**
- * Is the user allowed to add new options?
- */
- private boolean allowNewOptions;
-
- /**
- * Keymapper used to map key values.
- */
- protected KeyMapper itemIdMapper = new KeyMapper();
-
- /**
- * Item icons.
- */
- private final HashMap itemIcons = new HashMap();
-
- /**
- * Item captions.
- */
- private final HashMap itemCaptions = new HashMap();
-
- /**
- * Item caption mode.
- */
- private int itemCaptionMode = ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID;
-
- /**
- * Item caption source property id.
- */
- private Object itemCaptionPropertyId = null;
-
- /**
- * Item icon source property id.
- */
- private Object itemIconPropertyId = null;
-
- /**
- * List of property set change event listeners.
- */
- private Set propertySetEventListeners = null;
-
- /**
- * List of item set change event listeners.
- */
- private Set itemSetEventListeners = null;
-
- /**
- * Item id that represents null selection of this select.
- *
- * <p>
- * Data interface does not support nulls as item ids. Selecting the item
- * identified by this id is the same as selecting no items at all. This
- * setting only affects the single select mode.
- * </p>
- */
- private Object nullSelectionItemId = null;
-
- // Null (empty) selection is enabled by default
- private boolean nullSelectionAllowed = true;
- private NewItemHandler newItemHandler;
-
- // Caption (Item / Property) change listeners
- CaptionChangeListener captionChangeListener;
-
- /* Constructors */
-
- /**
- * Creates an empty Select. The caption is not used.
- */
- public AbstractSelect() {
- setContainerDataSource(new IndexedContainer());
- }
-
- /**
- * Creates an empty Select with caption.
- */
- public AbstractSelect(String caption) {
- setContainerDataSource(new IndexedContainer());
- setCaption(caption);
- }
-
- /**
- * Creates a new select that is connected to a data-source.
- *
- * @param caption
- * the Caption of the component.
- * @param dataSource
- * the Container datasource to be selected from by this select.
- */
- public AbstractSelect(String caption, Container dataSource) {
- setCaption(caption);
- setContainerDataSource(dataSource);
- }
-
- /**
- * Creates a new select that is filled from a collection of option values.
- *
- * @param caption
- * the Caption of this field.
- * @param options
- * the Collection containing the options.
- */
- public AbstractSelect(String caption, Collection options) {
-
- // Creates the options container and add given options to it
- final Container c = new IndexedContainer();
- if (options != null) {
- for (final Iterator i = options.iterator(); i.hasNext();) {
- c.addItem(i.next());
- }
- }
-
- setCaption(caption);
- setContainerDataSource(c);
- }
-
- /* Component methods */
-
- /**
- * Paints the content of this component.
- *
- * @param target
- * the Paint Event.
- * @throws PaintException
- * if the paint operation failed.
- */
- @Override
- public void paintContent(PaintTarget target) throws PaintException {
-
- // Paints field properties
- super.paintContent(target);
-
- // Paints select attributes
- if (isMultiSelect()) {
- target.addAttribute("selectmode", "multi");
- }
- if (isNewItemsAllowed()) {
- target.addAttribute("allownewitem", true);
- }
- if (isNullSelectionAllowed()) {
- target.addAttribute("nullselect", true);
- if (getNullSelectionItemId() != null) {
- target.addAttribute("nullselectitem", true);
- }
- }
-
- // Constructs selected keys array
- String[] selectedKeys;
- if (isMultiSelect()) {
- selectedKeys = new String[((Set) getValue()).size()];
- } else {
- selectedKeys = new String[(getValue() == null
- && getNullSelectionItemId() == null ? 0 : 1)];
- }
-
- // ==
- // first remove all previous item/property listeners
- getCaptionChangeListener().clear();
- // Paints the options and create array of selected id keys
-
- target.startTag("options");
- int keyIndex = 0;
- // Support for external null selection item id
- final Collection ids = getItemIds();
- if (isNullSelectionAllowed() && getNullSelectionItemId() != null
- && !ids.contains(getNullSelectionItemId())) {
- // Gets the option attribute values
- final Object id = getNullSelectionItemId();
- final String key = itemIdMapper.key(id);
- final String caption = getItemCaption(id);
- final Resource icon = getItemIcon(id);
- // Paints option
- target.startTag("so");
- if (icon != null) {
- target.addAttribute("icon", icon);
- }
- target.addAttribute("caption", caption);
- target.addAttribute("nullselection", true);
- target.addAttribute("key", key);
- if (isSelected(id)) {
- target.addAttribute("selected", true);
- selectedKeys[keyIndex++] = key;
- }
- target.endTag("so");
- }
-
- final Iterator i = getItemIds().iterator();
- // Paints the available selection options from data source
- while (i.hasNext()) {
- // Gets the option attribute values
- final Object id = i.next();
- if (!isNullSelectionAllowed() && id != null
- && id.equals(getNullSelectionItemId())) {
- // Remove item if it's the null selection item but null
- // selection is not allowed
- continue;
- }
- final String key = itemIdMapper.key(id);
- final String caption = getItemCaption(id);
- // add listener for each item, to cause repaint if an item changes
- getCaptionChangeListener().addNotifierForItem(id);
- final Resource icon = getItemIcon(id); // Paints the option
- target.startTag("so");
- if (icon != null) {
- target.addAttribute("icon", icon);
- }
- target.addAttribute("caption", caption);
- if (id != null && id.equals(getNullSelectionItemId())) {
- target.addAttribute("nullselection", true);
- }
- target.addAttribute("key", key);
- if (isSelected(id) && keyIndex < selectedKeys.length) {
- target.addAttribute("selected", true);
- selectedKeys[keyIndex++] = key;
- }
- target.endTag("so");
- }
- target.endTag("options");
- // ==
-
- // Paint variables
- target.addVariable(this, "selected", selectedKeys);
- if (isNewItemsAllowed()) {
- target.addVariable(this, "newitem", "");
- }
-
- }
-
- /**
- * 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<String, Object> variables) {
- super.changeVariables(source, variables);
-
- // New option entered (and it is allowed)
- if (isNewItemsAllowed()) {
- final String newitem = (String) variables.get("newitem");
- if (newitem != null && newitem.length() > 0) {
- getNewItemHandler().addNewItem(newitem);
- }
- }
-
- // Selection change
- if (variables.containsKey("selected")) {
- final String[] ka = (String[]) variables.get("selected");
-
- // Multiselect mode
- if (isMultiSelect()) {
-
- // TODO Optimize by adding repaintNotNeeded when applicable
-
- // Converts the key-array to id-set
- final LinkedList s = new LinkedList();
- for (int i = 0; i < ka.length; i++) {
- final Object id = itemIdMapper.get(ka[i]);
- if (!isNullSelectionAllowed()
- && (id == null || id == getNullSelectionItemId())) {
- // skip empty selection if nullselection is not allowed
- requestRepaint();
- } else if (id != null && containsId(id)) {
- s.add(id);
- }
- }
-
- if (!isNullSelectionAllowed() && s.size() < 1) {
- // empty selection not allowed, keep old value
- requestRepaint();
- return;
- }
-
- // Limits the deselection to the set of visible items
- // (non-visible items can not be deselected)
- final Collection visible = getVisibleItemIds();
- if (visible != null) {
- Set newsel = (Set) getValue();
- if (newsel == null) {
- newsel = new HashSet();
- } else {
- newsel = new HashSet(newsel);
- }
- newsel.removeAll(visible);
- newsel.addAll(s);
- setValue(newsel, true);
- }
- } else {
- // Single select mode
- if (!isNullSelectionAllowed()
- && (ka.length == 0 || ka[0] == null || ka[0] == getNullSelectionItemId())) {
- requestRepaint();
- return;
- }
- 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 (!isNullSelectionAllowed() && id == null) {
- requestRepaint();
- } else if (id != null
- && id.equals(getNullSelectionItemId())) {
- setValue(null, true);
- } else {
- setValue(id, true);
- }
- }
- }
- }
- }
-
- /**
- * TODO refine doc Setter for new item handler that is called when user adds
- * new item in newItemAllowed mode.
- *
- * @param newItemHandler
- */
- public void setNewItemHandler(NewItemHandler newItemHandler) {
- this.newItemHandler = newItemHandler;
- }
-
- /**
- * TODO refine doc
- *
- * @return
- */
- public NewItemHandler getNewItemHandler() {
- if (newItemHandler == null) {
- newItemHandler = new DefaultNewItemHandler();
- }
- return newItemHandler;
- }
-
- public interface NewItemHandler extends Serializable {
- void addNewItem(String newItemCaption);
- }
-
- /**
- * TODO refine doc
- *
- * This is a default class that handles adding new items that are typed by
- * user to selects container.
- *
- * By extending this class one may implement some logic on new item addition
- * like database inserts.
- *
- */
- public class DefaultNewItemHandler implements NewItemHandler {
- public void addNewItem(String newItemCaption) {
- // Checks for readonly
- if (isReadOnly()) {
- throw new Property.ReadOnlyException();
- }
-
- // Adds new option
- if (addItem(newItemCaption) != null) {
-
- // Sets the caption property, if used
- if (getItemCaptionPropertyId() != null) {
- try {
- getContainerProperty(newItemCaption,
- getItemCaptionPropertyId()).setValue(
- newItemCaption);
- } catch (final Property.ConversionException ignored) {
- /*
- * The conversion exception is safely ignored, the
- * caption is just missing
- */
- }
- }
- if (isMultiSelect()) {
- Set values = new HashSet((Collection) getValue());
- values.add(newItemCaption);
- setValue(values);
- } else {
- setValue(newItemCaption);
- }
- }
- }
- }
-
- /**
- * Gets the visible item ids. In Select, this returns list of all item ids,
- * but can be overriden in subclasses if they paint only part of the items
- * to the terminal or null if no items is visible.
- */
- public Collection getVisibleItemIds() {
- if (isVisible()) {
- return getItemIds();
- }
- return null;
- }
-
- /* Property methods */
-
- /**
- * Returns the type of the property. <code>getValue</code> and
- * <code>setValue</code> methods must be compatible with this type: one can
- * safely cast <code>getValue</code> to given type and pass any variable
- * assignable to this type as a parameter to <code>setValue</code>.
- *
- * @return the Type of the property.
- */
- @Override
- public Class getType() {
- if (isMultiSelect()) {
- return Set.class;
- } else {
- return Object.class;
- }
- }
-
- /**
- * Gets the selected item id or in multiselect mode a set of selected ids.
- *
- * @see com.vaadin.ui.AbstractField#getValue()
- */
- @Override
- public Object getValue() {
- final Object retValue = super.getValue();
-
- if (isMultiSelect()) {
-
- // If the return value is not a set
- if (retValue == null) {
- return new HashSet();
- }
- if (retValue instanceof Set) {
- return Collections.unmodifiableSet((Set) retValue);
- } else if (retValue instanceof Collection) {
- return new HashSet((Collection) retValue);
- } else {
- final Set s = new HashSet();
- if (items.containsId(retValue)) {
- s.add(retValue);
- }
- return s;
- }
-
- } else {
- return retValue;
- }
- }
-
- /**
- * Sets the visible value of the property.
- *
- * <p>
- * The value of the select is the selected item id. If the select is in
- * multiselect-mode, the value is a set of selected item keys. In
- * multiselect mode all collections of id:s can be assigned.
- * </p>
- *
- * @param newValue
- * the New selected item or collection of selected items.
- * @see com.vaadin.ui.AbstractField#setValue(java.lang.Object)
- */
- @Override
- public void setValue(Object newValue) throws Property.ReadOnlyException,
- Property.ConversionException {
- if (newValue == getNullSelectionItemId()) {
- newValue = null;
- }
-
- setValue(newValue, false);
- }
-
- /**
- * Sets the visible value of the property.
- *
- * <p>
- * The value of the select is the selected item id. If the select is in
- * multiselect-mode, the value is a set of selected item keys. In
- * multiselect mode all collections of id:s can be assigned.
- * </p>
- *
- * @param newValue
- * the New selected item or collection of selected items.
- * @param repaintIsNotNeeded
- * True if caller is sure that repaint is not needed.
- * @see com.vaadin.ui.AbstractField#setValue(java.lang.Object,
- * java.lang.Boolean)
- */
- @Override
- protected void setValue(Object newValue, boolean repaintIsNotNeeded)
- throws Property.ReadOnlyException, Property.ConversionException {
-
- if (isMultiSelect()) {
- if (newValue == null) {
- super.setValue(new HashSet(), repaintIsNotNeeded);
- } else if (Collection.class.isAssignableFrom(newValue.getClass())) {
- super.setValue(new HashSet((Collection) newValue),
- repaintIsNotNeeded);
- }
- } else if (newValue == null || items.containsId(newValue)) {
- super.setValue(newValue, repaintIsNotNeeded);
- }
- }
-
- /* Container methods */
-
- /**
- * Gets the item from the container with given id. If the container does not
- * contain the requested item, null is returned.
- *
- * @param itemId
- * the item id.
- * @return the item from the container.
- */
- public Item getItem(Object itemId) {
- return items.getItem(itemId);
- }
-
- /**
- * Gets the item Id collection from the container.
- *
- * @return the Collection of item ids.
- */
- public Collection getItemIds() {
- return items.getItemIds();
- }
-
- /**
- * Gets the property Id collection from the container.
- *
- * @return the Collection of property ids.
- */
- public Collection getContainerPropertyIds() {
- return items.getContainerPropertyIds();
- }
-
- /**
- * Gets the property type.
- *
- * @param propertyId
- * the Id identifying the property.
- * @see com.vaadin.data.Container#getType(java.lang.Object)
- */
- public Class getType(Object propertyId) {
- return items.getType(propertyId);
- }
-
- /*
- * Gets the number of items in the container.
- *
- * @return the Number of items in the container.
- *
- * @see com.vaadin.data.Container#size()
- */
- public int size() {
- return items.size();
- }
-
- /**
- * Tests, if the collection contains an item with given id.
- *
- * @param itemId
- * the Id the of item to be tested.
- */
- public boolean containsId(Object itemId) {
- if (itemId != null) {
- return items.containsId(itemId);
- } else {
- return false;
- }
- }
-
- /**
- * Gets the Property identified by the given itemId and propertyId from the
- * Container
- *
- * @see com.vaadin.data.Container#getContainerProperty(Object, Object)
- */
- public Property getContainerProperty(Object itemId, Object propertyId) {
- return items.getContainerProperty(itemId, propertyId);
- }
-
- /**
- * Adds the new property to all items. Adds a property with given id, type
- * and default value to all items in the container.
- *
- * This functionality is optional. If the function is unsupported, it always
- * returns false.
- *
- * @return True if the operation succeeded.
- * @see com.vaadin.data.Container#addContainerProperty(java.lang.Object,
- * java.lang.Class, java.lang.Object)
- */
- public boolean addContainerProperty(Object propertyId, Class<?> type,
- Object defaultValue) throws UnsupportedOperationException {
-
- final boolean retval = items.addContainerProperty(propertyId, type,
- defaultValue);
- if (retval && !(items instanceof Container.PropertySetChangeNotifier)) {
- firePropertySetChange();
- }
- return retval;
- }
-
- /**
- * Removes all items from the container.
- *
- * This functionality is optional. If the function is unsupported, it always
- * returns false.
- *
- * @return True if the operation succeeded.
- * @see com.vaadin.data.Container#removeAllItems()
- */
- public boolean removeAllItems() throws UnsupportedOperationException {
-
- final boolean retval = items.removeAllItems();
- itemIdMapper.removeAll();
- if (retval) {
- setValue(null);
- if (!(items instanceof Container.ItemSetChangeNotifier)) {
- fireItemSetChange();
- }
- }
- return retval;
- }
-
- /**
- * Creates a new item into container with container managed id. The id of
- * the created new item is returned. The item can be fetched with getItem()
- * method. if the creation fails, null is returned.
- *
- * @return the Id of the created item or null in case of failure.
- * @see com.vaadin.data.Container#addItem()
- */
- public Object addItem() throws UnsupportedOperationException {
-
- final Object retval = items.addItem();
- if (retval != null
- && !(items instanceof Container.ItemSetChangeNotifier)) {
- fireItemSetChange();
- }
- return retval;
- }
-
- /**
- * Create a new item into container. The created new item is returned and
- * ready for setting property values. if the creation fails, null is
- * returned. In case the container already contains the item, null is
- * returned.
- *
- * This functionality is optional. If the function is unsupported, it always
- * returns null.
- *
- * @param itemId
- * the Identification of the item to be created.
- * @return the Created item with the given id, or null in case of failure.
- * @see com.vaadin.data.Container#addItem(java.lang.Object)
- */
- public Item addItem(Object itemId) throws UnsupportedOperationException {
-
- final Item retval = items.addItem(itemId);
- if (retval != null
- && !(items instanceof Container.ItemSetChangeNotifier)) {
- fireItemSetChange();
- }
- return retval;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.Container#removeItem(java.lang.Object)
- */
- public boolean removeItem(Object itemId)
- throws UnsupportedOperationException {
-
- unselect(itemId);
- final boolean retval = items.removeItem(itemId);
- itemIdMapper.remove(itemId);
- if (retval && !(items instanceof Container.ItemSetChangeNotifier)) {
- fireItemSetChange();
- }
- return retval;
- }
-
- /**
- * Removes the property from all items. Removes a property with given id
- * from all the items in the container.
- *
- * This functionality is optional. If the function is unsupported, it always
- * returns false.
- *
- * @return True if the operation succeeded.
- * @see com.vaadin.data.Container#removeContainerProperty(java.lang.Object)
- */
- public boolean removeContainerProperty(Object propertyId)
- throws UnsupportedOperationException {
-
- final boolean retval = items.removeContainerProperty(propertyId);
- if (retval && !(items instanceof Container.PropertySetChangeNotifier)) {
- firePropertySetChange();
- }
- return retval;
- }
-
- /* Container.Viewer methods */
-
- /**
- * Sets the container as data-source for viewing.
- *
- * @param newDataSource
- * the new data source.
- */
- public void setContainerDataSource(Container newDataSource) {
- if (newDataSource == null) {
- newDataSource = new IndexedContainer();
- }
-
- getCaptionChangeListener().clear();
-
- if (items != newDataSource) {
-
- // Removes listeners from the old datasource
- if (items != null) {
- if (items instanceof Container.ItemSetChangeNotifier) {
- ((Container.ItemSetChangeNotifier) items)
- .removeListener(this);
- }
- if (items instanceof Container.PropertySetChangeNotifier) {
- ((Container.PropertySetChangeNotifier) items)
- .removeListener(this);
- }
- }
-
- // Assigns new data source
- items = newDataSource;
-
- // Clears itemIdMapper also
- itemIdMapper.removeAll();
-
- // Adds listeners
- if (items != null) {
- if (items instanceof Container.ItemSetChangeNotifier) {
- ((Container.ItemSetChangeNotifier) items).addListener(this);
- }
- if (items instanceof Container.PropertySetChangeNotifier) {
- ((Container.PropertySetChangeNotifier) items)
- .addListener(this);
- }
- }
-
- requestRepaint();
- }
- }
-
- /**
- * Gets the viewing data-source container.
- *
- * @see com.vaadin.data.Container.Viewer#getContainerDataSource()
- */
- public Container getContainerDataSource() {
- return items;
- }
-
- /* Select attributes */
-
- /**
- * Is the select in multiselect mode? In multiselect mode
- *
- * @return the Value of property multiSelect.
- */
- public boolean isMultiSelect() {
- return multiSelect;
- }
-
- /**
- * Sets the multiselect mode. Setting multiselect mode false may loose
- * selection information: if selected items set contains one or more
- * selected items, only one of the selected items is kept as selected.
- *
- * @param multiSelect
- * the New value of property multiSelect.
- */
- public void setMultiSelect(boolean multiSelect) {
- if (multiSelect && getNullSelectionItemId() != null) {
- throw new IllegalStateException(
- "Multiselect and NullSelectionItemId can not be set at the same time.");
- }
- if (multiSelect != this.multiSelect) {
-
- // Selection before mode change
- final Object oldValue = getValue();
-
- this.multiSelect = multiSelect;
-
- // Convert the value type
- if (multiSelect) {
- final Set s = new HashSet();
- if (oldValue != null) {
- s.add(oldValue);
- }
- setValue(s);
- } else {
- final Set s = (Set) oldValue;
- if (s == null || s.isEmpty()) {
- setValue(null);
- } else {
- // Set the single select to contain only the first
- // selected value in the multiselect
- setValue(s.iterator().next());
- }
- }
-
- requestRepaint();
- }
- }
-
- /**
- * Does the select allow adding new options by the user. If true, the new
- * options can be added to the Container. The text entered by the user is
- * used as id. Note that data-source must allow adding new items.
- *
- * @return True if additions are allowed.
- */
- public boolean isNewItemsAllowed() {
-
- return allowNewOptions;
- }
-
- /**
- * Enables or disables possibility to add new options by the user.
- *
- * @param allowNewOptions
- * the New value of property allowNewOptions.
- */
- public void setNewItemsAllowed(boolean allowNewOptions) {
-
- // Only handle change requests
- if (this.allowNewOptions != allowNewOptions) {
-
- this.allowNewOptions = allowNewOptions;
-
- requestRepaint();
- }
- }
-
- /**
- * Override the caption of an item. Setting caption explicitly overrides id,
- * item and index captions.
- *
- * @param itemId
- * the id of the item to be recaptioned.
- * @param caption
- * the New caption.
- */
- public void setItemCaption(Object itemId, String caption) {
- if (itemId != null) {
- itemCaptions.put(itemId, caption);
- requestRepaint();
- }
- }
-
- /**
- * Gets the caption of an item. The caption is generated as specified by the
- * item caption mode. See <code>setItemCaptionMode()</code> for more
- * details.
- *
- * @param itemId
- * the id of the item to be queried.
- * @return the caption for specified item.
- */
- public String getItemCaption(Object itemId) {
-
- // Null items can not be found
- if (itemId == null) {
- return null;
- }
-
- String caption = null;
-
- switch (getItemCaptionMode()) {
-
- case ITEM_CAPTION_MODE_ID:
- caption = itemId.toString();
- break;
-
- case ITEM_CAPTION_MODE_INDEX:
- if (items instanceof Container.Indexed) {
- caption = String.valueOf(((Container.Indexed) items)
- .indexOfId(itemId));
- } else {
- caption = "ERROR: Container is not indexed";
- }
- break;
-
- case ITEM_CAPTION_MODE_ITEM:
- final Item i = getItem(itemId);
- if (i != null) {
- caption = i.toString();
- }
- break;
-
- case ITEM_CAPTION_MODE_EXPLICIT:
- caption = (String) itemCaptions.get(itemId);
- break;
-
- case ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID:
- caption = (String) itemCaptions.get(itemId);
- if (caption == null) {
- caption = itemId.toString();
- }
- break;
-
- case ITEM_CAPTION_MODE_PROPERTY:
- final Property p = getContainerProperty(itemId,
- getItemCaptionPropertyId());
- if (p != null) {
- caption = p.toString();
- }
- break;
- }
-
- // All items must have some captions
- return caption != null ? caption : "";
- }
-
- /**
- * Sets the icon for an item.
- *
- * @param itemId
- * the id of the item to be assigned an icon.
- * @param icon
- * the New icon.
- */
- public void setItemIcon(Object itemId, Resource icon) {
- if (itemId != null) {
- if (icon == null) {
- itemIcons.remove(itemId);
- } else {
- itemIcons.put(itemId, icon);
- }
- requestRepaint();
- }
- }
-
- /**
- * Gets the item icon.
- *
- * @param itemId
- * the id of the item to be assigned an icon.
- * @return the Icon for the item or null, if not specified.
- */
- public Resource getItemIcon(Object itemId) {
- final Resource explicit = (Resource) itemIcons.get(itemId);
- if (explicit != null) {
- return explicit;
- }
-
- if (getItemIconPropertyId() == null) {
- return null;
- }
-
- final Property ip = getContainerProperty(itemId,
- getItemIconPropertyId());
- if (ip == null) {
- return null;
- }
- final Object icon = ip.getValue();
- if (icon instanceof Resource) {
- return (Resource) icon;
- }
-
- return null;
- }
-
- /**
- * Sets the item caption mode.
- *
- * <p>
- * The mode can be one of the following ones:
- * <ul>
- * <li><code>ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID</code> : Items
- * Id-objects <code>toString</code> is used as item caption. If caption is
- * explicitly specified, it overrides the id-caption.
- * <li><code>ITEM_CAPTION_MODE_ID</code> : Items Id-objects
- * <code>toString</code> is used as item caption.</li>
- * <li><code>ITEM_CAPTION_MODE_ITEM</code> : Item-objects
- * <code>toString</code> is used as item caption.</li>
- * <li><code>ITEM_CAPTION_MODE_INDEX</code> : The index of the item is used
- * as item caption. The index mode can only be used with the containers
- * implementing <code>Container.Indexed</code> interface.</li>
- * <li><code>ITEM_CAPTION_MODE_EXPLICIT</code> : The item captions must be
- * explicitly specified.</li>
- * <li><code>ITEM_CAPTION_MODE_PROPERTY</code> : The item captions are read
- * from property, that must be specified with
- * <code>setItemCaptionPropertyId</code>.</li>
- * </ul>
- * The <code>ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID</code> is the default
- * mode.
- * </p>
- *
- * @param mode
- * the One of the modes listed above.
- */
- public void setItemCaptionMode(int mode) {
- if (ITEM_CAPTION_MODE_ID <= mode && mode <= ITEM_CAPTION_MODE_PROPERTY) {
- itemCaptionMode = mode;
- requestRepaint();
- }
- }
-
- /**
- * Gets the item caption mode.
- *
- * <p>
- * The mode can be one of the following ones:
- * <ul>
- * <li><code>ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID</code> : Items
- * Id-objects <code>toString</code> is used as item caption. If caption is
- * explicitly specified, it overrides the id-caption.
- * <li><code>ITEM_CAPTION_MODE_ID</code> : Items Id-objects
- * <code>toString</code> is used as item caption.</li>
- * <li><code>ITEM_CAPTION_MODE_ITEM</code> : Item-objects
- * <code>toString</code> is used as item caption.</li>
- * <li><code>ITEM_CAPTION_MODE_INDEX</code> : The index of the item is used
- * as item caption. The index mode can only be used with the containers
- * implementing <code>Container.Indexed</code> interface.</li>
- * <li><code>ITEM_CAPTION_MODE_EXPLICIT</code> : The item captions must be
- * explicitly specified.</li>
- * <li><code>ITEM_CAPTION_MODE_PROPERTY</code> : The item captions are read
- * from property, that must be specified with
- * <code>setItemCaptionPropertyId</code>.</li>
- * </ul>
- * The <code>ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID</code> is the default
- * mode.
- * </p>
- *
- * @return the One of the modes listed above.
- */
- public int getItemCaptionMode() {
- return itemCaptionMode;
- }
-
- /**
- * Sets the item caption property.
- *
- * <p>
- * Setting the id to a existing property implicitly sets the item caption
- * mode to <code>ITEM_CAPTION_MODE_PROPERTY</code>. If the object is in
- * <code>ITEM_CAPTION_MODE_PROPERTY</code> mode, setting caption property id
- * null resets the item caption mode to
- * <code>ITEM_CAPTION_EXPLICIT_DEFAULTS_ID</code>.
- * </p>
- *
- * <p>
- * Setting the property id to null disables this feature. The id is null by
- * default
- * </p>
- * .
- *
- * @param propertyId
- * the id of the property.
- *
- */
- public void setItemCaptionPropertyId(Object propertyId) {
- if (propertyId != null) {
- itemCaptionPropertyId = propertyId;
- setItemCaptionMode(ITEM_CAPTION_MODE_PROPERTY);
- requestRepaint();
- } else {
- itemCaptionPropertyId = null;
- if (getItemCaptionMode() == ITEM_CAPTION_MODE_PROPERTY) {
- setItemCaptionMode(ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID);
- }
- requestRepaint();
- }
- }
-
- /**
- * Gets the item caption property.
- *
- * @return the Id of the property used as item caption source.
- */
- public Object getItemCaptionPropertyId() {
- return itemCaptionPropertyId;
- }
-
- /**
- * Sets the item icon property.
- *
- * <p>
- * If the property id is set to a valid value, each item is given an icon
- * got from the given property of the items. The type of the property must
- * be assignable to Icon.
- * </p>
- *
- * <p>
- * Note : The icons set with <code>setItemIcon</code> function override the
- * icons from the property.
- * </p>
- *
- * <p>
- * Setting the property id to null disables this feature. The id is null by
- * default
- * </p>
- * .
- *
- * @param propertyId
- * the Id of the property that specifies icons for items.
- */
- public void setItemIconPropertyId(Object propertyId) {
- if ((propertyId != null)
- && Resource.class.isAssignableFrom(getType(propertyId))) {
- itemIconPropertyId = propertyId;
- } else {
- itemIconPropertyId = null;
- }
- requestRepaint();
- }
-
- /**
- * Gets the item icon property.
- *
- * <p>
- * If the property id is set to a valid value, each item is given an icon
- * got from the given property of the items. The type of the property must
- * be assignable to Icon.
- * </p>
- *
- * <p>
- * Note : The icons set with <code>setItemIcon</code> function override the
- * icons from the property.
- * </p>
- *
- * <p>
- * Setting the property id to null disables this feature. The id is null by
- * default
- * </p>
- * .
- *
- * @return the Id of the property containing the item icons.
- */
- public Object getItemIconPropertyId() {
- return itemIconPropertyId;
- }
-
- /**
- * Tests if an item is selected.
- *
- * <p>
- * In single select mode testing selection status of the item identified by
- * {@link #getNullSelectionItemId()} returns true if the value of the
- * property is null.
- * </p>
- *
- * @param itemId
- * the Id the of the item to be tested.
- * @see #getNullSelectionItemId()
- * @see #setNullSelectionItemId(Object)
- *
- */
- public boolean isSelected(Object itemId) {
- if (itemId == null) {
- return false;
- }
- if (isMultiSelect()) {
- return ((Set) getValue()).contains(itemId);
- } else {
- final Object value = getValue();
- return itemId.equals(value == null ? getNullSelectionItemId()
- : value);
- }
- }
-
- /**
- * Selects an item.
- *
- * <p>
- * In single select mode selecting item identified by
- * {@link #getNullSelectionItemId()} sets the value of the property to null.
- * </p>
- *
- * @param itemId
- * the identifier of Item to be selected.
- * @see #getNullSelectionItemId()
- * @see #setNullSelectionItemId(Object)
- *
- */
- public void select(Object itemId) {
- if (!isMultiSelect()) {
- setValue(itemId);
- } else if (!isSelected(itemId) && itemId != null
- && items.containsId(itemId)) {
- final Set s = new HashSet((Set) getValue());
- s.add(itemId);
- setValue(s);
- }
- }
-
- /**
- * Unselects an item.
- *
- * @param itemId
- * the identifier of the Item to be unselected.
- * @see #getNullSelectionItemId()
- * @see #setNullSelectionItemId(Object)
- *
- */
- public void unselect(Object itemId) {
- if (isSelected(itemId)) {
- if (isMultiSelect()) {
- final Set s = new HashSet((Set) getValue());
- s.remove(itemId);
- setValue(s);
- } else {
- setValue(null);
- }
- }
- }
-
- /**
- * Notifies this listener that the Containers contents has changed.
- *
- * @see com.vaadin.data.Container.PropertySetChangeListener#containerPropertySetChange(com.vaadin.data.Container.PropertySetChangeEvent)
- */
- public void containerPropertySetChange(
- Container.PropertySetChangeEvent event) {
- firePropertySetChange();
- }
-
- /**
- * Adds a new Property set change listener for this Container.
- *
- * @see com.vaadin.data.Container.PropertySetChangeNotifier#addListener(com.vaadin.data.Container.PropertySetChangeListener)
- */
- public void addListener(Container.PropertySetChangeListener listener) {
- if (propertySetEventListeners == null) {
- propertySetEventListeners = new LinkedHashSet();
- }
- propertySetEventListeners.add(listener);
- }
-
- /**
- * Removes a previously registered Property set change listener.
- *
- * @see com.vaadin.data.Container.PropertySetChangeNotifier#removeListener(com.vaadin.data.Container.PropertySetChangeListener)
- */
- public void removeListener(Container.PropertySetChangeListener listener) {
- if (propertySetEventListeners != null) {
- propertySetEventListeners.remove(listener);
- if (propertySetEventListeners.isEmpty()) {
- propertySetEventListeners = null;
- }
- }
- }
-
- /**
- * Adds an Item set change listener for the object.
- *
- * @see com.vaadin.data.Container.ItemSetChangeNotifier#addListener(com.vaadin.data.Container.ItemSetChangeListener)
- */
- public void addListener(Container.ItemSetChangeListener listener) {
- if (itemSetEventListeners == null) {
- itemSetEventListeners = new LinkedHashSet();
- }
- itemSetEventListeners.add(listener);
- }
-
- /**
- * Removes the Item set change listener from the object.
- *
- * @see com.vaadin.data.Container.ItemSetChangeNotifier#removeListener(com.vaadin.data.Container.ItemSetChangeListener)
- */
- public void removeListener(Container.ItemSetChangeListener listener) {
- if (itemSetEventListeners != null) {
- itemSetEventListeners.remove(listener);
- if (itemSetEventListeners.isEmpty()) {
- itemSetEventListeners = null;
- }
- }
- }
-
- /**
- * Lets the listener know a Containers Item set has changed.
- *
- * @see com.vaadin.data.Container.ItemSetChangeListener#containerItemSetChange(com.vaadin.data.Container.ItemSetChangeEvent)
- */
- public void containerItemSetChange(Container.ItemSetChangeEvent event) {
- // Clears the item id mapping table
- itemIdMapper.removeAll();
-
- // Notify all listeners
- fireItemSetChange();
- }
-
- /**
- * Fires the property set change event.
- */
- protected void firePropertySetChange() {
- if (propertySetEventListeners != null
- && !propertySetEventListeners.isEmpty()) {
- final Container.PropertySetChangeEvent event = new PropertySetChangeEvent();
- final Object[] listeners = propertySetEventListeners.toArray();
- for (int i = 0; i < listeners.length; i++) {
- ((Container.PropertySetChangeListener) listeners[i])
- .containerPropertySetChange(event);
- }
- }
- requestRepaint();
- }
-
- /**
- * Fires the item set change event.
- */
- protected void fireItemSetChange() {
- if (itemSetEventListeners != null && !itemSetEventListeners.isEmpty()) {
- final Container.ItemSetChangeEvent event = new ItemSetChangeEvent();
- final Object[] listeners = itemSetEventListeners.toArray();
- for (int i = 0; i < listeners.length; i++) {
- ((Container.ItemSetChangeListener) listeners[i])
- .containerItemSetChange(event);
- }
- }
- requestRepaint();
- }
-
- /**
- * Implementation of item set change event.
- */
- private class ItemSetChangeEvent implements Serializable,
- Container.ItemSetChangeEvent {
-
- /**
- * Gets the Property where the event occurred.
- *
- * @see com.vaadin.data.Container.ItemSetChangeEvent#getContainer()
- */
- public Container getContainer() {
- return AbstractSelect.this;
- }
-
- }
-
- /**
- * Implementation of property set change event.
- */
- private class PropertySetChangeEvent implements
- Container.PropertySetChangeEvent, Serializable {
-
- /**
- * Retrieves the Container whose contents have been modified.
- *
- * @see com.vaadin.data.Container.PropertySetChangeEvent#getContainer()
- */
- public Container getContainer() {
- return AbstractSelect.this;
- }
-
- }
-
- /**
- * For multi-selectable fields, also an empty collection of values is
- * considered to be an empty field.
- *
- * @see AbstractField#isEmpty().
- */
- @Override
- protected boolean isEmpty() {
- if (!multiSelect) {
- return super.isEmpty();
- } else {
- Object value = getValue();
- return super.isEmpty()
- || (value instanceof Collection && ((Collection) value)
- .isEmpty());
- }
- }
-
- /**
- * Allow of disallow empty selection. If the select is in single-select
- * mode, you can make an item represent the empty selection by calling
- * <code>setNullSelectionItemId()</code>. This way you can for instance set
- * an icon and caption for the null selection item.
- *
- * @param nullSelectionAllowed
- * whether or not to allow empty selection
- * @see #setNullSelectionItemId(Object)
- * @see #isNullSelectionAllowed()
- */
- public void setNullSelectionAllowed(boolean nullSelectionAllowed) {
- if (nullSelectionAllowed != this.nullSelectionAllowed) {
- this.nullSelectionAllowed = nullSelectionAllowed;
- requestRepaint();
- }
- }
-
- /**
- * Checks if null empty selection is allowed.
- *
- * @return whether or not empty selection is allowed
- * @see #setNullSelectionAllowed(boolean)
- */
- public boolean isNullSelectionAllowed() {
- return nullSelectionAllowed;
- }
-
- /**
- * Returns the item id that represents null value of this select in single
- * select mode.
- *
- * <p>
- * Data interface does not support nulls as item ids. Selecting the item
- * identified by this id is the same as selecting no items at all. This
- * setting only affects the single select mode.
- * </p>
- *
- * @return the Object Null value item id.
- * @see #setNullSelectionItemId(Object)
- * @see #isSelected(Object)
- * @see #select(Object)
- */
- public final Object getNullSelectionItemId() {
- return nullSelectionItemId;
- }
-
- /**
- * Sets the item id that represents null value of this select.
- *
- * <p>
- * Data interface does not support nulls as item ids. Selecting the item
- * idetified by this id is the same as selecting no items at all. This
- * setting only affects the single select mode.
- * </p>
- *
- * @param nullSelectionItemId
- * the nullSelectionItemId to set.
- * @see #getNullSelectionItemId()
- * @see #isSelected(Object)
- * @see #select(Object)
- */
- public void setNullSelectionItemId(Object nullSelectionItemId) {
- if (nullSelectionItemId != null && isMultiSelect()) {
- throw new IllegalStateException(
- "Multiselect and NullSelectionItemId can not be set at the same time.");
- }
- this.nullSelectionItemId = nullSelectionItemId;
- }
-
- /**
- * Notifies the component that it is connected to an application.
- *
- * @see com.vaadin.ui.AbstractField#attach()
- */
- @Override
- public void attach() {
- super.attach();
- }
-
- /**
- * Detaches the component from application.
- *
- * @see com.vaadin.ui.AbstractComponent#detach()
- */
- @Override
- public void detach() {
- getCaptionChangeListener().clear();
- super.detach();
- }
-
- // Caption change listener
- protected CaptionChangeListener getCaptionChangeListener() {
- if (captionChangeListener == null) {
- captionChangeListener = new CaptionChangeListener();
- }
- return captionChangeListener;
- }
-
- /**
- * This is a listener helper for Item and Property changes that should cause
- * a repaint. It should be attached to all items that are displayed, and the
- * default implementation does this in paintContent(). Especially
- * "lazyloading" components should take care to add and remove listeners as
- * appropriate. Call addNotifierForItem() for each painted item (and
- * remember to clear).
- *
- * NOTE: singleton, use getCaptionChangeListener().
- *
- */
- protected class CaptionChangeListener implements
- Item.PropertySetChangeListener, Property.ValueChangeListener {
-
- HashSet captionChangeNotifiers = new HashSet();
-
- public void addNotifierForItem(Object itemId) {
- switch (getItemCaptionMode()) {
- case ITEM_CAPTION_MODE_ITEM:
- final Item i = getItem(itemId);
- if (i == null) {
- return;
- }
- if (i instanceof Item.PropertySetChangeNotifier) {
- ((Item.PropertySetChangeNotifier) i)
- .addListener(getCaptionChangeListener());
- captionChangeNotifiers.add(i);
- }
- Collection pids = i.getItemPropertyIds();
- if (pids != null) {
- for (Iterator it = pids.iterator(); it.hasNext();) {
- Property p = i.getItemProperty(it.next());
- if (p != null
- && p instanceof Property.ValueChangeNotifier) {
- ((Property.ValueChangeNotifier) p)
- .addListener(getCaptionChangeListener());
- captionChangeNotifiers.add(p);
- }
- }
-
- }
- break;
- case ITEM_CAPTION_MODE_PROPERTY:
- final Property p = getContainerProperty(itemId,
- getItemCaptionPropertyId());
- if (p != null && p instanceof Property.ValueChangeNotifier) {
- ((Property.ValueChangeNotifier) p)
- .addListener(getCaptionChangeListener());
- captionChangeNotifiers.add(p);
- }
- break;
-
- }
- }
-
- public void clear() {
- for (Iterator it = captionChangeNotifiers.iterator(); it.hasNext();) {
- Object notifier = it.next();
- if (notifier instanceof Item.PropertySetChangeNotifier) {
- ((Item.PropertySetChangeNotifier) notifier)
- .removeListener(getCaptionChangeListener());
- } else {
- ((Property.ValueChangeNotifier) notifier)
- .removeListener(getCaptionChangeListener());
- }
- }
- }
-
- public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
- requestRepaint();
- }
-
- public void itemPropertySetChange(
- com.vaadin.data.Item.PropertySetChangeEvent event) {
- requestRepaint();
- }
-
- }
-
- /**
- * Criterion which accepts a drop only if the drop target is (one of) the
- * given item identifier(s). Meaningful only for drop targets that extends
- * AbstractSelect.
- *
- * @since 6.3
- */
- @ClientCriterion(VIsOverId.class)
- public static class DropTargetItemId extends AbstractItemSetCriterion {
-
- public DropTargetItemId(AbstractSelect select, Object... itemId) {
- super(select, itemId);
- }
-
- public boolean accept(DragAndDropEvent dragEvent) {
- AbstractSelectDropTargetDetails dropTargetData = (AbstractSelectDropTargetDetails) dragEvent
- .getDropTargetDetails();
- return itemIds.contains(dropTargetData.getItemIdOver());
- }
-
- }
-
- /**
- * TODO Javadoc!
- *
- * @since 6.3
- *
- */
- private static abstract class AbstractItemSetCriterion extends
- ClientSideCriterion {
- protected final Collection<Object> itemIds = new HashSet<Object>();
- private AbstractSelect select;
-
- public AbstractItemSetCriterion(AbstractSelect select, Object... itemId) {
- if (itemIds == null || select == null) {
- throw new IllegalArgumentException(
- "Accepted item identifiers must be accepted.");
- }
- Collections.addAll(itemIds, itemId);
- this.select = select;
- }
-
- @Override
- public void paintContent(PaintTarget target) throws PaintException {
- super.paintContent(target);
- String[] keys = new String[itemIds.size()];
- int i = 0;
- for (Object itemId : itemIds) {
- String key = select.itemIdMapper.key(itemId);
- keys[i++] = key;
- }
- target.addVariable(select, "keys", keys);
- }
-
- }
-
- /**
- * Criterion which accepts a drop only if the transferable contains the
- * given item identifier(s). The item ids relate to the drag source
- * (AbstractSelect).
- *
- * @since 6.3
- */
- @ClientCriterion(VItemIdIs.class)
- public static class TransferableContainsItemId extends
- AbstractItemSetCriterion {
- public TransferableContainsItemId(AbstractSelect select,
- Object... itemId) {
- super(select, itemId);
- }
-
- public boolean accept(DragAndDropEvent dragEvent) {
- DataBoundTransferable transferable = (DataBoundTransferable) dragEvent
- .getTransferable();
- return itemIds.contains(transferable.getItemId());
- }
-
- }
-
- /**
- * Helper implementation for subclasses that implement {@link DropTarget}.
- *
- * @since 6.3
- */
- public class AbstractSelectDropTargetDetails extends DropTargetDetailsImpl {
-
- /**
- * The treenode id over which the drag event happened.
- */
- protected Object idOver;
-
- /**
- * Constructor that automatically converts itemIdOver key to
- * corresponding item Id
- *
- */
- protected AbstractSelectDropTargetDetails(
- Map<String, Object> rawVariables) {
- super(rawVariables, (DropTarget) AbstractSelect.this);
- // eagar fetch itemid, mapper may be emptied
- String keyover = (String) getData("itemIdOver");
- if (keyover != null) {
- idOver = itemIdMapper.get(keyover);
- }
- }
-
- /**
- * If the drag operation is currently over an {@link Item}, this method
- * returns the identifier of that {@link Item}.
- *
- */
- public Object getItemIdOver() {
- return idOver;
- }
-
- /**
- * Returns a detailed vertical location where the drop happened on Item.
- */
- public VerticalDropLocation getDropLocation() {
- String detail = (String) getData("detail");
- if (detail == null) {
- return null;
- }
- return VerticalDropLocation.valueOf(detail);
- }
-
- }
-
- }
|