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.

HierarchicalContainer.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.data.util;
  17. import java.util.Collection;
  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.HashSet;
  21. import java.util.LinkedHashSet;
  22. import java.util.LinkedList;
  23. import java.util.Set;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.vaadin.v7.data.Container;
  27. import com.vaadin.v7.data.Item;
  28. /**
  29. * A specialized Container whose contents can be accessed like it was a
  30. * tree-like structure.
  31. *
  32. * @author Vaadin Ltd.
  33. * @since 3.0
  34. *
  35. * @deprecated Use an appropriate implementation of
  36. * {@code HierarchicalDataProvider} such as {@code TreeDataProvider}
  37. * or {@code AbstractBackEndHierarchicalDataProvider}.
  38. */
  39. @Deprecated
  40. @SuppressWarnings("serial")
  41. public class HierarchicalContainer extends IndexedContainer
  42. implements Container.Hierarchical {
  43. /**
  44. * Set of IDs of those contained Items that can't have children.
  45. */
  46. private final HashSet<Object> noChildrenAllowed = new HashSet<Object>();
  47. /**
  48. * Mapping from Item ID to parent Item ID.
  49. */
  50. private final HashMap<Object, Object> parent = new HashMap<Object, Object>();
  51. /**
  52. * Mapping from Item ID to parent Item ID for items included in the filtered
  53. * container.
  54. */
  55. private HashMap<Object, Object> filteredParent = null;
  56. /**
  57. * Mapping from Item ID to a list of child IDs.
  58. */
  59. private final HashMap<Object, LinkedList<Object>> children = new HashMap<Object, LinkedList<Object>>();
  60. /**
  61. * Mapping from Item ID to a list of child IDs when filtered
  62. */
  63. private HashMap<Object, LinkedList<Object>> filteredChildren = null;
  64. /**
  65. * List that contains all root elements of the container.
  66. */
  67. private final LinkedList<Object> roots = new LinkedList<Object>();
  68. /**
  69. * List that contains all filtered root elements of the container.
  70. */
  71. private LinkedList<Object> filteredRoots = null;
  72. /**
  73. * Determines how filtering of the container is done.
  74. */
  75. private boolean includeParentsWhenFiltering = true;
  76. /**
  77. * Counts how many nested contents change disable calls are in progress.
  78. *
  79. * Pending events are only fired when the counter reaches zero again.
  80. */
  81. private int contentChangedEventsDisabledCount = 0;
  82. private boolean contentsChangedEventPending;
  83. /*
  84. * Can the specified Item have any children? Don't add a JavaDoc comment
  85. * here, we use the default documentation from implemented interface.
  86. */
  87. @Override
  88. public boolean areChildrenAllowed(Object itemId) {
  89. if (noChildrenAllowed.contains(itemId)) {
  90. return false;
  91. }
  92. return containsId(itemId);
  93. }
  94. /*
  95. * Gets the IDs of the children of the specified Item. Don't add a JavaDoc
  96. * comment here, we use the default documentation from implemented
  97. * interface.
  98. */
  99. @Override
  100. public Collection<?> getChildren(Object itemId) {
  101. LinkedList<Object> c;
  102. if (filteredChildren != null) {
  103. c = filteredChildren.get(itemId);
  104. } else {
  105. c = children.get(itemId);
  106. }
  107. if (c == null) {
  108. return null;
  109. }
  110. return Collections.unmodifiableCollection(c);
  111. }
  112. /*
  113. * Gets the ID of the parent of the specified Item. Don't add a JavaDoc
  114. * comment here, we use the default documentation from implemented
  115. * interface.
  116. */
  117. @Override
  118. public Object getParent(Object itemId) {
  119. if (filteredParent != null) {
  120. return filteredParent.get(itemId);
  121. }
  122. return parent.get(itemId);
  123. }
  124. /*
  125. * Is the Item corresponding to the given ID a leaf node? Don't add a
  126. * JavaDoc comment here, we use the default documentation from implemented
  127. * interface.
  128. */
  129. @Override
  130. public boolean hasChildren(Object itemId) {
  131. if (filteredChildren != null) {
  132. return filteredChildren.containsKey(itemId);
  133. } else {
  134. return children.containsKey(itemId);
  135. }
  136. }
  137. /*
  138. * Is the Item corresponding to the given ID a root node? Don't add a
  139. * JavaDoc comment here, we use the default documentation from implemented
  140. * interface.
  141. */
  142. @Override
  143. public boolean isRoot(Object itemId) {
  144. // If the container is filtered the itemId must be among filteredRoots
  145. // to be a root.
  146. if (filteredRoots != null) {
  147. if (!filteredRoots.contains(itemId)) {
  148. return false;
  149. }
  150. } else {
  151. // Container is not filtered
  152. if (parent.containsKey(itemId)) {
  153. return false;
  154. }
  155. }
  156. return containsId(itemId);
  157. }
  158. /*
  159. * Gets the IDs of the root elements in the container. Don't add a JavaDoc
  160. * comment here, we use the default documentation from implemented
  161. * interface.
  162. */
  163. @Override
  164. public Collection<?> rootItemIds() {
  165. if (filteredRoots != null) {
  166. return Collections.unmodifiableCollection(filteredRoots);
  167. } else {
  168. return Collections.unmodifiableCollection(roots);
  169. }
  170. }
  171. /**
  172. * <p>
  173. * Sets the given Item's capability to have children. If the Item identified
  174. * with the itemId already has children and the areChildrenAllowed is false
  175. * this method fails and <code>false</code> is returned; the children must
  176. * be first explicitly removed with
  177. * {@link #setParent(Object itemId, Object newParentId)} or
  178. * {@link Container#removeItem(Object itemId)}.
  179. * </p>
  180. *
  181. * @param itemId
  182. * the ID of the Item in the container whose child capability is
  183. * to be set.
  184. * @param childrenAllowed
  185. * the boolean value specifying if the Item can have children or
  186. * not.
  187. * @return <code>true</code> if the operation succeeded, <code>false</code>
  188. * if not
  189. */
  190. @Override
  191. public boolean setChildrenAllowed(Object itemId, boolean childrenAllowed) {
  192. // Checks that the item is in the container
  193. if (!containsId(itemId)) {
  194. return false;
  195. }
  196. // Updates status
  197. if (childrenAllowed) {
  198. noChildrenAllowed.remove(itemId);
  199. } else {
  200. noChildrenAllowed.add(itemId);
  201. }
  202. return true;
  203. }
  204. /**
  205. * <p>
  206. * Sets the parent of an Item. The new parent item must exist and be able to
  207. * have children. (<code>canHaveChildren(newParentId) == true</code>). It is
  208. * also possible to detach a node from the hierarchy (and thus make it root)
  209. * by setting the parent <code>null</code>.
  210. * </p>
  211. *
  212. * @param itemId
  213. * the ID of the item to be set as the child of the Item
  214. * identified with newParentId.
  215. * @param newParentId
  216. * the ID of the Item that's to be the new parent of the Item
  217. * identified with itemId.
  218. * @return <code>true</code> if the operation succeeded, <code>false</code>
  219. * if not
  220. */
  221. @Override
  222. public boolean setParent(Object itemId, Object newParentId) {
  223. // Checks that the item is in the container
  224. if (!containsId(itemId)) {
  225. return false;
  226. }
  227. // Gets the old parent
  228. final Object oldParentId = parent.get(itemId);
  229. // Checks if no change is necessary
  230. if ((newParentId == null && oldParentId == null)
  231. || ((newParentId != null) && newParentId.equals(oldParentId))) {
  232. return true;
  233. }
  234. // Making root?
  235. if (newParentId == null) {
  236. // The itemId should become a root so we need to
  237. // - Remove it from the old parent's children list
  238. // - Add it as a root
  239. // - Remove it from the item -> parent list (parent is null for
  240. // roots)
  241. // Removes from old parents children list
  242. final LinkedList<Object> l = children.get(oldParentId);
  243. if (l != null) {
  244. l.remove(itemId);
  245. if (l.isEmpty()) {
  246. children.remove(oldParentId);
  247. }
  248. }
  249. // Add to be a root
  250. roots.add(itemId);
  251. // Updates parent
  252. parent.remove(itemId);
  253. if (hasFilters()) {
  254. // Refilter the container if setParent is called when filters
  255. // are applied. Changing parent can change what is included in
  256. // the filtered version (if includeParentsWhenFiltering==true).
  257. doFilterContainer(hasFilters());
  258. }
  259. fireItemSetChange();
  260. return true;
  261. }
  262. // We get here when the item should not become a root and we need to
  263. // - Verify the new parent exists and can have children
  264. // - Check that the new parent is not a child of the selected itemId
  265. // - Updated the item -> parent mapping to point to the new parent
  266. // - Remove the item from the roots list if it was a root
  267. // - Remove the item from the old parent's children list if it was not a
  268. // root
  269. // Checks that the new parent exists in container and can have
  270. // children
  271. if (!containsId(newParentId)
  272. || noChildrenAllowed.contains(newParentId)) {
  273. return false;
  274. }
  275. // Checks that setting parent doesn't result to a loop
  276. Object o = newParentId;
  277. while (o != null && !o.equals(itemId)) {
  278. o = parent.get(o);
  279. }
  280. if (o != null) {
  281. return false;
  282. }
  283. // Updates parent
  284. parent.put(itemId, newParentId);
  285. LinkedList<Object> pcl = children.get(newParentId);
  286. if (pcl == null) {
  287. // Create an empty list for holding children if one were not
  288. // previously created
  289. pcl = new LinkedList<Object>();
  290. children.put(newParentId, pcl);
  291. }
  292. pcl.add(itemId);
  293. // Removes from old parent or root
  294. if (oldParentId == null) {
  295. roots.remove(itemId);
  296. } else {
  297. final LinkedList<Object> l = children.get(oldParentId);
  298. if (l != null) {
  299. l.remove(itemId);
  300. if (l.isEmpty()) {
  301. children.remove(oldParentId);
  302. }
  303. }
  304. }
  305. if (hasFilters()) {
  306. // Refilter the container if setParent is called when filters
  307. // are applied. Changing parent can change what is included in
  308. // the filtered version (if includeParentsWhenFiltering==true).
  309. doFilterContainer(hasFilters());
  310. }
  311. fireItemSetChange();
  312. return true;
  313. }
  314. private boolean hasFilters() {
  315. return (filteredRoots != null);
  316. }
  317. /**
  318. * Moves a node (an Item) in the container immediately after a sibling node.
  319. * The two nodes must have the same parent in the container.
  320. *
  321. * @param itemId
  322. * the identifier of the moved node (Item)
  323. * @param siblingId
  324. * the identifier of the reference node (Item), after which the
  325. * other node will be located
  326. */
  327. public void moveAfterSibling(Object itemId, Object siblingId) {
  328. Object parent2 = getParent(itemId);
  329. LinkedList<Object> childrenList;
  330. if (parent2 == null) {
  331. childrenList = roots;
  332. } else {
  333. childrenList = children.get(parent2);
  334. }
  335. if (siblingId == null) {
  336. childrenList.remove(itemId);
  337. childrenList.addFirst(itemId);
  338. } else {
  339. int oldIndex = childrenList.indexOf(itemId);
  340. int indexOfSibling = childrenList.indexOf(siblingId);
  341. if (indexOfSibling != -1 && oldIndex != -1) {
  342. int newIndex;
  343. if (oldIndex > indexOfSibling) {
  344. newIndex = indexOfSibling + 1;
  345. } else {
  346. newIndex = indexOfSibling;
  347. }
  348. childrenList.remove(oldIndex);
  349. childrenList.add(newIndex, itemId);
  350. } else {
  351. throw new IllegalArgumentException(
  352. "Given identifiers no not have the same parent.");
  353. }
  354. }
  355. fireItemSetChange();
  356. }
  357. @Override
  358. public Object addItem() {
  359. disableContentsChangeEvents();
  360. try {
  361. final Object itemId = super.addItem();
  362. if (itemId == null) {
  363. return null;
  364. }
  365. if (!roots.contains(itemId)) {
  366. roots.add(itemId);
  367. if (filteredRoots != null) {
  368. if (passesFilters(itemId)) {
  369. filteredRoots.add(itemId);
  370. }
  371. }
  372. }
  373. return itemId;
  374. } finally {
  375. enableAndFireContentsChangeEvents();
  376. }
  377. }
  378. @Override
  379. protected void fireItemSetChange(Container.ItemSetChangeEvent event) {
  380. if (contentsChangeEventsOn()) {
  381. super.fireItemSetChange(event);
  382. } else {
  383. contentsChangedEventPending = true;
  384. }
  385. }
  386. private boolean contentsChangeEventsOn() {
  387. return contentChangedEventsDisabledCount == 0;
  388. }
  389. private void disableContentsChangeEvents() {
  390. contentChangedEventsDisabledCount++;
  391. }
  392. private void enableAndFireContentsChangeEvents() {
  393. if (contentChangedEventsDisabledCount <= 0) {
  394. getLogger().log(Level.WARNING,
  395. "Mismatched calls to disable and enable contents change events in HierarchicalContainer");
  396. contentChangedEventsDisabledCount = 0;
  397. } else {
  398. contentChangedEventsDisabledCount--;
  399. }
  400. if (contentChangedEventsDisabledCount == 0) {
  401. if (contentsChangedEventPending) {
  402. fireItemSetChange();
  403. }
  404. contentsChangedEventPending = false;
  405. }
  406. }
  407. @Override
  408. public Item addItem(Object itemId) {
  409. disableContentsChangeEvents();
  410. try {
  411. final Item item = super.addItem(itemId);
  412. if (item == null) {
  413. return null;
  414. }
  415. roots.add(itemId);
  416. if (filteredRoots != null) {
  417. if (passesFilters(itemId)) {
  418. filteredRoots.add(itemId);
  419. }
  420. }
  421. return item;
  422. } finally {
  423. enableAndFireContentsChangeEvents();
  424. }
  425. }
  426. @Override
  427. public boolean removeAllItems() {
  428. disableContentsChangeEvents();
  429. try {
  430. final boolean success = super.removeAllItems();
  431. if (success) {
  432. roots.clear();
  433. parent.clear();
  434. children.clear();
  435. noChildrenAllowed.clear();
  436. if (filteredRoots != null) {
  437. filteredRoots = null;
  438. }
  439. if (filteredChildren != null) {
  440. filteredChildren = null;
  441. }
  442. if (filteredParent != null) {
  443. filteredParent = null;
  444. }
  445. }
  446. return success;
  447. } finally {
  448. enableAndFireContentsChangeEvents();
  449. }
  450. }
  451. @Override
  452. public boolean removeItem(Object itemId) {
  453. disableContentsChangeEvents();
  454. try {
  455. final boolean success = super.removeItem(itemId);
  456. if (success) {
  457. // Remove from roots if this was a root
  458. if (roots.remove(itemId)) {
  459. // If filtering is enabled we might need to remove it from
  460. // the filtered list also
  461. if (filteredRoots != null) {
  462. filteredRoots.remove(itemId);
  463. }
  464. }
  465. // Clear the children list. Old children will now become root
  466. // nodes
  467. LinkedList<Object> childNodeIds = children.remove(itemId);
  468. if (childNodeIds != null) {
  469. if (filteredChildren != null) {
  470. filteredChildren.remove(itemId);
  471. }
  472. for (Object childId : childNodeIds) {
  473. setParent(childId, null);
  474. }
  475. }
  476. // Parent of the item that we are removing will contain the item
  477. // id in its children list
  478. final Object parentItemId = parent.get(itemId);
  479. if (parentItemId != null) {
  480. final LinkedList<Object> c = children.get(parentItemId);
  481. if (c != null) {
  482. c.remove(itemId);
  483. if (c.isEmpty()) {
  484. children.remove(parentItemId);
  485. }
  486. // Found in the children list so might also be in the
  487. // filteredChildren list
  488. if (filteredChildren != null) {
  489. LinkedList<Object> f = filteredChildren
  490. .get(parentItemId);
  491. if (f != null) {
  492. f.remove(itemId);
  493. if (f.isEmpty()) {
  494. filteredChildren.remove(parentItemId);
  495. }
  496. }
  497. }
  498. }
  499. }
  500. parent.remove(itemId);
  501. if (filteredParent != null) {
  502. // Item id no longer has a parent as the item id is not in
  503. // the container.
  504. filteredParent.remove(itemId);
  505. }
  506. noChildrenAllowed.remove(itemId);
  507. }
  508. return success;
  509. } finally {
  510. enableAndFireContentsChangeEvents();
  511. }
  512. }
  513. /**
  514. * Removes the Item identified by given itemId and all its children.
  515. *
  516. * @see #removeItem(Object)
  517. * @param itemId
  518. * the identifier of the Item to be removed
  519. * @return true if the operation succeeded
  520. */
  521. public boolean removeItemRecursively(Object itemId) {
  522. disableContentsChangeEvents();
  523. try {
  524. boolean removeItemRecursively = removeItemRecursively(this, itemId);
  525. return removeItemRecursively;
  526. } finally {
  527. enableAndFireContentsChangeEvents();
  528. }
  529. }
  530. /**
  531. * Removes the Item identified by given itemId and all its children from the
  532. * given Container.
  533. *
  534. * @param container
  535. * the container where the item is to be removed
  536. * @param itemId
  537. * the identifier of the Item to be removed
  538. * @return true if the operation succeeded
  539. */
  540. public static boolean removeItemRecursively(
  541. Container.Hierarchical container, Object itemId) {
  542. boolean success = true;
  543. Collection<?> children2 = container.getChildren(itemId);
  544. if (children2 != null) {
  545. Object[] array = children2.toArray();
  546. for (int i = 0; i < array.length; i++) {
  547. boolean removeItemRecursively = removeItemRecursively(container,
  548. array[i]);
  549. if (!removeItemRecursively) {
  550. success = false;
  551. }
  552. }
  553. }
  554. // remove the root of subtree if children where succesfully removed
  555. if (success) {
  556. success = container.removeItem(itemId);
  557. }
  558. return success;
  559. }
  560. @Override
  561. protected void doSort() {
  562. super.doSort();
  563. Collections.sort(roots, getItemSorter());
  564. for (LinkedList<Object> childList : children.values()) {
  565. Collections.sort(childList, getItemSorter());
  566. }
  567. }
  568. /**
  569. * Used to control how filtering works. @see
  570. * {@link #setIncludeParentsWhenFiltering(boolean)} for more information.
  571. *
  572. * @return true if all parents for items that match the filter are included
  573. * when filtering, false if only the matching items are included
  574. */
  575. public boolean isIncludeParentsWhenFiltering() {
  576. return includeParentsWhenFiltering;
  577. }
  578. /**
  579. * Controls how the filtering of the container works. Set this to true to
  580. * make filtering include parents for all matched items in addition to the
  581. * items themselves. Setting this to false causes the filtering to only
  582. * include the matching items and make items with excluded parents into root
  583. * items.
  584. *
  585. * @param includeParentsWhenFiltering
  586. * true to include all parents for items that match the filter,
  587. * false to only include the matching items
  588. */
  589. public void setIncludeParentsWhenFiltering(
  590. boolean includeParentsWhenFiltering) {
  591. this.includeParentsWhenFiltering = includeParentsWhenFiltering;
  592. if (filteredRoots != null) {
  593. // Currently filtered so needs to be re-filtered
  594. doFilterContainer(true);
  595. }
  596. }
  597. @Override
  598. protected boolean doFilterContainer(boolean hasFilters) {
  599. if (!hasFilters) {
  600. // All filters removed
  601. filteredRoots = null;
  602. filteredChildren = null;
  603. filteredParent = null;
  604. return super.doFilterContainer(hasFilters);
  605. }
  606. // Reset data structures
  607. filteredRoots = new LinkedList<Object>();
  608. filteredChildren = new HashMap<Object, LinkedList<Object>>();
  609. filteredParent = new HashMap<Object, Object>();
  610. if (includeParentsWhenFiltering) {
  611. // Filter so that parents for items that match the filter are also
  612. // included
  613. HashSet<Object> includedItems = new HashSet<Object>();
  614. for (Object rootId : roots) {
  615. if (filterIncludingParents(rootId, includedItems)) {
  616. filteredRoots.add(rootId);
  617. addFilteredChildrenRecursively(rootId, includedItems);
  618. }
  619. }
  620. // includedItemIds now contains all the item ids that should be
  621. // included. Filter IndexedContainer based on this
  622. filterOverride = includedItems;
  623. super.doFilterContainer(hasFilters);
  624. filterOverride = null;
  625. return true;
  626. } else {
  627. // Filter by including all items that pass the filter and make items
  628. // with no parent new root items
  629. // Filter IndexedContainer first so getItemIds return the items that
  630. // match
  631. super.doFilterContainer(hasFilters);
  632. LinkedHashSet<Object> filteredItemIds = new LinkedHashSet<Object>(
  633. getItemIds());
  634. for (Object itemId : filteredItemIds) {
  635. Object itemParent = parent.get(itemId);
  636. if (itemParent == null
  637. || !filteredItemIds.contains(itemParent)) {
  638. // Parent is not included or this was a root, in both cases
  639. // this should be a filtered root
  640. filteredRoots.add(itemId);
  641. } else {
  642. // Parent is included. Add this to the children list (create
  643. // it first if necessary)
  644. addFilteredChild(itemParent, itemId);
  645. }
  646. }
  647. return true;
  648. }
  649. }
  650. /**
  651. * Adds the given childItemId as a filteredChildren for the parentItemId and
  652. * sets it filteredParent.
  653. *
  654. * @param parentItemId
  655. * @param childItemId
  656. */
  657. private void addFilteredChild(Object parentItemId, Object childItemId) {
  658. LinkedList<Object> parentToChildrenList = filteredChildren
  659. .get(parentItemId);
  660. if (parentToChildrenList == null) {
  661. parentToChildrenList = new LinkedList<Object>();
  662. filteredChildren.put(parentItemId, parentToChildrenList);
  663. }
  664. filteredParent.put(childItemId, parentItemId);
  665. parentToChildrenList.add(childItemId);
  666. }
  667. /**
  668. * Recursively adds all items in the includedItems list to the
  669. * filteredChildren map in the same order as they are in the children map.
  670. * Starts from parentItemId and recurses down as long as child items that
  671. * should be included are found.
  672. *
  673. * @param parentItemId
  674. * The item id to start recurse from. Not added to a
  675. * filteredChildren list
  676. * @param includedItems
  677. * Set containing the item ids for the items that should be
  678. * included in the filteredChildren map
  679. */
  680. private void addFilteredChildrenRecursively(Object parentItemId,
  681. HashSet<Object> includedItems) {
  682. LinkedList<Object> childList = children.get(parentItemId);
  683. if (childList == null) {
  684. return;
  685. }
  686. for (Object childItemId : childList) {
  687. if (includedItems.contains(childItemId)) {
  688. addFilteredChild(parentItemId, childItemId);
  689. addFilteredChildrenRecursively(childItemId, includedItems);
  690. }
  691. }
  692. }
  693. /**
  694. * Scans the itemId and all its children for which items should be included
  695. * when filtering. All items which passes the filters are included.
  696. * Additionally all items that have a child node that should be included are
  697. * also themselves included.
  698. *
  699. * @param itemId
  700. * @param includedItems
  701. * @return true if the itemId should be included in the filtered container.
  702. */
  703. private boolean filterIncludingParents(Object itemId,
  704. HashSet<Object> includedItems) {
  705. boolean toBeIncluded = passesFilters(itemId);
  706. LinkedList<Object> childList = children.get(itemId);
  707. if (childList != null) {
  708. for (Object childItemId : children.get(itemId)) {
  709. toBeIncluded |= filterIncludingParents(childItemId,
  710. includedItems);
  711. }
  712. }
  713. if (toBeIncluded) {
  714. includedItems.add(itemId);
  715. }
  716. return toBeIncluded;
  717. }
  718. private Set<Object> filterOverride = null;
  719. @Override
  720. protected boolean passesFilters(Object itemId) {
  721. if (filterOverride != null) {
  722. return filterOverride.contains(itemId);
  723. } else {
  724. return super.passesFilters(itemId);
  725. }
  726. }
  727. private static final Logger getLogger() {
  728. return Logger.getLogger(HierarchicalContainer.class.getName());
  729. }
  730. }