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 76KB

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