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.

TreeConnector.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.client.ui.tree;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import com.google.gwt.aria.client.Roles;
  22. import com.google.gwt.dom.client.Element;
  23. import com.google.gwt.dom.client.EventTarget;
  24. import com.vaadin.client.ApplicationConnection;
  25. import com.vaadin.client.BrowserInfo;
  26. import com.vaadin.client.Paintable;
  27. import com.vaadin.client.TooltipInfo;
  28. import com.vaadin.client.UIDL;
  29. import com.vaadin.client.VConsole;
  30. import com.vaadin.client.WidgetUtil;
  31. import com.vaadin.client.communication.StateChangeEvent;
  32. import com.vaadin.shared.MouseEventDetails;
  33. import com.vaadin.shared.ui.Connect;
  34. import com.vaadin.shared.ui.MultiSelectMode;
  35. import com.vaadin.v7.client.ui.AbstractLegacyComponentConnector;
  36. import com.vaadin.v7.client.ui.VTree;
  37. import com.vaadin.v7.client.ui.VTree.TreeNode;
  38. import com.vaadin.v7.shared.ui.tree.TreeConstants;
  39. import com.vaadin.v7.shared.ui.tree.TreeServerRpc;
  40. import com.vaadin.v7.shared.ui.tree.TreeState;
  41. import com.vaadin.v7.ui.Tree;
  42. @Connect(Tree.class)
  43. public class TreeConnector extends AbstractLegacyComponentConnector
  44. implements Paintable {
  45. protected final Map<TreeNode, TooltipInfo> tooltipMap = new HashMap<TreeNode, TooltipInfo>();
  46. @Override
  47. protected void init() {
  48. getWidget().connector = this;
  49. }
  50. @Override
  51. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  52. if (!isRealUpdate(uidl)) {
  53. return;
  54. }
  55. getWidget().rendering = true;
  56. getWidget().client = client;
  57. if (uidl.hasAttribute("partialUpdate")) {
  58. handleUpdate(uidl);
  59. // IE8 is no longer supported with Vaadin 8
  60. getWidget().rendering = false;
  61. return;
  62. }
  63. getWidget().paintableId = uidl.getId();
  64. getWidget().immediate = getState().immediate;
  65. getWidget().disabled = !isEnabled();
  66. getWidget().readonly = isReadOnly();
  67. getWidget().dragMode = uidl.hasAttribute("dragMode")
  68. ? uidl.getIntAttribute("dragMode")
  69. : 0;
  70. getWidget().isNullSelectionAllowed = uidl
  71. .getBooleanAttribute("nullselect");
  72. getWidget().isHtmlContentAllowed = uidl
  73. .getBooleanAttribute(TreeConstants.ATTRIBUTE_HTML_ALLOWED);
  74. if (uidl.hasAttribute("alb")) {
  75. getWidget().bodyActionKeys = uidl.getStringArrayAttribute("alb");
  76. }
  77. getWidget().body.clear();
  78. // clear out any references to nodes that no longer are attached
  79. getWidget().clearNodeToKeyMap();
  80. tooltipMap.clear();
  81. TreeNode childTree = null;
  82. UIDL childUidl = null;
  83. for (final Object child : uidl) {
  84. childUidl = (UIDL) child;
  85. if ("actions".equals(childUidl.getTag())) {
  86. updateActionMap(childUidl);
  87. continue;
  88. } else if ("-ac".equals(childUidl.getTag())) {
  89. getWidget().updateDropHandler(childUidl);
  90. continue;
  91. }
  92. childTree = getWidget().new TreeNode();
  93. getConnection().getVTooltip().connectHandlersToWidget(childTree);
  94. updateNodeFromUIDL(childTree, childUidl, 1);
  95. getWidget().body.add(childTree);
  96. childTree.addStyleDependentName("root");
  97. childTree.childNodeContainer.addStyleDependentName("root");
  98. }
  99. if (childTree != null && childUidl != null) {
  100. boolean leaf = !childUidl.getTag().equals("node");
  101. childTree.addStyleDependentName(leaf ? "leaf-last" : "last");
  102. childTree.childNodeContainer.addStyleDependentName("last");
  103. }
  104. final String selectMode = uidl.getStringAttribute("selectmode");
  105. getWidget().selectable = !"none".equals(selectMode);
  106. getWidget().isMultiselect = "multi".equals(selectMode);
  107. if (getWidget().isMultiselect) {
  108. Roles.getTreeRole().setAriaMultiselectableProperty(
  109. getWidget().getElement(), true);
  110. if (BrowserInfo.get().isTouchDevice()) {
  111. // Always use the simple mode for touch devices that do not have
  112. // shift/ctrl keys (#8595)
  113. getWidget().multiSelectMode = MultiSelectMode.SIMPLE;
  114. } else {
  115. getWidget().multiSelectMode = MultiSelectMode
  116. .valueOf(uidl.getStringAttribute("multiselectmode"));
  117. }
  118. } else {
  119. Roles.getTreeRole().setAriaMultiselectableProperty(
  120. getWidget().getElement(), false);
  121. }
  122. getWidget().selectedIds = uidl.getStringArrayVariableAsSet("selected");
  123. // Update lastSelection and focusedNode to point to *actual* nodes again
  124. // after the old ones have been cleared from the body. This fixes focus
  125. // and keyboard navigation issues as described in #7057 and other
  126. // tickets.
  127. if (getWidget().lastSelection != null) {
  128. getWidget().lastSelection = getWidget()
  129. .getNodeByKey(getWidget().lastSelection.key);
  130. }
  131. if (getWidget().focusedNode != null) {
  132. Set<String> selectedIds = getWidget().selectedIds;
  133. // If the focused node is not between the selected nodes, we need to
  134. // refresh the focused node to prevent an undesired scroll. #12618.
  135. if (!selectedIds.isEmpty()
  136. && !selectedIds.contains(getWidget().focusedNode.key)) {
  137. String keySelectedId = selectedIds.iterator().next();
  138. TreeNode nodeToSelect = getWidget().getNodeByKey(keySelectedId);
  139. getWidget().setFocusedNode(nodeToSelect);
  140. } else {
  141. getWidget().setFocusedNode(
  142. getWidget().getNodeByKey(getWidget().focusedNode.key));
  143. }
  144. }
  145. if (getWidget().lastSelection == null && getWidget().focusedNode == null
  146. && !getWidget().selectedIds.isEmpty()) {
  147. getWidget().setFocusedNode(getWidget()
  148. .getNodeByKey(getWidget().selectedIds.iterator().next()));
  149. getWidget().focusedNode.setFocused(false);
  150. }
  151. // IE8 is no longer supported with Vaadin 8
  152. getWidget().rendering = false;
  153. }
  154. @Override
  155. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  156. super.onStateChanged(stateChangeEvent);
  157. // VTree does not implement Focusable
  158. getWidget().setTabIndex(getState().tabIndex);
  159. }
  160. @Override
  161. public VTree getWidget() {
  162. return (VTree) super.getWidget();
  163. }
  164. private void handleUpdate(UIDL uidl) {
  165. final TreeNode rootNode = getWidget()
  166. .getNodeByKey(uidl.getStringAttribute("rootKey"));
  167. if (rootNode != null) {
  168. if (!rootNode.getState()) {
  169. // expanding node happened server side
  170. rootNode.setState(true, false);
  171. }
  172. String levelPropertyString = Roles.getTreeitemRole()
  173. .getAriaLevelProperty(rootNode.getElement());
  174. int levelProperty;
  175. try {
  176. levelProperty = Integer.valueOf(levelPropertyString);
  177. } catch (NumberFormatException e) {
  178. levelProperty = 1;
  179. VConsole.error(e);
  180. }
  181. renderChildNodes(rootNode, (Iterator) uidl.iterator(),
  182. levelProperty + 1);
  183. }
  184. }
  185. /**
  186. * Registers action for the root and also for individual nodes
  187. *
  188. * @param uidl
  189. */
  190. private void updateActionMap(UIDL uidl) {
  191. for (final Object child : uidl) {
  192. final UIDL action = (UIDL) child;
  193. final String key = action.getStringAttribute("key");
  194. final String caption = action
  195. .getStringAttribute(TreeConstants.ATTRIBUTE_ACTION_CAPTION);
  196. String iconUrl = null;
  197. if (action.hasAttribute(TreeConstants.ATTRIBUTE_ACTION_ICON)) {
  198. iconUrl = getConnection()
  199. .translateVaadinUri(action.getStringAttribute(
  200. TreeConstants.ATTRIBUTE_ACTION_ICON));
  201. }
  202. getWidget().registerAction(key, caption, iconUrl);
  203. }
  204. }
  205. public void updateNodeFromUIDL(TreeNode treeNode, UIDL uidl, int level) {
  206. Roles.getTreeitemRole().setAriaLevelProperty(treeNode.getElement(),
  207. level);
  208. String nodeKey = uidl.getStringAttribute("key");
  209. String caption = uidl
  210. .getStringAttribute(TreeConstants.ATTRIBUTE_NODE_CAPTION);
  211. if (getWidget().isHtmlContentAllowed) {
  212. treeNode.setHtml(caption);
  213. } else {
  214. treeNode.setText(caption);
  215. }
  216. treeNode.key = nodeKey;
  217. getWidget().registerNode(treeNode);
  218. if (uidl.hasAttribute("al")) {
  219. treeNode.actionKeys = uidl.getStringArrayAttribute("al");
  220. }
  221. if (uidl.getTag().equals("node")) {
  222. if (uidl.getChildCount() == 0) {
  223. treeNode.childNodeContainer.setVisible(false);
  224. } else {
  225. renderChildNodes(treeNode, (Iterator) uidl.iterator(),
  226. level + 1);
  227. treeNode.childrenLoaded = true;
  228. }
  229. } else {
  230. treeNode.addStyleName(TreeNode.CLASSNAME + "-leaf");
  231. }
  232. if (uidl.hasAttribute(TreeConstants.ATTRIBUTE_NODE_STYLE)) {
  233. treeNode.setNodeStyleName(uidl
  234. .getStringAttribute(TreeConstants.ATTRIBUTE_NODE_STYLE));
  235. }
  236. String description = uidl.getStringAttribute("descr");
  237. if (description != null) {
  238. tooltipMap.put(treeNode,
  239. new TooltipInfo(description, null, treeNode));
  240. }
  241. if (uidl.getBooleanAttribute("expanded") && !treeNode.getState()) {
  242. treeNode.setState(true, false);
  243. }
  244. if (uidl.getBooleanAttribute("selected")) {
  245. treeNode.setSelected(true);
  246. // ensure that identifier is in selectedIds array (this may be a
  247. // partial update)
  248. getWidget().selectedIds.add(nodeKey);
  249. }
  250. String iconUrl = uidl
  251. .getStringAttribute(TreeConstants.ATTRIBUTE_NODE_ICON);
  252. String iconAltText = uidl
  253. .getStringAttribute(TreeConstants.ATTRIBUTE_NODE_ICON_ALT);
  254. treeNode.setIcon(iconUrl, iconAltText);
  255. }
  256. void renderChildNodes(TreeNode containerNode, Iterator<UIDL> i, int level) {
  257. containerNode.childNodeContainer.clear();
  258. containerNode.childNodeContainer.setVisible(true);
  259. while (i.hasNext()) {
  260. final UIDL childUidl = i.next();
  261. // actions are in bit weird place, don't mix them with children,
  262. // but current node's actions
  263. if ("actions".equals(childUidl.getTag())) {
  264. updateActionMap(childUidl);
  265. continue;
  266. }
  267. final TreeNode childTree = getWidget().new TreeNode();
  268. getConnection().getVTooltip().connectHandlersToWidget(childTree);
  269. updateNodeFromUIDL(childTree, childUidl, level);
  270. containerNode.childNodeContainer.add(childTree);
  271. if (!i.hasNext()) {
  272. childTree.addStyleDependentName(
  273. childTree.isLeaf() ? "leaf-last" : "last");
  274. childTree.childNodeContainer.addStyleDependentName("last");
  275. }
  276. }
  277. containerNode.childrenLoaded = true;
  278. }
  279. @Override
  280. public boolean isReadOnly() {
  281. return super.isReadOnly() || getState().propertyReadOnly;
  282. }
  283. @Override
  284. public TreeState getState() {
  285. return (TreeState) super.getState();
  286. }
  287. @Override
  288. public TooltipInfo getTooltipInfo(Element element) {
  289. TooltipInfo info = null;
  290. // Try to find a tooltip for a node
  291. if (element != getWidget().getElement()) {
  292. Object node = WidgetUtil.findWidget(element, TreeNode.class);
  293. if (node != null) {
  294. TreeNode tnode = (TreeNode) node;
  295. if (tnode.isCaptionElement(element)) {
  296. info = tooltipMap.get(tnode);
  297. }
  298. }
  299. }
  300. // If no tooltip found for the node or if the target was not a node, use
  301. // the default tooltip
  302. if (info == null) {
  303. info = super.getTooltipInfo(element);
  304. }
  305. return info;
  306. }
  307. @Override
  308. public boolean hasTooltip() {
  309. /*
  310. * Item tooltips are not processed until updateFromUIDL, so we can't be
  311. * sure that there are no tooltips during onStateChange when this method
  312. * is used.
  313. */
  314. return true;
  315. }
  316. @Override
  317. protected void sendContextClickEvent(MouseEventDetails details,
  318. EventTarget eventTarget) {
  319. if (!Element.is(eventTarget)) {
  320. return;
  321. }
  322. Element e = Element.as(eventTarget);
  323. String key = null;
  324. if (getWidget().body.getElement().isOrHasChild(e)) {
  325. TreeNode t = WidgetUtil.findWidget(e, TreeNode.class);
  326. if (t != null) {
  327. key = t.key;
  328. }
  329. }
  330. getRpcProxy(TreeServerRpc.class).contextClick(key, details);
  331. WidgetUtil.clearTextSelection();
  332. }
  333. }