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

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