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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  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.LinkedHashSet;
  16. import java.util.LinkedList;
  17. import java.util.NoSuchElementException;
  18. import com.vaadin.data.Container;
  19. import com.vaadin.data.Item;
  20. import com.vaadin.data.Property;
  21. /**
  22. * An implementation of the <code>{@link Container.Indexed}</code> interface
  23. * with all important features.</p>
  24. *
  25. * Features:
  26. * <ul>
  27. * <li> {@link Container.Indexed}
  28. * <li> {@link Container.Ordered}
  29. * <li> {@link Container.Sortable}
  30. * <li> {@link Container.Filterable}
  31. * <li> {@link Cloneable}
  32. * <li>Sends all needed events on content changes.
  33. * </ul>
  34. *
  35. * @see com.vaadin.data.Container
  36. *
  37. * @author IT Mill Ltd.
  38. * @version
  39. * @VERSION@
  40. * @since 3.0
  41. */
  42. @SuppressWarnings("serial")
  43. public class IndexedContainer implements Container.Indexed,
  44. Container.ItemSetChangeNotifier, Container.PropertySetChangeNotifier,
  45. Property.ValueChangeNotifier, Container.Sortable, Cloneable,
  46. Container.Filterable {
  47. /* Internal structure */
  48. /**
  49. * Linked list of ordered Item IDs.
  50. */
  51. private ArrayList<Object> itemIds = new ArrayList<Object>();
  52. /** List of item ids that passes the filtering */
  53. private LinkedHashSet<Object> filteredItemIds = null;
  54. /**
  55. * Linked list of ordered Property IDs.
  56. */
  57. private ArrayList<Object> propertyIds = new ArrayList<Object>();
  58. /**
  59. * Property ID to type mapping.
  60. */
  61. private Hashtable types = new Hashtable();
  62. /**
  63. * Hash of Items, where each Item is implemented as a mapping from Property
  64. * ID to Property value.
  65. */
  66. private Hashtable items = new Hashtable();
  67. /**
  68. * Set of properties that are read-only.
  69. */
  70. private HashSet readOnlyProperties = new HashSet();
  71. /**
  72. * List of all Property value change event listeners listening all the
  73. * properties.
  74. */
  75. private LinkedList propertyValueChangeListeners = null;
  76. /**
  77. * Data structure containing all listeners interested in changes to single
  78. * Properties. The data structure is a hashtable mapping Property IDs to a
  79. * hashtable that maps Item IDs to a linked list of listeners listening
  80. * Property identified by given Property ID and Item ID.
  81. */
  82. private Hashtable singlePropertyValueChangeListeners = null;
  83. /**
  84. * List of all Property set change event listeners.
  85. */
  86. private LinkedList propertySetChangeListeners = null;
  87. /**
  88. * List of all container Item set change event listeners.
  89. */
  90. private LinkedList itemSetChangeListeners = null;
  91. /**
  92. * The item sorter which is used for sorting the container.
  93. */
  94. private ItemSorter itemSorter = new DefaultItemSorter();
  95. /**
  96. * Filters that are applied to the container to limit the items visible in
  97. * it
  98. */
  99. private HashSet<Filter> filters;
  100. private HashMap<Object, Object> defaultPropertyValues;
  101. private int nextGeneratedItemId = 1;
  102. /* Container constructors */
  103. public IndexedContainer() {
  104. }
  105. public IndexedContainer(Collection itemIds) {
  106. if (items != null) {
  107. for (final Iterator i = itemIds.iterator(); i.hasNext();) {
  108. this.addItem(i.next());
  109. }
  110. }
  111. }
  112. /* Container methods */
  113. /*
  114. * (non-Javadoc)
  115. *
  116. * @see com.vaadin.data.Container#getItem(java.lang.Object)
  117. */
  118. public Item getItem(Object itemId) {
  119. if (itemId != null
  120. && items.containsKey(itemId)
  121. && (filteredItemIds == null || filteredItemIds.contains(itemId))) {
  122. return new IndexedContainerItem(itemId);
  123. }
  124. return null;
  125. }
  126. /*
  127. * (non-Javadoc)
  128. *
  129. * @see com.vaadin.data.Container#getItemIds()
  130. */
  131. public Collection getItemIds() {
  132. if (filteredItemIds != null) {
  133. return Collections.unmodifiableCollection(filteredItemIds);
  134. }
  135. return Collections.unmodifiableCollection(itemIds);
  136. }
  137. /*
  138. * (non-Javadoc)
  139. *
  140. * @see com.vaadin.data.Container#getContainerPropertyIds()
  141. */
  142. public Collection getContainerPropertyIds() {
  143. return Collections.unmodifiableCollection(propertyIds);
  144. }
  145. /**
  146. * Gets the type of a Property stored in the list.
  147. *
  148. * @param id
  149. * the ID of the Property.
  150. * @return Type of the requested Property
  151. */
  152. public Class getType(Object propertyId) {
  153. return (Class) types.get(propertyId);
  154. }
  155. /*
  156. * (non-Javadoc)
  157. *
  158. * @see com.vaadin.data.Container#getContainerProperty(java.lang.Object,
  159. * java.lang.Object)
  160. */
  161. public Property getContainerProperty(Object itemId, Object propertyId) {
  162. if (itemId == null) {
  163. return null;
  164. } else if (filteredItemIds == null) {
  165. if (!items.containsKey(itemId)) {
  166. return null;
  167. }
  168. } else if (!filteredItemIds.contains(itemId)) {
  169. return null;
  170. }
  171. return new IndexedContainerProperty(itemId, propertyId);
  172. }
  173. /*
  174. * (non-Javadoc)
  175. *
  176. * @see com.vaadin.data.Container#size()
  177. */
  178. public int size() {
  179. if (filteredItemIds == null) {
  180. return itemIds.size();
  181. }
  182. return filteredItemIds.size();
  183. }
  184. /*
  185. * (non-Javadoc)
  186. *
  187. * @see com.vaadin.data.Container#containsId(java.lang.Object)
  188. */
  189. public boolean containsId(Object itemId) {
  190. if (itemId == null) {
  191. return false;
  192. }
  193. if (filteredItemIds != null) {
  194. return filteredItemIds.contains(itemId);
  195. }
  196. return items.containsKey(itemId);
  197. }
  198. /*
  199. * (non-Javadoc)
  200. *
  201. * @see com.vaadin.data.Container#addContainerProperty(java.lang.Object,
  202. * java.lang.Class, java.lang.Object)
  203. */
  204. public boolean addContainerProperty(Object propertyId, Class<?> type,
  205. Object defaultValue) {
  206. // Fails, if nulls are given
  207. if (propertyId == null || type == null) {
  208. return false;
  209. }
  210. // Fails if the Property is already present
  211. if (propertyIds.contains(propertyId)) {
  212. return false;
  213. }
  214. // Adds the Property to Property list and types
  215. propertyIds.add(propertyId);
  216. types.put(propertyId, type);
  217. // If default value is given, set it
  218. if (defaultValue != null) {
  219. // for existing rows
  220. for (final Iterator i = itemIds.iterator(); i.hasNext();) {
  221. getItem(i.next()).getItemProperty(propertyId).setValue(
  222. defaultValue);
  223. }
  224. // store for next rows
  225. if (defaultPropertyValues == null) {
  226. defaultPropertyValues = new HashMap<Object, Object>();
  227. }
  228. defaultPropertyValues.put(propertyId, defaultValue);
  229. }
  230. // Sends a change event
  231. fireContainerPropertySetChange();
  232. return true;
  233. }
  234. /*
  235. * (non-Javadoc)
  236. *
  237. * @see com.vaadin.data.Container#removeAllItems()
  238. */
  239. public boolean removeAllItems() {
  240. // Removes all Items
  241. itemIds.clear();
  242. items.clear();
  243. if (filteredItemIds != null) {
  244. filteredItemIds.clear();
  245. }
  246. // Sends a change event
  247. fireContentsChange(-1);
  248. return true;
  249. }
  250. /*
  251. * (non-Javadoc)
  252. *
  253. * @see com.vaadin.data.Container#addItem()
  254. */
  255. public Object addItem() {
  256. // Creates a new id
  257. final Object id = generateId();
  258. // Adds the Item into container
  259. addItem(id);
  260. return id;
  261. }
  262. /*
  263. * (non-Javadoc)
  264. *
  265. * @see com.vaadin.data.Container#addItem(java.lang.Object)
  266. */
  267. public Item addItem(Object itemId) {
  268. // Make sure that the Item is valid and has not been created yet
  269. if (itemId == null || items.containsKey(itemId)) {
  270. return null;
  271. }
  272. // Adds the Item to container (at the end of the unfiltered list)
  273. itemIds.add(itemId);
  274. Hashtable t = new Hashtable();
  275. items.put(itemId, t);
  276. addDefaultValues(t);
  277. // this optimization is why some code is duplicated with
  278. // addItemAtInternalIndex()
  279. if (filteredItemIds != null) {
  280. if (passesFilters(itemId)) {
  281. filteredItemIds.add(itemId);
  282. }
  283. }
  284. // Sends the event
  285. fireContentsChange(itemIds.size() - 1);
  286. return new IndexedContainerItem(itemId);
  287. }
  288. /**
  289. * Helper method to add default values for items if available
  290. *
  291. * @param t
  292. * data table of added item
  293. */
  294. private void addDefaultValues(Hashtable t) {
  295. if (defaultPropertyValues != null) {
  296. for (Object key : defaultPropertyValues.keySet()) {
  297. t.put(key, defaultPropertyValues.get(key));
  298. }
  299. }
  300. }
  301. /*
  302. * (non-Javadoc)
  303. *
  304. * @see com.vaadin.data.Container#removeItem(java.lang.Object)
  305. */
  306. public boolean removeItem(Object itemId) {
  307. if (itemId == null) {
  308. return false;
  309. }
  310. if (items.remove(itemId) == null) {
  311. return false;
  312. }
  313. itemIds.remove(itemId);
  314. if (filteredItemIds != null) {
  315. filteredItemIds.remove(itemId);
  316. }
  317. fireContentsChange(-1);
  318. return true;
  319. }
  320. /*
  321. * (non-Javadoc)
  322. *
  323. * @see com.vaadin.data.Container#removeContainerProperty(java.lang.Object )
  324. */
  325. public boolean removeContainerProperty(Object propertyId) {
  326. // Fails if the Property is not present
  327. if (!propertyIds.contains(propertyId)) {
  328. return false;
  329. }
  330. // Removes the Property to Property list and types
  331. propertyIds.remove(propertyId);
  332. types.remove(propertyId);
  333. if (defaultPropertyValues != null) {
  334. defaultPropertyValues.remove(propertyId);
  335. }
  336. // If remove the Property from all Items
  337. for (final Iterator i = itemIds.iterator(); i.hasNext();) {
  338. ((Hashtable) items.get(i.next())).remove(propertyId);
  339. }
  340. // Sends a change event
  341. fireContainerPropertySetChange();
  342. return true;
  343. }
  344. /* Container.Ordered methods */
  345. /*
  346. * (non-Javadoc)
  347. *
  348. * @see com.vaadin.data.Container.Ordered#firstItemId()
  349. */
  350. public Object firstItemId() {
  351. try {
  352. if (filteredItemIds != null) {
  353. return filteredItemIds.iterator().next();
  354. }
  355. return itemIds.get(0);
  356. } catch (final IndexOutOfBoundsException e) {
  357. } catch (final NoSuchElementException e) {
  358. }
  359. return null;
  360. }
  361. /*
  362. * (non-Javadoc)
  363. *
  364. * @see com.vaadin.data.Container.Ordered#lastItemId()
  365. */
  366. public Object lastItemId() {
  367. try {
  368. if (filteredItemIds != null) {
  369. final Iterator i = filteredItemIds.iterator();
  370. Object last = null;
  371. while (i.hasNext()) {
  372. last = i.next();
  373. }
  374. return last;
  375. }
  376. return itemIds.get(itemIds.size() - 1);
  377. } catch (final IndexOutOfBoundsException e) {
  378. }
  379. return null;
  380. }
  381. /*
  382. * (non-Javadoc)
  383. *
  384. * @see com.vaadin.data.Container.Ordered#nextItemId(java.lang.Object)
  385. */
  386. public Object nextItemId(Object itemId) {
  387. if (filteredItemIds != null) {
  388. if (itemId == null || !filteredItemIds.contains(itemId)) {
  389. return null;
  390. }
  391. final Iterator i = filteredItemIds.iterator();
  392. while (i.hasNext() && !itemId.equals(i.next())) {
  393. ;
  394. }
  395. if (i.hasNext()) {
  396. return i.next();
  397. }
  398. return null;
  399. }
  400. try {
  401. int idx = itemIds.indexOf(itemId);
  402. if (idx == -1) {
  403. // If the given Item is not found in the Container,
  404. // null is returned.
  405. return null;
  406. }
  407. return itemIds.get(idx + 1);
  408. } catch (final IndexOutOfBoundsException e) {
  409. return null;
  410. }
  411. }
  412. /*
  413. * (non-Javadoc)
  414. *
  415. * @see com.vaadin.data.Container.Ordered#prevItemId(java.lang.Object)
  416. */
  417. public Object prevItemId(Object itemId) {
  418. if (filteredItemIds != null) {
  419. if (!filteredItemIds.contains(itemId)) {
  420. return null;
  421. }
  422. final Iterator i = filteredItemIds.iterator();
  423. if (itemId == null) {
  424. return null;
  425. }
  426. Object prev = null;
  427. Object current;
  428. while (i.hasNext() && !itemId.equals(current = i.next())) {
  429. prev = current;
  430. }
  431. return prev;
  432. }
  433. try {
  434. return itemIds.get(itemIds.indexOf(itemId) - 1);
  435. } catch (final IndexOutOfBoundsException e) {
  436. return null;
  437. }
  438. }
  439. /*
  440. * (non-Javadoc)
  441. *
  442. * @see com.vaadin.data.Container.Ordered#isFirstId(java.lang.Object)
  443. */
  444. public boolean isFirstId(Object itemId) {
  445. if (filteredItemIds != null) {
  446. try {
  447. final Object first = filteredItemIds.iterator().next();
  448. return (itemId != null && itemId.equals(first));
  449. } catch (final NoSuchElementException e) {
  450. return false;
  451. }
  452. }
  453. return (size() >= 1 && itemIds.get(0).equals(itemId));
  454. }
  455. /*
  456. * (non-Javadoc)
  457. *
  458. * @see com.vaadin.data.Container.Ordered#isLastId(java.lang.Object)
  459. */
  460. public boolean isLastId(Object itemId) {
  461. if (filteredItemIds != null) {
  462. try {
  463. Object last = null;
  464. for (final Iterator i = filteredItemIds.iterator(); i.hasNext();) {
  465. last = i.next();
  466. }
  467. return (itemId != null && itemId.equals(last));
  468. } catch (final NoSuchElementException e) {
  469. return false;
  470. }
  471. }
  472. final int s = size();
  473. return (s >= 1 && itemIds.get(s - 1).equals(itemId));
  474. }
  475. /*
  476. * (non-Javadoc)
  477. *
  478. * @see com.vaadin.data.Container.Ordered#addItemAfter(java.lang.Object,
  479. * java.lang.Object)
  480. */
  481. public Item addItemAfter(Object previousItemId, Object newItemId) {
  482. // Adding an item after null item adds the item as first item of the
  483. // ordered container.
  484. if (previousItemId == null) {
  485. return addItemAt(0, newItemId);
  486. }
  487. // Get the index of the addition
  488. int index = -1;
  489. if (previousItemId != null) {
  490. index = 1 + indexOfId(previousItemId);
  491. if (index <= 0 || index > size()) {
  492. return null;
  493. }
  494. }
  495. return addItemAt(index, newItemId);
  496. }
  497. /*
  498. * (non-Javadoc)
  499. *
  500. * @see com.vaadin.data.Container.Ordered#addItemAfter(java.lang.Object)
  501. */
  502. public Object addItemAfter(Object previousItemId) {
  503. // Creates a new id
  504. final Object id = generateId();
  505. if (addItemAfter(previousItemId, id) != null) {
  506. return id;
  507. } else {
  508. return null;
  509. }
  510. }
  511. /*
  512. * (non-Javadoc)
  513. *
  514. * @see com.vaadin.data.Container.Indexed#getIdByIndex(int)
  515. */
  516. public Object getIdByIndex(int index) {
  517. if (filteredItemIds != null) {
  518. if (index < 0) {
  519. throw new IndexOutOfBoundsException();
  520. }
  521. try {
  522. final Iterator i = filteredItemIds.iterator();
  523. while (index-- > 0) {
  524. i.next();
  525. }
  526. return i.next();
  527. } catch (final NoSuchElementException e) {
  528. throw new IndexOutOfBoundsException();
  529. }
  530. }
  531. return itemIds.get(index);
  532. }
  533. /*
  534. * (non-Javadoc)
  535. *
  536. * @see com.vaadin.data.Container.Indexed#indexOfId(java.lang.Object)
  537. */
  538. public int indexOfId(Object itemId) {
  539. if (filteredItemIds != null) {
  540. int index = 0;
  541. if (itemId == null) {
  542. return -1;
  543. }
  544. final Iterator i = filteredItemIds.iterator();
  545. while (i.hasNext()) {
  546. Object id = i.next();
  547. if (itemId.equals(id)) {
  548. return index;
  549. }
  550. index++;
  551. }
  552. return -1;
  553. }
  554. return itemIds.indexOf(itemId);
  555. }
  556. /*
  557. * (non-Javadoc)
  558. *
  559. * @see com.vaadin.data.Container.Indexed#addItemAt(int, java.lang.Object)
  560. */
  561. public Item addItemAt(int index, Object newItemId) {
  562. // add item based on a filtered index
  563. int internalIndex = -1;
  564. if (filteredItemIds == null) {
  565. internalIndex = index;
  566. } else if (index == 0) {
  567. internalIndex = 0;
  568. } else if (index == size()) {
  569. // add just after the last item
  570. Object id = getIdByIndex(index - 1);
  571. internalIndex = itemIds.indexOf(id) + 1;
  572. } else if (index > 0 && index < size()) {
  573. // map the index of the visible item to its unfiltered index
  574. Object id = getIdByIndex(index);
  575. internalIndex = itemIds.indexOf(id);
  576. }
  577. if (internalIndex >= 0) {
  578. return addItemAtInternalIndex(internalIndex, newItemId);
  579. } else {
  580. return null;
  581. }
  582. }
  583. /*
  584. * (non-Javadoc)
  585. *
  586. * @see com.vaadin.data.Container.Indexed#addItemAt(int)
  587. */
  588. public Object addItemAt(int index) {
  589. // Creates a new id
  590. final Object id = generateId();
  591. // Adds the Item into container
  592. addItemAt(index, id);
  593. return id;
  594. }
  595. /**
  596. * Generates an unique identifier for use as an item id. Guarantees that the
  597. * generated id is not currently used as an id.
  598. *
  599. * @return
  600. */
  601. private Serializable generateId() {
  602. Serializable id;
  603. do {
  604. id = Integer.valueOf(nextGeneratedItemId++);
  605. } while (items.containsKey(id));
  606. return id;
  607. }
  608. /* Event notifiers */
  609. /**
  610. * Adds new item at given index of the internal (unfiltered) list.
  611. * <p>
  612. * The item is also added in the visible part of the list if it passes the
  613. * filters.
  614. * </p>
  615. *
  616. * @param index
  617. * Internal index to add the new item.
  618. * @param newItemId
  619. * Id of the new item to be added.
  620. * @return Returns new item or null if the operation fails.
  621. */
  622. private Item addItemAtInternalIndex(int index, Object newItemId) {
  623. // Make sure that the Item is valid and has not been created yet
  624. if (index < 0 || index > itemIds.size() || newItemId == null
  625. || items.containsKey(newItemId)) {
  626. return null;
  627. }
  628. // Adds the Item to container
  629. itemIds.add(index, newItemId);
  630. Hashtable t = new Hashtable();
  631. items.put(newItemId, t);
  632. addDefaultValues(t);
  633. if (filteredItemIds != null) {
  634. // when the item data is set later (IndexedContainerProperty),
  635. // filtering is updated
  636. updateContainerFiltering();
  637. } else {
  638. fireContentsChange(index);
  639. }
  640. return new IndexedContainerItem(newItemId);
  641. }
  642. /**
  643. * An <code>event</code> object specifying the list whose Property set has
  644. * changed.
  645. *
  646. * @author IT Mill Ltd.
  647. * @version
  648. * @VERSION@
  649. * @since 3.0
  650. */
  651. private class PropertySetChangeEvent extends EventObject implements
  652. Container.PropertySetChangeEvent, Serializable {
  653. private PropertySetChangeEvent(IndexedContainer source) {
  654. super(source);
  655. }
  656. /*
  657. * (non-Javadoc)
  658. *
  659. * @see com.vaadin.data.Container.PropertySetChangeEvent#getContainer ()
  660. */
  661. public Container getContainer() {
  662. return (Container) getSource();
  663. }
  664. }
  665. /**
  666. * An <code>event</code> object specifying the list whose Item set has
  667. * changed.
  668. *
  669. * @author IT Mill Ltd.
  670. * @version
  671. * @VERSION@
  672. * @since 3.0
  673. */
  674. public class ItemSetChangeEvent extends EventObject implements
  675. Container.ItemSetChangeEvent, Serializable {
  676. private final int addedItemIndex;
  677. private ItemSetChangeEvent(IndexedContainer source, int addedItemIndex) {
  678. super(source);
  679. this.addedItemIndex = addedItemIndex;
  680. }
  681. /*
  682. * (non-Javadoc)
  683. *
  684. * @see com.vaadin.data.Container.ItemSetChangeEvent#getContainer()
  685. */
  686. public Container getContainer() {
  687. return (Container) getSource();
  688. }
  689. /**
  690. * Iff one item is added, gives its index.
  691. *
  692. * @return -1 if either multiple items are changed or some other change
  693. * than add is done.
  694. */
  695. public int getAddedItemIndex() {
  696. return addedItemIndex;
  697. }
  698. }
  699. /**
  700. * An <code>event</code> object specifying the Property in a list whose
  701. * value has changed.
  702. *
  703. * @author IT Mill Ltd.
  704. * @version
  705. * @VERSION@
  706. * @since 3.0
  707. */
  708. private class PropertyValueChangeEvent extends EventObject implements
  709. Property.ValueChangeEvent, Serializable {
  710. private PropertyValueChangeEvent(Property source) {
  711. super(source);
  712. }
  713. /*
  714. * (non-Javadoc)
  715. *
  716. * @see com.vaadin.data.Property.ValueChangeEvent#getProperty()
  717. */
  718. public Property getProperty() {
  719. return (Property) getSource();
  720. }
  721. }
  722. /*
  723. * (non-Javadoc)
  724. *
  725. * @see com.vaadin.data.Container.PropertySetChangeNotifier#addListener
  726. * (com.vaadin.data.Container.PropertySetChangeListener)
  727. */
  728. public void addListener(Container.PropertySetChangeListener listener) {
  729. if (propertySetChangeListeners == null) {
  730. propertySetChangeListeners = new LinkedList();
  731. }
  732. propertySetChangeListeners.add(listener);
  733. }
  734. /*
  735. * (non-Javadoc)
  736. *
  737. * @see com.vaadin.data.Container.PropertySetChangeNotifier#removeListener
  738. * (com.vaadin.data.Container.PropertySetChangeListener)
  739. */
  740. public void removeListener(Container.PropertySetChangeListener listener) {
  741. if (propertySetChangeListeners != null) {
  742. propertySetChangeListeners.remove(listener);
  743. }
  744. }
  745. /*
  746. * (non-Javadoc)
  747. *
  748. * @see com.vaadin.data.Container.ItemSetChangeNotifier#addListener(com
  749. * .vaadin.data.Container.ItemSetChangeListener)
  750. */
  751. public void addListener(Container.ItemSetChangeListener listener) {
  752. if (itemSetChangeListeners == null) {
  753. itemSetChangeListeners = new LinkedList();
  754. }
  755. itemSetChangeListeners.add(listener);
  756. }
  757. /*
  758. * (non-Javadoc)
  759. *
  760. * @see com.vaadin.data.Container.ItemSetChangeNotifier#removeListener
  761. * (com.vaadin.data.Container.ItemSetChangeListener)
  762. */
  763. public void removeListener(Container.ItemSetChangeListener listener) {
  764. if (itemSetChangeListeners != null) {
  765. itemSetChangeListeners.remove(listener);
  766. }
  767. }
  768. /*
  769. * (non-Javadoc)
  770. *
  771. * @see com.vaadin.data.Property.ValueChangeNotifier#addListener(com.
  772. * vaadin.data.Property.ValueChangeListener)
  773. */
  774. public void addListener(Property.ValueChangeListener listener) {
  775. if (propertyValueChangeListeners == null) {
  776. propertyValueChangeListeners = new LinkedList();
  777. }
  778. propertyValueChangeListeners.add(listener);
  779. }
  780. /*
  781. * (non-Javadoc)
  782. *
  783. * @see com.vaadin.data.Property.ValueChangeNotifier#removeListener(com
  784. * .vaadin.data.Property.ValueChangeListener)
  785. */
  786. public void removeListener(Property.ValueChangeListener listener) {
  787. if (propertyValueChangeListeners != null) {
  788. propertyValueChangeListeners.remove(listener);
  789. }
  790. }
  791. /**
  792. * Sends a Property value change event to all interested listeners.
  793. *
  794. * @param source
  795. * the IndexedContainerProperty object.
  796. */
  797. private void firePropertyValueChange(IndexedContainerProperty source) {
  798. // Sends event to listeners listening all value changes
  799. if (propertyValueChangeListeners != null) {
  800. final Object[] l = propertyValueChangeListeners.toArray();
  801. final Property.ValueChangeEvent event = new IndexedContainer.PropertyValueChangeEvent(
  802. source);
  803. for (int i = 0; i < l.length; i++) {
  804. ((Property.ValueChangeListener) l[i]).valueChange(event);
  805. }
  806. }
  807. // Sends event to single property value change listeners
  808. if (singlePropertyValueChangeListeners != null) {
  809. final Hashtable propertySetToListenerListMap = (Hashtable) singlePropertyValueChangeListeners
  810. .get(source.propertyId);
  811. if (propertySetToListenerListMap != null) {
  812. final LinkedList listenerList = (LinkedList) propertySetToListenerListMap
  813. .get(source.itemId);
  814. if (listenerList != null) {
  815. final Property.ValueChangeEvent event = new IndexedContainer.PropertyValueChangeEvent(
  816. source);
  817. Object[] listeners = listenerList.toArray();
  818. for (int i = 0; i < listeners.length; i++) {
  819. ((Property.ValueChangeListener) listeners[i])
  820. .valueChange(event);
  821. }
  822. }
  823. }
  824. }
  825. }
  826. /**
  827. * Sends a Property set change event to all interested listeners.
  828. */
  829. private void fireContainerPropertySetChange() {
  830. if (propertySetChangeListeners != null) {
  831. final Object[] l = propertySetChangeListeners.toArray();
  832. final Container.PropertySetChangeEvent event = new IndexedContainer.PropertySetChangeEvent(
  833. this);
  834. for (int i = 0; i < l.length; i++) {
  835. ((Container.PropertySetChangeListener) l[i])
  836. .containerPropertySetChange(event);
  837. }
  838. }
  839. }
  840. /**
  841. * Sends Item set change event to all registered interested listeners.
  842. *
  843. * @param addedItemIndex
  844. * index of new item if change event was an item addition
  845. */
  846. protected void fireContentsChange(int addedItemIndex) {
  847. if (itemSetChangeListeners != null) {
  848. final Object[] l = itemSetChangeListeners.toArray();
  849. final Container.ItemSetChangeEvent event = new IndexedContainer.ItemSetChangeEvent(
  850. this, addedItemIndex);
  851. for (int i = 0; i < l.length; i++) {
  852. ((Container.ItemSetChangeListener) l[i])
  853. .containerItemSetChange(event);
  854. }
  855. }
  856. }
  857. /**
  858. * Adds new single Property change listener.
  859. *
  860. * @param propertyId
  861. * the ID of the Property to add.
  862. * @param itemId
  863. * the ID of the Item .
  864. * @param listener
  865. * the listener to be added.
  866. */
  867. private void addSinglePropertyChangeListener(Object propertyId,
  868. Object itemId, Property.ValueChangeListener listener) {
  869. if (listener != null) {
  870. if (singlePropertyValueChangeListeners == null) {
  871. singlePropertyValueChangeListeners = new Hashtable();
  872. }
  873. Hashtable propertySetToListenerListMap = (Hashtable) singlePropertyValueChangeListeners
  874. .get(propertyId);
  875. if (propertySetToListenerListMap == null) {
  876. propertySetToListenerListMap = new Hashtable();
  877. singlePropertyValueChangeListeners.put(propertyId,
  878. propertySetToListenerListMap);
  879. }
  880. LinkedList listenerList = (LinkedList) propertySetToListenerListMap
  881. .get(itemId);
  882. if (listenerList == null) {
  883. listenerList = new LinkedList();
  884. propertySetToListenerListMap.put(itemId, listenerList);
  885. }
  886. listenerList.addLast(listener);
  887. }
  888. }
  889. /**
  890. * Removes a previously registered single Property change listener.
  891. *
  892. * @param propertyId
  893. * the ID of the Property to remove.
  894. * @param itemId
  895. * the ID of the Item.
  896. * @param listener
  897. * the listener to be removed.
  898. */
  899. private void removeSinglePropertyChangeListener(Object propertyId,
  900. Object itemId, Property.ValueChangeListener listener) {
  901. if (listener != null && singlePropertyValueChangeListeners != null) {
  902. final Hashtable propertySetToListenerListMap = (Hashtable) singlePropertyValueChangeListeners
  903. .get(propertyId);
  904. if (propertySetToListenerListMap != null) {
  905. final LinkedList listenerList = (LinkedList) propertySetToListenerListMap
  906. .get(itemId);
  907. if (listenerList != null) {
  908. listenerList.remove(listener);
  909. if (listenerList.isEmpty()) {
  910. propertySetToListenerListMap.remove(itemId);
  911. }
  912. }
  913. if (propertySetToListenerListMap.isEmpty()) {
  914. singlePropertyValueChangeListeners.remove(propertyId);
  915. }
  916. }
  917. if (singlePropertyValueChangeListeners.isEmpty()) {
  918. singlePropertyValueChangeListeners = null;
  919. }
  920. }
  921. }
  922. /* Internal Item and Property implementations */
  923. /*
  924. * A class implementing the com.vaadin.data.Item interface to be contained
  925. * in the list.
  926. *
  927. * @author IT Mill Ltd.
  928. *
  929. * @version @VERSION@
  930. *
  931. * @since 3.0
  932. */
  933. class IndexedContainerItem implements Item {
  934. /**
  935. * Item ID in the host container for this Item.
  936. */
  937. private final Object itemId;
  938. /**
  939. * Constructs a new ListItem instance and connects it to a host
  940. * container.
  941. *
  942. * @param itemId
  943. * the Item ID of the new Item.
  944. */
  945. private IndexedContainerItem(Object itemId) {
  946. // Gets the item contents from the host
  947. if (itemId == null) {
  948. throw new NullPointerException();
  949. }
  950. this.itemId = itemId;
  951. }
  952. /*
  953. * (non-Javadoc)
  954. *
  955. * @see com.vaadin.data.Item#getItemProperty(java.lang.Object)
  956. */
  957. public Property getItemProperty(Object id) {
  958. return new IndexedContainerProperty(itemId, id);
  959. }
  960. public Collection getItemPropertyIds() {
  961. return Collections.unmodifiableCollection(propertyIds);
  962. }
  963. /**
  964. * Gets the <code>String</code> representation of the contents of the
  965. * Item. The format of the string is a space separated catenation of the
  966. * <code>String</code> representations of the Properties contained by
  967. * the Item.
  968. *
  969. * @return <code>String</code> representation of the Item contents
  970. */
  971. @Override
  972. public String toString() {
  973. String retValue = "";
  974. for (final Iterator i = propertyIds.iterator(); i.hasNext();) {
  975. final Object propertyId = i.next();
  976. retValue += getItemProperty(propertyId).toString();
  977. if (i.hasNext()) {
  978. retValue += " ";
  979. }
  980. }
  981. return retValue;
  982. }
  983. /**
  984. * Calculates a integer hash-code for the Item that's unique inside the
  985. * list. Two Items inside the same list have always different
  986. * hash-codes, though Items in different lists may have identical
  987. * hash-codes.
  988. *
  989. * @return A locally unique hash-code as integer
  990. */
  991. @Override
  992. public int hashCode() {
  993. return itemId.hashCode();
  994. }
  995. /**
  996. * Tests if the given object is the same as the this object. Two Items
  997. * got from a list container with the same ID are equal.
  998. *
  999. * @param obj
  1000. * an object to compare with this object
  1001. * @return <code>true</code> if the given object is the same as this
  1002. * object, <code>false</code> if not
  1003. */
  1004. @Override
  1005. public boolean equals(Object obj) {
  1006. if (obj == null
  1007. || !obj.getClass().equals(IndexedContainerItem.class)) {
  1008. return false;
  1009. }
  1010. final IndexedContainerItem li = (IndexedContainerItem) obj;
  1011. return getHost() == li.getHost() && itemId.equals(li.itemId);
  1012. }
  1013. private IndexedContainer getHost() {
  1014. return IndexedContainer.this;
  1015. }
  1016. /**
  1017. * IndexedContainerItem does not support adding new properties. Add
  1018. * properties at container level. See
  1019. * {@link IndexedContainer#addContainerProperty(Object, Class, Object)}
  1020. *
  1021. * @see com.vaadin.data.Item#addProperty(Object, Property)
  1022. */
  1023. public boolean addItemProperty(Object id, Property property)
  1024. throws UnsupportedOperationException {
  1025. throw new UnsupportedOperationException("Indexed container item "
  1026. + "does not support adding new properties");
  1027. }
  1028. /**
  1029. * Indexed container does not support removing properties. Remove
  1030. * properties at container level. See
  1031. * {@link IndexedContainer#removeContainerProperty(Object)}
  1032. *
  1033. * @see com.vaadin.data.Item#removeProperty(Object)
  1034. */
  1035. public boolean removeItemProperty(Object id)
  1036. throws UnsupportedOperationException {
  1037. throw new UnsupportedOperationException(
  1038. "Indexed container item does not support property removal");
  1039. }
  1040. }
  1041. /**
  1042. * A class implementing the {@link Property} interface to be contained in
  1043. * the {@link IndexedContainerItem} contained in the
  1044. * {@link IndexedContainer}.
  1045. *
  1046. * @author IT Mill Ltd.
  1047. *
  1048. * @version
  1049. * @VERSION@
  1050. * @since 3.0
  1051. */
  1052. private class IndexedContainerProperty implements Property,
  1053. Property.ValueChangeNotifier {
  1054. /**
  1055. * ID of the Item, where this property resides.
  1056. */
  1057. private final Object itemId;
  1058. /**
  1059. * Id of the Property.
  1060. */
  1061. private final Object propertyId;
  1062. /**
  1063. * Constructs a new {@link IndexedContainerProperty} object.
  1064. *
  1065. * @param itemId
  1066. * the ID of the Item to connect the new Property to.
  1067. * @param propertyId
  1068. * the Property ID of the new Property.
  1069. * @param host
  1070. * the list that contains the Item to contain the new
  1071. * Property.
  1072. */
  1073. private IndexedContainerProperty(Object itemId, Object propertyId) {
  1074. if (itemId == null || propertyId == null) {
  1075. // Null ids are not accepted
  1076. throw new NullPointerException(
  1077. "Container item or property ids can not be null");
  1078. }
  1079. this.propertyId = propertyId;
  1080. this.itemId = itemId;
  1081. }
  1082. /*
  1083. * (non-Javadoc)
  1084. *
  1085. * @see com.vaadin.data.Property#getType()
  1086. */
  1087. public Class getType() {
  1088. return (Class) types.get(propertyId);
  1089. }
  1090. /*
  1091. * (non-Javadoc)
  1092. *
  1093. * @see com.vaadin.data.Property#getValue()
  1094. */
  1095. public Object getValue() {
  1096. return ((Hashtable) items.get(itemId)).get(propertyId);
  1097. }
  1098. /*
  1099. * (non-Javadoc)
  1100. *
  1101. * @see com.vaadin.data.Property#isReadOnly()
  1102. */
  1103. public boolean isReadOnly() {
  1104. return readOnlyProperties.contains(this);
  1105. }
  1106. /*
  1107. * (non-Javadoc)
  1108. *
  1109. * @see com.vaadin.data.Property#setReadOnly(boolean)
  1110. */
  1111. public void setReadOnly(boolean newStatus) {
  1112. if (newStatus) {
  1113. readOnlyProperties.add(this);
  1114. } else {
  1115. readOnlyProperties.remove(this);
  1116. }
  1117. }
  1118. /*
  1119. * (non-Javadoc)
  1120. *
  1121. * @see com.vaadin.data.Property#setValue(java.lang.Object)
  1122. */
  1123. public void setValue(Object newValue)
  1124. throws Property.ReadOnlyException, Property.ConversionException {
  1125. // Gets the Property set
  1126. final Hashtable propertySet = (Hashtable) items.get(itemId);
  1127. // Support null values on all types
  1128. if (newValue == null) {
  1129. propertySet.remove(propertyId);
  1130. } else if (getType().isAssignableFrom(newValue.getClass())) {
  1131. propertySet.put(propertyId, newValue);
  1132. } else {
  1133. try {
  1134. // Gets the string constructor
  1135. final Constructor constr = getType().getConstructor(
  1136. new Class[] { String.class });
  1137. // Creates new object from the string
  1138. propertySet.put(propertyId, constr
  1139. .newInstance(new Object[] { newValue.toString() }));
  1140. } catch (final java.lang.Exception e) {
  1141. throw new Property.ConversionException(
  1142. "Conversion for value '" + newValue + "' of class "
  1143. + newValue.getClass().getName() + " to "
  1144. + getType().getName() + " failed");
  1145. }
  1146. }
  1147. // update the container filtering if this property is being filtered
  1148. updateContainerFiltering(propertyId);
  1149. firePropertyValueChange(this);
  1150. }
  1151. /**
  1152. * Returns the value of the Property in human readable textual format.
  1153. * The return value should be assignable to the <code>setValue</code>
  1154. * method if the Property is not in read-only mode.
  1155. *
  1156. * @return <code>String</code> representation of the value stored in the
  1157. * Property
  1158. */
  1159. @Override
  1160. public String toString() {
  1161. final Object value = getValue();
  1162. if (value == null) {
  1163. return null;
  1164. }
  1165. return value.toString();
  1166. }
  1167. /**
  1168. * Calculates a integer hash-code for the Property that's unique inside
  1169. * the Item containing the Property. Two different Properties inside the
  1170. * same Item contained in the same list always have different
  1171. * hash-codes, though Properties in different Items may have identical
  1172. * hash-codes.
  1173. *
  1174. * @return A locally unique hash-code as integer
  1175. */
  1176. @Override
  1177. public int hashCode() {
  1178. return itemId.hashCode() ^ propertyId.hashCode();
  1179. }
  1180. /**
  1181. * Tests if the given object is the same as the this object. Two
  1182. * Properties got from an Item with the same ID are equal.
  1183. *
  1184. * @param obj
  1185. * an object to compare with this object
  1186. * @return <code>true</code> if the given object is the same as this
  1187. * object, <code>false</code> if not
  1188. */
  1189. @Override
  1190. public boolean equals(Object obj) {
  1191. if (obj == null
  1192. || !obj.getClass().equals(IndexedContainerProperty.class)) {
  1193. return false;
  1194. }
  1195. final IndexedContainerProperty lp = (IndexedContainerProperty) obj;
  1196. return lp.getHost() == getHost()
  1197. && lp.propertyId.equals(propertyId)
  1198. && lp.itemId.equals(itemId);
  1199. }
  1200. /*
  1201. * (non-Javadoc)
  1202. *
  1203. * @see com.vaadin.data.Property.ValueChangeNotifier#addListener(
  1204. * com.vaadin.data.Property.ValueChangeListener)
  1205. */
  1206. public void addListener(Property.ValueChangeListener listener) {
  1207. addSinglePropertyChangeListener(propertyId, itemId, listener);
  1208. }
  1209. /*
  1210. * (non-Javadoc)
  1211. *
  1212. * @see com.vaadin.data.Property.ValueChangeNotifier#removeListener
  1213. * (com.vaadin.data.Property.ValueChangeListener)
  1214. */
  1215. public void removeListener(Property.ValueChangeListener listener) {
  1216. removeSinglePropertyChangeListener(propertyId, itemId, listener);
  1217. }
  1218. private IndexedContainer getHost() {
  1219. return IndexedContainer.this;
  1220. }
  1221. }
  1222. /*
  1223. * (non-Javadoc)
  1224. *
  1225. * @see com.vaadin.data.Container.Sortable#sort(java.lang.Object[],
  1226. * boolean[])
  1227. */
  1228. public void sort(Object[] propertyId, boolean[] ascending) {
  1229. // Set up the item sorter for the sort operation
  1230. itemSorter.setSortProperties(this, propertyId, ascending);
  1231. // Perform the actual sort
  1232. doSort();
  1233. // Post sort updates
  1234. if (filteredItemIds != null) {
  1235. updateContainerFiltering();
  1236. } else {
  1237. fireContentsChange(-1);
  1238. }
  1239. }
  1240. /**
  1241. * Perform the sorting of the data structures in the container. This is
  1242. * invoked when the <code>itemSorter</code> has been prepared for the sort
  1243. * operation. Typically this method calls
  1244. * <code>Collections.sort(aCollection, getItemSorter())</code> on all arrays
  1245. * (containing item ids) that need to be sorted.
  1246. *
  1247. */
  1248. protected void doSort() {
  1249. Collections.sort(itemIds, getItemSorter());
  1250. }
  1251. /*
  1252. * (non-Javadoc)
  1253. *
  1254. * @see com.vaadin.data.Container.Sortable#getSortableContainerPropertyIds
  1255. * ()
  1256. */
  1257. public Collection<?> getSortableContainerPropertyIds() {
  1258. final LinkedList list = new LinkedList();
  1259. for (final Iterator i = propertyIds.iterator(); i.hasNext();) {
  1260. final Object id = i.next();
  1261. final Class type = getType(id);
  1262. if (type != null && Comparable.class.isAssignableFrom(type)) {
  1263. list.add(id);
  1264. }
  1265. }
  1266. return list;
  1267. }
  1268. /**
  1269. * Returns the ItemSorter used for comparing items in a sort. See
  1270. * {@link #setItemSorter(ItemSorter)} for more information.
  1271. *
  1272. * @return The ItemSorter used for comparing two items in a sort.
  1273. */
  1274. public ItemSorter getItemSorter() {
  1275. return itemSorter;
  1276. }
  1277. /**
  1278. * Sets the ItemSorter used for comparing items in a sort. The ItemSorter is
  1279. * called for each collection that needs sorting. A default ItemSorter is
  1280. * used if this is not explicitly set.
  1281. *
  1282. * @param itemSorter
  1283. * The ItemSorter used for comparing two items in a sort.
  1284. */
  1285. public void setItemSorter(ItemSorter itemSorter) {
  1286. this.itemSorter = itemSorter;
  1287. }
  1288. /**
  1289. * Supports cloning of the IndexedContainer cleanly.
  1290. *
  1291. * @throws CloneNotSupportedException
  1292. * if an object cannot be cloned. .
  1293. */
  1294. @Override
  1295. public Object clone() throws CloneNotSupportedException {
  1296. // Creates the clone
  1297. final IndexedContainer nc = new IndexedContainer();
  1298. // Clone the shallow properties
  1299. nc.itemIds = itemIds != null ? (ArrayList) itemIds.clone() : null;
  1300. nc.itemSetChangeListeners = itemSetChangeListeners != null ? (LinkedList) itemSetChangeListeners
  1301. .clone() : null;
  1302. nc.propertyIds = propertyIds != null ? (ArrayList) propertyIds.clone()
  1303. : null;
  1304. nc.propertySetChangeListeners = propertySetChangeListeners != null ? (LinkedList) propertySetChangeListeners
  1305. .clone() : null;
  1306. nc.propertyValueChangeListeners = propertyValueChangeListeners != null ? (LinkedList) propertyValueChangeListeners
  1307. .clone() : null;
  1308. nc.readOnlyProperties = readOnlyProperties != null ? (HashSet) readOnlyProperties
  1309. .clone() : null;
  1310. nc.singlePropertyValueChangeListeners = singlePropertyValueChangeListeners != null ? (Hashtable) singlePropertyValueChangeListeners
  1311. .clone() : null;
  1312. nc.types = types != null ? (Hashtable) types.clone() : null;
  1313. nc.filters = filters == null ? null : (HashSet<Filter>) filters.clone();
  1314. nc.filteredItemIds = filteredItemIds == null ? null
  1315. : (LinkedHashSet) filteredItemIds.clone();
  1316. // Clone property-values
  1317. if (items == null) {
  1318. nc.items = null;
  1319. } else {
  1320. nc.items = new Hashtable();
  1321. for (final Iterator i = items.keySet().iterator(); i.hasNext();) {
  1322. final Object id = i.next();
  1323. final Hashtable it = (Hashtable) items.get(id);
  1324. nc.items.put(id, it.clone());
  1325. }
  1326. }
  1327. return nc;
  1328. }
  1329. public void addContainerFilter(Object propertyId, String filterString,
  1330. boolean ignoreCase, boolean onlyMatchPrefix) {
  1331. if (filters == null) {
  1332. filters = new HashSet<Filter>();
  1333. }
  1334. filters.add(new Filter(propertyId, filterString, ignoreCase,
  1335. onlyMatchPrefix));
  1336. updateContainerFiltering();
  1337. }
  1338. public void removeAllContainerFilters() {
  1339. if (filters == null) {
  1340. return;
  1341. }
  1342. filters.clear();
  1343. updateContainerFiltering();
  1344. }
  1345. public void removeContainerFilters(Object propertyId) {
  1346. if (filters == null || propertyId == null) {
  1347. return;
  1348. }
  1349. final Iterator<Filter> i = filters.iterator();
  1350. while (i.hasNext()) {
  1351. final Filter f = i.next();
  1352. if (propertyId.equals(f.propertyId)) {
  1353. i.remove();
  1354. }
  1355. }
  1356. updateContainerFiltering();
  1357. }
  1358. private void updateContainerFiltering(Object propertyId) {
  1359. if (filters == null || propertyId == null) {
  1360. return;
  1361. }
  1362. // update container filtering if there is a filter for the given
  1363. // property
  1364. final Iterator<Filter> i = filters.iterator();
  1365. while (i.hasNext()) {
  1366. final Filter f = i.next();
  1367. if (propertyId.equals(f.propertyId)) {
  1368. updateContainerFiltering();
  1369. return;
  1370. }
  1371. }
  1372. }
  1373. /**
  1374. * Called when the filters have changed or when another event that effects
  1375. * filtering has taken place. Updates internal data structures and fires an
  1376. * item set change if necessary.
  1377. */
  1378. private void updateContainerFiltering() {
  1379. // Clearing filters?
  1380. boolean hasFilters = (filters != null && !filters.isEmpty());
  1381. if (doFilterContainer(hasFilters)) {
  1382. fireContentsChange(-1);
  1383. }
  1384. }
  1385. /**
  1386. * Filters the data in the container and updates internal data structures.
  1387. * This method should reset any internal data structures and then repopulate
  1388. * them so {@link #getItemIds()} and other methods only return the filtered
  1389. * items.
  1390. *
  1391. * @param hasFilters
  1392. * true if filters has been set for the container, false
  1393. * otherwise
  1394. * @return true if the item set has changed as a result of the filtering
  1395. */
  1396. protected boolean doFilterContainer(boolean hasFilters) {
  1397. if (!hasFilters) {
  1398. filteredItemIds = null;
  1399. if (filters != null) {
  1400. filters = null;
  1401. return true;
  1402. }
  1403. return false;
  1404. }
  1405. // Reset filtered list
  1406. if (filteredItemIds == null) {
  1407. filteredItemIds = new LinkedHashSet();
  1408. } else {
  1409. filteredItemIds.clear();
  1410. }
  1411. // Filter
  1412. for (final Iterator i = itemIds.iterator(); i.hasNext();) {
  1413. final Object id = i.next();
  1414. if (passesFilters(id)) {
  1415. filteredItemIds.add(id);
  1416. }
  1417. }
  1418. return true;
  1419. }
  1420. /**
  1421. * Checks if the given itemId passes the filters set for the container. The
  1422. * caller should make sure the itemId exists in the container. For
  1423. * non-existing itemIds the behavior is undefined.
  1424. *
  1425. * @param itemId
  1426. * An itemId that exists in the container.
  1427. * @return true if the itemId passes all filters or no filters are set,
  1428. * false otherwise.
  1429. */
  1430. protected boolean passesFilters(Object itemId) {
  1431. IndexedContainerItem item = new IndexedContainerItem(itemId);
  1432. if (filters == null) {
  1433. return true;
  1434. }
  1435. if (item == null) {
  1436. return false;
  1437. }
  1438. final Iterator<Filter> i = filters.iterator();
  1439. while (i.hasNext()) {
  1440. final Filter f = i.next();
  1441. if (!f.passesFilter(item)) {
  1442. return false;
  1443. }
  1444. }
  1445. return true;
  1446. }
  1447. }