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.

VTree.java 70KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.Iterator;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Set;
  12. import com.google.gwt.core.client.JavaScriptObject;
  13. import com.google.gwt.core.client.Scheduler;
  14. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  15. import com.google.gwt.dom.client.NativeEvent;
  16. import com.google.gwt.dom.client.Node;
  17. import com.google.gwt.event.dom.client.BlurEvent;
  18. import com.google.gwt.event.dom.client.BlurHandler;
  19. import com.google.gwt.event.dom.client.ContextMenuEvent;
  20. import com.google.gwt.event.dom.client.ContextMenuHandler;
  21. import com.google.gwt.event.dom.client.FocusEvent;
  22. import com.google.gwt.event.dom.client.FocusHandler;
  23. import com.google.gwt.event.dom.client.KeyCodes;
  24. import com.google.gwt.event.dom.client.KeyDownEvent;
  25. import com.google.gwt.event.dom.client.KeyDownHandler;
  26. import com.google.gwt.event.dom.client.KeyPressEvent;
  27. import com.google.gwt.event.dom.client.KeyPressHandler;
  28. import com.google.gwt.user.client.Command;
  29. import com.google.gwt.user.client.DOM;
  30. import com.google.gwt.user.client.Element;
  31. import com.google.gwt.user.client.Event;
  32. import com.google.gwt.user.client.Window;
  33. import com.google.gwt.user.client.ui.FlowPanel;
  34. import com.google.gwt.user.client.ui.SimplePanel;
  35. import com.google.gwt.user.client.ui.UIObject;
  36. import com.google.gwt.user.client.ui.Widget;
  37. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  38. import com.vaadin.terminal.gwt.client.BrowserInfo;
  39. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  40. import com.vaadin.terminal.gwt.client.UIDL;
  41. import com.vaadin.terminal.gwt.client.Util;
  42. import com.vaadin.terminal.gwt.client.ConnectorMap;
  43. import com.vaadin.terminal.gwt.client.ComponentConnector;
  44. import com.vaadin.terminal.gwt.client.VTooltip;
  45. import com.vaadin.terminal.gwt.client.ui.dd.DDUtil;
  46. import com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler;
  47. import com.vaadin.terminal.gwt.client.ui.dd.VAcceptCallback;
  48. import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager;
  49. import com.vaadin.terminal.gwt.client.ui.dd.VDragEvent;
  50. import com.vaadin.terminal.gwt.client.ui.dd.VDropHandler;
  51. import com.vaadin.terminal.gwt.client.ui.dd.VHasDropHandler;
  52. import com.vaadin.terminal.gwt.client.ui.dd.VTransferable;
  53. import com.vaadin.terminal.gwt.client.ui.dd.VerticalDropLocation;
  54. /**
  55. *
  56. */
  57. public class VTree extends FocusElementPanel implements VHasDropHandler,
  58. FocusHandler, BlurHandler, KeyPressHandler, KeyDownHandler,
  59. SubPartAware, ActionOwner {
  60. public static final String CLASSNAME = "v-tree";
  61. public static final String ITEM_CLICK_EVENT_ID = "itemClick";
  62. public static final int MULTISELECT_MODE_DEFAULT = 0;
  63. public static final int MULTISELECT_MODE_SIMPLE = 1;
  64. private static final int CHARCODE_SPACE = 32;
  65. final FlowPanel body = new FlowPanel();
  66. Set<String> selectedIds = new HashSet<String>();
  67. ApplicationConnection client;
  68. String paintableId;
  69. boolean selectable;
  70. boolean isMultiselect;
  71. private String currentMouseOverKey;
  72. TreeNode lastSelection;
  73. TreeNode focusedNode;
  74. int multiSelectMode = MULTISELECT_MODE_DEFAULT;
  75. private final HashMap<String, TreeNode> keyToNode = new HashMap<String, TreeNode>();
  76. /**
  77. * This map contains captions and icon urls for actions like: * "33_c" ->
  78. * "Edit" * "33_i" -> "http://dom.com/edit.png"
  79. */
  80. private final HashMap<String, String> actionMap = new HashMap<String, String>();
  81. boolean immediate;
  82. boolean isNullSelectionAllowed = true;
  83. boolean disabled = false;
  84. boolean readonly;
  85. boolean rendering;
  86. private VAbstractDropHandler dropHandler;
  87. int dragMode;
  88. private boolean selectionHasChanged = false;
  89. String[] bodyActionKeys;
  90. public VLazyExecutor iconLoaded = new VLazyExecutor(50,
  91. new ScheduledCommand() {
  92. public void execute() {
  93. Util.notifyParentOfSizeChange(VTree.this, true);
  94. }
  95. });
  96. public VTree() {
  97. super();
  98. setStyleName(CLASSNAME);
  99. add(body);
  100. addFocusHandler(this);
  101. addBlurHandler(this);
  102. /*
  103. * Listen to context menu events on the empty space in the tree
  104. */
  105. sinkEvents(Event.ONCONTEXTMENU);
  106. addDomHandler(new ContextMenuHandler() {
  107. public void onContextMenu(ContextMenuEvent event) {
  108. handleBodyContextMenu(event);
  109. }
  110. }, ContextMenuEvent.getType());
  111. /*
  112. * Firefox auto-repeat works correctly only if we use a key press
  113. * handler, other browsers handle it correctly when using a key down
  114. * handler
  115. */
  116. if (BrowserInfo.get().isGecko() || BrowserInfo.get().isOpera()) {
  117. addKeyPressHandler(this);
  118. } else {
  119. addKeyDownHandler(this);
  120. }
  121. /*
  122. * We need to use the sinkEvents method to catch the keyUp events so we
  123. * can cache a single shift. KeyUpHandler cannot do this. At the same
  124. * time we catch the mouse down and up events so we can apply the text
  125. * selection patch in IE
  126. */
  127. sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP | Event.ONKEYUP);
  128. /*
  129. * Re-set the tab index to make sure that the FocusElementPanel's
  130. * (super) focus element gets the tab index and not the element
  131. * containing the tree.
  132. */
  133. setTabIndex(0);
  134. }
  135. /*
  136. * (non-Javadoc)
  137. *
  138. * @see
  139. * com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt.user
  140. * .client.Event)
  141. */
  142. @Override
  143. public void onBrowserEvent(Event event) {
  144. super.onBrowserEvent(event);
  145. if (event.getTypeInt() == Event.ONMOUSEDOWN) {
  146. // Prevent default text selection in IE
  147. if (BrowserInfo.get().isIE()) {
  148. ((Element) event.getEventTarget().cast()).setPropertyJSO(
  149. "onselectstart", applyDisableTextSelectionIEHack());
  150. }
  151. } else if (event.getTypeInt() == Event.ONMOUSEUP) {
  152. // Remove IE text selection hack
  153. if (BrowserInfo.get().isIE()) {
  154. ((Element) event.getEventTarget().cast()).setPropertyJSO(
  155. "onselectstart", null);
  156. }
  157. } else if (event.getTypeInt() == Event.ONKEYUP) {
  158. if (selectionHasChanged) {
  159. if (event.getKeyCode() == getNavigationDownKey()
  160. && !event.getShiftKey()) {
  161. sendSelectionToServer();
  162. event.preventDefault();
  163. } else if (event.getKeyCode() == getNavigationUpKey()
  164. && !event.getShiftKey()) {
  165. sendSelectionToServer();
  166. event.preventDefault();
  167. } else if (event.getKeyCode() == KeyCodes.KEY_SHIFT) {
  168. sendSelectionToServer();
  169. event.preventDefault();
  170. } else if (event.getKeyCode() == getNavigationSelectKey()) {
  171. sendSelectionToServer();
  172. event.preventDefault();
  173. }
  174. }
  175. }
  176. }
  177. public String getActionCaption(String actionKey) {
  178. return actionMap.get(actionKey + "_c");
  179. }
  180. public String getActionIcon(String actionKey) {
  181. return actionMap.get(actionKey + "_i");
  182. }
  183. /**
  184. * Returns the first root node of the tree or null if there are no root
  185. * nodes.
  186. *
  187. * @return The first root {@link TreeNode}
  188. */
  189. protected TreeNode getFirstRootNode() {
  190. if (body.getWidgetCount() == 0) {
  191. return null;
  192. }
  193. return (TreeNode) body.getWidget(0);
  194. }
  195. /**
  196. * Returns the last root node of the tree or null if there are no root
  197. * nodes.
  198. *
  199. * @return The last root {@link TreeNode}
  200. */
  201. protected TreeNode getLastRootNode() {
  202. if (body.getWidgetCount() == 0) {
  203. return null;
  204. }
  205. return (TreeNode) body.getWidget(body.getWidgetCount() - 1);
  206. }
  207. /**
  208. * Returns a list of all root nodes in the Tree in the order they appear in
  209. * the tree.
  210. *
  211. * @return A list of all root {@link TreeNode}s.
  212. */
  213. protected List<TreeNode> getRootNodes() {
  214. ArrayList<TreeNode> rootNodes = new ArrayList<TreeNode>();
  215. for (int i = 0; i < body.getWidgetCount(); i++) {
  216. rootNodes.add((TreeNode) body.getWidget(i));
  217. }
  218. return rootNodes;
  219. }
  220. private void updateTreeRelatedDragData(VDragEvent drag) {
  221. currentMouseOverKey = findCurrentMouseOverKey(drag.getElementOver());
  222. drag.getDropDetails().put("itemIdOver", currentMouseOverKey);
  223. if (currentMouseOverKey != null) {
  224. TreeNode treeNode = getNodeByKey(currentMouseOverKey);
  225. VerticalDropLocation detail = treeNode.getDropDetail(drag
  226. .getCurrentGwtEvent());
  227. Boolean overTreeNode = null;
  228. if (treeNode != null && !treeNode.isLeaf()
  229. && detail == VerticalDropLocation.MIDDLE) {
  230. overTreeNode = true;
  231. }
  232. drag.getDropDetails().put("itemIdOverIsNode", overTreeNode);
  233. drag.getDropDetails().put("detail", detail);
  234. } else {
  235. drag.getDropDetails().put("itemIdOverIsNode", null);
  236. drag.getDropDetails().put("detail", null);
  237. }
  238. }
  239. private String findCurrentMouseOverKey(Element elementOver) {
  240. TreeNode treeNode = Util.findWidget(elementOver, TreeNode.class);
  241. return treeNode == null ? null : treeNode.key;
  242. }
  243. void updateDropHandler(UIDL childUidl) {
  244. if (dropHandler == null) {
  245. dropHandler = new VAbstractDropHandler() {
  246. @Override
  247. public void dragEnter(VDragEvent drag) {
  248. }
  249. @Override
  250. protected void dragAccepted(final VDragEvent drag) {
  251. }
  252. @Override
  253. public void dragOver(final VDragEvent currentDrag) {
  254. final Object oldIdOver = currentDrag.getDropDetails().get(
  255. "itemIdOver");
  256. final VerticalDropLocation oldDetail = (VerticalDropLocation) currentDrag
  257. .getDropDetails().get("detail");
  258. updateTreeRelatedDragData(currentDrag);
  259. final VerticalDropLocation detail = (VerticalDropLocation) currentDrag
  260. .getDropDetails().get("detail");
  261. boolean nodeHasChanged = (currentMouseOverKey != null && currentMouseOverKey != oldIdOver)
  262. || (currentMouseOverKey == null && oldIdOver != null);
  263. boolean detailHasChanded = (detail != null && detail != oldDetail)
  264. || (detail == null && oldDetail != null);
  265. if (nodeHasChanged || detailHasChanded) {
  266. final String newKey = currentMouseOverKey;
  267. TreeNode treeNode = keyToNode.get(oldIdOver);
  268. if (treeNode != null) {
  269. // clear old styles
  270. treeNode.emphasis(null);
  271. }
  272. if (newKey != null) {
  273. validate(new VAcceptCallback() {
  274. public void accepted(VDragEvent event) {
  275. VerticalDropLocation curDetail = (VerticalDropLocation) event
  276. .getDropDetails().get("detail");
  277. if (curDetail == detail
  278. && newKey.equals(currentMouseOverKey)) {
  279. getNodeByKey(newKey).emphasis(detail);
  280. }
  281. /*
  282. * Else drag is already on a different
  283. * node-detail pair, new criteria check is
  284. * going on
  285. */
  286. }
  287. }, currentDrag);
  288. }
  289. }
  290. }
  291. @Override
  292. public void dragLeave(VDragEvent drag) {
  293. cleanUp();
  294. }
  295. private void cleanUp() {
  296. if (currentMouseOverKey != null) {
  297. getNodeByKey(currentMouseOverKey).emphasis(null);
  298. currentMouseOverKey = null;
  299. }
  300. }
  301. @Override
  302. public boolean drop(VDragEvent drag) {
  303. cleanUp();
  304. return super.drop(drag);
  305. }
  306. @Override
  307. public ComponentConnector getPaintable() {
  308. return ConnectorMap.get(client).getConnector(VTree.this);
  309. }
  310. public ApplicationConnection getApplicationConnection() {
  311. return client;
  312. }
  313. };
  314. }
  315. dropHandler.updateAcceptRules(childUidl);
  316. }
  317. public void setSelected(TreeNode treeNode, boolean selected) {
  318. if (selected) {
  319. if (!isMultiselect) {
  320. while (selectedIds.size() > 0) {
  321. final String id = selectedIds.iterator().next();
  322. final TreeNode oldSelection = getNodeByKey(id);
  323. if (oldSelection != null) {
  324. // can be null if the node is not visible (parent
  325. // collapsed)
  326. oldSelection.setSelected(false);
  327. }
  328. selectedIds.remove(id);
  329. }
  330. }
  331. treeNode.setSelected(true);
  332. selectedIds.add(treeNode.key);
  333. } else {
  334. if (!isNullSelectionAllowed) {
  335. if (!isMultiselect || selectedIds.size() == 1) {
  336. return;
  337. }
  338. }
  339. selectedIds.remove(treeNode.key);
  340. treeNode.setSelected(false);
  341. }
  342. sendSelectionToServer();
  343. }
  344. /**
  345. * Sends the selection to the server
  346. */
  347. private void sendSelectionToServer() {
  348. Command command = new Command() {
  349. public void execute() {
  350. client.updateVariable(paintableId, "selected",
  351. selectedIds.toArray(new String[selectedIds.size()]),
  352. immediate);
  353. selectionHasChanged = false;
  354. }
  355. };
  356. /*
  357. * Delaying the sending of the selection in webkit to ensure the
  358. * selection is always sent when the tree has focus and after click
  359. * events have been processed. This is due to the focusing
  360. * implementation in FocusImplSafari which uses timeouts when focusing
  361. * and blurring.
  362. */
  363. if (BrowserInfo.get().isWebkit()) {
  364. Scheduler.get().scheduleDeferred(command);
  365. } else {
  366. command.execute();
  367. }
  368. }
  369. /**
  370. * Is a node selected in the tree
  371. *
  372. * @param treeNode
  373. * The node to check
  374. * @return
  375. */
  376. public boolean isSelected(TreeNode treeNode) {
  377. return selectedIds.contains(treeNode.key);
  378. }
  379. public class TreeNode extends SimplePanel implements ActionOwner {
  380. public static final String CLASSNAME = "v-tree-node";
  381. public static final String CLASSNAME_FOCUSED = CLASSNAME + "-focused";
  382. public String key;
  383. String[] actionKeys = null;
  384. boolean childrenLoaded;
  385. Element nodeCaptionDiv;
  386. protected Element nodeCaptionSpan;
  387. FlowPanel childNodeContainer;
  388. private boolean open;
  389. private Icon icon;
  390. private Event mouseDownEvent;
  391. private int cachedHeight = -1;
  392. private boolean focused = false;
  393. public TreeNode() {
  394. constructDom();
  395. sinkEvents(Event.ONCLICK | Event.ONDBLCLICK | Event.MOUSEEVENTS
  396. | Event.TOUCHEVENTS | Event.ONCONTEXTMENU);
  397. }
  398. public VerticalDropLocation getDropDetail(NativeEvent currentGwtEvent) {
  399. if (cachedHeight < 0) {
  400. /*
  401. * Height is cached to avoid flickering (drop hints may change
  402. * the reported offsetheight -> would change the drop detail)
  403. */
  404. cachedHeight = nodeCaptionDiv.getOffsetHeight();
  405. }
  406. VerticalDropLocation verticalDropLocation = DDUtil
  407. .getVerticalDropLocation(nodeCaptionDiv, cachedHeight,
  408. currentGwtEvent, 0.15);
  409. return verticalDropLocation;
  410. }
  411. protected void emphasis(VerticalDropLocation detail) {
  412. String base = "v-tree-node-drag-";
  413. UIObject.setStyleName(getElement(), base + "top",
  414. VerticalDropLocation.TOP == detail);
  415. UIObject.setStyleName(getElement(), base + "bottom",
  416. VerticalDropLocation.BOTTOM == detail);
  417. UIObject.setStyleName(getElement(), base + "center",
  418. VerticalDropLocation.MIDDLE == detail);
  419. base = "v-tree-node-caption-drag-";
  420. UIObject.setStyleName(nodeCaptionDiv, base + "top",
  421. VerticalDropLocation.TOP == detail);
  422. UIObject.setStyleName(nodeCaptionDiv, base + "bottom",
  423. VerticalDropLocation.BOTTOM == detail);
  424. UIObject.setStyleName(nodeCaptionDiv, base + "center",
  425. VerticalDropLocation.MIDDLE == detail);
  426. // also add classname to "folder node" into which the drag is
  427. // targeted
  428. TreeNode folder = null;
  429. /* Possible parent of this TreeNode will be stored here */
  430. TreeNode parentFolder = getParentNode();
  431. // TODO fix my bugs
  432. if (isLeaf()) {
  433. folder = parentFolder;
  434. // note, parent folder may be null if this is root node => no
  435. // folder target exists
  436. } else {
  437. if (detail == VerticalDropLocation.TOP) {
  438. folder = parentFolder;
  439. } else {
  440. folder = this;
  441. }
  442. // ensure we remove the dragfolder classname from the previous
  443. // folder node
  444. setDragFolderStyleName(this, false);
  445. setDragFolderStyleName(parentFolder, false);
  446. }
  447. if (folder != null) {
  448. setDragFolderStyleName(folder, detail != null);
  449. }
  450. }
  451. private TreeNode getParentNode() {
  452. Widget parent2 = getParent().getParent();
  453. if (parent2 instanceof TreeNode) {
  454. return (TreeNode) parent2;
  455. }
  456. return null;
  457. }
  458. private void setDragFolderStyleName(TreeNode folder, boolean add) {
  459. if (folder != null) {
  460. UIObject.setStyleName(folder.getElement(),
  461. "v-tree-node-dragfolder", add);
  462. UIObject.setStyleName(folder.nodeCaptionDiv,
  463. "v-tree-node-caption-dragfolder", add);
  464. }
  465. }
  466. /**
  467. * Handles mouse selection
  468. *
  469. * @param ctrl
  470. * Was the ctrl-key pressed
  471. * @param shift
  472. * Was the shift-key pressed
  473. * @return Returns true if event was handled, else false
  474. */
  475. private boolean handleClickSelection(final boolean ctrl,
  476. final boolean shift) {
  477. // always when clicking an item, focus it
  478. setFocusedNode(this, false);
  479. if (!BrowserInfo.get().isOpera()) {
  480. /*
  481. * Ensure that the tree's focus element also gains focus
  482. * (TreeNodes focus is faked using FocusElementPanel in browsers
  483. * other than Opera).
  484. */
  485. focus();
  486. }
  487. ScheduledCommand command = new ScheduledCommand() {
  488. public void execute() {
  489. if (multiSelectMode == MULTISELECT_MODE_SIMPLE
  490. || !isMultiselect) {
  491. toggleSelection();
  492. lastSelection = TreeNode.this;
  493. } else if (multiSelectMode == MULTISELECT_MODE_DEFAULT) {
  494. // Handle ctrl+click
  495. if (isMultiselect && ctrl && !shift) {
  496. toggleSelection();
  497. lastSelection = TreeNode.this;
  498. // Handle shift+click
  499. } else if (isMultiselect && !ctrl && shift) {
  500. deselectAll();
  501. selectNodeRange(lastSelection.key, key);
  502. sendSelectionToServer();
  503. // Handle ctrl+shift click
  504. } else if (isMultiselect && ctrl && shift) {
  505. selectNodeRange(lastSelection.key, key);
  506. // Handle click
  507. } else {
  508. // TODO should happen only if this alone not yet
  509. // selected,
  510. // now sending excess server calls
  511. deselectAll();
  512. toggleSelection();
  513. lastSelection = TreeNode.this;
  514. }
  515. }
  516. }
  517. };
  518. if (BrowserInfo.get().isWebkit() && !treeHasFocus) {
  519. /*
  520. * Safari may need to wait for focus. See FocusImplSafari.
  521. */
  522. // VConsole.log("Deferring click handling to let webkit gain focus...");
  523. Scheduler.get().scheduleDeferred(command);
  524. } else {
  525. command.execute();
  526. }
  527. return true;
  528. }
  529. /*
  530. * (non-Javadoc)
  531. *
  532. * @see
  533. * com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt
  534. * .user.client.Event)
  535. */
  536. @Override
  537. public void onBrowserEvent(Event event) {
  538. super.onBrowserEvent(event);
  539. final int type = DOM.eventGetType(event);
  540. final Element target = DOM.eventGetTarget(event);
  541. if (type == Event.ONLOAD && target == icon.getElement()) {
  542. iconLoaded.trigger();
  543. }
  544. if (disabled) {
  545. return;
  546. }
  547. if (target == nodeCaptionSpan) {
  548. client.handleTooltipEvent(event, VTree.this, key);
  549. }
  550. final boolean inCaption = target == nodeCaptionSpan
  551. || (icon != null && target == icon.getElement());
  552. if (inCaption
  553. && client.hasEventListeners(VTree.this,
  554. ITEM_CLICK_EVENT_ID)
  555. && (type == Event.ONDBLCLICK || type == Event.ONMOUSEUP)) {
  556. fireClick(event);
  557. }
  558. if (type == Event.ONCLICK) {
  559. if (getElement() == target) {
  560. // state change
  561. toggleState();
  562. } else if (!readonly && inCaption) {
  563. if (selectable) {
  564. // caption click = selection change && possible click
  565. // event
  566. if (handleClickSelection(
  567. event.getCtrlKey() || event.getMetaKey(),
  568. event.getShiftKey())) {
  569. event.preventDefault();
  570. }
  571. } else {
  572. // Not selectable, only focus the node.
  573. setFocusedNode(this);
  574. }
  575. }
  576. event.stopPropagation();
  577. } else if (type == Event.ONCONTEXTMENU) {
  578. showContextMenu(event);
  579. }
  580. if (dragMode != 0 || dropHandler != null) {
  581. if (type == Event.ONMOUSEDOWN || type == Event.ONTOUCHSTART) {
  582. if (nodeCaptionDiv.isOrHasChild((Node) event
  583. .getEventTarget().cast())) {
  584. if (dragMode > 0
  585. && (type == Event.ONTOUCHSTART || event
  586. .getButton() == NativeEvent.BUTTON_LEFT)) {
  587. mouseDownEvent = event; // save event for possible
  588. // dd operation
  589. if (type == Event.ONMOUSEDOWN) {
  590. event.preventDefault(); // prevent text
  591. // selection
  592. } else {
  593. /*
  594. * FIXME We prevent touch start event to be used
  595. * as a scroll start event. Note that we cannot
  596. * easily distinguish whether the user wants to
  597. * drag or scroll. The same issue is in table
  598. * that has scrollable area and has drag and
  599. * drop enable. Some kind of timer might be used
  600. * to resolve the issue.
  601. */
  602. event.stopPropagation();
  603. }
  604. }
  605. }
  606. } else if (type == Event.ONMOUSEMOVE
  607. || type == Event.ONMOUSEOUT
  608. || type == Event.ONTOUCHMOVE) {
  609. if (mouseDownEvent != null) {
  610. // start actual drag on slight move when mouse is down
  611. VTransferable t = new VTransferable();
  612. t.setDragSource(ConnectorMap.get(client).getConnector(
  613. VTree.this));
  614. t.setData("itemId", key);
  615. VDragEvent drag = VDragAndDropManager.get().startDrag(
  616. t, mouseDownEvent, true);
  617. drag.createDragImage(nodeCaptionDiv, true);
  618. event.stopPropagation();
  619. mouseDownEvent = null;
  620. }
  621. } else if (type == Event.ONMOUSEUP) {
  622. mouseDownEvent = null;
  623. }
  624. if (type == Event.ONMOUSEOVER) {
  625. mouseDownEvent = null;
  626. currentMouseOverKey = key;
  627. event.stopPropagation();
  628. }
  629. } else if (type == Event.ONMOUSEDOWN
  630. && event.getButton() == NativeEvent.BUTTON_LEFT) {
  631. event.preventDefault(); // text selection
  632. }
  633. }
  634. private void fireClick(final Event evt) {
  635. /*
  636. * Ensure we have focus in tree before sending variables. Otherwise
  637. * previously modified field may contain dirty variables.
  638. */
  639. if (!treeHasFocus) {
  640. if (BrowserInfo.get().isOpera()) {
  641. if (focusedNode == null) {
  642. getNodeByKey(key).setFocused(true);
  643. } else {
  644. focusedNode.setFocused(true);
  645. }
  646. } else {
  647. focus();
  648. }
  649. }
  650. final MouseEventDetails details = new MouseEventDetails(evt);
  651. ScheduledCommand command = new ScheduledCommand() {
  652. public void execute() {
  653. // Determine if we should send the event immediately to the
  654. // server. We do not want to send the event if there is a
  655. // selection event happening after this. In all other cases
  656. // we want to send it immediately.
  657. boolean sendClickEventNow = true;
  658. if (details.getButton() == NativeEvent.BUTTON_LEFT
  659. && immediate && selectable) {
  660. // Probably a selection that will cause a value change
  661. // event to be sent
  662. sendClickEventNow = false;
  663. // The exception is that user clicked on the
  664. // currently selected row and null selection is not
  665. // allowed == no selection event
  666. if (isSelected() && selectedIds.size() == 1
  667. && !isNullSelectionAllowed) {
  668. sendClickEventNow = true;
  669. }
  670. }
  671. client.updateVariable(paintableId, "clickedKey", key, false);
  672. client.updateVariable(paintableId, "clickEvent",
  673. details.toString(), sendClickEventNow);
  674. }
  675. };
  676. if (treeHasFocus) {
  677. command.execute();
  678. } else {
  679. /*
  680. * Webkits need a deferring due to FocusImplSafari uses timeout
  681. */
  682. Scheduler.get().scheduleDeferred(command);
  683. }
  684. }
  685. private void toggleSelection() {
  686. if (selectable) {
  687. VTree.this.setSelected(this, !isSelected());
  688. }
  689. }
  690. private void toggleState() {
  691. setState(!getState(), true);
  692. }
  693. protected void constructDom() {
  694. addStyleName(CLASSNAME);
  695. nodeCaptionDiv = DOM.createDiv();
  696. DOM.setElementProperty(nodeCaptionDiv, "className", CLASSNAME
  697. + "-caption");
  698. Element wrapper = DOM.createDiv();
  699. nodeCaptionSpan = DOM.createSpan();
  700. DOM.sinkEvents(nodeCaptionSpan, VTooltip.TOOLTIP_EVENTS);
  701. DOM.appendChild(getElement(), nodeCaptionDiv);
  702. DOM.appendChild(nodeCaptionDiv, wrapper);
  703. DOM.appendChild(wrapper, nodeCaptionSpan);
  704. if (BrowserInfo.get().isOpera()) {
  705. /*
  706. * Focus the caption div of the node to get keyboard navigation
  707. * to work without scrolling up or down when focusing a node.
  708. */
  709. nodeCaptionDiv.setTabIndex(-1);
  710. }
  711. childNodeContainer = new FlowPanel();
  712. childNodeContainer.setStyleName(CLASSNAME + "-children");
  713. setWidget(childNodeContainer);
  714. }
  715. public boolean isLeaf() {
  716. String[] styleNames = getStyleName().split(" ");
  717. for (String styleName : styleNames) {
  718. if (styleName.equals(CLASSNAME + "-leaf")) {
  719. return true;
  720. }
  721. }
  722. return false;
  723. }
  724. void setState(boolean state, boolean notifyServer) {
  725. if (open == state) {
  726. return;
  727. }
  728. if (state) {
  729. if (!childrenLoaded && notifyServer) {
  730. client.updateVariable(paintableId, "requestChildTree",
  731. true, false);
  732. }
  733. if (notifyServer) {
  734. client.updateVariable(paintableId, "expand",
  735. new String[] { key }, true);
  736. }
  737. addStyleName(CLASSNAME + "-expanded");
  738. childNodeContainer.setVisible(true);
  739. } else {
  740. removeStyleName(CLASSNAME + "-expanded");
  741. childNodeContainer.setVisible(false);
  742. if (notifyServer) {
  743. client.updateVariable(paintableId, "collapse",
  744. new String[] { key }, true);
  745. }
  746. }
  747. open = state;
  748. if (!rendering) {
  749. Util.notifyParentOfSizeChange(VTree.this, false);
  750. }
  751. }
  752. boolean getState() {
  753. return open;
  754. }
  755. void setText(String text) {
  756. DOM.setInnerText(nodeCaptionSpan, text);
  757. }
  758. public boolean isChildrenLoaded() {
  759. return childrenLoaded;
  760. }
  761. /**
  762. * Returns the children of the node
  763. *
  764. * @return A set of tree nodes
  765. */
  766. public List<TreeNode> getChildren() {
  767. List<TreeNode> nodes = new LinkedList<TreeNode>();
  768. if (!isLeaf() && isChildrenLoaded()) {
  769. Iterator<Widget> iter = childNodeContainer.iterator();
  770. while (iter.hasNext()) {
  771. TreeNode node = (TreeNode) iter.next();
  772. nodes.add(node);
  773. }
  774. }
  775. return nodes;
  776. }
  777. public Action[] getActions() {
  778. if (actionKeys == null) {
  779. return new Action[] {};
  780. }
  781. final Action[] actions = new Action[actionKeys.length];
  782. for (int i = 0; i < actions.length; i++) {
  783. final String actionKey = actionKeys[i];
  784. final TreeAction a = new TreeAction(this, String.valueOf(key),
  785. actionKey);
  786. a.setCaption(getActionCaption(actionKey));
  787. a.setIconUrl(getActionIcon(actionKey));
  788. actions[i] = a;
  789. }
  790. return actions;
  791. }
  792. public ApplicationConnection getClient() {
  793. return client;
  794. }
  795. public String getPaintableId() {
  796. return paintableId;
  797. }
  798. /**
  799. * Adds/removes Vaadin specific style name. This method ought to be
  800. * called only from VTree.
  801. *
  802. * @param selected
  803. */
  804. protected void setSelected(boolean selected) {
  805. // add style name to caption dom structure only, not to subtree
  806. setStyleName(nodeCaptionDiv, "v-tree-node-selected", selected);
  807. }
  808. protected boolean isSelected() {
  809. return VTree.this.isSelected(this);
  810. }
  811. /**
  812. * Travels up the hierarchy looking for this node
  813. *
  814. * @param child
  815. * The child which grandparent this is or is not
  816. * @return True if this is a grandparent of the child node
  817. */
  818. public boolean isGrandParentOf(TreeNode child) {
  819. TreeNode currentNode = child;
  820. boolean isGrandParent = false;
  821. while (currentNode != null) {
  822. currentNode = currentNode.getParentNode();
  823. if (currentNode == this) {
  824. isGrandParent = true;
  825. break;
  826. }
  827. }
  828. return isGrandParent;
  829. }
  830. public boolean isSibling(TreeNode node) {
  831. return node.getParentNode() == getParentNode();
  832. }
  833. public void showContextMenu(Event event) {
  834. if (!readonly && !disabled) {
  835. if (actionKeys != null) {
  836. int left = event.getClientX();
  837. int top = event.getClientY();
  838. top += Window.getScrollTop();
  839. left += Window.getScrollLeft();
  840. client.getContextMenu().showAt(this, left, top);
  841. }
  842. event.stopPropagation();
  843. event.preventDefault();
  844. }
  845. }
  846. /*
  847. * (non-Javadoc)
  848. *
  849. * @see com.google.gwt.user.client.ui.Widget#onDetach()
  850. */
  851. @Override
  852. protected void onDetach() {
  853. super.onDetach();
  854. client.getContextMenu().ensureHidden(this);
  855. }
  856. /*
  857. * (non-Javadoc)
  858. *
  859. * @see com.google.gwt.user.client.ui.UIObject#toString()
  860. */
  861. @Override
  862. public String toString() {
  863. return nodeCaptionSpan.getInnerText();
  864. }
  865. /**
  866. * Is the node focused?
  867. *
  868. * @param focused
  869. * True if focused, false if not
  870. */
  871. public void setFocused(boolean focused) {
  872. if (!this.focused && focused) {
  873. nodeCaptionDiv.addClassName(CLASSNAME_FOCUSED);
  874. this.focused = focused;
  875. if (BrowserInfo.get().isOpera()) {
  876. nodeCaptionDiv.focus();
  877. }
  878. treeHasFocus = true;
  879. } else if (this.focused && !focused) {
  880. nodeCaptionDiv.removeClassName(CLASSNAME_FOCUSED);
  881. this.focused = focused;
  882. treeHasFocus = false;
  883. }
  884. }
  885. /**
  886. * Scrolls the caption into view
  887. */
  888. public void scrollIntoView() {
  889. Util.scrollIntoViewVertically(nodeCaptionDiv);
  890. }
  891. public void setIcon(String iconUrl) {
  892. if (iconUrl != null) {
  893. // Add icon if not present
  894. if (icon == null) {
  895. icon = new Icon(client);
  896. DOM.insertBefore(DOM.getFirstChild(nodeCaptionDiv),
  897. icon.getElement(), nodeCaptionSpan);
  898. }
  899. icon.setUri(iconUrl);
  900. } else {
  901. // Remove icon if present
  902. if (icon != null) {
  903. DOM.removeChild(DOM.getFirstChild(nodeCaptionDiv),
  904. icon.getElement());
  905. icon = null;
  906. }
  907. }
  908. }
  909. public void setNodeStyleName(String styleName) {
  910. addStyleName(TreeNode.CLASSNAME + "-" + styleName);
  911. setStyleName(nodeCaptionDiv, TreeNode.CLASSNAME + "-caption-"
  912. + styleName, true);
  913. childNodeContainer.addStyleName(TreeNode.CLASSNAME + "-children-"
  914. + styleName);
  915. }
  916. }
  917. public VDropHandler getDropHandler() {
  918. return dropHandler;
  919. }
  920. public TreeNode getNodeByKey(String key) {
  921. return keyToNode.get(key);
  922. }
  923. /**
  924. * Deselects all items in the tree
  925. */
  926. public void deselectAll() {
  927. for (String key : selectedIds) {
  928. TreeNode node = keyToNode.get(key);
  929. if (node != null) {
  930. node.setSelected(false);
  931. }
  932. }
  933. selectedIds.clear();
  934. selectionHasChanged = true;
  935. }
  936. /**
  937. * Selects a range of nodes
  938. *
  939. * @param startNodeKey
  940. * The start node key
  941. * @param endNodeKey
  942. * The end node key
  943. */
  944. private void selectNodeRange(String startNodeKey, String endNodeKey) {
  945. TreeNode startNode = keyToNode.get(startNodeKey);
  946. TreeNode endNode = keyToNode.get(endNodeKey);
  947. // The nodes have the same parent
  948. if (startNode.getParent() == endNode.getParent()) {
  949. doSiblingSelection(startNode, endNode);
  950. // The start node is a grandparent of the end node
  951. } else if (startNode.isGrandParentOf(endNode)) {
  952. doRelationSelection(startNode, endNode);
  953. // The end node is a grandparent of the start node
  954. } else if (endNode.isGrandParentOf(startNode)) {
  955. doRelationSelection(endNode, startNode);
  956. } else {
  957. doNoRelationSelection(startNode, endNode);
  958. }
  959. }
  960. /**
  961. * Selects a node and deselect all other nodes
  962. *
  963. * @param node
  964. * The node to select
  965. */
  966. private void selectNode(TreeNode node, boolean deselectPrevious) {
  967. if (deselectPrevious) {
  968. deselectAll();
  969. }
  970. if (node != null) {
  971. node.setSelected(true);
  972. selectedIds.add(node.key);
  973. lastSelection = node;
  974. }
  975. selectionHasChanged = true;
  976. }
  977. /**
  978. * Deselects a node
  979. *
  980. * @param node
  981. * The node to deselect
  982. */
  983. private void deselectNode(TreeNode node) {
  984. node.setSelected(false);
  985. selectedIds.remove(node.key);
  986. selectionHasChanged = true;
  987. }
  988. /**
  989. * Selects all the open children to a node
  990. *
  991. * @param node
  992. * The parent node
  993. */
  994. private void selectAllChildren(TreeNode node, boolean includeRootNode) {
  995. if (includeRootNode) {
  996. node.setSelected(true);
  997. selectedIds.add(node.key);
  998. }
  999. for (TreeNode child : node.getChildren()) {
  1000. if (!child.isLeaf() && child.getState()) {
  1001. selectAllChildren(child, true);
  1002. } else {
  1003. child.setSelected(true);
  1004. selectedIds.add(child.key);
  1005. }
  1006. }
  1007. selectionHasChanged = true;
  1008. }
  1009. /**
  1010. * Selects all children until a stop child is reached
  1011. *
  1012. * @param root
  1013. * The root not to start from
  1014. * @param stopNode
  1015. * The node to finish with
  1016. * @param includeRootNode
  1017. * Should the root node be selected
  1018. * @param includeStopNode
  1019. * Should the stop node be selected
  1020. *
  1021. * @return Returns false if the stop child was found, else true if all
  1022. * children was selected
  1023. */
  1024. private boolean selectAllChildrenUntil(TreeNode root, TreeNode stopNode,
  1025. boolean includeRootNode, boolean includeStopNode) {
  1026. if (includeRootNode) {
  1027. root.setSelected(true);
  1028. selectedIds.add(root.key);
  1029. }
  1030. if (root.getState() && root != stopNode) {
  1031. for (TreeNode child : root.getChildren()) {
  1032. if (!child.isLeaf() && child.getState() && child != stopNode) {
  1033. if (!selectAllChildrenUntil(child, stopNode, true,
  1034. includeStopNode)) {
  1035. return false;
  1036. }
  1037. } else if (child == stopNode) {
  1038. if (includeStopNode) {
  1039. child.setSelected(true);
  1040. selectedIds.add(child.key);
  1041. }
  1042. return false;
  1043. } else if (child.isLeaf()) {
  1044. child.setSelected(true);
  1045. selectedIds.add(child.key);
  1046. }
  1047. }
  1048. }
  1049. selectionHasChanged = true;
  1050. return true;
  1051. }
  1052. /**
  1053. * Select a range between two nodes which have no relation to each other
  1054. *
  1055. * @param startNode
  1056. * The start node to start the selection from
  1057. * @param endNode
  1058. * The end node to end the selection to
  1059. */
  1060. private void doNoRelationSelection(TreeNode startNode, TreeNode endNode) {
  1061. TreeNode commonParent = getCommonGrandParent(startNode, endNode);
  1062. TreeNode startBranch = null, endBranch = null;
  1063. // Find the children of the common parent
  1064. List<TreeNode> children;
  1065. if (commonParent != null) {
  1066. children = commonParent.getChildren();
  1067. } else {
  1068. children = getRootNodes();
  1069. }
  1070. // Find the start and end branches
  1071. for (TreeNode node : children) {
  1072. if (nodeIsInBranch(startNode, node)) {
  1073. startBranch = node;
  1074. }
  1075. if (nodeIsInBranch(endNode, node)) {
  1076. endBranch = node;
  1077. }
  1078. }
  1079. // Swap nodes if necessary
  1080. if (children.indexOf(startBranch) > children.indexOf(endBranch)) {
  1081. TreeNode temp = startBranch;
  1082. startBranch = endBranch;
  1083. endBranch = temp;
  1084. temp = startNode;
  1085. startNode = endNode;
  1086. endNode = temp;
  1087. }
  1088. // Select all children under the start node
  1089. selectAllChildren(startNode, true);
  1090. TreeNode startParent = startNode.getParentNode();
  1091. TreeNode currentNode = startNode;
  1092. while (startParent != null && startParent != commonParent) {
  1093. List<TreeNode> startChildren = startParent.getChildren();
  1094. for (int i = startChildren.indexOf(currentNode) + 1; i < startChildren
  1095. .size(); i++) {
  1096. selectAllChildren(startChildren.get(i), true);
  1097. }
  1098. currentNode = startParent;
  1099. startParent = startParent.getParentNode();
  1100. }
  1101. // Select nodes until the end node is reached
  1102. for (int i = children.indexOf(startBranch) + 1; i <= children
  1103. .indexOf(endBranch); i++) {
  1104. selectAllChildrenUntil(children.get(i), endNode, true, true);
  1105. }
  1106. // Ensure end node was selected
  1107. endNode.setSelected(true);
  1108. selectedIds.add(endNode.key);
  1109. selectionHasChanged = true;
  1110. }
  1111. /**
  1112. * Examines the children of the branch node and returns true if a node is in
  1113. * that branch
  1114. *
  1115. * @param node
  1116. * The node to search for
  1117. * @param branch
  1118. * The branch to search in
  1119. * @return True if found, false if not found
  1120. */
  1121. private boolean nodeIsInBranch(TreeNode node, TreeNode branch) {
  1122. if (node == branch) {
  1123. return true;
  1124. }
  1125. for (TreeNode child : branch.getChildren()) {
  1126. if (child == node) {
  1127. return true;
  1128. }
  1129. if (!child.isLeaf() && child.getState()) {
  1130. if (nodeIsInBranch(node, child)) {
  1131. return true;
  1132. }
  1133. }
  1134. }
  1135. return false;
  1136. }
  1137. /**
  1138. * Selects a range of items which are in direct relation with each other.<br/>
  1139. * NOTE: The start node <b>MUST</b> be before the end node!
  1140. *
  1141. * @param startNode
  1142. *
  1143. * @param endNode
  1144. */
  1145. private void doRelationSelection(TreeNode startNode, TreeNode endNode) {
  1146. TreeNode currentNode = endNode;
  1147. while (currentNode != startNode) {
  1148. currentNode.setSelected(true);
  1149. selectedIds.add(currentNode.key);
  1150. // Traverse children above the selection
  1151. List<TreeNode> subChildren = currentNode.getParentNode()
  1152. .getChildren();
  1153. if (subChildren.size() > 1) {
  1154. selectNodeRange(subChildren.iterator().next().key,
  1155. currentNode.key);
  1156. } else if (subChildren.size() == 1) {
  1157. TreeNode n = subChildren.get(0);
  1158. n.setSelected(true);
  1159. selectedIds.add(n.key);
  1160. }
  1161. currentNode = currentNode.getParentNode();
  1162. }
  1163. startNode.setSelected(true);
  1164. selectedIds.add(startNode.key);
  1165. selectionHasChanged = true;
  1166. }
  1167. /**
  1168. * Selects a range of items which have the same parent.
  1169. *
  1170. * @param startNode
  1171. * The start node
  1172. * @param endNode
  1173. * The end node
  1174. */
  1175. private void doSiblingSelection(TreeNode startNode, TreeNode endNode) {
  1176. TreeNode parent = startNode.getParentNode();
  1177. List<TreeNode> children;
  1178. if (parent == null) {
  1179. // Topmost parent
  1180. children = getRootNodes();
  1181. } else {
  1182. children = parent.getChildren();
  1183. }
  1184. // Swap start and end point if needed
  1185. if (children.indexOf(startNode) > children.indexOf(endNode)) {
  1186. TreeNode temp = startNode;
  1187. startNode = endNode;
  1188. endNode = temp;
  1189. }
  1190. Iterator<TreeNode> childIter = children.iterator();
  1191. boolean startFound = false;
  1192. while (childIter.hasNext()) {
  1193. TreeNode node = childIter.next();
  1194. if (node == startNode) {
  1195. startFound = true;
  1196. }
  1197. if (startFound && node != endNode && node.getState()) {
  1198. selectAllChildren(node, true);
  1199. } else if (startFound && node != endNode) {
  1200. node.setSelected(true);
  1201. selectedIds.add(node.key);
  1202. }
  1203. if (node == endNode) {
  1204. node.setSelected(true);
  1205. selectedIds.add(node.key);
  1206. break;
  1207. }
  1208. }
  1209. selectionHasChanged = true;
  1210. }
  1211. /**
  1212. * Returns the first common parent of two nodes
  1213. *
  1214. * @param node1
  1215. * The first node
  1216. * @param node2
  1217. * The second node
  1218. * @return The common parent or null
  1219. */
  1220. public TreeNode getCommonGrandParent(TreeNode node1, TreeNode node2) {
  1221. // If either one does not have a grand parent then return null
  1222. if (node1.getParentNode() == null || node2.getParentNode() == null) {
  1223. return null;
  1224. }
  1225. // If the nodes are parents of each other then return null
  1226. if (node1.isGrandParentOf(node2) || node2.isGrandParentOf(node1)) {
  1227. return null;
  1228. }
  1229. // Get parents of node1
  1230. List<TreeNode> parents1 = new ArrayList<TreeNode>();
  1231. TreeNode parent1 = node1.getParentNode();
  1232. while (parent1 != null) {
  1233. parents1.add(parent1);
  1234. parent1 = parent1.getParentNode();
  1235. }
  1236. // Get parents of node2
  1237. List<TreeNode> parents2 = new ArrayList<TreeNode>();
  1238. TreeNode parent2 = node2.getParentNode();
  1239. while (parent2 != null) {
  1240. parents2.add(parent2);
  1241. parent2 = parent2.getParentNode();
  1242. }
  1243. // Search the parents for the first common parent
  1244. for (int i = 0; i < parents1.size(); i++) {
  1245. parent1 = parents1.get(i);
  1246. for (int j = 0; j < parents2.size(); j++) {
  1247. parent2 = parents2.get(j);
  1248. if (parent1 == parent2) {
  1249. return parent1;
  1250. }
  1251. }
  1252. }
  1253. return null;
  1254. }
  1255. /**
  1256. * Sets the node currently in focus
  1257. *
  1258. * @param node
  1259. * The node to focus or null to remove the focus completely
  1260. * @param scrollIntoView
  1261. * Scroll the node into view
  1262. */
  1263. public void setFocusedNode(TreeNode node, boolean scrollIntoView) {
  1264. // Unfocus previously focused node
  1265. if (focusedNode != null) {
  1266. focusedNode.setFocused(false);
  1267. }
  1268. if (node != null) {
  1269. node.setFocused(true);
  1270. }
  1271. focusedNode = node;
  1272. if (node != null && scrollIntoView) {
  1273. /*
  1274. * Delay scrolling the focused node into view if we are still
  1275. * rendering. #5396
  1276. */
  1277. if (!rendering) {
  1278. node.scrollIntoView();
  1279. } else {
  1280. Scheduler.get().scheduleDeferred(new Command() {
  1281. public void execute() {
  1282. focusedNode.scrollIntoView();
  1283. }
  1284. });
  1285. }
  1286. }
  1287. }
  1288. /**
  1289. * Focuses a node and scrolls it into view
  1290. *
  1291. * @param node
  1292. * The node to focus
  1293. */
  1294. public void setFocusedNode(TreeNode node) {
  1295. setFocusedNode(node, true);
  1296. }
  1297. /*
  1298. * (non-Javadoc)
  1299. *
  1300. * @see
  1301. * com.google.gwt.event.dom.client.FocusHandler#onFocus(com.google.gwt.event
  1302. * .dom.client.FocusEvent)
  1303. */
  1304. public void onFocus(FocusEvent event) {
  1305. treeHasFocus = true;
  1306. // If no node has focus, focus the first item in the tree
  1307. if (focusedNode == null && lastSelection == null && selectable) {
  1308. setFocusedNode(getFirstRootNode(), false);
  1309. } else if (focusedNode != null && selectable) {
  1310. setFocusedNode(focusedNode, false);
  1311. } else if (lastSelection != null && selectable) {
  1312. setFocusedNode(lastSelection, false);
  1313. }
  1314. }
  1315. /*
  1316. * (non-Javadoc)
  1317. *
  1318. * @see
  1319. * com.google.gwt.event.dom.client.BlurHandler#onBlur(com.google.gwt.event
  1320. * .dom.client.BlurEvent)
  1321. */
  1322. public void onBlur(BlurEvent event) {
  1323. treeHasFocus = false;
  1324. if (focusedNode != null) {
  1325. focusedNode.setFocused(false);
  1326. }
  1327. }
  1328. /*
  1329. * (non-Javadoc)
  1330. *
  1331. * @see
  1332. * com.google.gwt.event.dom.client.KeyPressHandler#onKeyPress(com.google
  1333. * .gwt.event.dom.client.KeyPressEvent)
  1334. */
  1335. public void onKeyPress(KeyPressEvent event) {
  1336. NativeEvent nativeEvent = event.getNativeEvent();
  1337. int keyCode = nativeEvent.getKeyCode();
  1338. if (keyCode == 0 && nativeEvent.getCharCode() == ' ') {
  1339. // Provide a keyCode for space to be compatible with FireFox
  1340. // keypress event
  1341. keyCode = CHARCODE_SPACE;
  1342. }
  1343. if (handleKeyNavigation(keyCode,
  1344. event.isControlKeyDown() || event.isMetaKeyDown(),
  1345. event.isShiftKeyDown())) {
  1346. event.preventDefault();
  1347. event.stopPropagation();
  1348. }
  1349. }
  1350. /*
  1351. * (non-Javadoc)
  1352. *
  1353. * @see
  1354. * com.google.gwt.event.dom.client.KeyDownHandler#onKeyDown(com.google.gwt
  1355. * .event.dom.client.KeyDownEvent)
  1356. */
  1357. public void onKeyDown(KeyDownEvent event) {
  1358. if (handleKeyNavigation(event.getNativeEvent().getKeyCode(),
  1359. event.isControlKeyDown() || event.isMetaKeyDown(),
  1360. event.isShiftKeyDown())) {
  1361. event.preventDefault();
  1362. event.stopPropagation();
  1363. }
  1364. }
  1365. /**
  1366. * Handles the keyboard navigation
  1367. *
  1368. * @param keycode
  1369. * The keycode of the pressed key
  1370. * @param ctrl
  1371. * Was ctrl pressed
  1372. * @param shift
  1373. * Was shift pressed
  1374. * @return Returns true if the key was handled, else false
  1375. */
  1376. protected boolean handleKeyNavigation(int keycode, boolean ctrl,
  1377. boolean shift) {
  1378. // Navigate down
  1379. if (keycode == getNavigationDownKey()) {
  1380. TreeNode node = null;
  1381. // If node is open and has children then move in to the children
  1382. if (!focusedNode.isLeaf() && focusedNode.getState()
  1383. && focusedNode.getChildren().size() > 0) {
  1384. node = focusedNode.getChildren().get(0);
  1385. }
  1386. // Else move down to the next sibling
  1387. else {
  1388. node = getNextSibling(focusedNode);
  1389. if (node == null) {
  1390. // Else jump to the parent and try to select the next
  1391. // sibling there
  1392. TreeNode current = focusedNode;
  1393. while (node == null && current.getParentNode() != null) {
  1394. node = getNextSibling(current.getParentNode());
  1395. current = current.getParentNode();
  1396. }
  1397. }
  1398. }
  1399. if (node != null) {
  1400. setFocusedNode(node);
  1401. if (selectable) {
  1402. if (!ctrl && !shift) {
  1403. selectNode(node, true);
  1404. } else if (shift && isMultiselect) {
  1405. deselectAll();
  1406. selectNodeRange(lastSelection.key, node.key);
  1407. } else if (shift) {
  1408. selectNode(node, true);
  1409. }
  1410. }
  1411. }
  1412. return true;
  1413. }
  1414. // Navigate up
  1415. if (keycode == getNavigationUpKey()) {
  1416. TreeNode prev = getPreviousSibling(focusedNode);
  1417. TreeNode node = null;
  1418. if (prev != null) {
  1419. node = getLastVisibleChildInTree(prev);
  1420. } else if (focusedNode.getParentNode() != null) {
  1421. node = focusedNode.getParentNode();
  1422. }
  1423. if (node != null) {
  1424. setFocusedNode(node);
  1425. if (selectable) {
  1426. if (!ctrl && !shift) {
  1427. selectNode(node, true);
  1428. } else if (shift && isMultiselect) {
  1429. deselectAll();
  1430. selectNodeRange(lastSelection.key, node.key);
  1431. } else if (shift) {
  1432. selectNode(node, true);
  1433. }
  1434. }
  1435. }
  1436. return true;
  1437. }
  1438. // Navigate left (close branch)
  1439. if (keycode == getNavigationLeftKey()) {
  1440. if (!focusedNode.isLeaf() && focusedNode.getState()) {
  1441. focusedNode.setState(false, true);
  1442. } else if (focusedNode.getParentNode() != null
  1443. && (focusedNode.isLeaf() || !focusedNode.getState())) {
  1444. if (ctrl || !selectable) {
  1445. setFocusedNode(focusedNode.getParentNode());
  1446. } else if (shift) {
  1447. doRelationSelection(focusedNode.getParentNode(),
  1448. focusedNode);
  1449. setFocusedNode(focusedNode.getParentNode());
  1450. } else {
  1451. focusAndSelectNode(focusedNode.getParentNode());
  1452. }
  1453. }
  1454. return true;
  1455. }
  1456. // Navigate right (open branch)
  1457. if (keycode == getNavigationRightKey()) {
  1458. if (!focusedNode.isLeaf() && !focusedNode.getState()) {
  1459. focusedNode.setState(true, true);
  1460. } else if (!focusedNode.isLeaf()) {
  1461. if (ctrl || !selectable) {
  1462. setFocusedNode(focusedNode.getChildren().get(0));
  1463. } else if (shift) {
  1464. setSelected(focusedNode, true);
  1465. setFocusedNode(focusedNode.getChildren().get(0));
  1466. setSelected(focusedNode, true);
  1467. } else {
  1468. focusAndSelectNode(focusedNode.getChildren().get(0));
  1469. }
  1470. }
  1471. return true;
  1472. }
  1473. // Selection
  1474. if (keycode == getNavigationSelectKey()) {
  1475. if (!focusedNode.isSelected()) {
  1476. selectNode(
  1477. focusedNode,
  1478. (!isMultiselect || multiSelectMode == MULTISELECT_MODE_SIMPLE)
  1479. && selectable);
  1480. } else {
  1481. deselectNode(focusedNode);
  1482. }
  1483. return true;
  1484. }
  1485. // Home selection
  1486. if (keycode == getNavigationStartKey()) {
  1487. TreeNode node = getFirstRootNode();
  1488. if (ctrl || !selectable) {
  1489. setFocusedNode(node);
  1490. } else if (shift) {
  1491. deselectAll();
  1492. selectNodeRange(focusedNode.key, node.key);
  1493. } else {
  1494. selectNode(node, true);
  1495. }
  1496. sendSelectionToServer();
  1497. return true;
  1498. }
  1499. // End selection
  1500. if (keycode == getNavigationEndKey()) {
  1501. TreeNode lastNode = getLastRootNode();
  1502. TreeNode node = getLastVisibleChildInTree(lastNode);
  1503. if (ctrl || !selectable) {
  1504. setFocusedNode(node);
  1505. } else if (shift) {
  1506. deselectAll();
  1507. selectNodeRange(focusedNode.key, node.key);
  1508. } else {
  1509. selectNode(node, true);
  1510. }
  1511. sendSelectionToServer();
  1512. return true;
  1513. }
  1514. return false;
  1515. }
  1516. private void focusAndSelectNode(TreeNode node) {
  1517. /*
  1518. * Keyboard navigation doesn't work reliably if the tree is in
  1519. * multiselect mode as well as isNullSelectionAllowed = false. It first
  1520. * tries to deselect the old focused node, which fails since there must
  1521. * be at least one selection. After this the newly focused node is
  1522. * selected and we've ended up with two selected nodes even though we
  1523. * only navigated with the arrow keys.
  1524. *
  1525. * Because of this, we first select the next node and later de-select
  1526. * the old one.
  1527. */
  1528. TreeNode oldFocusedNode = focusedNode;
  1529. setFocusedNode(node);
  1530. setSelected(focusedNode, true);
  1531. setSelected(oldFocusedNode, false);
  1532. }
  1533. /**
  1534. * Traverses the tree to the bottom most child
  1535. *
  1536. * @param root
  1537. * The root of the tree
  1538. * @return The bottom most child
  1539. */
  1540. private TreeNode getLastVisibleChildInTree(TreeNode root) {
  1541. if (root.isLeaf() || !root.getState() || root.getChildren().size() == 0) {
  1542. return root;
  1543. }
  1544. List<TreeNode> children = root.getChildren();
  1545. return getLastVisibleChildInTree(children.get(children.size() - 1));
  1546. }
  1547. /**
  1548. * Gets the next sibling in the tree
  1549. *
  1550. * @param node
  1551. * The node to get the sibling for
  1552. * @return The sibling node or null if the node is the last sibling
  1553. */
  1554. private TreeNode getNextSibling(TreeNode node) {
  1555. TreeNode parent = node.getParentNode();
  1556. List<TreeNode> children;
  1557. if (parent == null) {
  1558. children = getRootNodes();
  1559. } else {
  1560. children = parent.getChildren();
  1561. }
  1562. int idx = children.indexOf(node);
  1563. if (idx < children.size() - 1) {
  1564. return children.get(idx + 1);
  1565. }
  1566. return null;
  1567. }
  1568. /**
  1569. * Returns the previous sibling in the tree
  1570. *
  1571. * @param node
  1572. * The node to get the sibling for
  1573. * @return The sibling node or null if the node is the first sibling
  1574. */
  1575. private TreeNode getPreviousSibling(TreeNode node) {
  1576. TreeNode parent = node.getParentNode();
  1577. List<TreeNode> children;
  1578. if (parent == null) {
  1579. children = getRootNodes();
  1580. } else {
  1581. children = parent.getChildren();
  1582. }
  1583. int idx = children.indexOf(node);
  1584. if (idx > 0) {
  1585. return children.get(idx - 1);
  1586. }
  1587. return null;
  1588. }
  1589. /**
  1590. * Add this to the element mouse down event by using element.setPropertyJSO
  1591. * ("onselectstart",applyDisableTextSelectionIEHack()); Remove it then again
  1592. * when the mouse is depressed in the mouse up event.
  1593. *
  1594. * @return Returns the JSO preventing text selection
  1595. */
  1596. private native JavaScriptObject applyDisableTextSelectionIEHack()
  1597. /*-{
  1598. return function(){ return false; };
  1599. }-*/;
  1600. /**
  1601. * Get the key that moves the selection head upwards. By default it is the
  1602. * up arrow key but by overriding this you can change the key to whatever
  1603. * you want.
  1604. *
  1605. * @return The keycode of the key
  1606. */
  1607. protected int getNavigationUpKey() {
  1608. return KeyCodes.KEY_UP;
  1609. }
  1610. /**
  1611. * Get the key that moves the selection head downwards. By default it is the
  1612. * down arrow key but by overriding this you can change the key to whatever
  1613. * you want.
  1614. *
  1615. * @return The keycode of the key
  1616. */
  1617. protected int getNavigationDownKey() {
  1618. return KeyCodes.KEY_DOWN;
  1619. }
  1620. /**
  1621. * Get the key that scrolls to the left in the table. By default it is the
  1622. * left arrow key but by overriding this you can change the key to whatever
  1623. * you want.
  1624. *
  1625. * @return The keycode of the key
  1626. */
  1627. protected int getNavigationLeftKey() {
  1628. return KeyCodes.KEY_LEFT;
  1629. }
  1630. /**
  1631. * Get the key that scroll to the right on the table. By default it is the
  1632. * right arrow key but by overriding this you can change the key to whatever
  1633. * you want.
  1634. *
  1635. * @return The keycode of the key
  1636. */
  1637. protected int getNavigationRightKey() {
  1638. return KeyCodes.KEY_RIGHT;
  1639. }
  1640. /**
  1641. * Get the key that selects an item in the table. By default it is the space
  1642. * bar key but by overriding this you can change the key to whatever you
  1643. * want.
  1644. *
  1645. * @return
  1646. */
  1647. protected int getNavigationSelectKey() {
  1648. return CHARCODE_SPACE;
  1649. }
  1650. /**
  1651. * Get the key the moves the selection one page up in the table. By default
  1652. * this is the Page Up key but by overriding this you can change the key to
  1653. * whatever you want.
  1654. *
  1655. * @return
  1656. */
  1657. protected int getNavigationPageUpKey() {
  1658. return KeyCodes.KEY_PAGEUP;
  1659. }
  1660. /**
  1661. * Get the key the moves the selection one page down in the table. By
  1662. * default this is the Page Down key but by overriding this you can change
  1663. * the key to whatever you want.
  1664. *
  1665. * @return
  1666. */
  1667. protected int getNavigationPageDownKey() {
  1668. return KeyCodes.KEY_PAGEDOWN;
  1669. }
  1670. /**
  1671. * Get the key the moves the selection to the beginning of the table. By
  1672. * default this is the Home key but by overriding this you can change the
  1673. * key to whatever you want.
  1674. *
  1675. * @return
  1676. */
  1677. protected int getNavigationStartKey() {
  1678. return KeyCodes.KEY_HOME;
  1679. }
  1680. /**
  1681. * Get the key the moves the selection to the end of the table. By default
  1682. * this is the End key but by overriding this you can change the key to
  1683. * whatever you want.
  1684. *
  1685. * @return
  1686. */
  1687. protected int getNavigationEndKey() {
  1688. return KeyCodes.KEY_END;
  1689. }
  1690. private final String SUBPART_NODE_PREFIX = "n";
  1691. private final String EXPAND_IDENTIFIER = "expand";
  1692. /*
  1693. * In webkit, focus may have been requested for this component but not yet
  1694. * gained. Use this to trac if tree has gained the focus on webkit. See
  1695. * FocusImplSafari and #6373
  1696. */
  1697. private boolean treeHasFocus;
  1698. /*
  1699. * (non-Javadoc)
  1700. *
  1701. * @see
  1702. * com.vaadin.terminal.gwt.client.ui.SubPartAware#getSubPartElement(java
  1703. * .lang.String)
  1704. */
  1705. public Element getSubPartElement(String subPart) {
  1706. if ("fe".equals(subPart)) {
  1707. if (BrowserInfo.get().isOpera() && focusedNode != null) {
  1708. return focusedNode.getElement();
  1709. }
  1710. return getFocusElement();
  1711. }
  1712. if (subPart.startsWith(SUBPART_NODE_PREFIX + "[")) {
  1713. boolean expandCollapse = false;
  1714. // Node
  1715. String[] nodes = subPart.split("/");
  1716. TreeNode treeNode = null;
  1717. try {
  1718. for (String node : nodes) {
  1719. if (node.startsWith(SUBPART_NODE_PREFIX)) {
  1720. // skip SUBPART_NODE_PREFIX"["
  1721. node = node.substring(SUBPART_NODE_PREFIX.length() + 1);
  1722. // skip "]"
  1723. node = node.substring(0, node.length() - 1);
  1724. int position = Integer.parseInt(node);
  1725. if (treeNode == null) {
  1726. treeNode = getRootNodes().get(position);
  1727. } else {
  1728. treeNode = treeNode.getChildren().get(position);
  1729. }
  1730. } else if (node.startsWith(EXPAND_IDENTIFIER)) {
  1731. expandCollapse = true;
  1732. }
  1733. }
  1734. if (expandCollapse) {
  1735. return treeNode.getElement();
  1736. } else {
  1737. return treeNode.nodeCaptionSpan;
  1738. }
  1739. } catch (Exception e) {
  1740. // Invalid locator string or node could not be found
  1741. return null;
  1742. }
  1743. }
  1744. return null;
  1745. }
  1746. /*
  1747. * (non-Javadoc)
  1748. *
  1749. * @see
  1750. * com.vaadin.terminal.gwt.client.ui.SubPartAware#getSubPartName(com.google
  1751. * .gwt.user.client.Element)
  1752. */
  1753. public String getSubPartName(Element subElement) {
  1754. // Supported identifiers:
  1755. //
  1756. // n[index]/n[index]/n[index]{/expand}
  1757. //
  1758. // Ends with "/expand" if the target is expand/collapse indicator,
  1759. // otherwise ends with the node
  1760. boolean isExpandCollapse = false;
  1761. if (!getElement().isOrHasChild(subElement)) {
  1762. return null;
  1763. }
  1764. if (subElement == getFocusElement()) {
  1765. return "fe";
  1766. }
  1767. TreeNode treeNode = Util.findWidget(subElement, TreeNode.class);
  1768. if (treeNode == null) {
  1769. // Did not click on a node, let somebody else take care of the
  1770. // locator string
  1771. return null;
  1772. }
  1773. if (subElement == treeNode.getElement()) {
  1774. // Targets expand/collapse arrow
  1775. isExpandCollapse = true;
  1776. }
  1777. ArrayList<Integer> positions = new ArrayList<Integer>();
  1778. while (treeNode.getParentNode() != null) {
  1779. positions.add(0,
  1780. treeNode.getParentNode().getChildren().indexOf(treeNode));
  1781. treeNode = treeNode.getParentNode();
  1782. }
  1783. positions.add(0, getRootNodes().indexOf(treeNode));
  1784. String locator = "";
  1785. for (Integer i : positions) {
  1786. locator += SUBPART_NODE_PREFIX + "[" + i + "]/";
  1787. }
  1788. locator = locator.substring(0, locator.length() - 1);
  1789. if (isExpandCollapse) {
  1790. locator += "/" + EXPAND_IDENTIFIER;
  1791. }
  1792. return locator;
  1793. }
  1794. public Action[] getActions() {
  1795. if (bodyActionKeys == null) {
  1796. return new Action[] {};
  1797. }
  1798. final Action[] actions = new Action[bodyActionKeys.length];
  1799. for (int i = 0; i < actions.length; i++) {
  1800. final String actionKey = bodyActionKeys[i];
  1801. final TreeAction a = new TreeAction(this, null, actionKey);
  1802. a.setCaption(getActionCaption(actionKey));
  1803. a.setIconUrl(getActionIcon(actionKey));
  1804. actions[i] = a;
  1805. }
  1806. return actions;
  1807. }
  1808. public ApplicationConnection getClient() {
  1809. return client;
  1810. }
  1811. public String getPaintableId() {
  1812. return paintableId;
  1813. }
  1814. private void handleBodyContextMenu(ContextMenuEvent event) {
  1815. if (!readonly && !disabled) {
  1816. if (bodyActionKeys != null) {
  1817. int left = event.getNativeEvent().getClientX();
  1818. int top = event.getNativeEvent().getClientY();
  1819. top += Window.getScrollTop();
  1820. left += Window.getScrollLeft();
  1821. client.getContextMenu().showAt(this, left, top);
  1822. }
  1823. event.stopPropagation();
  1824. event.preventDefault();
  1825. }
  1826. }
  1827. public void registerAction(String key, String caption, String iconUrl) {
  1828. actionMap.put(key + "_c", caption);
  1829. if (iconUrl != null) {
  1830. actionMap.put(key + "_i", iconUrl);
  1831. } else {
  1832. actionMap.remove(key + "_i");
  1833. }
  1834. }
  1835. public void registerNode(TreeNode treeNode) {
  1836. keyToNode.put(treeNode.key, treeNode);
  1837. }
  1838. public void clearNodeToKeyMap() {
  1839. keyToNode.clear();
  1840. }
  1841. }