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.

IndexedContainer.java 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /*
  2. * Copyright 2000-2014 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.data.util;
  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.Hashtable;
  25. import java.util.Iterator;
  26. import java.util.LinkedList;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.logging.Logger;
  30. import com.vaadin.data.Container;
  31. import com.vaadin.data.Item;
  32. import com.vaadin.data.Property;
  33. import com.vaadin.data.util.filter.SimpleStringFilter;
  34. import com.vaadin.data.util.filter.UnsupportedFilterException;
  35. /**
  36. * An implementation of the <code>{@link Container.Indexed}</code> interface
  37. * with all important features.</p>
  38. *
  39. * Features:
  40. * <ul>
  41. * <li> {@link Container.Indexed}
  42. * <li> {@link Container.Ordered}
  43. * <li> {@link Container.Sortable}
  44. * <li> {@link Container.Filterable}
  45. * <li> {@link Cloneable} (deprecated, might be removed in the future)
  46. * <li>Sends all needed events on content changes.
  47. * </ul>
  48. *
  49. * @see com.vaadin.data.Container
  50. *
  51. * @author Vaadin Ltd.
  52. * @since 3.0
  53. */
  54. @SuppressWarnings("serial")
  55. // item type is really IndexedContainerItem, but using Item not to show it in
  56. // public API
  57. public class IndexedContainer extends
  58. AbstractInMemoryContainer<Object, Object, Item> implements
  59. Container.PropertySetChangeNotifier, Property.ValueChangeNotifier,
  60. Container.Sortable, Cloneable, Container.Filterable,
  61. Container.SimpleFilterable {
  62. /* Internal structure */
  63. /**
  64. * Linked list of ordered Property IDs.
  65. */
  66. private ArrayList<Object> propertyIds = new ArrayList<Object>();
  67. /**
  68. * Property ID to type mapping.
  69. */
  70. private Hashtable<Object, Class<?>> types = new Hashtable<Object, Class<?>>();
  71. /**
  72. * Hash of Items, where each Item is implemented as a mapping from Property
  73. * ID to Property value.
  74. */
  75. private Hashtable<Object, Map<Object, Object>> items = new Hashtable<Object, Map<Object, Object>>();
  76. /**
  77. * Set of properties that are read-only.
  78. */
  79. private HashSet<Property<?>> readOnlyProperties = new HashSet<Property<?>>();
  80. /**
  81. * List of all Property value change event listeners listening all the
  82. * properties.
  83. */
  84. private LinkedList<Property.ValueChangeListener> propertyValueChangeListeners = null;
  85. /**
  86. * Data structure containing all listeners interested in changes to single
  87. * Properties. The data structure is a hashtable mapping Property IDs to a
  88. * hashtable that maps Item IDs to a linked list of listeners listening
  89. * Property identified by given Property ID and Item ID.
  90. */
  91. private Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>> singlePropertyValueChangeListeners = null;
  92. private HashMap<Object, Object> defaultPropertyValues;
  93. private int nextGeneratedItemId = 1;
  94. /* Container constructors */
  95. public IndexedContainer() {
  96. super();
  97. }
  98. public IndexedContainer(Collection<?> itemIds) {
  99. this();
  100. if (items != null) {
  101. for (final Iterator<?> i = itemIds.iterator(); i.hasNext();) {
  102. Object itemId = i.next();
  103. internalAddItemAtEnd(itemId, new IndexedContainerItem(itemId),
  104. false);
  105. }
  106. filterAll();
  107. }
  108. }
  109. /* Container methods */
  110. @Override
  111. protected Item getUnfilteredItem(Object itemId) {
  112. if (itemId != null && items.containsKey(itemId)) {
  113. return new IndexedContainerItem(itemId);
  114. }
  115. return null;
  116. }
  117. /*
  118. * (non-Javadoc)
  119. *
  120. * @see com.vaadin.data.Container#getContainerPropertyIds()
  121. */
  122. @Override
  123. public Collection<?> getContainerPropertyIds() {
  124. return Collections.unmodifiableCollection(propertyIds);
  125. }
  126. /**
  127. * Gets the type of a Property stored in the list.
  128. *
  129. * @param id
  130. * the ID of the Property.
  131. * @return Type of the requested Property
  132. */
  133. @Override
  134. public Class<?> getType(Object propertyId) {
  135. return types.get(propertyId);
  136. }
  137. /*
  138. * (non-Javadoc)
  139. *
  140. * @see com.vaadin.data.Container#getContainerProperty(java.lang.Object,
  141. * java.lang.Object)
  142. */
  143. @Override
  144. public Property getContainerProperty(Object itemId, Object propertyId) {
  145. // map lookup more efficient than propertyIds if there are many
  146. // properties
  147. if (!containsId(itemId) || propertyId == null
  148. || !types.containsKey(propertyId)) {
  149. return null;
  150. }
  151. return new IndexedContainerProperty(itemId, propertyId);
  152. }
  153. /*
  154. * (non-Javadoc)
  155. *
  156. * @see com.vaadin.data.Container#addContainerProperty(java.lang.Object,
  157. * java.lang.Class, java.lang.Object)
  158. */
  159. @Override
  160. public boolean addContainerProperty(Object propertyId, Class<?> type,
  161. Object defaultValue) {
  162. // Fails, if nulls are given
  163. if (propertyId == null || type == null) {
  164. return false;
  165. }
  166. // Fails if the Property is already present
  167. if (propertyIds.contains(propertyId)) {
  168. return false;
  169. }
  170. // Adds the Property to Property list and types
  171. propertyIds.add(propertyId);
  172. types.put(propertyId, type);
  173. // If default value is given, set it
  174. if (defaultValue != null) {
  175. // for existing rows
  176. for (final Iterator<?> i = getAllItemIds().iterator(); i.hasNext();) {
  177. getItem(i.next()).getItemProperty(propertyId).setValue(
  178. defaultValue);
  179. }
  180. // store for next rows
  181. if (defaultPropertyValues == null) {
  182. defaultPropertyValues = new HashMap<Object, Object>();
  183. }
  184. defaultPropertyValues.put(propertyId, defaultValue);
  185. }
  186. // Sends a change event
  187. fireContainerPropertySetChange();
  188. return true;
  189. }
  190. /*
  191. * (non-Javadoc)
  192. *
  193. * @see com.vaadin.data.Container#removeAllItems()
  194. */
  195. @Override
  196. public boolean removeAllItems() {
  197. int origSize = size();
  198. Object firstItem = getFirstVisibleItem();
  199. internalRemoveAllItems();
  200. items.clear();
  201. // fire event only if the visible view changed, regardless of whether
  202. // filtered out items were removed or not
  203. if (origSize != 0) {
  204. // Sends a change event
  205. fireItemsRemoved(0, firstItem, origSize);
  206. }
  207. return true;
  208. }
  209. /**
  210. * {@inheritDoc}
  211. * <p>
  212. * The item ID is generated from a sequence of Integers. The id of the first
  213. * added item is 1.
  214. */
  215. @Override
  216. public Object addItem() {
  217. // Creates a new id
  218. final Object id = generateId();
  219. // Adds the Item into container
  220. addItem(id);
  221. return id;
  222. }
  223. /*
  224. * (non-Javadoc)
  225. *
  226. * @see com.vaadin.data.Container#addItem(java.lang.Object)
  227. */
  228. @Override
  229. public Item addItem(Object itemId) {
  230. Item item = internalAddItemAtEnd(itemId, new IndexedContainerItem(
  231. itemId), false);
  232. if (!isFiltered()) {
  233. // always the last item
  234. fireItemAdded(size() - 1, itemId, item);
  235. } else if (passesFilters(itemId) && !containsId(itemId)) {
  236. getFilteredItemIds().add(itemId);
  237. // always the last item
  238. fireItemAdded(size() - 1, itemId, item);
  239. }
  240. return item;
  241. }
  242. /**
  243. * Helper method to add default values for items if available
  244. *
  245. * @param t
  246. * data table of added item
  247. */
  248. private void addDefaultValues(Hashtable<Object, Object> t) {
  249. if (defaultPropertyValues != null) {
  250. for (Object key : defaultPropertyValues.keySet()) {
  251. t.put(key, defaultPropertyValues.get(key));
  252. }
  253. }
  254. }
  255. /*
  256. * (non-Javadoc)
  257. *
  258. * @see com.vaadin.data.Container#removeItem(java.lang.Object)
  259. */
  260. @Override
  261. public boolean removeItem(Object itemId) {
  262. if (itemId == null || items.remove(itemId) == null) {
  263. return false;
  264. }
  265. int origSize = size();
  266. int position = indexOfId(itemId);
  267. if (internalRemoveItem(itemId)) {
  268. // fire event only if the visible view changed, regardless of
  269. // whether filtered out items were removed or not
  270. if (size() != origSize) {
  271. fireItemRemoved(position, itemId);
  272. }
  273. return true;
  274. } else {
  275. return false;
  276. }
  277. }
  278. /*
  279. * (non-Javadoc)
  280. *
  281. * @see com.vaadin.data.Container#removeContainerProperty(java.lang.Object )
  282. */
  283. @Override
  284. public boolean removeContainerProperty(Object propertyId) {
  285. // Fails if the Property is not present
  286. if (!propertyIds.contains(propertyId)) {
  287. return false;
  288. }
  289. // Removes the Property to Property list and types
  290. propertyIds.remove(propertyId);
  291. types.remove(propertyId);
  292. if (defaultPropertyValues != null) {
  293. defaultPropertyValues.remove(propertyId);
  294. }
  295. // If remove the Property from all Items
  296. for (final Iterator<Object> i = getAllItemIds().iterator(); i.hasNext();) {
  297. items.get(i.next()).remove(propertyId);
  298. }
  299. // Sends a change event
  300. fireContainerPropertySetChange();
  301. return true;
  302. }
  303. /* Container.Ordered methods */
  304. /*
  305. * (non-Javadoc)
  306. *
  307. * @see com.vaadin.data.Container.Ordered#addItemAfter(java.lang.Object,
  308. * java.lang.Object)
  309. */
  310. @Override
  311. public Item addItemAfter(Object previousItemId, Object newItemId) {
  312. return internalAddItemAfter(previousItemId, newItemId,
  313. new IndexedContainerItem(newItemId), true);
  314. }
  315. /**
  316. * {@inheritDoc}
  317. * <p>
  318. * The item ID is generated from a sequence of Integers. The id of the first
  319. * added item is 1.
  320. */
  321. @Override
  322. public Object addItemAfter(Object previousItemId) {
  323. // Creates a new id
  324. final Object id = generateId();
  325. if (addItemAfter(previousItemId, id) != null) {
  326. return id;
  327. } else {
  328. return null;
  329. }
  330. }
  331. /*
  332. * (non-Javadoc)
  333. *
  334. * @see com.vaadin.data.Container.Indexed#addItemAt(int, java.lang.Object)
  335. */
  336. @Override
  337. public Item addItemAt(int index, Object newItemId) {
  338. return internalAddItemAt(index, newItemId, new IndexedContainerItem(
  339. newItemId), true);
  340. }
  341. /**
  342. * {@inheritDoc}
  343. * <p>
  344. * The item ID is generated from a sequence of Integers. The id of the first
  345. * added item is 1.
  346. */
  347. @Override
  348. public Object addItemAt(int index) {
  349. // Creates a new id
  350. final Object id = generateId();
  351. // Adds the Item into container
  352. addItemAt(index, id);
  353. return id;
  354. }
  355. /**
  356. * Generates an unique identifier for use as an item id. Guarantees that the
  357. * generated id is not currently used as an id.
  358. *
  359. * @return
  360. */
  361. private Serializable generateId() {
  362. Serializable id;
  363. do {
  364. id = Integer.valueOf(nextGeneratedItemId++);
  365. } while (items.containsKey(id));
  366. return id;
  367. }
  368. @Override
  369. protected void registerNewItem(int index, Object newItemId, Item item) {
  370. Hashtable<Object, Object> t = new Hashtable<Object, Object>();
  371. items.put(newItemId, t);
  372. addDefaultValues(t);
  373. }
  374. /* Event notifiers */
  375. /**
  376. * An <code>event</code> object specifying the list whose Item set has
  377. * changed.
  378. *
  379. * @author Vaadin Ltd.
  380. * @since 3.0
  381. */
  382. public static class ItemSetChangeEvent extends BaseItemSetChangeEvent {
  383. private final int addedItemIndex;
  384. private ItemSetChangeEvent(IndexedContainer source, int addedItemIndex) {
  385. super(source);
  386. this.addedItemIndex = addedItemIndex;
  387. }
  388. /**
  389. * Iff one item is added, gives its index.
  390. *
  391. * @return -1 if either multiple items are changed or some other change
  392. * than add is done.
  393. */
  394. public int getAddedItemIndex() {
  395. return addedItemIndex;
  396. }
  397. }
  398. /**
  399. * An <code>event</code> object specifying the Property in a list whose
  400. * value has changed.
  401. *
  402. * @author Vaadin Ltd.
  403. * @since 3.0
  404. */
  405. private static class PropertyValueChangeEvent extends EventObject implements
  406. Property.ValueChangeEvent, Serializable {
  407. private PropertyValueChangeEvent(Property source) {
  408. super(source);
  409. }
  410. /*
  411. * (non-Javadoc)
  412. *
  413. * @see com.vaadin.data.Property.ValueChangeEvent#getProperty()
  414. */
  415. @Override
  416. public Property getProperty() {
  417. return (Property) getSource();
  418. }
  419. }
  420. @Override
  421. public void addPropertySetChangeListener(
  422. Container.PropertySetChangeListener listener) {
  423. super.addPropertySetChangeListener(listener);
  424. }
  425. /**
  426. * @deprecated As of 7.0, replaced by
  427. * {@link #addPropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)}
  428. **/
  429. @Deprecated
  430. @Override
  431. public void addListener(Container.PropertySetChangeListener listener) {
  432. addPropertySetChangeListener(listener);
  433. }
  434. @Override
  435. public void removePropertySetChangeListener(
  436. Container.PropertySetChangeListener listener) {
  437. super.removePropertySetChangeListener(listener);
  438. }
  439. /**
  440. * @deprecated As of 7.0, replaced by
  441. * {@link #removePropertySetChangeListener(com.vaadin.data.Container.PropertySetChangeListener)}
  442. **/
  443. @Deprecated
  444. @Override
  445. public void removeListener(Container.PropertySetChangeListener listener) {
  446. removePropertySetChangeListener(listener);
  447. }
  448. /*
  449. * (non-Javadoc)
  450. *
  451. * @see com.vaadin.data.Property.ValueChangeNotifier#addListener(com.
  452. * vaadin.data.Property.ValueChangeListener)
  453. */
  454. @Override
  455. public void addValueChangeListener(Property.ValueChangeListener listener) {
  456. if (propertyValueChangeListeners == null) {
  457. propertyValueChangeListeners = new LinkedList<Property.ValueChangeListener>();
  458. }
  459. propertyValueChangeListeners.add(listener);
  460. }
  461. /**
  462. * @deprecated As of 7.0, replaced by
  463. * {@link #addValueChangeListener(com.vaadin.data.Property.ValueChangeListener)}
  464. **/
  465. @Override
  466. @Deprecated
  467. public void addListener(Property.ValueChangeListener listener) {
  468. addValueChangeListener(listener);
  469. }
  470. /*
  471. * (non-Javadoc)
  472. *
  473. * @see com.vaadin.data.Property.ValueChangeNotifier#removeListener(com
  474. * .vaadin.data.Property.ValueChangeListener)
  475. */
  476. @Override
  477. public void removeValueChangeListener(Property.ValueChangeListener listener) {
  478. if (propertyValueChangeListeners != null) {
  479. propertyValueChangeListeners.remove(listener);
  480. }
  481. }
  482. /**
  483. * @deprecated As of 7.0, replaced by
  484. * {@link #removeValueChangeListener(com.vaadin.data.Property.ValueChangeListener)}
  485. **/
  486. @Override
  487. @Deprecated
  488. public void removeListener(Property.ValueChangeListener listener) {
  489. removeValueChangeListener(listener);
  490. }
  491. /**
  492. * Sends a Property value change event to all interested listeners.
  493. *
  494. * @param source
  495. * the IndexedContainerProperty object.
  496. */
  497. private void firePropertyValueChange(IndexedContainerProperty source) {
  498. // Sends event to listeners listening all value changes
  499. if (propertyValueChangeListeners != null) {
  500. final Object[] l = propertyValueChangeListeners.toArray();
  501. final Property.ValueChangeEvent event = new IndexedContainer.PropertyValueChangeEvent(
  502. source);
  503. for (int i = 0; i < l.length; i++) {
  504. ((Property.ValueChangeListener) l[i]).valueChange(event);
  505. }
  506. }
  507. // Sends event to single property value change listeners
  508. if (singlePropertyValueChangeListeners != null) {
  509. final Map<Object, List<Property.ValueChangeListener>> propertySetToListenerListMap = singlePropertyValueChangeListeners
  510. .get(source.propertyId);
  511. if (propertySetToListenerListMap != null) {
  512. final List<Property.ValueChangeListener> listenerList = propertySetToListenerListMap
  513. .get(source.itemId);
  514. if (listenerList != null) {
  515. final Property.ValueChangeEvent event = new IndexedContainer.PropertyValueChangeEvent(
  516. source);
  517. Object[] listeners = listenerList.toArray();
  518. for (int i = 0; i < listeners.length; i++) {
  519. ((Property.ValueChangeListener) listeners[i])
  520. .valueChange(event);
  521. }
  522. }
  523. }
  524. }
  525. }
  526. @Override
  527. public Collection<?> getListeners(Class<?> eventType) {
  528. if (Property.ValueChangeEvent.class.isAssignableFrom(eventType)) {
  529. if (propertyValueChangeListeners == null) {
  530. return Collections.EMPTY_LIST;
  531. } else {
  532. return Collections
  533. .unmodifiableCollection(propertyValueChangeListeners);
  534. }
  535. }
  536. return super.getListeners(eventType);
  537. }
  538. @Override
  539. protected void fireItemAdded(int position, Object itemId, Item item) {
  540. if (position >= 0) {
  541. super.fireItemAdded(position, itemId, item);
  542. }
  543. }
  544. @Override
  545. protected void fireItemSetChange() {
  546. fireItemSetChange(new IndexedContainer.ItemSetChangeEvent(this, -1));
  547. }
  548. /**
  549. * Adds new single Property change listener.
  550. *
  551. * @param propertyId
  552. * the ID of the Property to add.
  553. * @param itemId
  554. * the ID of the Item .
  555. * @param listener
  556. * the listener to be added.
  557. */
  558. private void addSinglePropertyChangeListener(Object propertyId,
  559. Object itemId, Property.ValueChangeListener listener) {
  560. if (listener != null) {
  561. if (singlePropertyValueChangeListeners == null) {
  562. singlePropertyValueChangeListeners = new Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>>();
  563. }
  564. Map<Object, List<Property.ValueChangeListener>> propertySetToListenerListMap = singlePropertyValueChangeListeners
  565. .get(propertyId);
  566. if (propertySetToListenerListMap == null) {
  567. propertySetToListenerListMap = new Hashtable<Object, List<Property.ValueChangeListener>>();
  568. singlePropertyValueChangeListeners.put(propertyId,
  569. propertySetToListenerListMap);
  570. }
  571. List<Property.ValueChangeListener> listenerList = propertySetToListenerListMap
  572. .get(itemId);
  573. if (listenerList == null) {
  574. listenerList = new LinkedList<Property.ValueChangeListener>();
  575. propertySetToListenerListMap.put(itemId, listenerList);
  576. }
  577. listenerList.add(listener);
  578. }
  579. }
  580. /**
  581. * Removes a previously registered single Property change listener.
  582. *
  583. * @param propertyId
  584. * the ID of the Property to remove.
  585. * @param itemId
  586. * the ID of the Item.
  587. * @param listener
  588. * the listener to be removed.
  589. */
  590. private void removeSinglePropertyChangeListener(Object propertyId,
  591. Object itemId, Property.ValueChangeListener listener) {
  592. if (listener != null && singlePropertyValueChangeListeners != null) {
  593. final Map<Object, List<Property.ValueChangeListener>> propertySetToListenerListMap = singlePropertyValueChangeListeners
  594. .get(propertyId);
  595. if (propertySetToListenerListMap != null) {
  596. final List<Property.ValueChangeListener> listenerList = propertySetToListenerListMap
  597. .get(itemId);
  598. if (listenerList != null) {
  599. listenerList.remove(listener);
  600. if (listenerList.isEmpty()) {
  601. propertySetToListenerListMap.remove(itemId);
  602. }
  603. }
  604. if (propertySetToListenerListMap.isEmpty()) {
  605. singlePropertyValueChangeListeners.remove(propertyId);
  606. }
  607. }
  608. if (singlePropertyValueChangeListeners.isEmpty()) {
  609. singlePropertyValueChangeListeners = null;
  610. }
  611. }
  612. }
  613. /* Internal Item and Property implementations */
  614. /*
  615. * A class implementing the com.vaadin.data.Item interface to be contained
  616. * in the list.
  617. *
  618. * @author Vaadin Ltd.
  619. *
  620. *
  621. * @since 3.0
  622. */
  623. class IndexedContainerItem implements Item {
  624. /**
  625. * Item ID in the host container for this Item.
  626. */
  627. private final Object itemId;
  628. /**
  629. * Constructs a new ListItem instance and connects it to a host
  630. * container.
  631. *
  632. * @param itemId
  633. * the Item ID of the new Item.
  634. */
  635. private IndexedContainerItem(Object itemId) {
  636. // Gets the item contents from the host
  637. if (itemId == null) {
  638. throw new NullPointerException();
  639. }
  640. this.itemId = itemId;
  641. }
  642. /*
  643. * (non-Javadoc)
  644. *
  645. * @see com.vaadin.data.Item#getItemProperty(java.lang.Object)
  646. */
  647. @Override
  648. public Property getItemProperty(Object id) {
  649. return new IndexedContainerProperty(itemId, id);
  650. }
  651. @Override
  652. public Collection<?> getItemPropertyIds() {
  653. return Collections.unmodifiableCollection(propertyIds);
  654. }
  655. /**
  656. * Gets the <code>String</code> representation of the contents of the
  657. * Item. The format of the string is a space separated catenation of the
  658. * <code>String</code> representations of the values of the Properties
  659. * contained by the Item.
  660. *
  661. * @return <code>String</code> representation of the Item contents
  662. */
  663. @Override
  664. public String toString() {
  665. String retValue = "";
  666. for (final Iterator<?> i = propertyIds.iterator(); i.hasNext();) {
  667. final Object propertyId = i.next();
  668. retValue += getItemProperty(propertyId).getValue();
  669. if (i.hasNext()) {
  670. retValue += " ";
  671. }
  672. }
  673. return retValue;
  674. }
  675. /**
  676. * Calculates a integer hash-code for the Item that's unique inside the
  677. * list. Two Items inside the same list have always different
  678. * hash-codes, though Items in different lists may have identical
  679. * hash-codes.
  680. *
  681. * @return A locally unique hash-code as integer
  682. */
  683. @Override
  684. public int hashCode() {
  685. return itemId.hashCode();
  686. }
  687. /**
  688. * Tests if the given object is the same as the this object. Two Items
  689. * got from a list container with the same ID are equal.
  690. *
  691. * @param obj
  692. * an object to compare with this object
  693. * @return <code>true</code> if the given object is the same as this
  694. * object, <code>false</code> if not
  695. */
  696. @Override
  697. public boolean equals(Object obj) {
  698. if (obj == null
  699. || !obj.getClass().equals(IndexedContainerItem.class)) {
  700. return false;
  701. }
  702. final IndexedContainerItem li = (IndexedContainerItem) obj;
  703. return getHost() == li.getHost() && itemId.equals(li.itemId);
  704. }
  705. private IndexedContainer getHost() {
  706. return IndexedContainer.this;
  707. }
  708. /**
  709. * IndexedContainerItem does not support adding new properties. Add
  710. * properties at container level. See
  711. * {@link IndexedContainer#addContainerProperty(Object, Class, Object)}
  712. *
  713. * @see com.vaadin.data.Item#addProperty(Object, Property)
  714. */
  715. @Override
  716. public boolean addItemProperty(Object id, Property property)
  717. throws UnsupportedOperationException {
  718. throw new UnsupportedOperationException("Indexed container item "
  719. + "does not support adding new properties");
  720. }
  721. /**
  722. * Indexed container does not support removing properties. Remove
  723. * properties at container level. See
  724. * {@link IndexedContainer#removeContainerProperty(Object)}
  725. *
  726. * @see com.vaadin.data.Item#removeProperty(Object)
  727. */
  728. @Override
  729. public boolean removeItemProperty(Object id)
  730. throws UnsupportedOperationException {
  731. throw new UnsupportedOperationException(
  732. "Indexed container item does not support property removal");
  733. }
  734. }
  735. /**
  736. * A class implementing the {@link Property} interface to be contained in
  737. * the {@link IndexedContainerItem} contained in the
  738. * {@link IndexedContainer}.
  739. *
  740. * @author Vaadin Ltd.
  741. *
  742. * @since 3.0
  743. */
  744. private class IndexedContainerProperty<T> implements Property<T>,
  745. Property.ValueChangeNotifier {
  746. /**
  747. * ID of the Item, where this property resides.
  748. */
  749. private final Object itemId;
  750. /**
  751. * Id of the Property.
  752. */
  753. private final Object propertyId;
  754. /**
  755. * Constructs a new {@link IndexedContainerProperty} object.
  756. *
  757. * @param itemId
  758. * the ID of the Item to connect the new Property to.
  759. * @param propertyId
  760. * the Property ID of the new Property.
  761. * @param host
  762. * the list that contains the Item to contain the new
  763. * Property.
  764. */
  765. private IndexedContainerProperty(Object itemId, Object propertyId) {
  766. if (itemId == null || propertyId == null) {
  767. // Null ids are not accepted
  768. throw new NullPointerException(
  769. "Container item or property ids can not be null");
  770. }
  771. this.propertyId = propertyId;
  772. this.itemId = itemId;
  773. }
  774. /*
  775. * (non-Javadoc)
  776. *
  777. * @see com.vaadin.data.Property#getType()
  778. */
  779. @Override
  780. public Class<T> getType() {
  781. return (Class<T>) types.get(propertyId);
  782. }
  783. /*
  784. * (non-Javadoc)
  785. *
  786. * @see com.vaadin.data.Property#getValue()
  787. */
  788. @Override
  789. public T getValue() {
  790. return (T) items.get(itemId).get(propertyId);
  791. }
  792. /*
  793. * (non-Javadoc)
  794. *
  795. * @see com.vaadin.data.Property#isReadOnly()
  796. */
  797. @Override
  798. public boolean isReadOnly() {
  799. return readOnlyProperties.contains(this);
  800. }
  801. /*
  802. * (non-Javadoc)
  803. *
  804. * @see com.vaadin.data.Property#setReadOnly(boolean)
  805. */
  806. @Override
  807. public void setReadOnly(boolean newStatus) {
  808. if (newStatus) {
  809. readOnlyProperties.add(this);
  810. } else {
  811. readOnlyProperties.remove(this);
  812. }
  813. }
  814. /*
  815. * (non-Javadoc)
  816. *
  817. * @see com.vaadin.data.Property#setValue(java.lang.Object)
  818. */
  819. @Override
  820. public void setValue(Object newValue) throws Property.ReadOnlyException {
  821. // Gets the Property set
  822. final Map<Object, Object> propertySet = items.get(itemId);
  823. // Support null values on all types
  824. if (newValue == null) {
  825. propertySet.remove(propertyId);
  826. } else if (getType().isAssignableFrom(newValue.getClass())) {
  827. propertySet.put(propertyId, newValue);
  828. } else {
  829. throw new IllegalArgumentException(
  830. "Value is of invalid type, got "
  831. + newValue.getClass().getName() + " but "
  832. + getType().getName() + " was expected");
  833. }
  834. // update the container filtering if this property is being filtered
  835. if (isPropertyFiltered(propertyId)) {
  836. filterAll();
  837. }
  838. firePropertyValueChange(this);
  839. }
  840. /**
  841. * Returns a string representation of this object. The returned string
  842. * representation depends on if the legacy Property toString mode is
  843. * enabled or disabled.
  844. * <p>
  845. * If legacy Property toString mode is enabled, returns the value of the
  846. * <code>Property</code> converted to a String.
  847. * </p>
  848. * <p>
  849. * If legacy Property toString mode is disabled, the string
  850. * representation has no special meaning
  851. * </p>
  852. *
  853. * @return A string representation of the value value stored in the
  854. * Property or a string representation of the Property object.
  855. * @deprecated As of 7.0. To get the property value, use
  856. * {@link #getValue()} instead (and possibly toString on
  857. * that)
  858. */
  859. @Deprecated
  860. @Override
  861. public String toString() {
  862. if (!LegacyPropertyHelper.isLegacyToStringEnabled()) {
  863. return super.toString();
  864. } else {
  865. return LegacyPropertyHelper.legacyPropertyToString(this);
  866. }
  867. }
  868. private Logger getLogger() {
  869. return Logger.getLogger(IndexedContainerProperty.class.getName());
  870. }
  871. /**
  872. * Calculates a integer hash-code for the Property that's unique inside
  873. * the Item containing the Property. Two different Properties inside the
  874. * same Item contained in the same list always have different
  875. * hash-codes, though Properties in different Items may have identical
  876. * hash-codes.
  877. *
  878. * @return A locally unique hash-code as integer
  879. */
  880. @Override
  881. public int hashCode() {
  882. return itemId.hashCode() ^ propertyId.hashCode();
  883. }
  884. /**
  885. * Tests if the given object is the same as the this object. Two
  886. * Properties got from an Item with the same ID are equal.
  887. *
  888. * @param obj
  889. * an object to compare with this object
  890. * @return <code>true</code> if the given object is the same as this
  891. * object, <code>false</code> if not
  892. */
  893. @Override
  894. public boolean equals(Object obj) {
  895. if (obj == null
  896. || !obj.getClass().equals(IndexedContainerProperty.class)) {
  897. return false;
  898. }
  899. final IndexedContainerProperty lp = (IndexedContainerProperty) obj;
  900. return lp.getHost() == getHost()
  901. && lp.propertyId.equals(propertyId)
  902. && lp.itemId.equals(itemId);
  903. }
  904. /*
  905. * (non-Javadoc)
  906. *
  907. * @see com.vaadin.data.Property.ValueChangeNotifier#addListener(
  908. * com.vaadin.data.Property.ValueChangeListener)
  909. */
  910. @Override
  911. public void addValueChangeListener(Property.ValueChangeListener listener) {
  912. addSinglePropertyChangeListener(propertyId, itemId, listener);
  913. }
  914. /**
  915. * @deprecated As of 7.0, replaced by
  916. * {@link #addValueChangeListener(com.vaadin.data.Property.ValueChangeListener)}
  917. **/
  918. @Override
  919. @Deprecated
  920. public void addListener(Property.ValueChangeListener listener) {
  921. addValueChangeListener(listener);
  922. }
  923. /*
  924. * (non-Javadoc)
  925. *
  926. * @see com.vaadin.data.Property.ValueChangeNotifier#removeListener
  927. * (com.vaadin.data.Property.ValueChangeListener)
  928. */
  929. @Override
  930. public void removeValueChangeListener(
  931. Property.ValueChangeListener listener) {
  932. removeSinglePropertyChangeListener(propertyId, itemId, listener);
  933. }
  934. /**
  935. * @deprecated As of 7.0, replaced by
  936. * {@link #removeValueChangeListener(com.vaadin.data.Property.ValueChangeListener)}
  937. **/
  938. @Override
  939. @Deprecated
  940. public void removeListener(Property.ValueChangeListener listener) {
  941. removeValueChangeListener(listener);
  942. }
  943. private IndexedContainer getHost() {
  944. return IndexedContainer.this;
  945. }
  946. }
  947. /*
  948. * (non-Javadoc)
  949. *
  950. * @see com.vaadin.data.Container.Sortable#sort(java.lang.Object[],
  951. * boolean[])
  952. */
  953. @Override
  954. public void sort(Object[] propertyId, boolean[] ascending) {
  955. sortContainer(propertyId, ascending);
  956. }
  957. /*
  958. * (non-Javadoc)
  959. *
  960. * @see com.vaadin.data.Container.Sortable#getSortableContainerPropertyIds
  961. * ()
  962. */
  963. @Override
  964. public Collection<?> getSortableContainerPropertyIds() {
  965. return getSortablePropertyIds();
  966. }
  967. @Override
  968. public ItemSorter getItemSorter() {
  969. return super.getItemSorter();
  970. }
  971. @Override
  972. public void setItemSorter(ItemSorter itemSorter) {
  973. super.setItemSorter(itemSorter);
  974. }
  975. /**
  976. * Supports cloning of the IndexedContainer cleanly.
  977. *
  978. * @throws CloneNotSupportedException
  979. * if an object cannot be cloned. .
  980. *
  981. * @deprecated As of 6.6. Cloning support might be removed from
  982. * IndexedContainer in the future
  983. */
  984. @Deprecated
  985. @Override
  986. public Object clone() throws CloneNotSupportedException {
  987. // Creates the clone
  988. final IndexedContainer nc = new IndexedContainer();
  989. // Clone the shallow properties
  990. nc.setAllItemIds(getAllItemIds() != null ? (ListSet<Object>) ((ListSet<Object>) getAllItemIds())
  991. .clone() : null);
  992. nc.setItemSetChangeListeners(getItemSetChangeListeners() != null ? new LinkedList<Container.ItemSetChangeListener>(
  993. getItemSetChangeListeners()) : null);
  994. nc.propertyIds = propertyIds != null ? (ArrayList<Object>) propertyIds
  995. .clone() : null;
  996. nc.setPropertySetChangeListeners(getPropertySetChangeListeners() != null ? new LinkedList<Container.PropertySetChangeListener>(
  997. getPropertySetChangeListeners()) : null);
  998. nc.propertyValueChangeListeners = propertyValueChangeListeners != null ? (LinkedList<Property.ValueChangeListener>) propertyValueChangeListeners
  999. .clone() : null;
  1000. nc.readOnlyProperties = readOnlyProperties != null ? (HashSet<Property<?>>) readOnlyProperties
  1001. .clone() : null;
  1002. nc.singlePropertyValueChangeListeners = singlePropertyValueChangeListeners != null ? (Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>>) singlePropertyValueChangeListeners
  1003. .clone() : null;
  1004. nc.types = types != null ? (Hashtable<Object, Class<?>>) types.clone()
  1005. : null;
  1006. nc.setFilters((HashSet<Filter>) ((HashSet<Filter>) getFilters())
  1007. .clone());
  1008. nc.setFilteredItemIds(getFilteredItemIds() == null ? null
  1009. : (ListSet<Object>) ((ListSet<Object>) getFilteredItemIds())
  1010. .clone());
  1011. // Clone property-values
  1012. if (items == null) {
  1013. nc.items = null;
  1014. } else {
  1015. nc.items = new Hashtable<Object, Map<Object, Object>>();
  1016. for (final Iterator<?> i = items.keySet().iterator(); i.hasNext();) {
  1017. final Object id = i.next();
  1018. final Hashtable<Object, Object> it = (Hashtable<Object, Object>) items
  1019. .get(id);
  1020. nc.items.put(id, (Map<Object, Object>) it.clone());
  1021. }
  1022. }
  1023. return nc;
  1024. }
  1025. @Override
  1026. public void addContainerFilter(Object propertyId, String filterString,
  1027. boolean ignoreCase, boolean onlyMatchPrefix) {
  1028. try {
  1029. addFilter(new SimpleStringFilter(propertyId, filterString,
  1030. ignoreCase, onlyMatchPrefix));
  1031. } catch (UnsupportedFilterException e) {
  1032. // the filter instance created here is always valid for in-memory
  1033. // containers
  1034. }
  1035. }
  1036. @Override
  1037. public void removeAllContainerFilters() {
  1038. removeAllFilters();
  1039. }
  1040. @Override
  1041. public void removeContainerFilters(Object propertyId) {
  1042. removeFilters(propertyId);
  1043. }
  1044. @Override
  1045. public void addContainerFilter(Filter filter)
  1046. throws UnsupportedFilterException {
  1047. addFilter(filter);
  1048. }
  1049. @Override
  1050. public void removeContainerFilter(Filter filter) {
  1051. removeFilter(filter);
  1052. }
  1053. /*
  1054. * (non-Javadoc)
  1055. *
  1056. * @see com.vaadin.data.util.AbstractInMemoryContainer#getContainerFilters()
  1057. */
  1058. @Override
  1059. public boolean hasContainerFilters() {
  1060. return super.hasContainerFilters();
  1061. }
  1062. /*
  1063. * (non-Javadoc)
  1064. *
  1065. * @see com.vaadin.data.util.AbstractInMemoryContainer#getContainerFilters()
  1066. */
  1067. @Override
  1068. public Collection<Filter> getContainerFilters() {
  1069. return super.getContainerFilters();
  1070. }
  1071. }