Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TreeTable.java 30KB

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