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.

TreeTable.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * Copyright 2011 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.ui;
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.HashSet;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.logging.Logger;
  25. import com.vaadin.data.Collapsible;
  26. import com.vaadin.data.Container;
  27. import com.vaadin.data.Container.Hierarchical;
  28. import com.vaadin.data.Container.ItemSetChangeEvent;
  29. import com.vaadin.data.util.ContainerHierarchicalWrapper;
  30. import com.vaadin.data.util.HierarchicalContainer;
  31. import com.vaadin.data.util.HierarchicalContainerOrderedWrapper;
  32. import com.vaadin.server.PaintException;
  33. import com.vaadin.server.PaintTarget;
  34. import com.vaadin.server.Resource;
  35. import com.vaadin.shared.ui.treetable.TreeTableConstants;
  36. import com.vaadin.ui.Tree.CollapseEvent;
  37. import com.vaadin.ui.Tree.CollapseListener;
  38. import com.vaadin.ui.Tree.ExpandEvent;
  39. import com.vaadin.ui.Tree.ExpandListener;
  40. /**
  41. * TreeTable extends the {@link Table} component so that it can also visualize a
  42. * hierarchy of its Items in a similar manner that {@link Tree} does. The tree
  43. * hierarchy is always displayed in the first actual column of the TreeTable.
  44. * <p>
  45. * The TreeTable supports the usual {@link Table} features like lazy loading, so
  46. * it should be no problem to display lots of items at once. Only required rows
  47. * and some cache rows are sent to the client.
  48. * <p>
  49. * TreeTable supports standard {@link Hierarchical} container interfaces, but
  50. * also a more fine tuned version - {@link Collapsible}. A container
  51. * implementing the {@link Collapsible} interface stores the collapsed/expanded
  52. * state internally and can this way scale better on the server side than with
  53. * standard Hierarchical implementations. Developer must however note that
  54. * {@link Collapsible} containers can not be shared among several users as they
  55. * share UI state in the container.
  56. */
  57. @SuppressWarnings({ "serial" })
  58. public class TreeTable extends Table implements Hierarchical {
  59. private interface ContainerStrategy extends Serializable {
  60. public int size();
  61. public boolean isNodeOpen(Object itemId);
  62. public int getDepth(Object itemId);
  63. public void toggleChildVisibility(Object itemId);
  64. public Object getIdByIndex(int index);
  65. public int indexOfId(Object id);
  66. public Object nextItemId(Object itemId);
  67. public Object lastItemId();
  68. public Object prevItemId(Object itemId);
  69. public boolean isLastId(Object itemId);
  70. public Collection<?> getItemIds();
  71. public void containerItemSetChange(ItemSetChangeEvent event);
  72. }
  73. private abstract class AbstractStrategy implements ContainerStrategy {
  74. /**
  75. * Consider adding getDepth to {@link Collapsible}, might help
  76. * scalability with some container implementations.
  77. */
  78. @Override
  79. public int getDepth(Object itemId) {
  80. int depth = 0;
  81. Hierarchical hierarchicalContainer = getContainerDataSource();
  82. while (!hierarchicalContainer.isRoot(itemId)) {
  83. depth++;
  84. itemId = hierarchicalContainer.getParent(itemId);
  85. }
  86. return depth;
  87. }
  88. @Override
  89. public void containerItemSetChange(ItemSetChangeEvent event) {
  90. }
  91. }
  92. /**
  93. * This strategy is used if current container implements {@link Collapsible}
  94. * .
  95. *
  96. * open-collapsed logic diverted to container, otherwise use default
  97. * implementations.
  98. */
  99. private class CollapsibleStrategy extends AbstractStrategy {
  100. private Collapsible c() {
  101. return (Collapsible) getContainerDataSource();
  102. }
  103. @Override
  104. public void toggleChildVisibility(Object itemId) {
  105. c().setCollapsed(itemId, !c().isCollapsed(itemId));
  106. }
  107. @Override
  108. public boolean isNodeOpen(Object itemId) {
  109. return !c().isCollapsed(itemId);
  110. }
  111. @Override
  112. public int size() {
  113. return TreeTable.super.size();
  114. }
  115. @Override
  116. public Object getIdByIndex(int index) {
  117. return TreeTable.super.getIdByIndex(index);
  118. }
  119. @Override
  120. public int indexOfId(Object id) {
  121. return TreeTable.super.indexOfId(id);
  122. }
  123. @Override
  124. public boolean isLastId(Object itemId) {
  125. // using the default impl
  126. return TreeTable.super.isLastId(itemId);
  127. }
  128. @Override
  129. public Object lastItemId() {
  130. // using the default impl
  131. return TreeTable.super.lastItemId();
  132. }
  133. @Override
  134. public Object nextItemId(Object itemId) {
  135. return TreeTable.super.nextItemId(itemId);
  136. }
  137. @Override
  138. public Object prevItemId(Object itemId) {
  139. return TreeTable.super.prevItemId(itemId);
  140. }
  141. @Override
  142. public Collection<?> getItemIds() {
  143. return TreeTable.super.getItemIds();
  144. }
  145. }
  146. /**
  147. * Strategy for Hierarchical but not Collapsible container like
  148. * {@link HierarchicalContainer}.
  149. *
  150. * Store collapsed/open states internally, fool Table to use preorder when
  151. * accessing items from container via Ordered/Indexed methods.
  152. */
  153. private class HierarchicalStrategy extends AbstractStrategy {
  154. private final HashSet<Object> openItems = new HashSet<Object>();
  155. @Override
  156. public boolean isNodeOpen(Object itemId) {
  157. return openItems.contains(itemId);
  158. }
  159. @Override
  160. public int size() {
  161. return getPreOrder().size();
  162. }
  163. @Override
  164. public Collection<Object> getItemIds() {
  165. return Collections.unmodifiableCollection(getPreOrder());
  166. }
  167. @Override
  168. public boolean isLastId(Object itemId) {
  169. if (itemId == null) {
  170. return false;
  171. }
  172. return itemId.equals(lastItemId());
  173. }
  174. @Override
  175. public Object lastItemId() {
  176. if (getPreOrder().size() > 0) {
  177. return getPreOrder().get(getPreOrder().size() - 1);
  178. } else {
  179. return null;
  180. }
  181. }
  182. @Override
  183. public Object nextItemId(Object itemId) {
  184. int indexOf = getPreOrder().indexOf(itemId);
  185. if (indexOf == -1) {
  186. return null;
  187. }
  188. indexOf++;
  189. if (indexOf == getPreOrder().size()) {
  190. return null;
  191. } else {
  192. return getPreOrder().get(indexOf);
  193. }
  194. }
  195. @Override
  196. public Object prevItemId(Object itemId) {
  197. int indexOf = getPreOrder().indexOf(itemId);
  198. indexOf--;
  199. if (indexOf < 0) {
  200. return null;
  201. } else {
  202. return getPreOrder().get(indexOf);
  203. }
  204. }
  205. @Override
  206. public void toggleChildVisibility(Object itemId) {
  207. boolean removed = openItems.remove(itemId);
  208. if (!removed) {
  209. openItems.add(itemId);
  210. getLogger().finest("Item " + itemId + " is now expanded");
  211. } else {
  212. getLogger().finest("Item " + itemId + " is now collapsed");
  213. }
  214. clearPreorderCache();
  215. }
  216. private void clearPreorderCache() {
  217. preOrder = null; // clear preorder cache
  218. }
  219. List<Object> preOrder;
  220. /**
  221. * Preorder of ids currently visible
  222. *
  223. * @return
  224. */
  225. private List<Object> getPreOrder() {
  226. if (preOrder == null) {
  227. preOrder = new ArrayList<Object>();
  228. Collection<?> rootItemIds = getContainerDataSource()
  229. .rootItemIds();
  230. for (Object id : rootItemIds) {
  231. preOrder.add(id);
  232. addVisibleChildTree(id);
  233. }
  234. }
  235. return preOrder;
  236. }
  237. private void addVisibleChildTree(Object id) {
  238. if (isNodeOpen(id)) {
  239. Collection<?> children = getContainerDataSource().getChildren(
  240. id);
  241. if (children != null) {
  242. for (Object childId : children) {
  243. preOrder.add(childId);
  244. addVisibleChildTree(childId);
  245. }
  246. }
  247. }
  248. }
  249. @Override
  250. public int indexOfId(Object id) {
  251. return getPreOrder().indexOf(id);
  252. }
  253. @Override
  254. public Object getIdByIndex(int index) {
  255. return getPreOrder().get(index);
  256. }
  257. @Override
  258. public void containerItemSetChange(ItemSetChangeEvent event) {
  259. // preorder becomes invalid on sort, item additions etc.
  260. clearPreorderCache();
  261. super.containerItemSetChange(event);
  262. }
  263. }
  264. /**
  265. * Creates an empty TreeTable with a default container.
  266. */
  267. public TreeTable() {
  268. super(null, new HierarchicalContainer());
  269. }
  270. /**
  271. * Creates an empty TreeTable with a default container.
  272. *
  273. * @param caption
  274. * the caption for the TreeTable
  275. */
  276. public TreeTable(String caption) {
  277. this();
  278. setCaption(caption);
  279. }
  280. /**
  281. * Creates a TreeTable instance with given captions and data source.
  282. *
  283. * @param caption
  284. * the caption for the component
  285. * @param dataSource
  286. * the dataSource that is used to list items in the component
  287. */
  288. public TreeTable(String caption, Container dataSource) {
  289. super(caption, dataSource);
  290. }
  291. private ContainerStrategy cStrategy;
  292. private Object focusedRowId = null;
  293. private Object hierarchyColumnId;
  294. /**
  295. * The item id that was expanded or collapsed during this request. Reset at
  296. * the end of paint and only used for determining if a partial or full paint
  297. * should be done.
  298. *
  299. * Can safely be reset to null whenever a change occurs that would prevent a
  300. * partial update from rendering the correct result, e.g. rows added or
  301. * removed during an expand operation.
  302. */
  303. private Object toggledItemId;
  304. private boolean animationsEnabled;
  305. private boolean clearFocusedRowPending;
  306. /**
  307. * If the container does not send item set change events, always do a full
  308. * repaint instead of a partial update when expanding/collapsing nodes.
  309. */
  310. private boolean containerSupportsPartialUpdates;
  311. private ContainerStrategy getContainerStrategy() {
  312. if (cStrategy == null) {
  313. if (getContainerDataSource() instanceof Collapsible) {
  314. cStrategy = new CollapsibleStrategy();
  315. } else {
  316. cStrategy = new HierarchicalStrategy();
  317. }
  318. }
  319. return cStrategy;
  320. }
  321. @Override
  322. protected void paintRowAttributes(PaintTarget target, Object itemId)
  323. throws PaintException {
  324. super.paintRowAttributes(target, itemId);
  325. target.addAttribute("depth", getContainerStrategy().getDepth(itemId));
  326. if (getContainerDataSource().areChildrenAllowed(itemId)) {
  327. target.addAttribute("ca", true);
  328. target.addAttribute("open",
  329. getContainerStrategy().isNodeOpen(itemId));
  330. }
  331. }
  332. @Override
  333. protected void paintRowIcon(PaintTarget target, Object[][] cells,
  334. int indexInRowbuffer) throws PaintException {
  335. // always paint if present (in parent only if row headers visible)
  336. if (getRowHeaderMode() == ROW_HEADER_MODE_HIDDEN) {
  337. Resource itemIcon = getItemIcon(cells[CELL_ITEMID][indexInRowbuffer]);
  338. if (itemIcon != null) {
  339. target.addAttribute("icon", itemIcon);
  340. }
  341. } else if (cells[CELL_ICON][indexInRowbuffer] != null) {
  342. target.addAttribute("icon",
  343. (Resource) cells[CELL_ICON][indexInRowbuffer]);
  344. }
  345. }
  346. @Override
  347. public void changeVariables(Object source, Map<String, Object> variables) {
  348. super.changeVariables(source, variables);
  349. if (variables.containsKey("toggleCollapsed")) {
  350. String object = (String) variables.get("toggleCollapsed");
  351. Object itemId = itemIdMapper.get(object);
  352. toggledItemId = itemId;
  353. toggleChildVisibility(itemId, false);
  354. if (variables.containsKey("selectCollapsed")) {
  355. // ensure collapsed is selected unless opened with selection
  356. // head
  357. if (isSelectable()) {
  358. select(itemId);
  359. }
  360. }
  361. } else if (variables.containsKey("focusParent")) {
  362. String key = (String) variables.get("focusParent");
  363. Object refId = itemIdMapper.get(key);
  364. Object itemId = getParent(refId);
  365. focusParent(itemId);
  366. }
  367. }
  368. private void focusParent(Object itemId) {
  369. boolean inView = false;
  370. Object inPageId = getCurrentPageFirstItemId();
  371. for (int i = 0; inPageId != null && i < getPageLength(); i++) {
  372. if (inPageId.equals(itemId)) {
  373. inView = true;
  374. break;
  375. }
  376. inPageId = nextItemId(inPageId);
  377. i++;
  378. }
  379. if (!inView) {
  380. setCurrentPageFirstItemId(itemId);
  381. }
  382. // Select the row if it is selectable.
  383. if (isSelectable()) {
  384. if (isMultiSelect()) {
  385. setValue(Collections.singleton(itemId));
  386. } else {
  387. setValue(itemId);
  388. }
  389. }
  390. setFocusedRow(itemId);
  391. }
  392. private void setFocusedRow(Object itemId) {
  393. focusedRowId = itemId;
  394. if (focusedRowId == null) {
  395. // Must still inform the client that the focusParent request has
  396. // been processed
  397. clearFocusedRowPending = true;
  398. }
  399. markAsDirty();
  400. }
  401. @Override
  402. public void paintContent(PaintTarget target) throws PaintException {
  403. if (focusedRowId != null) {
  404. target.addAttribute("focusedRow", itemIdMapper.key(focusedRowId));
  405. focusedRowId = null;
  406. } else if (clearFocusedRowPending) {
  407. // Must still inform the client that the focusParent request has
  408. // been processed
  409. target.addAttribute("clearFocusPending", true);
  410. clearFocusedRowPending = false;
  411. }
  412. target.addAttribute("animate", animationsEnabled);
  413. if (hierarchyColumnId != null) {
  414. Object[] visibleColumns2 = getVisibleColumns();
  415. for (int i = 0; i < visibleColumns2.length; i++) {
  416. Object object = visibleColumns2[i];
  417. if (hierarchyColumnId.equals(object)) {
  418. target.addAttribute(
  419. TreeTableConstants.ATTRIBUTE_HIERARCHY_COLUMN_INDEX,
  420. i);
  421. break;
  422. }
  423. }
  424. }
  425. super.paintContent(target);
  426. toggledItemId = null;
  427. }
  428. /*
  429. * Override methods for partial row updates and additions when expanding /
  430. * collapsing nodes.
  431. */
  432. @Override
  433. protected boolean isPartialRowUpdate() {
  434. return toggledItemId != null && containerSupportsPartialUpdates
  435. && !isRowCacheInvalidated();
  436. }
  437. @Override
  438. protected int getFirstAddedItemIndex() {
  439. return indexOfId(toggledItemId) + 1;
  440. }
  441. @Override
  442. protected int getAddedRowCount() {
  443. return countSubNodesRecursively(getContainerDataSource(), toggledItemId);
  444. }
  445. private int countSubNodesRecursively(Hierarchical hc, Object itemId) {
  446. int count = 0;
  447. // we need the number of children for toggledItemId no matter if its
  448. // collapsed or expanded. Other items' children are only counted if the
  449. // item is expanded.
  450. if (getContainerStrategy().isNodeOpen(itemId)
  451. || itemId == toggledItemId) {
  452. Collection<?> children = hc.getChildren(itemId);
  453. if (children != null) {
  454. count += children != null ? children.size() : 0;
  455. for (Object id : children) {
  456. count += countSubNodesRecursively(hc, id);
  457. }
  458. }
  459. }
  460. return count;
  461. }
  462. @Override
  463. protected int getFirstUpdatedItemIndex() {
  464. return indexOfId(toggledItemId);
  465. }
  466. @Override
  467. protected int getUpdatedRowCount() {
  468. return 1;
  469. }
  470. @Override
  471. protected boolean shouldHideAddedRows() {
  472. return !getContainerStrategy().isNodeOpen(toggledItemId);
  473. }
  474. private void toggleChildVisibility(Object itemId, boolean forceFullRefresh) {
  475. getContainerStrategy().toggleChildVisibility(itemId);
  476. // ensure that page still has first item in page, DON'T clear the
  477. // caches.
  478. setCurrentPageFirstItemIndex(getCurrentPageFirstItemIndex(), false);
  479. if (isCollapsed(itemId)) {
  480. fireCollapseEvent(itemId);
  481. } else {
  482. fireExpandEvent(itemId);
  483. }
  484. if (containerSupportsPartialUpdates && !forceFullRefresh) {
  485. markAsDirty();
  486. } else {
  487. // For containers that do not send item set change events, always do
  488. // full repaint instead of partial row update.
  489. refreshRowCache();
  490. }
  491. }
  492. @Override
  493. public int size() {
  494. return getContainerStrategy().size();
  495. }
  496. @Override
  497. public Hierarchical getContainerDataSource() {
  498. return (Hierarchical) super.getContainerDataSource();
  499. }
  500. @Override
  501. public void setContainerDataSource(Container newDataSource) {
  502. cStrategy = null;
  503. // FIXME: This disables partial updates until TreeTable is fixed so it
  504. // does not change component hierarchy during paint
  505. containerSupportsPartialUpdates = (newDataSource instanceof ItemSetChangeNotifier) && false;
  506. if (!(newDataSource instanceof Hierarchical)) {
  507. newDataSource = new ContainerHierarchicalWrapper(newDataSource);
  508. }
  509. if (!(newDataSource instanceof Ordered)) {
  510. newDataSource = new HierarchicalContainerOrderedWrapper(
  511. (Hierarchical) newDataSource);
  512. }
  513. super.setContainerDataSource(newDataSource);
  514. }
  515. @Override
  516. public void containerItemSetChange(
  517. com.vaadin.data.Container.ItemSetChangeEvent event) {
  518. // Can't do partial repaints if items are added or removed during the
  519. // expand/collapse request
  520. toggledItemId = null;
  521. getContainerStrategy().containerItemSetChange(event);
  522. super.containerItemSetChange(event);
  523. }
  524. @Override
  525. protected Object getIdByIndex(int index) {
  526. return getContainerStrategy().getIdByIndex(index);
  527. }
  528. @Override
  529. protected int indexOfId(Object itemId) {
  530. return getContainerStrategy().indexOfId(itemId);
  531. }
  532. @Override
  533. public Object nextItemId(Object itemId) {
  534. return getContainerStrategy().nextItemId(itemId);
  535. }
  536. @Override
  537. public Object lastItemId() {
  538. return getContainerStrategy().lastItemId();
  539. }
  540. @Override
  541. public Object prevItemId(Object itemId) {
  542. return getContainerStrategy().prevItemId(itemId);
  543. }
  544. @Override
  545. public boolean isLastId(Object itemId) {
  546. return getContainerStrategy().isLastId(itemId);
  547. }
  548. @Override
  549. public Collection<?> getItemIds() {
  550. return getContainerStrategy().getItemIds();
  551. }
  552. @Override
  553. public boolean areChildrenAllowed(Object itemId) {
  554. return getContainerDataSource().areChildrenAllowed(itemId);
  555. }
  556. @Override
  557. public Collection<?> getChildren(Object itemId) {
  558. return getContainerDataSource().getChildren(itemId);
  559. }
  560. @Override
  561. public Object getParent(Object itemId) {
  562. return getContainerDataSource().getParent(itemId);
  563. }
  564. @Override
  565. public boolean hasChildren(Object itemId) {
  566. return getContainerDataSource().hasChildren(itemId);
  567. }
  568. @Override
  569. public boolean isRoot(Object itemId) {
  570. return getContainerDataSource().isRoot(itemId);
  571. }
  572. @Override
  573. public Collection<?> rootItemIds() {
  574. return getContainerDataSource().rootItemIds();
  575. }
  576. @Override
  577. public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed)
  578. throws UnsupportedOperationException {
  579. return getContainerDataSource().setChildrenAllowed(itemId,
  580. areChildrenAllowed);
  581. }
  582. @Override
  583. public boolean setParent(Object itemId, Object newParentId)
  584. throws UnsupportedOperationException {
  585. return getContainerDataSource().setParent(itemId, newParentId);
  586. }
  587. /**
  588. * Sets the Item specified by given identifier as collapsed or expanded. If
  589. * the Item is collapsed, its children are not displayed to the user.
  590. *
  591. * @param itemId
  592. * the identifier of the Item
  593. * @param collapsed
  594. * true if the Item should be collapsed, false if expanded
  595. */
  596. public void setCollapsed(Object itemId, boolean collapsed) {
  597. if (isCollapsed(itemId) != collapsed) {
  598. if (null == toggledItemId && !isRowCacheInvalidated()
  599. && getVisibleItemIds().contains(itemId)) {
  600. // optimization: partial refresh if only one item is
  601. // collapsed/expanded
  602. toggledItemId = itemId;
  603. toggleChildVisibility(itemId, false);
  604. } else {
  605. // make sure a full refresh takes place - otherwise neither
  606. // partial nor full repaint of table content is performed
  607. toggledItemId = null;
  608. toggleChildVisibility(itemId, true);
  609. }
  610. }
  611. }
  612. /**
  613. * Checks if Item with given identifier is collapsed in the UI.
  614. *
  615. * <p>
  616. *
  617. * @param itemId
  618. * the identifier of the checked Item
  619. * @return true if the Item with given id is collapsed
  620. * @see Collapsible#isCollapsed(Object)
  621. */
  622. public boolean isCollapsed(Object itemId) {
  623. return !getContainerStrategy().isNodeOpen(itemId);
  624. }
  625. /**
  626. * Explicitly sets the column in which the TreeTable visualizes the
  627. * hierarchy. If hierarchyColumnId is not set, the hierarchy is visualized
  628. * in the first visible column.
  629. *
  630. * @param hierarchyColumnId
  631. */
  632. public void setHierarchyColumn(Object hierarchyColumnId) {
  633. this.hierarchyColumnId = hierarchyColumnId;
  634. }
  635. /**
  636. * @return the identifier of column into which the hierarchy will be
  637. * visualized or null if the column is not explicitly defined.
  638. */
  639. public Object getHierarchyColumnId() {
  640. return hierarchyColumnId;
  641. }
  642. /**
  643. * Adds an expand listener.
  644. *
  645. * @param listener
  646. * the Listener to be added.
  647. */
  648. public void addExpandListener(ExpandListener listener) {
  649. addListener(ExpandEvent.class, listener, ExpandListener.EXPAND_METHOD);
  650. }
  651. /**
  652. * @deprecated As of 7.0, replaced by
  653. * {@link #addExpandListener(ExpandListener)}
  654. **/
  655. @Deprecated
  656. public void addListener(ExpandListener listener) {
  657. addExpandListener(listener);
  658. }
  659. /**
  660. * Removes an expand listener.
  661. *
  662. * @param listener
  663. * the Listener to be removed.
  664. */
  665. public void removeExpandListener(ExpandListener listener) {
  666. removeListener(ExpandEvent.class, listener,
  667. ExpandListener.EXPAND_METHOD);
  668. }
  669. /**
  670. * @deprecated As of 7.0, replaced by
  671. * {@link #removeExpandListener(ExpandListener)}
  672. **/
  673. @Deprecated
  674. public void removeListener(ExpandListener listener) {
  675. removeExpandListener(listener);
  676. }
  677. /**
  678. * Emits an expand event.
  679. *
  680. * @param itemId
  681. * the item id.
  682. */
  683. protected void fireExpandEvent(Object itemId) {
  684. fireEvent(new ExpandEvent(this, itemId));
  685. }
  686. /**
  687. * Adds a collapse listener.
  688. *
  689. * @param listener
  690. * the Listener to be added.
  691. */
  692. public void addCollapseListener(CollapseListener listener) {
  693. addListener(CollapseEvent.class, listener,
  694. CollapseListener.COLLAPSE_METHOD);
  695. }
  696. /**
  697. * @deprecated As of 7.0, replaced by
  698. * {@link #addCollapseListener(CollapseListener)}
  699. **/
  700. @Deprecated
  701. public void addListener(CollapseListener listener) {
  702. addCollapseListener(listener);
  703. }
  704. /**
  705. * Removes a collapse listener.
  706. *
  707. * @param listener
  708. * the Listener to be removed.
  709. */
  710. public void removeCollapseListener(CollapseListener listener) {
  711. removeListener(CollapseEvent.class, listener,
  712. CollapseListener.COLLAPSE_METHOD);
  713. }
  714. /**
  715. * @deprecated As of 7.0, replaced by
  716. * {@link #removeCollapseListener(CollapseListener)}
  717. **/
  718. @Deprecated
  719. public void removeListener(CollapseListener listener) {
  720. removeCollapseListener(listener);
  721. }
  722. /**
  723. * Emits a collapse event.
  724. *
  725. * @param itemId
  726. * the item id.
  727. */
  728. protected void fireCollapseEvent(Object itemId) {
  729. fireEvent(new CollapseEvent(this, itemId));
  730. }
  731. /**
  732. * @return true if animations are enabled
  733. */
  734. public boolean isAnimationsEnabled() {
  735. return animationsEnabled;
  736. }
  737. /**
  738. * Animations can be enabled by passing true to this method. Currently
  739. * expanding rows slide in from the top and collapsing rows slide out the
  740. * same way. NOTE! not supported in Internet Explorer 6 or 7.
  741. *
  742. * @param animationsEnabled
  743. * true or false whether to enable animations or not.
  744. */
  745. public void setAnimationsEnabled(boolean animationsEnabled) {
  746. this.animationsEnabled = animationsEnabled;
  747. markAsDirty();
  748. }
  749. private static final Logger getLogger() {
  750. return Logger.getLogger(TreeTable.class.getName());
  751. }
  752. @Override
  753. protected List<Object> getItemIds(int firstIndex, int rows) {
  754. List<Object> itemIds = new ArrayList<Object>();
  755. for (int i = firstIndex; i < firstIndex + rows; i++) {
  756. itemIds.add(getIdByIndex(i));
  757. }
  758. return itemIds;
  759. }
  760. }