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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.io.Serializable;
  6. import java.lang.reflect.Constructor;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.Collections;
  10. import java.util.EventObject;
  11. import java.util.HashMap;
  12. import java.util.HashSet;
  13. import java.util.Hashtable;
  14. import java.util.Iterator;
  15. import java.util.LinkedList;
  16. import java.util.List;
  17. import java.util.Map;
  18. import com.vaadin.data.Container;
  19. import com.vaadin.data.Item;
  20. import com.vaadin.data.Property;
  21. import com.vaadin.data.util.filter.SimpleStringFilter;
  22. import com.vaadin.data.util.filter.UnsupportedFilterException;
  23. /**
  24. * An implementation of the <code>{@link Container.Indexed}</code> interface
  25. * with all important features.</p>
  26. *
  27. * Features:
  28. * <ul>
  29. * <li> {@link Container.Indexed}
  30. * <li> {@link Container.Ordered}
  31. * <li> {@link Container.Sortable}
  32. * <li> {@link Container.Filterable}
  33. * <li> {@link Cloneable} (deprecated, might be removed in the future)
  34. * <li>Sends all needed events on content changes.
  35. * </ul>
  36. *
  37. * @see com.vaadin.data.Container
  38. *
  39. * @author Vaadin Ltd.
  40. * @version
  41. * @VERSION@
  42. * @since 3.0
  43. */
  44. @SuppressWarnings("serial")
  45. // item type is really IndexedContainerItem, but using Item not to show it in
  46. // public API
  47. public class IndexedContainer extends
  48. AbstractInMemoryContainer<Object, Object, Item> implements
  49. Container.PropertySetChangeNotifier, Property.ValueChangeNotifier,
  50. Container.Sortable, Cloneable, Container.Filterable,
  51. Container.SimpleFilterable {
  52. /* Internal structure */
  53. /**
  54. * Linked list of ordered Property IDs.
  55. */
  56. private ArrayList<Object> propertyIds = new ArrayList<Object>();
  57. /**
  58. * Property ID to type mapping.
  59. */
  60. private Hashtable<Object, Class<?>> types = new Hashtable<Object, Class<?>>();
  61. /**
  62. * Hash of Items, where each Item is implemented as a mapping from Property
  63. * ID to Property value.
  64. */
  65. private Hashtable<Object, Map<Object, Object>> items = new Hashtable<Object, Map<Object, Object>>();
  66. /**
  67. * Set of properties that are read-only.
  68. */
  69. private HashSet<Property> readOnlyProperties = new HashSet<Property>();
  70. /**
  71. * List of all Property value change event listeners listening all the
  72. * properties.
  73. */
  74. private LinkedList<Property.ValueChangeListener> propertyValueChangeListeners = null;
  75. /**
  76. * Data structure containing all listeners interested in changes to single
  77. * Properties. The data structure is a hashtable mapping Property IDs to a
  78. * hashtable that maps Item IDs to a linked list of listeners listening
  79. * Property identified by given Property ID and Item ID.
  80. */
  81. private Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>> singlePropertyValueChangeListeners = null;
  82. private HashMap<Object, Object> defaultPropertyValues;
  83. private int nextGeneratedItemId = 1;
  84. /* Container constructors */
  85. public IndexedContainer() {
  86. super();
  87. }
  88. public IndexedContainer(Collection<?> itemIds) {
  89. this();
  90. if (items != null) {
  91. for (final Iterator<?> i = itemIds.iterator(); i.hasNext();) {
  92. Object itemId = i.next();
  93. internalAddItemAtEnd(itemId, new IndexedContainerItem(itemId),
  94. false);
  95. }
  96. filterAll();
  97. }
  98. }
  99. /* Container methods */
  100. @Override
  101. protected Item getUnfilteredItem(Object itemId) {
  102. if (itemId != null && items.containsKey(itemId)) {
  103. return new IndexedContainerItem(itemId);
  104. }
  105. return null;
  106. }
  107. /*
  108. * (non-Javadoc)
  109. *
  110. * @see com.vaadin.data.Container#getContainerPropertyIds()
  111. */
  112. public Collection<?> getContainerPropertyIds() {
  113. return Collections.unmodifiableCollection(propertyIds);
  114. }
  115. /**
  116. * Gets the type of a Property stored in the list.
  117. *
  118. * @param id
  119. * the ID of the Property.
  120. * @return Type of the requested Property
  121. */
  122. public Class<?> getType(Object propertyId) {
  123. return types.get(propertyId);
  124. }
  125. /*
  126. * (non-Javadoc)
  127. *
  128. * @see com.vaadin.data.Container#getContainerProperty(java.lang.Object,
  129. * java.lang.Object)
  130. */
  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 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 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. public Property getProperty() {
  400. return (Property) getSource();
  401. }
  402. }
  403. @Override
  404. public void addListener(Container.PropertySetChangeListener listener) {
  405. super.addListener(listener);
  406. }
  407. @Override
  408. public void removeListener(Container.PropertySetChangeListener listener) {
  409. super.removeListener(listener);
  410. }
  411. /*
  412. * (non-Javadoc)
  413. *
  414. * @see com.vaadin.data.Property.ValueChangeNotifier#addListener(com.
  415. * vaadin.data.Property.ValueChangeListener)
  416. */
  417. public void addListener(Property.ValueChangeListener listener) {
  418. if (propertyValueChangeListeners == null) {
  419. propertyValueChangeListeners = new LinkedList<Property.ValueChangeListener>();
  420. }
  421. propertyValueChangeListeners.add(listener);
  422. }
  423. /*
  424. * (non-Javadoc)
  425. *
  426. * @see com.vaadin.data.Property.ValueChangeNotifier#removeListener(com
  427. * .vaadin.data.Property.ValueChangeListener)
  428. */
  429. public void removeListener(Property.ValueChangeListener listener) {
  430. if (propertyValueChangeListeners != null) {
  431. propertyValueChangeListeners.remove(listener);
  432. }
  433. }
  434. /**
  435. * Sends a Property value change event to all interested listeners.
  436. *
  437. * @param source
  438. * the IndexedContainerProperty object.
  439. */
  440. private void firePropertyValueChange(IndexedContainerProperty source) {
  441. // Sends event to listeners listening all value changes
  442. if (propertyValueChangeListeners != null) {
  443. final Object[] l = propertyValueChangeListeners.toArray();
  444. final Property.ValueChangeEvent event = new IndexedContainer.PropertyValueChangeEvent(
  445. source);
  446. for (int i = 0; i < l.length; i++) {
  447. ((Property.ValueChangeListener) l[i]).valueChange(event);
  448. }
  449. }
  450. // Sends event to single property value change listeners
  451. if (singlePropertyValueChangeListeners != null) {
  452. final Map<Object, List<Property.ValueChangeListener>> propertySetToListenerListMap = singlePropertyValueChangeListeners
  453. .get(source.propertyId);
  454. if (propertySetToListenerListMap != null) {
  455. final List<Property.ValueChangeListener> listenerList = propertySetToListenerListMap
  456. .get(source.itemId);
  457. if (listenerList != null) {
  458. final Property.ValueChangeEvent event = new IndexedContainer.PropertyValueChangeEvent(
  459. source);
  460. Object[] listeners = listenerList.toArray();
  461. for (int i = 0; i < listeners.length; i++) {
  462. ((Property.ValueChangeListener) listeners[i])
  463. .valueChange(event);
  464. }
  465. }
  466. }
  467. }
  468. }
  469. @Override
  470. public Collection<?> getListeners(Class<?> eventType) {
  471. if (Property.ValueChangeEvent.class.isAssignableFrom(eventType)) {
  472. if (propertyValueChangeListeners == null) {
  473. return Collections.EMPTY_LIST;
  474. } else {
  475. return Collections
  476. .unmodifiableCollection(propertyValueChangeListeners);
  477. }
  478. }
  479. return super.getListeners(eventType);
  480. }
  481. @Override
  482. protected void fireItemAdded(int position, Object itemId, Item item) {
  483. if (position >= 0) {
  484. fireItemSetChange(new IndexedContainer.ItemSetChangeEvent(this,
  485. position));
  486. }
  487. }
  488. @Override
  489. protected void fireItemSetChange() {
  490. fireItemSetChange(new IndexedContainer.ItemSetChangeEvent(this, -1));
  491. }
  492. /**
  493. * Adds new single Property change listener.
  494. *
  495. * @param propertyId
  496. * the ID of the Property to add.
  497. * @param itemId
  498. * the ID of the Item .
  499. * @param listener
  500. * the listener to be added.
  501. */
  502. private void addSinglePropertyChangeListener(Object propertyId,
  503. Object itemId, Property.ValueChangeListener listener) {
  504. if (listener != null) {
  505. if (singlePropertyValueChangeListeners == null) {
  506. singlePropertyValueChangeListeners = new Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>>();
  507. }
  508. Map<Object, List<Property.ValueChangeListener>> propertySetToListenerListMap = singlePropertyValueChangeListeners
  509. .get(propertyId);
  510. if (propertySetToListenerListMap == null) {
  511. propertySetToListenerListMap = new Hashtable<Object, List<Property.ValueChangeListener>>();
  512. singlePropertyValueChangeListeners.put(propertyId,
  513. propertySetToListenerListMap);
  514. }
  515. List<Property.ValueChangeListener> listenerList = propertySetToListenerListMap
  516. .get(itemId);
  517. if (listenerList == null) {
  518. listenerList = new LinkedList<Property.ValueChangeListener>();
  519. propertySetToListenerListMap.put(itemId, listenerList);
  520. }
  521. listenerList.add(listener);
  522. }
  523. }
  524. /**
  525. * Removes a previously registered single Property change listener.
  526. *
  527. * @param propertyId
  528. * the ID of the Property to remove.
  529. * @param itemId
  530. * the ID of the Item.
  531. * @param listener
  532. * the listener to be removed.
  533. */
  534. private void removeSinglePropertyChangeListener(Object propertyId,
  535. Object itemId, Property.ValueChangeListener listener) {
  536. if (listener != null && singlePropertyValueChangeListeners != null) {
  537. final Map<Object, List<Property.ValueChangeListener>> propertySetToListenerListMap = singlePropertyValueChangeListeners
  538. .get(propertyId);
  539. if (propertySetToListenerListMap != null) {
  540. final List<Property.ValueChangeListener> listenerList = propertySetToListenerListMap
  541. .get(itemId);
  542. if (listenerList != null) {
  543. listenerList.remove(listener);
  544. if (listenerList.isEmpty()) {
  545. propertySetToListenerListMap.remove(itemId);
  546. }
  547. }
  548. if (propertySetToListenerListMap.isEmpty()) {
  549. singlePropertyValueChangeListeners.remove(propertyId);
  550. }
  551. }
  552. if (singlePropertyValueChangeListeners.isEmpty()) {
  553. singlePropertyValueChangeListeners = null;
  554. }
  555. }
  556. }
  557. /* Internal Item and Property implementations */
  558. /*
  559. * A class implementing the com.vaadin.data.Item interface to be contained
  560. * in the list.
  561. *
  562. * @author Vaadin Ltd.
  563. *
  564. * @version @VERSION@
  565. *
  566. * @since 3.0
  567. */
  568. class IndexedContainerItem implements Item {
  569. /**
  570. * Item ID in the host container for this Item.
  571. */
  572. private final Object itemId;
  573. /**
  574. * Constructs a new ListItem instance and connects it to a host
  575. * container.
  576. *
  577. * @param itemId
  578. * the Item ID of the new Item.
  579. */
  580. private IndexedContainerItem(Object itemId) {
  581. // Gets the item contents from the host
  582. if (itemId == null) {
  583. throw new NullPointerException();
  584. }
  585. this.itemId = itemId;
  586. }
  587. /*
  588. * (non-Javadoc)
  589. *
  590. * @see com.vaadin.data.Item#getItemProperty(java.lang.Object)
  591. */
  592. public Property getItemProperty(Object id) {
  593. return new IndexedContainerProperty(itemId, id);
  594. }
  595. public Collection<?> getItemPropertyIds() {
  596. return Collections.unmodifiableCollection(propertyIds);
  597. }
  598. /**
  599. * Gets the <code>String</code> representation of the contents of the
  600. * Item. The format of the string is a space separated catenation of the
  601. * <code>String</code> representations of the Properties contained by
  602. * the Item.
  603. *
  604. * @return <code>String</code> representation of the Item contents
  605. */
  606. @Override
  607. public String toString() {
  608. String retValue = "";
  609. for (final Iterator<?> i = propertyIds.iterator(); i.hasNext();) {
  610. final Object propertyId = i.next();
  611. retValue += getItemProperty(propertyId).toString();
  612. if (i.hasNext()) {
  613. retValue += " ";
  614. }
  615. }
  616. return retValue;
  617. }
  618. /**
  619. * Calculates a integer hash-code for the Item that's unique inside the
  620. * list. Two Items inside the same list have always different
  621. * hash-codes, though Items in different lists may have identical
  622. * hash-codes.
  623. *
  624. * @return A locally unique hash-code as integer
  625. */
  626. @Override
  627. public int hashCode() {
  628. return itemId.hashCode();
  629. }
  630. /**
  631. * Tests if the given object is the same as the this object. Two Items
  632. * got from a list container with the same ID are equal.
  633. *
  634. * @param obj
  635. * an object to compare with this object
  636. * @return <code>true</code> if the given object is the same as this
  637. * object, <code>false</code> if not
  638. */
  639. @Override
  640. public boolean equals(Object obj) {
  641. if (obj == null
  642. || !obj.getClass().equals(IndexedContainerItem.class)) {
  643. return false;
  644. }
  645. final IndexedContainerItem li = (IndexedContainerItem) obj;
  646. return getHost() == li.getHost() && itemId.equals(li.itemId);
  647. }
  648. private IndexedContainer getHost() {
  649. return IndexedContainer.this;
  650. }
  651. /**
  652. * IndexedContainerItem does not support adding new properties. Add
  653. * properties at container level. See
  654. * {@link IndexedContainer#addContainerProperty(Object, Class, Object)}
  655. *
  656. * @see com.vaadin.data.Item#addProperty(Object, Property)
  657. */
  658. public boolean addItemProperty(Object id, Property property)
  659. throws UnsupportedOperationException {
  660. throw new UnsupportedOperationException("Indexed container item "
  661. + "does not support adding new properties");
  662. }
  663. /**
  664. * Indexed container does not support removing properties. Remove
  665. * properties at container level. See
  666. * {@link IndexedContainer#removeContainerProperty(Object)}
  667. *
  668. * @see com.vaadin.data.Item#removeProperty(Object)
  669. */
  670. public boolean removeItemProperty(Object id)
  671. throws UnsupportedOperationException {
  672. throw new UnsupportedOperationException(
  673. "Indexed container item does not support property removal");
  674. }
  675. }
  676. /**
  677. * A class implementing the {@link Property} interface to be contained in
  678. * the {@link IndexedContainerItem} contained in the
  679. * {@link IndexedContainer}.
  680. *
  681. * @author Vaadin Ltd.
  682. *
  683. * @version
  684. * @VERSION@
  685. * @since 3.0
  686. */
  687. private class IndexedContainerProperty implements Property,
  688. Property.ValueChangeNotifier {
  689. /**
  690. * ID of the Item, where this property resides.
  691. */
  692. private final Object itemId;
  693. /**
  694. * Id of the Property.
  695. */
  696. private final Object propertyId;
  697. /**
  698. * Constructs a new {@link IndexedContainerProperty} object.
  699. *
  700. * @param itemId
  701. * the ID of the Item to connect the new Property to.
  702. * @param propertyId
  703. * the Property ID of the new Property.
  704. * @param host
  705. * the list that contains the Item to contain the new
  706. * Property.
  707. */
  708. private IndexedContainerProperty(Object itemId, Object propertyId) {
  709. if (itemId == null || propertyId == null) {
  710. // Null ids are not accepted
  711. throw new NullPointerException(
  712. "Container item or property ids can not be null");
  713. }
  714. this.propertyId = propertyId;
  715. this.itemId = itemId;
  716. }
  717. /*
  718. * (non-Javadoc)
  719. *
  720. * @see com.vaadin.data.Property#getType()
  721. */
  722. public Class<?> getType() {
  723. return types.get(propertyId);
  724. }
  725. /*
  726. * (non-Javadoc)
  727. *
  728. * @see com.vaadin.data.Property#getValue()
  729. */
  730. public Object getValue() {
  731. return items.get(itemId).get(propertyId);
  732. }
  733. /*
  734. * (non-Javadoc)
  735. *
  736. * @see com.vaadin.data.Property#isReadOnly()
  737. */
  738. public boolean isReadOnly() {
  739. return readOnlyProperties.contains(this);
  740. }
  741. /*
  742. * (non-Javadoc)
  743. *
  744. * @see com.vaadin.data.Property#setReadOnly(boolean)
  745. */
  746. public void setReadOnly(boolean newStatus) {
  747. if (newStatus) {
  748. readOnlyProperties.add(this);
  749. } else {
  750. readOnlyProperties.remove(this);
  751. }
  752. }
  753. /*
  754. * (non-Javadoc)
  755. *
  756. * @see com.vaadin.data.Property#setValue(java.lang.Object)
  757. */
  758. public void setValue(Object newValue)
  759. throws Property.ReadOnlyException, Property.ConversionException {
  760. // Gets the Property set
  761. final Map<Object, Object> propertySet = items.get(itemId);
  762. // Support null values on all types
  763. if (newValue == null) {
  764. propertySet.remove(propertyId);
  765. } else if (getType().isAssignableFrom(newValue.getClass())) {
  766. propertySet.put(propertyId, newValue);
  767. } else {
  768. try {
  769. // Gets the string constructor
  770. final Constructor<?> constr = getType().getConstructor(
  771. new Class[] { String.class });
  772. // Creates new object from the string
  773. propertySet.put(propertyId, constr
  774. .newInstance(new Object[] { newValue.toString() }));
  775. } catch (final java.lang.Exception e) {
  776. throw new Property.ConversionException(
  777. "Conversion for value '" + newValue + "' of class "
  778. + newValue.getClass().getName() + " to "
  779. + getType().getName() + " failed", e);
  780. }
  781. }
  782. // update the container filtering if this property is being filtered
  783. if (isPropertyFiltered(propertyId)) {
  784. filterAll();
  785. }
  786. firePropertyValueChange(this);
  787. }
  788. /**
  789. * Returns the value of the Property in human readable textual format.
  790. * The return value should be assignable to the <code>setValue</code>
  791. * method if the Property is not in read-only mode.
  792. *
  793. * @return <code>String</code> representation of the value stored in the
  794. * Property
  795. */
  796. @Override
  797. public String toString() {
  798. final Object value = getValue();
  799. if (value == null) {
  800. return null;
  801. }
  802. return value.toString();
  803. }
  804. /**
  805. * Calculates a integer hash-code for the Property that's unique inside
  806. * the Item containing the Property. Two different Properties inside the
  807. * same Item contained in the same list always have different
  808. * hash-codes, though Properties in different Items may have identical
  809. * hash-codes.
  810. *
  811. * @return A locally unique hash-code as integer
  812. */
  813. @Override
  814. public int hashCode() {
  815. return itemId.hashCode() ^ propertyId.hashCode();
  816. }
  817. /**
  818. * Tests if the given object is the same as the this object. Two
  819. * Properties got from an Item with the same ID are equal.
  820. *
  821. * @param obj
  822. * an object to compare with this object
  823. * @return <code>true</code> if the given object is the same as this
  824. * object, <code>false</code> if not
  825. */
  826. @Override
  827. public boolean equals(Object obj) {
  828. if (obj == null
  829. || !obj.getClass().equals(IndexedContainerProperty.class)) {
  830. return false;
  831. }
  832. final IndexedContainerProperty lp = (IndexedContainerProperty) obj;
  833. return lp.getHost() == getHost()
  834. && lp.propertyId.equals(propertyId)
  835. && lp.itemId.equals(itemId);
  836. }
  837. /*
  838. * (non-Javadoc)
  839. *
  840. * @see com.vaadin.data.Property.ValueChangeNotifier#addListener(
  841. * com.vaadin.data.Property.ValueChangeListener)
  842. */
  843. public void addListener(Property.ValueChangeListener listener) {
  844. addSinglePropertyChangeListener(propertyId, itemId, listener);
  845. }
  846. /*
  847. * (non-Javadoc)
  848. *
  849. * @see com.vaadin.data.Property.ValueChangeNotifier#removeListener
  850. * (com.vaadin.data.Property.ValueChangeListener)
  851. */
  852. public void removeListener(Property.ValueChangeListener listener) {
  853. removeSinglePropertyChangeListener(propertyId, itemId, listener);
  854. }
  855. private IndexedContainer getHost() {
  856. return IndexedContainer.this;
  857. }
  858. }
  859. /*
  860. * (non-Javadoc)
  861. *
  862. * @see com.vaadin.data.Container.Sortable#sort(java.lang.Object[],
  863. * boolean[])
  864. */
  865. public void sort(Object[] propertyId, boolean[] ascending) {
  866. sortContainer(propertyId, ascending);
  867. }
  868. /*
  869. * (non-Javadoc)
  870. *
  871. * @see com.vaadin.data.Container.Sortable#getSortableContainerPropertyIds
  872. * ()
  873. */
  874. public Collection<?> getSortableContainerPropertyIds() {
  875. return getSortablePropertyIds();
  876. }
  877. @Override
  878. public ItemSorter getItemSorter() {
  879. return super.getItemSorter();
  880. }
  881. @Override
  882. public void setItemSorter(ItemSorter itemSorter) {
  883. super.setItemSorter(itemSorter);
  884. }
  885. /**
  886. * Supports cloning of the IndexedContainer cleanly.
  887. *
  888. * @throws CloneNotSupportedException
  889. * if an object cannot be cloned. .
  890. *
  891. * @deprecated cloning support might be removed from IndexedContainer in the
  892. * future
  893. */
  894. @Deprecated
  895. @Override
  896. public Object clone() throws CloneNotSupportedException {
  897. // Creates the clone
  898. final IndexedContainer nc = new IndexedContainer();
  899. // Clone the shallow properties
  900. nc.setAllItemIds(getAllItemIds() != null ? (ListSet<Object>) ((ListSet<Object>) getAllItemIds())
  901. .clone() : null);
  902. nc.setItemSetChangeListeners(getItemSetChangeListeners() != null ? new LinkedList<Container.ItemSetChangeListener>(
  903. getItemSetChangeListeners()) : null);
  904. nc.propertyIds = propertyIds != null ? (ArrayList<Object>) propertyIds
  905. .clone() : null;
  906. nc.setPropertySetChangeListeners(getPropertySetChangeListeners() != null ? new LinkedList<Container.PropertySetChangeListener>(
  907. getPropertySetChangeListeners()) : null);
  908. nc.propertyValueChangeListeners = propertyValueChangeListeners != null ? (LinkedList<Property.ValueChangeListener>) propertyValueChangeListeners
  909. .clone() : null;
  910. nc.readOnlyProperties = readOnlyProperties != null ? (HashSet<Property>) readOnlyProperties
  911. .clone() : null;
  912. nc.singlePropertyValueChangeListeners = singlePropertyValueChangeListeners != null ? (Hashtable<Object, Map<Object, List<Property.ValueChangeListener>>>) singlePropertyValueChangeListeners
  913. .clone() : null;
  914. nc.types = types != null ? (Hashtable<Object, Class<?>>) types.clone()
  915. : null;
  916. nc.setFilters((HashSet<Filter>) ((HashSet<Filter>) getFilters())
  917. .clone());
  918. nc.setFilteredItemIds(getFilteredItemIds() == null ? null
  919. : (ListSet<Object>) ((ListSet<Object>) getFilteredItemIds())
  920. .clone());
  921. // Clone property-values
  922. if (items == null) {
  923. nc.items = null;
  924. } else {
  925. nc.items = new Hashtable<Object, Map<Object, Object>>();
  926. for (final Iterator<?> i = items.keySet().iterator(); i.hasNext();) {
  927. final Object id = i.next();
  928. final Hashtable<Object, Object> it = (Hashtable<Object, Object>) items
  929. .get(id);
  930. nc.items.put(id, (Map<Object, Object>) it.clone());
  931. }
  932. }
  933. return nc;
  934. }
  935. public void addContainerFilter(Object propertyId, String filterString,
  936. boolean ignoreCase, boolean onlyMatchPrefix) {
  937. try {
  938. addFilter(new SimpleStringFilter(propertyId, filterString,
  939. ignoreCase, onlyMatchPrefix));
  940. } catch (UnsupportedFilterException e) {
  941. // the filter instance created here is always valid for in-memory
  942. // containers
  943. }
  944. }
  945. public void removeAllContainerFilters() {
  946. removeAllFilters();
  947. }
  948. public void removeContainerFilters(Object propertyId) {
  949. removeFilters(propertyId);
  950. }
  951. public void addContainerFilter(Filter filter)
  952. throws UnsupportedFilterException {
  953. addFilter(filter);
  954. }
  955. public void removeContainerFilter(Filter filter) {
  956. removeFilter(filter);
  957. }
  958. }