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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.Iterator;
  8. import java.util.Set;
  9. import com.google.gwt.dom.client.NativeEvent;
  10. import com.google.gwt.dom.client.Style;
  11. import com.google.gwt.user.client.DOM;
  12. import com.google.gwt.user.client.Element;
  13. import com.google.gwt.user.client.Event;
  14. import com.google.gwt.user.client.EventListener;
  15. import com.google.gwt.user.client.Window;
  16. import com.google.gwt.user.client.ui.FlowPanel;
  17. import com.google.gwt.user.client.ui.SimplePanel;
  18. import com.google.gwt.user.client.ui.Widget;
  19. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  20. import com.vaadin.terminal.gwt.client.BrowserInfo;
  21. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  22. import com.vaadin.terminal.gwt.client.Paintable;
  23. import com.vaadin.terminal.gwt.client.UIDL;
  24. import com.vaadin.terminal.gwt.client.Util;
  25. import com.vaadin.terminal.gwt.client.ValueMap;
  26. import com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler;
  27. import com.vaadin.terminal.gwt.client.ui.dd.VAcceptCallback;
  28. import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager;
  29. import com.vaadin.terminal.gwt.client.ui.dd.VDragEvent;
  30. import com.vaadin.terminal.gwt.client.ui.dd.VDropHandler;
  31. import com.vaadin.terminal.gwt.client.ui.dd.VHasDropHandler;
  32. import com.vaadin.terminal.gwt.client.ui.dd.VTransferable;
  33. import com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager.DragEventType;
  34. /**
  35. *
  36. */
  37. public class VTree extends FlowPanel implements Paintable, VHasDropHandler {
  38. public static final String CLASSNAME = "v-tree";
  39. public static final String ITEM_CLICK_EVENT_ID = "itemClick";
  40. private Set<String> selectedIds = new HashSet<String>();
  41. private ApplicationConnection client;
  42. private String paintableId;
  43. private boolean selectable;
  44. private boolean isMultiselect;
  45. private String currentMouseOverKey;
  46. private final HashMap<String, TreeNode> keyToNode = new HashMap<String, TreeNode>();
  47. /**
  48. * This map contains captions and icon urls for actions like: * "33_c" ->
  49. * "Edit" * "33_i" -> "http://dom.com/edit.png"
  50. */
  51. private final HashMap<String, String> actionMap = new HashMap<String, String>();
  52. private boolean immediate;
  53. private boolean isNullSelectionAllowed = true;
  54. private boolean disabled = false;
  55. private boolean readonly;
  56. private boolean rendering;
  57. private int dragModes;
  58. private VAbstractDropHandler dropHandler;
  59. public VTree() {
  60. super();
  61. setStyleName(CLASSNAME);
  62. }
  63. private void updateActionMap(UIDL c) {
  64. final Iterator it = c.getChildIterator();
  65. while (it.hasNext()) {
  66. final UIDL action = (UIDL) it.next();
  67. final String key = action.getStringAttribute("key");
  68. final String caption = action.getStringAttribute("caption");
  69. actionMap.put(key + "_c", caption);
  70. if (action.hasAttribute("icon")) {
  71. // TODO need some uri handling ??
  72. actionMap.put(key + "_i", client.translateVaadinUri(action
  73. .getStringAttribute("icon")));
  74. }
  75. }
  76. }
  77. public String getActionCaption(String actionKey) {
  78. return actionMap.get(actionKey + "_c");
  79. }
  80. public String getActionIcon(String actionKey) {
  81. return actionMap.get(actionKey + "_i");
  82. }
  83. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  84. // Ensure correct implementation and let container manage caption
  85. if (client.updateComponent(this, uidl, true)) {
  86. return;
  87. }
  88. rendering = true;
  89. this.client = client;
  90. if (uidl.hasAttribute("partialUpdate")) {
  91. handleUpdate(uidl);
  92. rendering = false;
  93. return;
  94. }
  95. paintableId = uidl.getId();
  96. immediate = uidl.hasAttribute("immediate");
  97. disabled = uidl.getBooleanAttribute("disabled");
  98. readonly = uidl.getBooleanAttribute("readonly");
  99. isNullSelectionAllowed = uidl.getBooleanAttribute("nullselect");
  100. clear();
  101. for (final Iterator i = uidl.getChildIterator(); i.hasNext();) {
  102. final UIDL childUidl = (UIDL) i.next();
  103. if ("actions".equals(childUidl.getTag())) {
  104. updateActionMap(childUidl);
  105. continue;
  106. } else if ("dh".equals(childUidl.getTag())) {
  107. updateDropHandler(childUidl);
  108. continue;
  109. }
  110. final TreeNode childTree = new TreeNode();
  111. if (childTree.ie6compatnode != null) {
  112. this.add(childTree);
  113. }
  114. childTree.updateFromUIDL(childUidl, client);
  115. if (childTree.ie6compatnode == null) {
  116. this.add(childTree);
  117. }
  118. }
  119. final String selectMode = uidl.getStringAttribute("selectmode");
  120. selectable = !"none".equals(selectMode);
  121. isMultiselect = "multi".equals(selectMode);
  122. selectedIds = uidl.getStringArrayVariableAsSet("selected");
  123. if (uidl.hasAttribute("dragModes")) {
  124. dragModes = uidl.getIntAttribute("dragModes");
  125. }
  126. rendering = false;
  127. }
  128. private void updateTreeRelatedDragData(VDragEvent drag) {
  129. currentMouseOverKey = findCurrentMouseOverKey(drag.getElementOver());
  130. drag.getEventDetails().put("itemIdOver", currentMouseOverKey);
  131. if (currentMouseOverKey != null) {
  132. String detail = getDropDetail(drag.getCurrentGwtEvent());
  133. Boolean overTreeNode = null;
  134. if (!keyToNode.get(currentMouseOverKey).isLeaf()
  135. && "Center".equals(detail)) {
  136. overTreeNode = true;
  137. }
  138. drag.getEventDetails().put("itemIdOverIsNode", overTreeNode);
  139. drag.getEventDetails().put("detail", detail);
  140. }
  141. }
  142. private String findCurrentMouseOverKey(Element elementOver) {
  143. TreeNode treeNode = null;
  144. Element curEl = elementOver;
  145. while (curEl != null) {
  146. try {
  147. EventListener eventListener = Event.getEventListener(curEl);
  148. if (eventListener != null) {
  149. // found a widget
  150. if (eventListener instanceof TreeNode) {
  151. treeNode = (TreeNode) eventListener;
  152. }
  153. break;
  154. } else {
  155. curEl = (Element) curEl.getParentElement();
  156. }
  157. } catch (Exception e) {
  158. ApplicationConnection.getConsole().log(e.getMessage());
  159. e.printStackTrace();
  160. }
  161. }
  162. return treeNode == null ? null : treeNode.key;
  163. }
  164. private void updateDropHandler(UIDL childUidl) {
  165. if (dropHandler == null) {
  166. dropHandler = new VAbstractDropHandler() {
  167. @Override
  168. public void dragEnter(VDragEvent drag) {
  169. updateTreeRelatedDragData(drag);
  170. super.dragEnter(drag);
  171. }
  172. @Override
  173. protected void dragAccepted(final VDragEvent drag) {
  174. }
  175. @Override
  176. public void dragOver(final VDragEvent currentDrag) {
  177. final Object oldIdOver = currentDrag.getEventDetails().get(
  178. "itemIdOver");
  179. final String oldDetail = (String) currentDrag
  180. .getEventDetails().get("detail");
  181. updateTreeRelatedDragData(currentDrag);
  182. final String detail = getDropDetail(currentDrag
  183. .getCurrentGwtEvent());
  184. boolean nodeHasChanged = (currentMouseOverKey != null && currentMouseOverKey != oldIdOver)
  185. || (oldIdOver != null);
  186. boolean detailHasChanded = !detail.equals(oldDetail);
  187. if (nodeHasChanged || detailHasChanded) {
  188. ApplicationConnection.getConsole().log(
  189. "Change in Transferable " + currentMouseOverKey
  190. + " " + detail);
  191. VAcceptCallback accpectedCb = new VAcceptCallback() {
  192. public void handleResponse(ValueMap responseData) {
  193. if (responseData == null // via client
  194. // side
  195. // validation
  196. || responseData.containsKey("accepted")) {
  197. keyToNode.get(currentMouseOverKey)
  198. .emphasis(detail);
  199. }
  200. }
  201. };
  202. if (validateOnServer()) {
  203. VDragAndDropManager.get().visitServer(
  204. DragEventType.OVER, accpectedCb);
  205. } else {
  206. if (validates(currentDrag)) {
  207. accpectedCb.handleResponse(null);
  208. } else {
  209. keyToNode.get(currentMouseOverKey).emphasis(
  210. null);
  211. }
  212. if (oldIdOver != null
  213. && oldIdOver != currentMouseOverKey) {
  214. keyToNode.get(oldIdOver).emphasis(null);
  215. }
  216. }
  217. }
  218. }
  219. @Override
  220. public void dragLeave(VDragEvent drag) {
  221. cleanUp();
  222. }
  223. private void cleanUp() {
  224. if (currentMouseOverKey != null) {
  225. keyToNode.get(currentMouseOverKey).emphasis(null);
  226. currentMouseOverKey = null;
  227. }
  228. }
  229. @Override
  230. public boolean drop(VDragEvent drag) {
  231. cleanUp();
  232. return super.drop(drag);
  233. }
  234. @Override
  235. public Paintable getPaintable() {
  236. return VTree.this;
  237. }
  238. public ApplicationConnection getApplicationConnection() {
  239. return client;
  240. }
  241. };
  242. }
  243. dropHandler.updateRules(childUidl);
  244. }
  245. public String getDropDetail(NativeEvent event) {
  246. TreeNode treeNode = keyToNode.get(currentMouseOverKey);
  247. // TODO no scroll support
  248. int offsetHeight = treeNode.nodeCaptionDiv.getOffsetHeight();
  249. int absoluteTop = treeNode.getAbsoluteTop();
  250. int clientY = event.getClientY();
  251. int fromTop = clientY - absoluteTop;
  252. String detail;
  253. float percentageFromTop = (fromTop / (float) offsetHeight);
  254. if (percentageFromTop < 0.2) {
  255. detail = "Top";
  256. } else if (percentageFromTop > 0.8) {
  257. detail = "Bottom";
  258. } else {
  259. detail = "Center";
  260. }
  261. return detail;
  262. }
  263. private void handleUpdate(UIDL uidl) {
  264. final TreeNode rootNode = keyToNode.get(uidl
  265. .getStringAttribute("rootKey"));
  266. if (rootNode != null) {
  267. if (!rootNode.getState()) {
  268. // expanding node happened server side
  269. rootNode.setState(true, false);
  270. }
  271. rootNode.renderChildNodes(uidl.getChildIterator());
  272. }
  273. }
  274. public void setSelected(TreeNode treeNode, boolean selected) {
  275. if (selected) {
  276. if (!isMultiselect) {
  277. while (selectedIds.size() > 0) {
  278. final String id = selectedIds.iterator().next();
  279. final TreeNode oldSelection = keyToNode.get(id);
  280. if (oldSelection != null) {
  281. // can be null if the node is not visible (parent
  282. // collapsed)
  283. oldSelection.setSelected(false);
  284. }
  285. selectedIds.remove(id);
  286. }
  287. }
  288. treeNode.setSelected(true);
  289. selectedIds.add(treeNode.key);
  290. } else {
  291. if (!isNullSelectionAllowed) {
  292. if (!isMultiselect || selectedIds.size() == 1) {
  293. return;
  294. }
  295. }
  296. selectedIds.remove(treeNode.key);
  297. treeNode.setSelected(false);
  298. }
  299. client.updateVariable(paintableId, "selected", selectedIds
  300. .toArray(new String[selectedIds.size()]), immediate);
  301. }
  302. public boolean isSelected(TreeNode treeNode) {
  303. return selectedIds.contains(treeNode.key);
  304. }
  305. protected class TreeNode extends SimplePanel implements ActionOwner {
  306. public static final String CLASSNAME = "v-tree-node";
  307. String key;
  308. private String[] actionKeys = null;
  309. private boolean childrenLoaded;
  310. private Element nodeCaptionDiv;
  311. protected Element nodeCaptionSpan;
  312. private FlowPanel childNodeContainer;
  313. private boolean open;
  314. private Icon icon;
  315. private Element ie6compatnode;
  316. private Event mouseDownEvent;
  317. public TreeNode() {
  318. constructDom();
  319. sinkEvents(Event.ONCLICK | Event.ONDBLCLICK | Event.MOUSEEVENTS
  320. | Event.ONCONTEXTMENU);
  321. }
  322. public void emphasis(String string) {
  323. // ApplicationConnection.getConsole().log("OUTLINE" + string);
  324. Style style = nodeCaptionDiv.getStyle();
  325. String top = "Top".equals(string) ? "2px solid green" : "";
  326. String bottom = "Bottom".equals(string) ? "2px solid green" : "";
  327. String bg = "Center".equals(string) ? "green" : "";
  328. style.setProperty("borderTop", top);
  329. style.setProperty("borderBottom", bottom);
  330. style.setBackgroundColor(bg);
  331. }
  332. @Override
  333. public void onBrowserEvent(Event event) {
  334. super.onBrowserEvent(event);
  335. if (disabled) {
  336. return;
  337. }
  338. final int type = DOM.eventGetType(event);
  339. final Element target = DOM.eventGetTarget(event);
  340. if (client.hasEventListeners(VTree.this, ITEM_CLICK_EVENT_ID)
  341. && target == nodeCaptionSpan
  342. && (type == Event.ONDBLCLICK || type == Event.ONMOUSEUP)) {
  343. fireClick(event);
  344. }
  345. if (type == Event.ONCLICK) {
  346. if (getElement() == target || ie6compatnode == target) {
  347. // state change
  348. toggleState();
  349. } else if (!readonly && target == nodeCaptionSpan) {
  350. // caption click = selection change && possible click event
  351. toggleSelection();
  352. }
  353. DOM.eventCancelBubble(event, true);
  354. } else if (type == Event.ONCONTEXTMENU) {
  355. showContextMenu(event);
  356. }
  357. if (dragModes != 0 || dropHandler != null) {
  358. if (type == Event.ONMOUSEDOWN) {
  359. if (nodeCaptionDiv.isOrHasChild(event.getTarget())) {
  360. ApplicationConnection.getConsole().log(
  361. "TreeNode m down");
  362. event.preventDefault(); // prevent text selection
  363. mouseDownEvent = event;
  364. }
  365. } else if (type == Event.ONMOUSEMOVE
  366. || type == Event.ONMOUSEOUT) {
  367. if (mouseDownEvent != null) {
  368. ApplicationConnection.getConsole().log(
  369. "TreeNode drag start " + event.getType());
  370. // start actual drag on slight move when mouse is down
  371. VTransferable t = new VTransferable();
  372. t.setComponent(VTree.this);
  373. t.setItemId(key);
  374. VDragEvent drag = VDragAndDropManager.get().startDrag(
  375. t, mouseDownEvent, true);
  376. drag.createDragImage(nodeCaptionDiv, true);
  377. event.stopPropagation();
  378. mouseDownEvent = null;
  379. }
  380. } else if (type == Event.ONMOUSEUP) {
  381. mouseDownEvent = null;
  382. }
  383. if (type == Event.ONMOUSEOVER) {
  384. ApplicationConnection.getConsole().log(
  385. "Treenode mouse over");
  386. mouseDownEvent = null;
  387. currentMouseOverKey = key;
  388. event.stopPropagation();
  389. }
  390. }
  391. }
  392. private void fireClick(Event evt) {
  393. // non-immediate iff an immediate select event is going to happen
  394. boolean imm = !immediate
  395. || !selectable
  396. || (!isNullSelectionAllowed && isSelected() && selectedIds
  397. .size() == 1);
  398. MouseEventDetails details = new MouseEventDetails(evt);
  399. client.updateVariable(paintableId, "clickedKey", key, false);
  400. client.updateVariable(paintableId, "clickEvent",
  401. details.toString(), imm);
  402. }
  403. private void toggleSelection() {
  404. if (selectable) {
  405. VTree.this.setSelected(this, !isSelected());
  406. }
  407. }
  408. private void toggleState() {
  409. setState(!getState(), true);
  410. }
  411. protected void constructDom() {
  412. // workaround for a very weird IE6 issue #1245
  413. if (BrowserInfo.get().isIE6()) {
  414. ie6compatnode = DOM.createDiv();
  415. setStyleName(ie6compatnode, CLASSNAME + "-ie6compatnode");
  416. DOM.setInnerText(ie6compatnode, " ");
  417. DOM.appendChild(getElement(), ie6compatnode);
  418. DOM.sinkEvents(ie6compatnode, Event.ONCLICK);
  419. }
  420. nodeCaptionDiv = DOM.createDiv();
  421. DOM.setElementProperty(nodeCaptionDiv, "className", CLASSNAME
  422. + "-caption");
  423. Element wrapper = DOM.createDiv();
  424. nodeCaptionSpan = DOM.createSpan();
  425. DOM.appendChild(getElement(), nodeCaptionDiv);
  426. DOM.appendChild(nodeCaptionDiv, wrapper);
  427. DOM.appendChild(wrapper, nodeCaptionSpan);
  428. childNodeContainer = new FlowPanel();
  429. childNodeContainer.setStylePrimaryName(CLASSNAME + "-children");
  430. setWidget(childNodeContainer);
  431. }
  432. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  433. setText(uidl.getStringAttribute("caption"));
  434. key = uidl.getStringAttribute("key");
  435. keyToNode.put(key, this);
  436. if (uidl.hasAttribute("al")) {
  437. actionKeys = uidl.getStringArrayAttribute("al");
  438. }
  439. if (uidl.getTag().equals("node")) {
  440. if (uidl.getChildCount() == 0) {
  441. childNodeContainer.setVisible(false);
  442. } else {
  443. renderChildNodes(uidl.getChildIterator());
  444. childrenLoaded = true;
  445. }
  446. } else {
  447. addStyleName(CLASSNAME + "-leaf");
  448. }
  449. addStyleName(CLASSNAME);
  450. if (uidl.hasAttribute("style")) {
  451. addStyleName(CLASSNAME + "-" + uidl.getStringAttribute("style"));
  452. Widget.setStyleName(nodeCaptionDiv, CLASSNAME + "-caption-"
  453. + uidl.getStringAttribute("style"), true);
  454. childNodeContainer.addStyleName(CLASSNAME + "-children-"
  455. + uidl.getStringAttribute("style"));
  456. }
  457. if (uidl.getBooleanAttribute("expanded") && !getState()) {
  458. setState(true, false);
  459. }
  460. if (uidl.getBooleanAttribute("selected")) {
  461. setSelected(true);
  462. // ensure that identifier is in selectedIds array (this may be a
  463. // partial update)
  464. selectedIds.add(key);
  465. }
  466. if (uidl.hasAttribute("icon")) {
  467. if (icon == null) {
  468. icon = new Icon(client);
  469. DOM.insertBefore(DOM.getFirstChild(nodeCaptionDiv), icon
  470. .getElement(), nodeCaptionSpan);
  471. }
  472. icon.setUri(uidl.getStringAttribute("icon"));
  473. } else {
  474. if (icon != null) {
  475. DOM.removeChild(DOM.getFirstChild(nodeCaptionDiv), icon
  476. .getElement());
  477. icon = null;
  478. }
  479. }
  480. if (BrowserInfo.get().isIE6() && isAttached()) {
  481. fixWidth();
  482. }
  483. }
  484. public boolean isLeaf() {
  485. return getStyleName().contains("leaf");
  486. }
  487. private void setState(boolean state, boolean notifyServer) {
  488. if (open == state) {
  489. return;
  490. }
  491. if (state) {
  492. if (!childrenLoaded && notifyServer) {
  493. client.updateVariable(paintableId, "requestChildTree",
  494. true, false);
  495. }
  496. if (notifyServer) {
  497. client.updateVariable(paintableId, "expand",
  498. new String[] { key }, true);
  499. }
  500. addStyleName(CLASSNAME + "-expanded");
  501. childNodeContainer.setVisible(true);
  502. } else {
  503. removeStyleName(CLASSNAME + "-expanded");
  504. childNodeContainer.setVisible(false);
  505. if (notifyServer) {
  506. client.updateVariable(paintableId, "collapse",
  507. new String[] { key }, true);
  508. }
  509. }
  510. open = state;
  511. if (!rendering) {
  512. Util.notifyParentOfSizeChange(VTree.this, false);
  513. }
  514. }
  515. private boolean getState() {
  516. return open;
  517. }
  518. private void setText(String text) {
  519. DOM.setInnerText(nodeCaptionSpan, text);
  520. }
  521. private void renderChildNodes(Iterator i) {
  522. childNodeContainer.clear();
  523. childNodeContainer.setVisible(true);
  524. while (i.hasNext()) {
  525. final UIDL childUidl = (UIDL) i.next();
  526. // actions are in bit weird place, don't mix them with children,
  527. // but current node's actions
  528. if ("actions".equals(childUidl.getTag())) {
  529. updateActionMap(childUidl);
  530. continue;
  531. }
  532. final TreeNode childTree = new TreeNode();
  533. if (ie6compatnode != null) {
  534. childNodeContainer.add(childTree);
  535. }
  536. childTree.updateFromUIDL(childUidl, client);
  537. if (ie6compatnode == null) {
  538. childNodeContainer.add(childTree);
  539. }
  540. }
  541. childrenLoaded = true;
  542. }
  543. public boolean isChildrenLoaded() {
  544. return childrenLoaded;
  545. }
  546. public Action[] getActions() {
  547. if (actionKeys == null) {
  548. return new Action[] {};
  549. }
  550. final Action[] actions = new Action[actionKeys.length];
  551. for (int i = 0; i < actions.length; i++) {
  552. final String actionKey = actionKeys[i];
  553. final TreeAction a = new TreeAction(this, String.valueOf(key),
  554. actionKey);
  555. a.setCaption(getActionCaption(actionKey));
  556. a.setIconUrl(getActionIcon(actionKey));
  557. actions[i] = a;
  558. }
  559. return actions;
  560. }
  561. public ApplicationConnection getClient() {
  562. return client;
  563. }
  564. public String getPaintableId() {
  565. return paintableId;
  566. }
  567. /**
  568. * Adds/removes Vaadin specific style name. This method ought to be
  569. * called only from VTree.
  570. *
  571. * @param selected
  572. */
  573. protected void setSelected(boolean selected) {
  574. // add style name to caption dom structure only, not to subtree
  575. setStyleName(nodeCaptionDiv, "v-tree-node-selected", selected);
  576. }
  577. protected boolean isSelected() {
  578. return VTree.this.isSelected(this);
  579. }
  580. public void showContextMenu(Event event) {
  581. if (!readonly && !disabled) {
  582. if (actionKeys != null) {
  583. int left = event.getClientX();
  584. int top = event.getClientY();
  585. top += Window.getScrollTop();
  586. left += Window.getScrollLeft();
  587. client.getContextMenu().showAt(this, left, top);
  588. }
  589. event.cancelBubble(true);
  590. event.preventDefault();
  591. }
  592. }
  593. /*
  594. * We need to fix the width of TreeNodes so that the float in
  595. * ie6compatNode does not wrap (see ticket #1245)
  596. */
  597. private void fixWidth() {
  598. nodeCaptionDiv.getStyle().setProperty("styleFloat", "left");
  599. nodeCaptionDiv.getStyle().setProperty("display", "inline");
  600. nodeCaptionDiv.getStyle().setProperty("marginLeft", "0");
  601. final int captionWidth = ie6compatnode.getOffsetWidth()
  602. + nodeCaptionDiv.getOffsetWidth();
  603. setWidth(captionWidth + "px");
  604. }
  605. @Override
  606. public void onAttach() {
  607. super.onAttach();
  608. if (ie6compatnode != null) {
  609. fixWidth();
  610. }
  611. }
  612. }
  613. public VDropHandler getDropHandler() {
  614. return dropHandler;
  615. }
  616. }