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.

ITree.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package com.itmill.toolkit.terminal.gwt.client.ui;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6. import com.google.gwt.user.client.DOM;
  7. import com.google.gwt.user.client.Element;
  8. import com.google.gwt.user.client.Event;
  9. import com.google.gwt.user.client.Window;
  10. import com.google.gwt.user.client.ui.Tree;
  11. import com.google.gwt.user.client.ui.TreeItem;
  12. import com.google.gwt.user.client.ui.TreeListener;
  13. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  14. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  15. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  16. import com.itmill.toolkit.terminal.gwt.client.Util;
  17. /**
  18. * TODO dump GWT's Tree implementation and use Toolkit 4 style TODO update node
  19. * close/opens to server (even if no content fetch is needed)
  20. *
  21. * DOM structure
  22. *
  23. */
  24. public class ITree extends Tree implements Paintable {
  25. public static final String CLASSNAME = "i-tree";
  26. Set selectedIds = new HashSet();
  27. ApplicationConnection client;
  28. String paintableId;
  29. private boolean selectable;
  30. private boolean multiselect;
  31. private HashMap keyToNode = new HashMap();
  32. /**
  33. * This map contains captions and icon urls for actions like: * "33_c" ->
  34. * "Edit" * "33_i" -> "http://dom.com/edit.png"
  35. */
  36. private HashMap actionMap = new HashMap();
  37. private boolean immediate;
  38. public ITree() {
  39. super();
  40. setStyleName(CLASSNAME);
  41. }
  42. private void updateActionMap(UIDL c) {
  43. Iterator it = c.getChildIterator();
  44. while (it.hasNext()) {
  45. UIDL action = (UIDL) it.next();
  46. String key = action.getStringAttribute("key");
  47. String caption = action.getStringAttribute("caption");
  48. actionMap.put(key + "_c", caption);
  49. if (action.hasAttribute("icon")) {
  50. // TODO need some uri handling ??
  51. actionMap.put(key + "_i", action.getStringAttribute("icon"));
  52. }
  53. }
  54. }
  55. public String getActionCaption(String actionKey) {
  56. return (String) actionMap.get(actionKey + "_c");
  57. }
  58. public String getActionIcon(String actionKey) {
  59. return (String) actionMap.get(actionKey + "_i");
  60. }
  61. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  62. // Ensure correct implementation and let container manage caption
  63. if (client.updateComponent(this, uidl, true))
  64. return;
  65. this.client = client;
  66. if (uidl.hasAttribute("partialUpdate")) {
  67. handleUpdate(uidl);
  68. return;
  69. }
  70. this.paintableId = uidl.getId();
  71. this.immediate = uidl.hasAttribute("immediate");
  72. clear();
  73. for (Iterator i = uidl.getChildIterator(); i.hasNext();) {
  74. UIDL childUidl = (UIDL) i.next();
  75. if ("actions".equals(childUidl.getTag())) {
  76. updateActionMap(childUidl);
  77. continue;
  78. }
  79. TreeNode childTree = new TreeNode();
  80. addItem(childTree);
  81. childTree.updateFromUIDL(childUidl, client);
  82. }
  83. String selectMode = uidl.getStringAttribute("selectmode");
  84. selectable = selectMode != null;
  85. multiselect = "multi".equals(selectMode);
  86. addTreeListener(new TreeListener() {
  87. public void onTreeItemStateChanged(TreeItem item) {
  88. if (item instanceof TreeNode) {
  89. TreeNode tn = (TreeNode) item;
  90. if (item.getState()) {
  91. if (!tn.isChildrenLoaded()) {
  92. String key = tn.key;
  93. ITree.this.client.updateVariable(paintableId,
  94. "expand", new String[] { key }, true);
  95. }
  96. } else {
  97. // TODO collapse
  98. }
  99. }
  100. }
  101. public void onTreeItemSelected(TreeItem item) {
  102. TreeNode n = ((TreeNode) item);
  103. if (!selectable)
  104. return;
  105. String key = n.key;
  106. if (key != null) {
  107. if (selectedIds.contains(key) && multiselect) {
  108. selectedIds.remove(key);
  109. n.setISelected(false);
  110. } else {
  111. if (!multiselect) {
  112. selectedIds.clear();
  113. }
  114. selectedIds.add(key);
  115. n.setISelected(true);
  116. }
  117. ITree.this.client.updateVariable(ITree.this.paintableId,
  118. "selected", selectedIds.toArray(), immediate);
  119. }
  120. }
  121. });
  122. selectedIds = uidl.getStringArrayVariableAsSet("selected");
  123. }
  124. private void handleUpdate(UIDL uidl) {
  125. TreeNode rootNode = (TreeNode) keyToNode.get(uidl
  126. .getStringAttribute("rootKey"));
  127. if (rootNode != null) {
  128. rootNode.renderChildNodes(uidl.getChildIterator());
  129. }
  130. }
  131. private class TreeNode extends TreeItem implements ActionOwner {
  132. String key;
  133. boolean isLeaf = false;
  134. private String[] actionKeys = null;
  135. private boolean childrenLoaded;
  136. public TreeNode() {
  137. super();
  138. attachContextMenuEvent(getElement());
  139. }
  140. public void remove() {
  141. Util.removeContextMenuEvent(getElement());
  142. super.remove();
  143. }
  144. public void setSelected(boolean selected) {
  145. if (!selected && !ITree.this.multiselect) {
  146. this.setISelected(false);
  147. }
  148. super.setSelected(selected);
  149. }
  150. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  151. this.setText(uidl.getStringAttribute("caption"));
  152. key = uidl.getStringAttribute("key");
  153. keyToNode.put(key, this);
  154. if (uidl.hasAttribute("al"))
  155. actionKeys = uidl.getStringArrayAttribute("al");
  156. if (uidl.getTag().equals("node")) {
  157. isLeaf = false;
  158. if (uidl.getChidlCount() == 0) {
  159. TreeNode childTree = new TreeNode();
  160. childTree.setText("Loading...");
  161. childrenLoaded = false;
  162. this.addItem(childTree);
  163. } else {
  164. renderChildNodes(uidl.getChildIterator());
  165. }
  166. } else {
  167. isLeaf = true;
  168. }
  169. if (uidl.getBooleanAttribute("expanded") && !getState()) {
  170. setState(true);
  171. }
  172. setSelected(uidl.getBooleanAttribute("selected"));
  173. }
  174. private void renderChildNodes(Iterator i) {
  175. removeItems();
  176. while (i.hasNext()) {
  177. UIDL childUidl = (UIDL) i.next();
  178. if ("actions".equals(childUidl.getTag())) {
  179. updateActionMap(childUidl);
  180. continue;
  181. }
  182. TreeNode childTree = new TreeNode();
  183. this.addItem(childTree);
  184. childTree.updateFromUIDL(childUidl, client);
  185. }
  186. childrenLoaded = true;
  187. }
  188. public boolean isChildrenLoaded() {
  189. return childrenLoaded;
  190. }
  191. public Action[] getActions() {
  192. if (actionKeys == null)
  193. return new Action[] {};
  194. Action[] actions = new Action[actionKeys.length];
  195. for (int i = 0; i < actions.length; i++) {
  196. String actionKey = actionKeys[i];
  197. TreeAction a = new TreeAction(this, String.valueOf(key),
  198. actionKey);
  199. a.setCaption(getActionCaption(actionKey));
  200. a.setIconUrl(getActionIcon(actionKey));
  201. actions[i] = a;
  202. }
  203. return actions;
  204. }
  205. public ApplicationConnection getClient() {
  206. return client;
  207. }
  208. public String getPaintableId() {
  209. return paintableId;
  210. }
  211. /**
  212. * Adds/removes IT Mill Toolkit spesific style name. (GWT treenode does
  213. * not support multiselects)
  214. *
  215. * @param selected
  216. */
  217. public void setISelected(boolean selected) {
  218. setStyleName(getElement(), "i-tree-node-selected", selected);
  219. }
  220. public void showContextMenu(Event event) {
  221. if (actionKeys != null) {
  222. int left = DOM.eventGetClientX(event);
  223. int top = DOM.eventGetClientY(event);
  224. top += Window.getScrollTop();
  225. left += Window.getScrollLeft();
  226. client.getContextMenu().showAt(this, left, top);
  227. }
  228. DOM.eventCancelBubble(event, true);
  229. }
  230. private native void attachContextMenuEvent(Element el)
  231. /*-{
  232. var node = this;
  233. el.oncontextmenu = function(e) {
  234. if(!e)
  235. e = $wnd.event;
  236. node.@com.itmill.toolkit.terminal.gwt.client.ui.ITree.TreeNode::showContextMenu(Lcom/google/gwt/user/client/Event;)(e);
  237. return false;
  238. };
  239. }-*/;
  240. }
  241. }