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

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