You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractSelect.java 64KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  1. /*
  2. * @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.ui;
  5. import java.io.Serializable;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8. import java.util.HashMap;
  9. import java.util.HashSet;
  10. import java.util.Iterator;
  11. import java.util.LinkedHashSet;
  12. import java.util.LinkedList;
  13. import java.util.Map;
  14. import java.util.Set;
  15. import com.vaadin.data.Container;
  16. import com.vaadin.data.Item;
  17. import com.vaadin.data.Property;
  18. import com.vaadin.data.util.IndexedContainer;
  19. import com.vaadin.event.DataBoundTransferable;
  20. import com.vaadin.event.Transferable;
  21. import com.vaadin.event.dd.DragAndDropEvent;
  22. import com.vaadin.event.dd.DropTarget;
  23. import com.vaadin.event.dd.TargetDetailsImpl;
  24. import com.vaadin.event.dd.acceptcriteria.ClientSideCriterion;
  25. import com.vaadin.event.dd.acceptcriteria.ContainsDataFlavor;
  26. import com.vaadin.event.dd.acceptcriteria.TargetDetailIs;
  27. import com.vaadin.terminal.KeyMapper;
  28. import com.vaadin.terminal.PaintException;
  29. import com.vaadin.terminal.PaintTarget;
  30. import com.vaadin.terminal.Resource;
  31. import com.vaadin.terminal.Vaadin6Component;
  32. import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation;
  33. import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
  34. /**
  35. * <p>
  36. * A class representing a selection of items the user has selected in a UI. The
  37. * set of choices is presented as a set of {@link com.vaadin.data.Item}s in a
  38. * {@link com.vaadin.data.Container}.
  39. * </p>
  40. *
  41. * <p>
  42. * A <code>Select</code> component may be in single- or multiselect mode.
  43. * Multiselect mode means that more than one item can be selected
  44. * simultaneously.
  45. * </p>
  46. *
  47. * @author Vaadin Ltd.
  48. * @version
  49. * @VERSION@
  50. * @since 5.0
  51. */
  52. @SuppressWarnings("serial")
  53. // TODO currently cannot specify type more precisely in case of multi-select
  54. public abstract class AbstractSelect extends AbstractField<Object> implements
  55. Container, Container.Viewer, Container.PropertySetChangeListener,
  56. Container.PropertySetChangeNotifier, Container.ItemSetChangeNotifier,
  57. Container.ItemSetChangeListener, Vaadin6Component {
  58. public enum ItemCaptionMode {
  59. /**
  60. * Item caption mode: Item's ID's <code>String</code> representation is
  61. * used as caption.
  62. */
  63. ID,
  64. /**
  65. * Item caption mode: Item's <code>String</code> representation is used
  66. * as caption.
  67. */
  68. ITEM,
  69. /**
  70. * Item caption mode: Index of the item is used as caption. The index
  71. * mode can only be used with the containers implementing the
  72. * {@link com.vaadin.data.Container.Indexed} interface.
  73. */
  74. INDEX,
  75. /**
  76. * Item caption mode: If an Item has a caption it's used, if not, Item's
  77. * ID's <code>String</code> representation is used as caption. <b>This
  78. * is the default</b>.
  79. */
  80. EXPLICIT_DEFAULTS_ID,
  81. /**
  82. * Item caption mode: Captions must be explicitly specified.
  83. */
  84. EXPLICIT,
  85. /**
  86. * Item caption mode: Only icons are shown, captions are hidden.
  87. */
  88. ICON_ONLY,
  89. /**
  90. * Item caption mode: Item captions are read from property specified
  91. * with <code>setItemCaptionPropertyId</code>.
  92. */
  93. PROPERTY;
  94. }
  95. /**
  96. * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead
  97. */
  98. @Deprecated
  99. public static final ItemCaptionMode ITEM_CAPTION_MODE_ID = ItemCaptionMode.ID;
  100. /**
  101. * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead
  102. */
  103. @Deprecated
  104. public static final ItemCaptionMode ITEM_CAPTION_MODE_ITEM = ItemCaptionMode.ITEM;
  105. /**
  106. * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead
  107. */
  108. @Deprecated
  109. public static final ItemCaptionMode ITEM_CAPTION_MODE_INDEX = ItemCaptionMode.INDEX;
  110. /**
  111. * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead
  112. */
  113. @Deprecated
  114. public static final ItemCaptionMode ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID = ItemCaptionMode.EXPLICIT_DEFAULTS_ID;
  115. /**
  116. * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead
  117. */
  118. @Deprecated
  119. public static final ItemCaptionMode ITEM_CAPTION_MODE_EXPLICIT = ItemCaptionMode.EXPLICIT;
  120. /**
  121. * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead
  122. */
  123. @Deprecated
  124. public static final ItemCaptionMode ITEM_CAPTION_MODE_ICON_ONLY = ItemCaptionMode.ICON_ONLY;
  125. /**
  126. * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead
  127. */
  128. @Deprecated
  129. public static final ItemCaptionMode ITEM_CAPTION_MODE_PROPERTY = ItemCaptionMode.PROPERTY;
  130. /**
  131. * Interface for option filtering, used to filter options based on user
  132. * entered value. The value is matched to the item caption.
  133. * <code>FILTERINGMODE_OFF</code> (0) turns the filtering off.
  134. * <code>FILTERINGMODE_STARTSWITH</code> (1) matches from the start of the
  135. * caption. <code>FILTERINGMODE_CONTAINS</code> (1) matches anywhere in the
  136. * caption.
  137. */
  138. public interface Filtering extends Serializable {
  139. public static final int FILTERINGMODE_OFF = 0;
  140. public static final int FILTERINGMODE_STARTSWITH = 1;
  141. public static final int FILTERINGMODE_CONTAINS = 2;
  142. /**
  143. * Sets the option filtering mode.
  144. *
  145. * @param filteringMode
  146. * the filtering mode to use
  147. */
  148. public void setFilteringMode(int filteringMode);
  149. /**
  150. * Gets the current filtering mode.
  151. *
  152. * @return the filtering mode in use
  153. */
  154. public int getFilteringMode();
  155. }
  156. /**
  157. * Multi select modes that controls how multi select behaves.
  158. */
  159. public enum MultiSelectMode {
  160. /**
  161. * The default behavior of the multi select mode
  162. */
  163. DEFAULT,
  164. /**
  165. * The previous more simple behavior of the multselect
  166. */
  167. SIMPLE
  168. }
  169. /**
  170. * Is the select in multiselect mode?
  171. */
  172. private boolean multiSelect = false;
  173. /**
  174. * Select options.
  175. */
  176. protected Container items;
  177. /**
  178. * Is the user allowed to add new options?
  179. */
  180. private boolean allowNewOptions;
  181. /**
  182. * Keymapper used to map key values.
  183. */
  184. protected KeyMapper<Object> itemIdMapper = new KeyMapper<Object>();
  185. /**
  186. * Item icons.
  187. */
  188. private final HashMap<Object, Resource> itemIcons = new HashMap<Object, Resource>();
  189. /**
  190. * Item captions.
  191. */
  192. private final HashMap<Object, String> itemCaptions = new HashMap<Object, String>();
  193. /**
  194. * Item caption mode.
  195. */
  196. private ItemCaptionMode itemCaptionMode = ItemCaptionMode.EXPLICIT_DEFAULTS_ID;
  197. /**
  198. * Item caption source property id.
  199. */
  200. private Object itemCaptionPropertyId = null;
  201. /**
  202. * Item icon source property id.
  203. */
  204. private Object itemIconPropertyId = null;
  205. /**
  206. * List of property set change event listeners.
  207. */
  208. private Set<Container.PropertySetChangeListener> propertySetEventListeners = null;
  209. /**
  210. * List of item set change event listeners.
  211. */
  212. private Set<Container.ItemSetChangeListener> itemSetEventListeners = null;
  213. /**
  214. * Item id that represents null selection of this select.
  215. *
  216. * <p>
  217. * Data interface does not support nulls as item ids. Selecting the item
  218. * identified by this id is the same as selecting no items at all. This
  219. * setting only affects the single select mode.
  220. * </p>
  221. */
  222. private Object nullSelectionItemId = null;
  223. // Null (empty) selection is enabled by default
  224. private boolean nullSelectionAllowed = true;
  225. private NewItemHandler newItemHandler;
  226. // Caption (Item / Property) change listeners
  227. CaptionChangeListener captionChangeListener;
  228. /* Constructors */
  229. /**
  230. * Creates an empty Select. The caption is not used.
  231. */
  232. public AbstractSelect() {
  233. setContainerDataSource(new IndexedContainer());
  234. }
  235. /**
  236. * Creates an empty Select with caption.
  237. */
  238. public AbstractSelect(String caption) {
  239. setContainerDataSource(new IndexedContainer());
  240. setCaption(caption);
  241. }
  242. /**
  243. * Creates a new select that is connected to a data-source.
  244. *
  245. * @param caption
  246. * the Caption of the component.
  247. * @param dataSource
  248. * the Container datasource to be selected from by this select.
  249. */
  250. public AbstractSelect(String caption, Container dataSource) {
  251. setCaption(caption);
  252. setContainerDataSource(dataSource);
  253. }
  254. /**
  255. * Creates a new select that is filled from a collection of option values.
  256. *
  257. * @param caption
  258. * the Caption of this field.
  259. * @param options
  260. * the Collection containing the options.
  261. */
  262. public AbstractSelect(String caption, Collection<?> options) {
  263. // Creates the options container and add given options to it
  264. final Container c = new IndexedContainer();
  265. if (options != null) {
  266. for (final Iterator<?> i = options.iterator(); i.hasNext();) {
  267. c.addItem(i.next());
  268. }
  269. }
  270. setCaption(caption);
  271. setContainerDataSource(c);
  272. }
  273. /* Component methods */
  274. /**
  275. * Paints the content of this component.
  276. *
  277. * @param target
  278. * the Paint Event.
  279. * @throws PaintException
  280. * if the paint operation failed.
  281. */
  282. public void paintContent(PaintTarget target) throws PaintException {
  283. // Paints select attributes
  284. if (isMultiSelect()) {
  285. target.addAttribute("selectmode", "multi");
  286. }
  287. if (isNewItemsAllowed()) {
  288. target.addAttribute("allownewitem", true);
  289. }
  290. if (isNullSelectionAllowed()) {
  291. target.addAttribute("nullselect", true);
  292. if (getNullSelectionItemId() != null) {
  293. target.addAttribute("nullselectitem", true);
  294. }
  295. }
  296. // Constructs selected keys array
  297. String[] selectedKeys;
  298. if (isMultiSelect()) {
  299. selectedKeys = new String[((Set<?>) getValue()).size()];
  300. } else {
  301. selectedKeys = new String[(getValue() == null
  302. && getNullSelectionItemId() == null ? 0 : 1)];
  303. }
  304. // ==
  305. // first remove all previous item/property listeners
  306. getCaptionChangeListener().clear();
  307. // Paints the options and create array of selected id keys
  308. target.startTag("options");
  309. int keyIndex = 0;
  310. // Support for external null selection item id
  311. final Collection<?> ids = getItemIds();
  312. if (isNullSelectionAllowed() && getNullSelectionItemId() != null
  313. && !ids.contains(getNullSelectionItemId())) {
  314. final Object id = getNullSelectionItemId();
  315. // Paints option
  316. target.startTag("so");
  317. paintItem(target, id);
  318. if (isSelected(id)) {
  319. selectedKeys[keyIndex++] = itemIdMapper.key(id);
  320. }
  321. target.endTag("so");
  322. }
  323. final Iterator<?> i = getItemIds().iterator();
  324. // Paints the available selection options from data source
  325. while (i.hasNext()) {
  326. // Gets the option attribute values
  327. final Object id = i.next();
  328. if (!isNullSelectionAllowed() && id != null
  329. && id.equals(getNullSelectionItemId())) {
  330. // Remove item if it's the null selection item but null
  331. // selection is not allowed
  332. continue;
  333. }
  334. final String key = itemIdMapper.key(id);
  335. // add listener for each item, to cause repaint if an item changes
  336. getCaptionChangeListener().addNotifierForItem(id);
  337. target.startTag("so");
  338. paintItem(target, id);
  339. if (isSelected(id) && keyIndex < selectedKeys.length) {
  340. selectedKeys[keyIndex++] = key;
  341. }
  342. target.endTag("so");
  343. }
  344. target.endTag("options");
  345. // ==
  346. // Paint variables
  347. target.addVariable(this, "selected", selectedKeys);
  348. if (isNewItemsAllowed()) {
  349. target.addVariable(this, "newitem", "");
  350. }
  351. }
  352. protected void paintItem(PaintTarget target, Object itemId)
  353. throws PaintException {
  354. final String key = itemIdMapper.key(itemId);
  355. final String caption = getItemCaption(itemId);
  356. final Resource icon = getItemIcon(itemId);
  357. if (icon != null) {
  358. target.addAttribute("icon", icon);
  359. }
  360. target.addAttribute("caption", caption);
  361. if (itemId != null && itemId.equals(getNullSelectionItemId())) {
  362. target.addAttribute("nullselection", true);
  363. }
  364. target.addAttribute("key", key);
  365. if (isSelected(itemId)) {
  366. target.addAttribute("selected", true);
  367. }
  368. }
  369. /**
  370. * Invoked when the value of a variable has changed.
  371. *
  372. * @see com.vaadin.ui.AbstractComponent#changeVariables(java.lang.Object,
  373. * java.util.Map)
  374. */
  375. public void changeVariables(Object source, Map<String, Object> variables) {
  376. // New option entered (and it is allowed)
  377. if (isNewItemsAllowed()) {
  378. final String newitem = (String) variables.get("newitem");
  379. if (newitem != null && newitem.length() > 0) {
  380. getNewItemHandler().addNewItem(newitem);
  381. }
  382. }
  383. // Selection change
  384. if (variables.containsKey("selected")) {
  385. final String[] ka = (String[]) variables.get("selected");
  386. // Multiselect mode
  387. if (isMultiSelect()) {
  388. // TODO Optimize by adding repaintNotNeeded when applicable
  389. // Converts the key-array to id-set
  390. final LinkedList<Object> s = new LinkedList<Object>();
  391. for (int i = 0; i < ka.length; i++) {
  392. final Object id = itemIdMapper.get(ka[i]);
  393. if (!isNullSelectionAllowed()
  394. && (id == null || id == getNullSelectionItemId())) {
  395. // skip empty selection if nullselection is not allowed
  396. requestRepaint();
  397. } else if (id != null && containsId(id)) {
  398. s.add(id);
  399. }
  400. }
  401. if (!isNullSelectionAllowed() && s.size() < 1) {
  402. // empty selection not allowed, keep old value
  403. requestRepaint();
  404. return;
  405. }
  406. // Limits the deselection to the set of visible items
  407. // (non-visible items can not be deselected)
  408. final Collection<?> visible = getVisibleItemIds();
  409. if (visible != null) {
  410. @SuppressWarnings("unchecked")
  411. Set<Object> newsel = (Set<Object>) getValue();
  412. if (newsel == null) {
  413. newsel = new HashSet<Object>();
  414. } else {
  415. newsel = new HashSet<Object>(newsel);
  416. }
  417. newsel.removeAll(visible);
  418. newsel.addAll(s);
  419. setValue(newsel, true);
  420. }
  421. } else {
  422. // Single select mode
  423. if (!isNullSelectionAllowed()
  424. && (ka.length == 0 || ka[0] == null || ka[0] == getNullSelectionItemId())) {
  425. requestRepaint();
  426. return;
  427. }
  428. if (ka.length == 0) {
  429. // Allows deselection only if the deselected item is
  430. // visible
  431. final Object current = getValue();
  432. final Collection<?> visible = getVisibleItemIds();
  433. if (visible != null && visible.contains(current)) {
  434. setValue(null, true);
  435. }
  436. } else {
  437. final Object id = itemIdMapper.get(ka[0]);
  438. if (!isNullSelectionAllowed() && id == null) {
  439. requestRepaint();
  440. } else if (id != null
  441. && id.equals(getNullSelectionItemId())) {
  442. setValue(null, true);
  443. } else {
  444. setValue(id, true);
  445. }
  446. }
  447. }
  448. }
  449. }
  450. /**
  451. * TODO refine doc Setter for new item handler that is called when user adds
  452. * new item in newItemAllowed mode.
  453. *
  454. * @param newItemHandler
  455. */
  456. public void setNewItemHandler(NewItemHandler newItemHandler) {
  457. this.newItemHandler = newItemHandler;
  458. }
  459. /**
  460. * TODO refine doc
  461. *
  462. * @return
  463. */
  464. public NewItemHandler getNewItemHandler() {
  465. if (newItemHandler == null) {
  466. newItemHandler = new DefaultNewItemHandler();
  467. }
  468. return newItemHandler;
  469. }
  470. public interface NewItemHandler extends Serializable {
  471. void addNewItem(String newItemCaption);
  472. }
  473. /**
  474. * TODO refine doc
  475. *
  476. * This is a default class that handles adding new items that are typed by
  477. * user to selects container.
  478. *
  479. * By extending this class one may implement some logic on new item addition
  480. * like database inserts.
  481. *
  482. */
  483. public class DefaultNewItemHandler implements NewItemHandler {
  484. public void addNewItem(String newItemCaption) {
  485. // Checks for readonly
  486. if (isReadOnly()) {
  487. throw new Property.ReadOnlyException();
  488. }
  489. // Adds new option
  490. if (addItem(newItemCaption) != null) {
  491. // Sets the caption property, if used
  492. if (getItemCaptionPropertyId() != null) {
  493. getContainerProperty(newItemCaption,
  494. getItemCaptionPropertyId())
  495. .setValue(newItemCaption);
  496. }
  497. if (isMultiSelect()) {
  498. Set values = new HashSet((Collection) getValue());
  499. values.add(newItemCaption);
  500. setValue(values);
  501. } else {
  502. setValue(newItemCaption);
  503. }
  504. }
  505. }
  506. }
  507. /**
  508. * Gets the visible item ids. In Select, this returns list of all item ids,
  509. * but can be overriden in subclasses if they paint only part of the items
  510. * to the terminal or null if no items is visible.
  511. */
  512. public Collection<?> getVisibleItemIds() {
  513. return getItemIds();
  514. }
  515. /* Property methods */
  516. /**
  517. * Returns the type of the property. <code>getValue</code> and
  518. * <code>setValue</code> methods must be compatible with this type: one can
  519. * safely cast <code>getValue</code> to given type and pass any variable
  520. * assignable to this type as a parameter to <code>setValue</code>.
  521. *
  522. * @return the Type of the property.
  523. */
  524. @Override
  525. public Class<?> getType() {
  526. if (isMultiSelect()) {
  527. return Set.class;
  528. } else {
  529. return Object.class;
  530. }
  531. }
  532. /**
  533. * Gets the selected item id or in multiselect mode a set of selected ids.
  534. *
  535. * @see com.vaadin.ui.AbstractField#getValue()
  536. */
  537. @Override
  538. public Object getValue() {
  539. final Object retValue = super.getValue();
  540. if (isMultiSelect()) {
  541. // If the return value is not a set
  542. if (retValue == null) {
  543. return new HashSet<Object>();
  544. }
  545. if (retValue instanceof Set) {
  546. return Collections.unmodifiableSet((Set<?>) retValue);
  547. } else if (retValue instanceof Collection) {
  548. return new HashSet<Object>((Collection<?>) retValue);
  549. } else {
  550. final Set<Object> s = new HashSet<Object>();
  551. if (items.containsId(retValue)) {
  552. s.add(retValue);
  553. }
  554. return s;
  555. }
  556. } else {
  557. return retValue;
  558. }
  559. }
  560. /**
  561. * Sets the visible value of the property.
  562. *
  563. * <p>
  564. * The value of the select is the selected item id. If the select is in
  565. * multiselect-mode, the value is a set of selected item keys. In
  566. * multiselect mode all collections of id:s can be assigned.
  567. * </p>
  568. *
  569. * @param newValue
  570. * the New selected item or collection of selected items.
  571. * @see com.vaadin.ui.AbstractField#setValue(java.lang.Object)
  572. */
  573. @Override
  574. public void setValue(Object newValue) throws Property.ReadOnlyException {
  575. if (newValue == getNullSelectionItemId()) {
  576. newValue = null;
  577. }
  578. setValue(newValue, false);
  579. }
  580. /**
  581. * Sets the visible value of the property.
  582. *
  583. * <p>
  584. * The value of the select is the selected item id. If the select is in
  585. * multiselect-mode, the value is a set of selected item keys. In
  586. * multiselect mode all collections of id:s can be assigned.
  587. * </p>
  588. *
  589. * @param newValue
  590. * the New selected item or collection of selected items.
  591. * @param repaintIsNotNeeded
  592. * True if caller is sure that repaint is not needed.
  593. * @see com.vaadin.ui.AbstractField#setValue(java.lang.Object,
  594. * java.lang.Boolean)
  595. */
  596. @Override
  597. protected void setValue(Object newValue, boolean repaintIsNotNeeded)
  598. throws Property.ReadOnlyException {
  599. if (isMultiSelect()) {
  600. if (newValue == null) {
  601. super.setValue(new HashSet<Object>(), repaintIsNotNeeded);
  602. } else if (Collection.class.isAssignableFrom(newValue.getClass())) {
  603. super.setValue(new HashSet<Object>((Collection<?>) newValue),
  604. repaintIsNotNeeded);
  605. }
  606. } else if (newValue == null || items.containsId(newValue)) {
  607. super.setValue(newValue, repaintIsNotNeeded);
  608. }
  609. }
  610. /* Container methods */
  611. /**
  612. * Gets the item from the container with given id. If the container does not
  613. * contain the requested item, null is returned.
  614. *
  615. * @param itemId
  616. * the item id.
  617. * @return the item from the container.
  618. */
  619. public Item getItem(Object itemId) {
  620. return items.getItem(itemId);
  621. }
  622. /**
  623. * Gets the item Id collection from the container.
  624. *
  625. * @return the Collection of item ids.
  626. */
  627. public Collection<?> getItemIds() {
  628. return items.getItemIds();
  629. }
  630. /**
  631. * Gets the property Id collection from the container.
  632. *
  633. * @return the Collection of property ids.
  634. */
  635. public Collection<?> getContainerPropertyIds() {
  636. return items.getContainerPropertyIds();
  637. }
  638. /**
  639. * Gets the property type.
  640. *
  641. * @param propertyId
  642. * the Id identifying the property.
  643. * @see com.vaadin.data.Container#getType(java.lang.Object)
  644. */
  645. public Class<?> getType(Object propertyId) {
  646. return items.getType(propertyId);
  647. }
  648. /*
  649. * Gets the number of items in the container.
  650. *
  651. * @return the Number of items in the container.
  652. *
  653. * @see com.vaadin.data.Container#size()
  654. */
  655. public int size() {
  656. return items.size();
  657. }
  658. /**
  659. * Tests, if the collection contains an item with given id.
  660. *
  661. * @param itemId
  662. * the Id the of item to be tested.
  663. */
  664. public boolean containsId(Object itemId) {
  665. if (itemId != null) {
  666. return items.containsId(itemId);
  667. } else {
  668. return false;
  669. }
  670. }
  671. /**
  672. * Gets the Property identified by the given itemId and propertyId from the
  673. * Container
  674. *
  675. * @see com.vaadin.data.Container#getContainerProperty(Object, Object)
  676. */
  677. public Property<?> getContainerProperty(Object itemId, Object propertyId) {
  678. return items.getContainerProperty(itemId, propertyId);
  679. }
  680. /**
  681. * Adds the new property to all items. Adds a property with given id, type
  682. * and default value to all items in the container.
  683. *
  684. * This functionality is optional. If the function is unsupported, it always
  685. * returns false.
  686. *
  687. * @return True if the operation succeeded.
  688. * @see com.vaadin.data.Container#addContainerProperty(java.lang.Object,
  689. * java.lang.Class, java.lang.Object)
  690. */
  691. public boolean addContainerProperty(Object propertyId, Class<?> type,
  692. Object defaultValue) throws UnsupportedOperationException {
  693. final boolean retval = items.addContainerProperty(propertyId, type,
  694. defaultValue);
  695. if (retval && !(items instanceof Container.PropertySetChangeNotifier)) {
  696. firePropertySetChange();
  697. }
  698. return retval;
  699. }
  700. /**
  701. * Removes all items from the container.
  702. *
  703. * This functionality is optional. If the function is unsupported, it always
  704. * returns false.
  705. *
  706. * @return True if the operation succeeded.
  707. * @see com.vaadin.data.Container#removeAllItems()
  708. */
  709. public boolean removeAllItems() throws UnsupportedOperationException {
  710. final boolean retval = items.removeAllItems();
  711. itemIdMapper.removeAll();
  712. if (retval) {
  713. setValue(null);
  714. if (!(items instanceof Container.ItemSetChangeNotifier)) {
  715. fireItemSetChange();
  716. }
  717. }
  718. return retval;
  719. }
  720. /**
  721. * Creates a new item into container with container managed id. The id of
  722. * the created new item is returned. The item can be fetched with getItem()
  723. * method. if the creation fails, null is returned.
  724. *
  725. * @return the Id of the created item or null in case of failure.
  726. * @see com.vaadin.data.Container#addItem()
  727. */
  728. public Object addItem() throws UnsupportedOperationException {
  729. final Object retval = items.addItem();
  730. if (retval != null
  731. && !(items instanceof Container.ItemSetChangeNotifier)) {
  732. fireItemSetChange();
  733. }
  734. return retval;
  735. }
  736. /**
  737. * Create a new item into container. The created new item is returned and
  738. * ready for setting property values. if the creation fails, null is
  739. * returned. In case the container already contains the item, null is
  740. * returned.
  741. *
  742. * This functionality is optional. If the function is unsupported, it always
  743. * returns null.
  744. *
  745. * @param itemId
  746. * the Identification of the item to be created.
  747. * @return the Created item with the given id, or null in case of failure.
  748. * @see com.vaadin.data.Container#addItem(java.lang.Object)
  749. */
  750. public Item addItem(Object itemId) throws UnsupportedOperationException {
  751. final Item retval = items.addItem(itemId);
  752. if (retval != null
  753. && !(items instanceof Container.ItemSetChangeNotifier)) {
  754. fireItemSetChange();
  755. }
  756. return retval;
  757. }
  758. /*
  759. * (non-Javadoc)
  760. *
  761. * @see com.vaadin.data.Container#removeItem(java.lang.Object)
  762. */
  763. public boolean removeItem(Object itemId)
  764. throws UnsupportedOperationException {
  765. unselect(itemId);
  766. final boolean retval = items.removeItem(itemId);
  767. itemIdMapper.remove(itemId);
  768. if (retval && !(items instanceof Container.ItemSetChangeNotifier)) {
  769. fireItemSetChange();
  770. }
  771. return retval;
  772. }
  773. /**
  774. * Removes the property from all items. Removes a property with given id
  775. * from all the items in the container.
  776. *
  777. * This functionality is optional. If the function is unsupported, it always
  778. * returns false.
  779. *
  780. * @return True if the operation succeeded.
  781. * @see com.vaadin.data.Container#removeContainerProperty(java.lang.Object)
  782. */
  783. public boolean removeContainerProperty(Object propertyId)
  784. throws UnsupportedOperationException {
  785. final boolean retval = items.removeContainerProperty(propertyId);
  786. if (retval && !(items instanceof Container.PropertySetChangeNotifier)) {
  787. firePropertySetChange();
  788. }
  789. return retval;
  790. }
  791. /* Container.Viewer methods */
  792. /**
  793. * Sets the Container that serves as the data source of the viewer.
  794. *
  795. * As a side-effect the fields value (selection) is set to null due old
  796. * selection not necessary exists in new Container.
  797. *
  798. * @see com.vaadin.data.Container.Viewer#setContainerDataSource(Container)
  799. *
  800. * @param newDataSource
  801. * the new data source.
  802. */
  803. public void setContainerDataSource(Container newDataSource) {
  804. if (newDataSource == null) {
  805. newDataSource = new IndexedContainer();
  806. }
  807. getCaptionChangeListener().clear();
  808. if (items != newDataSource) {
  809. // Removes listeners from the old datasource
  810. if (items != null) {
  811. if (items instanceof Container.ItemSetChangeNotifier) {
  812. ((Container.ItemSetChangeNotifier) items)
  813. .removeListener(this);
  814. }
  815. if (items instanceof Container.PropertySetChangeNotifier) {
  816. ((Container.PropertySetChangeNotifier) items)
  817. .removeListener(this);
  818. }
  819. }
  820. // Assigns new data source
  821. items = newDataSource;
  822. // Clears itemIdMapper also
  823. itemIdMapper.removeAll();
  824. // Adds listeners
  825. if (items != null) {
  826. if (items instanceof Container.ItemSetChangeNotifier) {
  827. ((Container.ItemSetChangeNotifier) items).addListener(this);
  828. }
  829. if (items instanceof Container.PropertySetChangeNotifier) {
  830. ((Container.PropertySetChangeNotifier) items)
  831. .addListener(this);
  832. }
  833. }
  834. /*
  835. * We expect changing the data source should also clean value. See
  836. * #810, #4607, #5281
  837. */
  838. setValue(null);
  839. requestRepaint();
  840. }
  841. }
  842. /**
  843. * Gets the viewing data-source container.
  844. *
  845. * @see com.vaadin.data.Container.Viewer#getContainerDataSource()
  846. */
  847. public Container getContainerDataSource() {
  848. return items;
  849. }
  850. /* Select attributes */
  851. /**
  852. * Is the select in multiselect mode? In multiselect mode
  853. *
  854. * @return the Value of property multiSelect.
  855. */
  856. public boolean isMultiSelect() {
  857. return multiSelect;
  858. }
  859. /**
  860. * Sets the multiselect mode. Setting multiselect mode false may lose
  861. * selection information: if selected items set contains one or more
  862. * selected items, only one of the selected items is kept as selected.
  863. *
  864. * Subclasses of AbstractSelect can choose not to support changing the
  865. * multiselect mode, and may throw {@link UnsupportedOperationException}.
  866. *
  867. * @param multiSelect
  868. * the New value of property multiSelect.
  869. */
  870. public void setMultiSelect(boolean multiSelect) {
  871. if (multiSelect && getNullSelectionItemId() != null) {
  872. throw new IllegalStateException(
  873. "Multiselect and NullSelectionItemId can not be set at the same time.");
  874. }
  875. if (multiSelect != this.multiSelect) {
  876. // Selection before mode change
  877. final Object oldValue = getValue();
  878. this.multiSelect = multiSelect;
  879. // Convert the value type
  880. if (multiSelect) {
  881. final Set<Object> s = new HashSet<Object>();
  882. if (oldValue != null) {
  883. s.add(oldValue);
  884. }
  885. setValue(s);
  886. } else {
  887. final Set<?> s = (Set<?>) oldValue;
  888. if (s == null || s.isEmpty()) {
  889. setValue(null);
  890. } else {
  891. // Set the single select to contain only the first
  892. // selected value in the multiselect
  893. setValue(s.iterator().next());
  894. }
  895. }
  896. requestRepaint();
  897. }
  898. }
  899. /**
  900. * Does the select allow adding new options by the user. If true, the new
  901. * options can be added to the Container. The text entered by the user is
  902. * used as id. Note that data-source must allow adding new items.
  903. *
  904. * @return True if additions are allowed.
  905. */
  906. public boolean isNewItemsAllowed() {
  907. return allowNewOptions;
  908. }
  909. /**
  910. * Enables or disables possibility to add new options by the user.
  911. *
  912. * @param allowNewOptions
  913. * the New value of property allowNewOptions.
  914. */
  915. public void setNewItemsAllowed(boolean allowNewOptions) {
  916. // Only handle change requests
  917. if (this.allowNewOptions != allowNewOptions) {
  918. this.allowNewOptions = allowNewOptions;
  919. requestRepaint();
  920. }
  921. }
  922. /**
  923. * Override the caption of an item. Setting caption explicitly overrides id,
  924. * item and index captions.
  925. *
  926. * @param itemId
  927. * the id of the item to be recaptioned.
  928. * @param caption
  929. * the New caption.
  930. */
  931. public void setItemCaption(Object itemId, String caption) {
  932. if (itemId != null) {
  933. itemCaptions.put(itemId, caption);
  934. requestRepaint();
  935. }
  936. }
  937. /**
  938. * Gets the caption of an item. The caption is generated as specified by the
  939. * item caption mode. See <code>setItemCaptionMode()</code> for more
  940. * details.
  941. *
  942. * @param itemId
  943. * the id of the item to be queried.
  944. * @return the caption for specified item.
  945. */
  946. public String getItemCaption(Object itemId) {
  947. // Null items can not be found
  948. if (itemId == null) {
  949. return null;
  950. }
  951. String caption = null;
  952. switch (getItemCaptionMode()) {
  953. case ID:
  954. caption = itemId.toString();
  955. break;
  956. case INDEX:
  957. if (items instanceof Container.Indexed) {
  958. caption = String.valueOf(((Container.Indexed) items)
  959. .indexOfId(itemId));
  960. } else {
  961. caption = "ERROR: Container is not indexed";
  962. }
  963. break;
  964. case ITEM:
  965. final Item i = getItem(itemId);
  966. if (i != null) {
  967. caption = i.toString();
  968. }
  969. break;
  970. case EXPLICIT:
  971. caption = itemCaptions.get(itemId);
  972. break;
  973. case EXPLICIT_DEFAULTS_ID:
  974. caption = itemCaptions.get(itemId);
  975. if (caption == null) {
  976. caption = itemId.toString();
  977. }
  978. break;
  979. case PROPERTY:
  980. final Property<?> p = getContainerProperty(itemId,
  981. getItemCaptionPropertyId());
  982. if (p != null) {
  983. Object value = p.getValue();
  984. if (value != null) {
  985. caption = value.toString();
  986. }
  987. }
  988. break;
  989. }
  990. // All items must have some captions
  991. return caption != null ? caption : "";
  992. }
  993. /**
  994. * Sets tqhe icon for an item.
  995. *
  996. * @param itemId
  997. * the id of the item to be assigned an icon.
  998. * @param icon
  999. * the icon to use or null.
  1000. */
  1001. public void setItemIcon(Object itemId, Resource icon) {
  1002. if (itemId != null) {
  1003. if (icon == null) {
  1004. itemIcons.remove(itemId);
  1005. } else {
  1006. itemIcons.put(itemId, icon);
  1007. }
  1008. requestRepaint();
  1009. }
  1010. }
  1011. /**
  1012. * Gets the item icon.
  1013. *
  1014. * @param itemId
  1015. * the id of the item to be assigned an icon.
  1016. * @return the icon for the item or null, if not specified.
  1017. */
  1018. public Resource getItemIcon(Object itemId) {
  1019. final Resource explicit = itemIcons.get(itemId);
  1020. if (explicit != null) {
  1021. return explicit;
  1022. }
  1023. if (getItemIconPropertyId() == null) {
  1024. return null;
  1025. }
  1026. final Property<?> ip = getContainerProperty(itemId,
  1027. getItemIconPropertyId());
  1028. if (ip == null) {
  1029. return null;
  1030. }
  1031. final Object icon = ip.getValue();
  1032. if (icon instanceof Resource) {
  1033. return (Resource) icon;
  1034. }
  1035. return null;
  1036. }
  1037. /**
  1038. * Sets the item caption mode.
  1039. *
  1040. * <p>
  1041. * The mode can be one of the following ones:
  1042. * <ul>
  1043. * <li><code>ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID</code> : Items
  1044. * Id-objects <code>toString</code> is used as item caption. If caption is
  1045. * explicitly specified, it overrides the id-caption.
  1046. * <li><code>ITEM_CAPTION_MODE_ID</code> : Items Id-objects
  1047. * <code>toString</code> is used as item caption.</li>
  1048. * <li><code>ITEM_CAPTION_MODE_ITEM</code> : Item-objects
  1049. * <code>toString</code> is used as item caption.</li>
  1050. * <li><code>ITEM_CAPTION_MODE_INDEX</code> : The index of the item is used
  1051. * as item caption. The index mode can only be used with the containers
  1052. * implementing <code>Container.Indexed</code> interface.</li>
  1053. * <li><code>ITEM_CAPTION_MODE_EXPLICIT</code> : The item captions must be
  1054. * explicitly specified.</li>
  1055. * <li><code>ITEM_CAPTION_MODE_PROPERTY</code> : The item captions are read
  1056. * from property, that must be specified with
  1057. * <code>setItemCaptionPropertyId</code>.</li>
  1058. * </ul>
  1059. * The <code>ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID</code> is the default
  1060. * mode.
  1061. * </p>
  1062. *
  1063. * @param mode
  1064. * the One of the modes listed above.
  1065. */
  1066. public void setItemCaptionMode(ItemCaptionMode mode) {
  1067. if (mode != null) {
  1068. itemCaptionMode = mode;
  1069. requestRepaint();
  1070. }
  1071. }
  1072. /**
  1073. * Gets the item caption mode.
  1074. *
  1075. * <p>
  1076. * The mode can be one of the following ones:
  1077. * <ul>
  1078. * <li><code>ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID</code> : Items
  1079. * Id-objects <code>toString</code> is used as item caption. If caption is
  1080. * explicitly specified, it overrides the id-caption.
  1081. * <li><code>ITEM_CAPTION_MODE_ID</code> : Items Id-objects
  1082. * <code>toString</code> is used as item caption.</li>
  1083. * <li><code>ITEM_CAPTION_MODE_ITEM</code> : Item-objects
  1084. * <code>toString</code> is used as item caption.</li>
  1085. * <li><code>ITEM_CAPTION_MODE_INDEX</code> : The index of the item is used
  1086. * as item caption. The index mode can only be used with the containers
  1087. * implementing <code>Container.Indexed</code> interface.</li>
  1088. * <li><code>ITEM_CAPTION_MODE_EXPLICIT</code> : The item captions must be
  1089. * explicitly specified.</li>
  1090. * <li><code>ITEM_CAPTION_MODE_PROPERTY</code> : The item captions are read
  1091. * from property, that must be specified with
  1092. * <code>setItemCaptionPropertyId</code>.</li>
  1093. * </ul>
  1094. * The <code>ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID</code> is the default
  1095. * mode.
  1096. * </p>
  1097. *
  1098. * @return the One of the modes listed above.
  1099. */
  1100. public ItemCaptionMode getItemCaptionMode() {
  1101. return itemCaptionMode;
  1102. }
  1103. /**
  1104. * Sets the item caption property.
  1105. *
  1106. * <p>
  1107. * Setting the id to a existing property implicitly sets the item caption
  1108. * mode to <code>ITEM_CAPTION_MODE_PROPERTY</code>. If the object is in
  1109. * <code>ITEM_CAPTION_MODE_PROPERTY</code> mode, setting caption property id
  1110. * null resets the item caption mode to
  1111. * <code>ITEM_CAPTION_EXPLICIT_DEFAULTS_ID</code>.
  1112. * </p>
  1113. * <p>
  1114. * Note that the type of the property used for caption must be String
  1115. * </p>
  1116. * <p>
  1117. * Setting the property id to null disables this feature. The id is null by
  1118. * default
  1119. * </p>
  1120. * .
  1121. *
  1122. * @param propertyId
  1123. * the id of the property.
  1124. *
  1125. */
  1126. public void setItemCaptionPropertyId(Object propertyId) {
  1127. if (propertyId != null) {
  1128. itemCaptionPropertyId = propertyId;
  1129. setItemCaptionMode(ITEM_CAPTION_MODE_PROPERTY);
  1130. requestRepaint();
  1131. } else {
  1132. itemCaptionPropertyId = null;
  1133. if (getItemCaptionMode() == ITEM_CAPTION_MODE_PROPERTY) {
  1134. setItemCaptionMode(ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID);
  1135. }
  1136. requestRepaint();
  1137. }
  1138. }
  1139. /**
  1140. * Gets the item caption property.
  1141. *
  1142. * @return the Id of the property used as item caption source.
  1143. */
  1144. public Object getItemCaptionPropertyId() {
  1145. return itemCaptionPropertyId;
  1146. }
  1147. /**
  1148. * Sets the item icon property.
  1149. *
  1150. * <p>
  1151. * If the property id is set to a valid value, each item is given an icon
  1152. * got from the given property of the items. The type of the property must
  1153. * be assignable to Resource.
  1154. * </p>
  1155. *
  1156. * <p>
  1157. * Note : The icons set with <code>setItemIcon</code> function override the
  1158. * icons from the property.
  1159. * </p>
  1160. *
  1161. * <p>
  1162. * Setting the property id to null disables this feature. The id is null by
  1163. * default
  1164. * </p>
  1165. * .
  1166. *
  1167. * @param propertyId
  1168. * the id of the property that specifies icons for items or null
  1169. * @throws IllegalArgumentException
  1170. * If the propertyId is not in the container or is not of a
  1171. * valid type
  1172. */
  1173. public void setItemIconPropertyId(Object propertyId)
  1174. throws IllegalArgumentException {
  1175. if (propertyId == null) {
  1176. itemIconPropertyId = null;
  1177. } else if (!getContainerPropertyIds().contains(propertyId)) {
  1178. throw new IllegalArgumentException(
  1179. "Property id not found in the container");
  1180. } else if (Resource.class.isAssignableFrom(getType(propertyId))) {
  1181. itemIconPropertyId = propertyId;
  1182. } else {
  1183. throw new IllegalArgumentException(
  1184. "Property type must be assignable to Resource");
  1185. }
  1186. requestRepaint();
  1187. }
  1188. /**
  1189. * Gets the item icon property.
  1190. *
  1191. * <p>
  1192. * If the property id is set to a valid value, each item is given an icon
  1193. * got from the given property of the items. The type of the property must
  1194. * be assignable to Icon.
  1195. * </p>
  1196. *
  1197. * <p>
  1198. * Note : The icons set with <code>setItemIcon</code> function override the
  1199. * icons from the property.
  1200. * </p>
  1201. *
  1202. * <p>
  1203. * Setting the property id to null disables this feature. The id is null by
  1204. * default
  1205. * </p>
  1206. * .
  1207. *
  1208. * @return the Id of the property containing the item icons.
  1209. */
  1210. public Object getItemIconPropertyId() {
  1211. return itemIconPropertyId;
  1212. }
  1213. /**
  1214. * Tests if an item is selected.
  1215. *
  1216. * <p>
  1217. * In single select mode testing selection status of the item identified by
  1218. * {@link #getNullSelectionItemId()} returns true if the value of the
  1219. * property is null.
  1220. * </p>
  1221. *
  1222. * @param itemId
  1223. * the Id the of the item to be tested.
  1224. * @see #getNullSelectionItemId()
  1225. * @see #setNullSelectionItemId(Object)
  1226. *
  1227. */
  1228. public boolean isSelected(Object itemId) {
  1229. if (itemId == null) {
  1230. return false;
  1231. }
  1232. if (isMultiSelect()) {
  1233. return ((Set<?>) getValue()).contains(itemId);
  1234. } else {
  1235. final Object value = getValue();
  1236. return itemId.equals(value == null ? getNullSelectionItemId()
  1237. : value);
  1238. }
  1239. }
  1240. /**
  1241. * Selects an item.
  1242. *
  1243. * <p>
  1244. * In single select mode selecting item identified by
  1245. * {@link #getNullSelectionItemId()} sets the value of the property to null.
  1246. * </p>
  1247. *
  1248. * @param itemId
  1249. * the identifier of Item to be selected.
  1250. * @see #getNullSelectionItemId()
  1251. * @see #setNullSelectionItemId(Object)
  1252. *
  1253. */
  1254. public void select(Object itemId) {
  1255. if (!isMultiSelect()) {
  1256. setValue(itemId);
  1257. } else if (!isSelected(itemId) && itemId != null
  1258. && items.containsId(itemId)) {
  1259. final Set<Object> s = new HashSet<Object>((Set<?>) getValue());
  1260. s.add(itemId);
  1261. setValue(s);
  1262. }
  1263. }
  1264. /**
  1265. * Unselects an item.
  1266. *
  1267. * @param itemId
  1268. * the identifier of the Item to be unselected.
  1269. * @see #getNullSelectionItemId()
  1270. * @see #setNullSelectionItemId(Object)
  1271. *
  1272. */
  1273. public void unselect(Object itemId) {
  1274. if (isSelected(itemId)) {
  1275. if (isMultiSelect()) {
  1276. final Set<Object> s = new HashSet<Object>((Set<?>) getValue());
  1277. s.remove(itemId);
  1278. setValue(s);
  1279. } else {
  1280. setValue(null);
  1281. }
  1282. }
  1283. }
  1284. /**
  1285. * Notifies this listener that the Containers contents has changed.
  1286. *
  1287. * @see com.vaadin.data.Container.PropertySetChangeListener#containerPropertySetChange(com.vaadin.data.Container.PropertySetChangeEvent)
  1288. */
  1289. public void containerPropertySetChange(
  1290. Container.PropertySetChangeEvent event) {
  1291. firePropertySetChange();
  1292. }
  1293. /**
  1294. * Adds a new Property set change listener for this Container.
  1295. *
  1296. * @see com.vaadin.data.Container.PropertySetChangeNotifier#addListener(com.vaadin.data.Container.PropertySetChangeListener)
  1297. */
  1298. public void addListener(Container.PropertySetChangeListener listener) {
  1299. if (propertySetEventListeners == null) {
  1300. propertySetEventListeners = new LinkedHashSet<Container.PropertySetChangeListener>();
  1301. }
  1302. propertySetEventListeners.add(listener);
  1303. }
  1304. /**
  1305. * Removes a previously registered Property set change listener.
  1306. *
  1307. * @see com.vaadin.data.Container.PropertySetChangeNotifier#removeListener(com.vaadin.data.Container.PropertySetChangeListener)
  1308. */
  1309. public void removeListener(Container.PropertySetChangeListener listener) {
  1310. if (propertySetEventListeners != null) {
  1311. propertySetEventListeners.remove(listener);
  1312. if (propertySetEventListeners.isEmpty()) {
  1313. propertySetEventListeners = null;
  1314. }
  1315. }
  1316. }
  1317. /**
  1318. * Adds an Item set change listener for the object.
  1319. *
  1320. * @see com.vaadin.data.Container.ItemSetChangeNotifier#addListener(com.vaadin.data.Container.ItemSetChangeListener)
  1321. */
  1322. public void addListener(Container.ItemSetChangeListener listener) {
  1323. if (itemSetEventListeners == null) {
  1324. itemSetEventListeners = new LinkedHashSet<Container.ItemSetChangeListener>();
  1325. }
  1326. itemSetEventListeners.add(listener);
  1327. }
  1328. /**
  1329. * Removes the Item set change listener from the object.
  1330. *
  1331. * @see com.vaadin.data.Container.ItemSetChangeNotifier#removeListener(com.vaadin.data.Container.ItemSetChangeListener)
  1332. */
  1333. public void removeListener(Container.ItemSetChangeListener listener) {
  1334. if (itemSetEventListeners != null) {
  1335. itemSetEventListeners.remove(listener);
  1336. if (itemSetEventListeners.isEmpty()) {
  1337. itemSetEventListeners = null;
  1338. }
  1339. }
  1340. }
  1341. @Override
  1342. public Collection<?> getListeners(Class<?> eventType) {
  1343. if (Container.ItemSetChangeEvent.class.isAssignableFrom(eventType)) {
  1344. if (itemSetEventListeners == null) {
  1345. return Collections.EMPTY_LIST;
  1346. } else {
  1347. return Collections
  1348. .unmodifiableCollection(itemSetEventListeners);
  1349. }
  1350. } else if (Container.PropertySetChangeEvent.class
  1351. .isAssignableFrom(eventType)) {
  1352. if (propertySetEventListeners == null) {
  1353. return Collections.EMPTY_LIST;
  1354. } else {
  1355. return Collections
  1356. .unmodifiableCollection(propertySetEventListeners);
  1357. }
  1358. }
  1359. return super.getListeners(eventType);
  1360. }
  1361. /**
  1362. * Lets the listener know a Containers Item set has changed.
  1363. *
  1364. * @see com.vaadin.data.Container.ItemSetChangeListener#containerItemSetChange(com.vaadin.data.Container.ItemSetChangeEvent)
  1365. */
  1366. public void containerItemSetChange(Container.ItemSetChangeEvent event) {
  1367. // Clears the item id mapping table
  1368. itemIdMapper.removeAll();
  1369. // Notify all listeners
  1370. fireItemSetChange();
  1371. }
  1372. /**
  1373. * Fires the property set change event.
  1374. */
  1375. protected void firePropertySetChange() {
  1376. if (propertySetEventListeners != null
  1377. && !propertySetEventListeners.isEmpty()) {
  1378. final Container.PropertySetChangeEvent event = new PropertySetChangeEvent();
  1379. final Object[] listeners = propertySetEventListeners.toArray();
  1380. for (int i = 0; i < listeners.length; i++) {
  1381. ((Container.PropertySetChangeListener) listeners[i])
  1382. .containerPropertySetChange(event);
  1383. }
  1384. }
  1385. requestRepaint();
  1386. }
  1387. /**
  1388. * Fires the item set change event.
  1389. */
  1390. protected void fireItemSetChange() {
  1391. if (itemSetEventListeners != null && !itemSetEventListeners.isEmpty()) {
  1392. final Container.ItemSetChangeEvent event = new ItemSetChangeEvent();
  1393. final Object[] listeners = itemSetEventListeners.toArray();
  1394. for (int i = 0; i < listeners.length; i++) {
  1395. ((Container.ItemSetChangeListener) listeners[i])
  1396. .containerItemSetChange(event);
  1397. }
  1398. }
  1399. requestRepaint();
  1400. }
  1401. /**
  1402. * Implementation of item set change event.
  1403. */
  1404. private class ItemSetChangeEvent implements Serializable,
  1405. Container.ItemSetChangeEvent {
  1406. /**
  1407. * Gets the Property where the event occurred.
  1408. *
  1409. * @see com.vaadin.data.Container.ItemSetChangeEvent#getContainer()
  1410. */
  1411. public Container getContainer() {
  1412. return AbstractSelect.this;
  1413. }
  1414. }
  1415. /**
  1416. * Implementation of property set change event.
  1417. */
  1418. private class PropertySetChangeEvent implements
  1419. Container.PropertySetChangeEvent, Serializable {
  1420. /**
  1421. * Retrieves the Container whose contents have been modified.
  1422. *
  1423. * @see com.vaadin.data.Container.PropertySetChangeEvent#getContainer()
  1424. */
  1425. public Container getContainer() {
  1426. return AbstractSelect.this;
  1427. }
  1428. }
  1429. /**
  1430. * For multi-selectable fields, also an empty collection of values is
  1431. * considered to be an empty field.
  1432. *
  1433. * @see AbstractField#isEmpty().
  1434. */
  1435. @Override
  1436. protected boolean isEmpty() {
  1437. if (!multiSelect) {
  1438. return super.isEmpty();
  1439. } else {
  1440. Object value = getValue();
  1441. return super.isEmpty()
  1442. || (value instanceof Collection && ((Collection<?>) value)
  1443. .isEmpty());
  1444. }
  1445. }
  1446. /**
  1447. * Allow or disallow empty selection by the user. If the select is in
  1448. * single-select mode, you can make an item represent the empty selection by
  1449. * calling <code>setNullSelectionItemId()</code>. This way you can for
  1450. * instance set an icon and caption for the null selection item.
  1451. *
  1452. * @param nullSelectionAllowed
  1453. * whether or not to allow empty selection
  1454. * @see #setNullSelectionItemId(Object)
  1455. * @see #isNullSelectionAllowed()
  1456. */
  1457. public void setNullSelectionAllowed(boolean nullSelectionAllowed) {
  1458. if (nullSelectionAllowed != this.nullSelectionAllowed) {
  1459. this.nullSelectionAllowed = nullSelectionAllowed;
  1460. requestRepaint();
  1461. }
  1462. }
  1463. /**
  1464. * Checks if null empty selection is allowed by the user.
  1465. *
  1466. * @return whether or not empty selection is allowed
  1467. * @see #setNullSelectionAllowed(boolean)
  1468. */
  1469. public boolean isNullSelectionAllowed() {
  1470. return nullSelectionAllowed;
  1471. }
  1472. /**
  1473. * Returns the item id that represents null value of this select in single
  1474. * select mode.
  1475. *
  1476. * <p>
  1477. * Data interface does not support nulls as item ids. Selecting the item
  1478. * identified by this id is the same as selecting no items at all. This
  1479. * setting only affects the single select mode.
  1480. * </p>
  1481. *
  1482. * @return the Object Null value item id.
  1483. * @see #setNullSelectionItemId(Object)
  1484. * @see #isSelected(Object)
  1485. * @see #select(Object)
  1486. */
  1487. public Object getNullSelectionItemId() {
  1488. return nullSelectionItemId;
  1489. }
  1490. /**
  1491. * Sets the item id that represents null value of this select.
  1492. *
  1493. * <p>
  1494. * Data interface does not support nulls as item ids. Selecting the item
  1495. * identified by this id is the same as selecting no items at all. This
  1496. * setting only affects the single select mode.
  1497. * </p>
  1498. *
  1499. * @param nullSelectionItemId
  1500. * the nullSelectionItemId to set.
  1501. * @see #getNullSelectionItemId()
  1502. * @see #isSelected(Object)
  1503. * @see #select(Object)
  1504. */
  1505. public void setNullSelectionItemId(Object nullSelectionItemId) {
  1506. if (nullSelectionItemId != null && isMultiSelect()) {
  1507. throw new IllegalStateException(
  1508. "Multiselect and NullSelectionItemId can not be set at the same time.");
  1509. }
  1510. this.nullSelectionItemId = nullSelectionItemId;
  1511. }
  1512. /**
  1513. * Notifies the component that it is connected to an application.
  1514. *
  1515. * @see com.vaadin.ui.AbstractField#attach()
  1516. */
  1517. @Override
  1518. public void attach() {
  1519. super.attach();
  1520. }
  1521. /**
  1522. * Detaches the component from application.
  1523. *
  1524. * @see com.vaadin.ui.AbstractComponent#detach()
  1525. */
  1526. @Override
  1527. public void detach() {
  1528. getCaptionChangeListener().clear();
  1529. super.detach();
  1530. }
  1531. // Caption change listener
  1532. protected CaptionChangeListener getCaptionChangeListener() {
  1533. if (captionChangeListener == null) {
  1534. captionChangeListener = new CaptionChangeListener();
  1535. }
  1536. return captionChangeListener;
  1537. }
  1538. /**
  1539. * This is a listener helper for Item and Property changes that should cause
  1540. * a repaint. It should be attached to all items that are displayed, and the
  1541. * default implementation does this in paintContent(). Especially
  1542. * "lazyloading" components should take care to add and remove listeners as
  1543. * appropriate. Call addNotifierForItem() for each painted item (and
  1544. * remember to clear).
  1545. *
  1546. * NOTE: singleton, use getCaptionChangeListener().
  1547. *
  1548. */
  1549. protected class CaptionChangeListener implements
  1550. Item.PropertySetChangeListener, Property.ValueChangeListener {
  1551. // TODO clean this up - type is either Item.PropertySetChangeNotifier or
  1552. // Property.ValueChangeNotifier
  1553. HashSet<Object> captionChangeNotifiers = new HashSet<Object>();
  1554. public void addNotifierForItem(Object itemId) {
  1555. switch (getItemCaptionMode()) {
  1556. case ITEM:
  1557. final Item i = getItem(itemId);
  1558. if (i == null) {
  1559. return;
  1560. }
  1561. if (i instanceof Item.PropertySetChangeNotifier) {
  1562. ((Item.PropertySetChangeNotifier) i)
  1563. .addListener(getCaptionChangeListener());
  1564. captionChangeNotifiers.add(i);
  1565. }
  1566. Collection<?> pids = i.getItemPropertyIds();
  1567. if (pids != null) {
  1568. for (Iterator<?> it = pids.iterator(); it.hasNext();) {
  1569. Property<?> p = i.getItemProperty(it.next());
  1570. if (p != null
  1571. && p instanceof Property.ValueChangeNotifier) {
  1572. ((Property.ValueChangeNotifier) p)
  1573. .addListener(getCaptionChangeListener());
  1574. captionChangeNotifiers.add(p);
  1575. }
  1576. }
  1577. }
  1578. break;
  1579. case PROPERTY:
  1580. final Property<?> p = getContainerProperty(itemId,
  1581. getItemCaptionPropertyId());
  1582. if (p != null && p instanceof Property.ValueChangeNotifier) {
  1583. ((Property.ValueChangeNotifier) p)
  1584. .addListener(getCaptionChangeListener());
  1585. captionChangeNotifiers.add(p);
  1586. }
  1587. break;
  1588. }
  1589. }
  1590. public void clear() {
  1591. for (Iterator<Object> it = captionChangeNotifiers.iterator(); it
  1592. .hasNext();) {
  1593. Object notifier = it.next();
  1594. if (notifier instanceof Item.PropertySetChangeNotifier) {
  1595. ((Item.PropertySetChangeNotifier) notifier)
  1596. .removeListener(getCaptionChangeListener());
  1597. } else {
  1598. ((Property.ValueChangeNotifier) notifier)
  1599. .removeListener(getCaptionChangeListener());
  1600. }
  1601. }
  1602. captionChangeNotifiers.clear();
  1603. }
  1604. public void valueChange(com.vaadin.data.Property.ValueChangeEvent event) {
  1605. requestRepaint();
  1606. }
  1607. public void itemPropertySetChange(
  1608. com.vaadin.data.Item.PropertySetChangeEvent event) {
  1609. requestRepaint();
  1610. }
  1611. }
  1612. /**
  1613. * Criterion which accepts a drop only if the drop target is (one of) the
  1614. * given Item identifier(s). Criterion can be used only on a drop targets
  1615. * that extends AbstractSelect like {@link Table} and {@link Tree}. The
  1616. * target and identifiers of valid Items are given in constructor.
  1617. *
  1618. * @since 6.3
  1619. */
  1620. public static class TargetItemIs extends AbstractItemSetCriterion {
  1621. /**
  1622. * @param select
  1623. * the select implementation that is used as a drop target
  1624. * @param itemId
  1625. * the identifier(s) that are valid drop locations
  1626. */
  1627. public TargetItemIs(AbstractSelect select, Object... itemId) {
  1628. super(select, itemId);
  1629. }
  1630. public boolean accept(DragAndDropEvent dragEvent) {
  1631. AbstractSelectTargetDetails dropTargetData = (AbstractSelectTargetDetails) dragEvent
  1632. .getTargetDetails();
  1633. if (dropTargetData.getTarget() != select) {
  1634. return false;
  1635. }
  1636. return itemIds.contains(dropTargetData.getItemIdOver());
  1637. }
  1638. }
  1639. /**
  1640. * Abstract helper class to implement item id based criterion.
  1641. *
  1642. * Note, inner class used not to open itemIdMapper for public access.
  1643. *
  1644. * @since 6.3
  1645. *
  1646. */
  1647. private static abstract class AbstractItemSetCriterion extends
  1648. ClientSideCriterion {
  1649. protected final Collection<Object> itemIds = new HashSet<Object>();
  1650. protected AbstractSelect select;
  1651. public AbstractItemSetCriterion(AbstractSelect select, Object... itemId) {
  1652. if (itemIds == null || select == null) {
  1653. throw new IllegalArgumentException(
  1654. "Accepted item identifiers must be accepted.");
  1655. }
  1656. Collections.addAll(itemIds, itemId);
  1657. this.select = select;
  1658. }
  1659. @Override
  1660. public void paintContent(PaintTarget target) throws PaintException {
  1661. super.paintContent(target);
  1662. String[] keys = new String[itemIds.size()];
  1663. int i = 0;
  1664. for (Object itemId : itemIds) {
  1665. String key = select.itemIdMapper.key(itemId);
  1666. keys[i++] = key;
  1667. }
  1668. target.addAttribute("keys", keys);
  1669. target.addAttribute("s", select);
  1670. }
  1671. }
  1672. /**
  1673. * This criterion accepts a only a {@link Transferable} that contains given
  1674. * Item (practically its identifier) from a specific AbstractSelect.
  1675. *
  1676. * @since 6.3
  1677. */
  1678. public static class AcceptItem extends AbstractItemSetCriterion {
  1679. /**
  1680. * @param select
  1681. * the select from which the item id's are checked
  1682. * @param itemId
  1683. * the item identifier(s) of the select that are accepted
  1684. */
  1685. public AcceptItem(AbstractSelect select, Object... itemId) {
  1686. super(select, itemId);
  1687. }
  1688. public boolean accept(DragAndDropEvent dragEvent) {
  1689. DataBoundTransferable transferable = (DataBoundTransferable) dragEvent
  1690. .getTransferable();
  1691. if (transferable.getSourceComponent() != select) {
  1692. return false;
  1693. }
  1694. return itemIds.contains(transferable.getItemId());
  1695. }
  1696. /**
  1697. * A simple accept criterion which ensures that {@link Transferable}
  1698. * contains an {@link Item} (or actually its identifier). In other words
  1699. * the criterion check that drag is coming from a {@link Container} like
  1700. * {@link Tree} or {@link Table}.
  1701. */
  1702. public static final ClientSideCriterion ALL = new ContainsDataFlavor(
  1703. "itemId");
  1704. }
  1705. /**
  1706. * TargetDetails implementation for subclasses of {@link AbstractSelect}
  1707. * that implement {@link DropTarget}.
  1708. *
  1709. * @since 6.3
  1710. */
  1711. public class AbstractSelectTargetDetails extends TargetDetailsImpl {
  1712. /**
  1713. * The item id over which the drag event happened.
  1714. */
  1715. protected Object idOver;
  1716. /**
  1717. * Constructor that automatically converts itemIdOver key to
  1718. * corresponding item Id
  1719. *
  1720. */
  1721. protected AbstractSelectTargetDetails(Map<String, Object> rawVariables) {
  1722. super(rawVariables, (DropTarget) AbstractSelect.this);
  1723. // eagar fetch itemid, mapper may be emptied
  1724. String keyover = (String) getData("itemIdOver");
  1725. if (keyover != null) {
  1726. idOver = itemIdMapper.get(keyover);
  1727. }
  1728. }
  1729. /**
  1730. * If the drag operation is currently over an {@link Item}, this method
  1731. * returns the identifier of that {@link Item}.
  1732. *
  1733. */
  1734. public Object getItemIdOver() {
  1735. return idOver;
  1736. }
  1737. /**
  1738. * Returns a detailed vertical location where the drop happened on Item.
  1739. */
  1740. public VerticalDropLocation getDropLocation() {
  1741. String detail = (String) getData("detail");
  1742. if (detail == null) {
  1743. return null;
  1744. }
  1745. return VerticalDropLocation.valueOf(detail);
  1746. }
  1747. }
  1748. /**
  1749. * An accept criterion to accept drops only on a specific vertical location
  1750. * of an item.
  1751. * <p>
  1752. * This accept criterion is currently usable in Tree and Table
  1753. * implementations.
  1754. */
  1755. public static class VerticalLocationIs extends TargetDetailIs {
  1756. public static VerticalLocationIs TOP = new VerticalLocationIs(
  1757. VerticalDropLocation.TOP);
  1758. public static VerticalLocationIs BOTTOM = new VerticalLocationIs(
  1759. VerticalDropLocation.BOTTOM);
  1760. public static VerticalLocationIs MIDDLE = new VerticalLocationIs(
  1761. VerticalDropLocation.MIDDLE);
  1762. private VerticalLocationIs(VerticalDropLocation l) {
  1763. super("detail", l.name());
  1764. }
  1765. }
  1766. /**
  1767. * Implement this interface and pass it to Tree.setItemDescriptionGenerator
  1768. * or Table.setItemDescriptionGenerator to generate mouse over descriptions
  1769. * ("tooltips") for the rows and cells in Table or for the items in Tree.
  1770. */
  1771. public interface ItemDescriptionGenerator extends Serializable {
  1772. /**
  1773. * Called by Table when a cell (and row) is painted or a item is painted
  1774. * in Tree
  1775. *
  1776. * @param source
  1777. * The source of the generator, the Tree or Table the
  1778. * generator is attached to
  1779. * @param itemId
  1780. * The itemId of the painted cell
  1781. * @param propertyId
  1782. * The propertyId of the cell, null when getting row
  1783. * description
  1784. * @return The description or "tooltip" of the item.
  1785. */
  1786. public String generateDescription(Component source, Object itemId,
  1787. Object propertyId);
  1788. }
  1789. }