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

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