Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

VTree.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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.user.client.DOM;
  10. import com.google.gwt.user.client.Element;
  11. import com.google.gwt.user.client.Event;
  12. import com.google.gwt.user.client.Window;
  13. import com.google.gwt.user.client.ui.FlowPanel;
  14. import com.google.gwt.user.client.ui.SimplePanel;
  15. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  16. import com.vaadin.terminal.gwt.client.BrowserInfo;
  17. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  18. import com.vaadin.terminal.gwt.client.Paintable;
  19. import com.vaadin.terminal.gwt.client.UIDL;
  20. import com.vaadin.terminal.gwt.client.Util;
  21. /**
  22. *
  23. */
  24. public class VTree extends FlowPanel implements Paintable {
  25. public static final String CLASSNAME = "i-tree";
  26. private Set<String> selectedIds = new HashSet<String>();
  27. private ApplicationConnection client;
  28. private String paintableId;
  29. private boolean selectable;
  30. private boolean isMultiselect;
  31. private final HashMap<String, TreeNode> keyToNode = new HashMap<String, TreeNode>();
  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 final HashMap<String, String> actionMap = new HashMap<String, String>();
  37. private boolean immediate;
  38. private boolean isNullSelectionAllowed = true;
  39. private boolean disabled = false;
  40. private boolean readonly;
  41. private boolean emitClickEvents;
  42. private boolean rendering;
  43. public VTree() {
  44. super();
  45. setStyleName(CLASSNAME);
  46. }
  47. private void updateActionMap(UIDL c) {
  48. final Iterator it = c.getChildIterator();
  49. while (it.hasNext()) {
  50. final UIDL action = (UIDL) it.next();
  51. final String key = action.getStringAttribute("key");
  52. final String caption = action.getStringAttribute("caption");
  53. actionMap.put(key + "_c", caption);
  54. if (action.hasAttribute("icon")) {
  55. // TODO need some uri handling ??
  56. actionMap.put(key + "_i", client.translateToolkitUri(action
  57. .getStringAttribute("icon")));
  58. }
  59. }
  60. }
  61. public String getActionCaption(String actionKey) {
  62. return actionMap.get(actionKey + "_c");
  63. }
  64. public String getActionIcon(String actionKey) {
  65. return actionMap.get(actionKey + "_i");
  66. }
  67. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  68. // Ensure correct implementation and let container manage caption
  69. if (client.updateComponent(this, uidl, true)) {
  70. return;
  71. }
  72. rendering = true;
  73. this.client = client;
  74. if (uidl.hasAttribute("partialUpdate")) {
  75. handleUpdate(uidl);
  76. rendering = false;
  77. return;
  78. }
  79. paintableId = uidl.getId();
  80. immediate = uidl.hasAttribute("immediate");
  81. disabled = uidl.getBooleanAttribute("disabled");
  82. readonly = uidl.getBooleanAttribute("readonly");
  83. emitClickEvents = uidl.getBooleanAttribute("listenClicks");
  84. isNullSelectionAllowed = uidl.getBooleanAttribute("nullselect");
  85. clear();
  86. for (final Iterator i = uidl.getChildIterator(); i.hasNext();) {
  87. final UIDL childUidl = (UIDL) i.next();
  88. if ("actions".equals(childUidl.getTag())) {
  89. updateActionMap(childUidl);
  90. continue;
  91. }
  92. final TreeNode childTree = new TreeNode();
  93. this.add(childTree);
  94. childTree.updateFromUIDL(childUidl, client);
  95. }
  96. final String selectMode = uidl.getStringAttribute("selectmode");
  97. selectable = !"none".equals(selectMode);
  98. isMultiselect = "multi".equals(selectMode);
  99. selectedIds = uidl.getStringArrayVariableAsSet("selected");
  100. rendering = false;
  101. }
  102. private void handleUpdate(UIDL uidl) {
  103. final TreeNode rootNode = keyToNode.get(uidl
  104. .getStringAttribute("rootKey"));
  105. if (rootNode != null) {
  106. if (!rootNode.getState()) {
  107. // expanding node happened server side
  108. rootNode.setState(true, false);
  109. }
  110. rootNode.renderChildNodes(uidl.getChildIterator());
  111. }
  112. if (uidl.hasVariable("selected")) {
  113. // update selection in case selected nodes were not visible
  114. selectedIds = uidl.getStringArrayVariableAsSet("selected");
  115. }
  116. }
  117. public void setSelected(TreeNode treeNode, boolean selected) {
  118. if (selected) {
  119. if (!isMultiselect) {
  120. while (selectedIds.size() > 0) {
  121. final String id = selectedIds.iterator().next();
  122. final TreeNode oldSelection = keyToNode.get(id);
  123. if (oldSelection != null) {
  124. // can be null if the node is not visible (parent
  125. // collapsed)
  126. oldSelection.setSelected(false);
  127. }
  128. selectedIds.remove(id);
  129. }
  130. }
  131. treeNode.setSelected(true);
  132. selectedIds.add(treeNode.key);
  133. } else {
  134. if (!isNullSelectionAllowed) {
  135. if (!isMultiselect || selectedIds.size() == 1) {
  136. return;
  137. }
  138. }
  139. selectedIds.remove(treeNode.key);
  140. treeNode.setSelected(false);
  141. }
  142. client.updateVariable(paintableId, "selected", selectedIds.toArray(),
  143. immediate);
  144. }
  145. public boolean isSelected(TreeNode treeNode) {
  146. return selectedIds.contains(treeNode.key);
  147. }
  148. protected class TreeNode extends SimplePanel implements ActionOwner {
  149. public static final String CLASSNAME = "i-tree-node";
  150. String key;
  151. private String[] actionKeys = null;
  152. private boolean childrenLoaded;
  153. private Element nodeCaptionDiv;
  154. protected Element nodeCaptionSpan;
  155. private FlowPanel childNodeContainer;
  156. private boolean open;
  157. private Icon icon;
  158. private Element ie6compatnode;
  159. public TreeNode() {
  160. constructDom();
  161. sinkEvents(Event.ONCLICK | Event.ONDBLCLICK | Event.ONMOUSEUP
  162. | Event.ONCONTEXTMENU);
  163. }
  164. @Override
  165. public void onBrowserEvent(Event event) {
  166. super.onBrowserEvent(event);
  167. if (disabled) {
  168. return;
  169. }
  170. final int type = DOM.eventGetType(event);
  171. final Element target = DOM.eventGetTarget(event);
  172. if (emitClickEvents && target == nodeCaptionSpan
  173. && (type == Event.ONDBLCLICK || type == Event.ONMOUSEUP)) {
  174. fireClick(event);
  175. }
  176. if (type == Event.ONCLICK) {
  177. if (getElement() == target || ie6compatnode == target) {
  178. // state change
  179. toggleState();
  180. } else if (!readonly && target == nodeCaptionSpan) {
  181. // caption click = selection change && possible click event
  182. toggleSelection();
  183. }
  184. DOM.eventCancelBubble(event, true);
  185. } else if (type == Event.ONCONTEXTMENU) {
  186. showContextMenu(event);
  187. }
  188. }
  189. private void fireClick(Event evt) {
  190. // non-immediate iff an immediate select event is going to happen
  191. boolean imm = !immediate
  192. || !selectable
  193. || (!isNullSelectionAllowed && isSelected() && selectedIds
  194. .size() == 1);
  195. MouseEventDetails details = new MouseEventDetails(evt);
  196. client.updateVariable(paintableId, "clickedKey", key, false);
  197. client.updateVariable(paintableId, "clickEvent",
  198. details.toString(), imm);
  199. }
  200. private void toggleSelection() {
  201. if (selectable) {
  202. VTree.this.setSelected(this, !isSelected());
  203. }
  204. }
  205. private void toggleState() {
  206. setState(!getState(), true);
  207. }
  208. protected void constructDom() {
  209. // workaround for a very weird IE6 issue #1245
  210. ie6compatnode = DOM.createDiv();
  211. setStyleName(ie6compatnode, CLASSNAME + "-ie6compatnode");
  212. DOM.setInnerText(ie6compatnode, " ");
  213. DOM.appendChild(getElement(), ie6compatnode);
  214. DOM.sinkEvents(ie6compatnode, Event.ONCLICK);
  215. nodeCaptionDiv = DOM.createDiv();
  216. DOM.setElementProperty(nodeCaptionDiv, "className", CLASSNAME
  217. + "-caption");
  218. Element wrapper = DOM.createDiv();
  219. nodeCaptionSpan = DOM.createSpan();
  220. DOM.appendChild(getElement(), nodeCaptionDiv);
  221. DOM.appendChild(nodeCaptionDiv, wrapper);
  222. DOM.appendChild(wrapper, nodeCaptionSpan);
  223. childNodeContainer = new FlowPanel();
  224. childNodeContainer.setStylePrimaryName(CLASSNAME + "-children");
  225. setWidget(childNodeContainer);
  226. }
  227. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  228. setText(uidl.getStringAttribute("caption"));
  229. key = uidl.getStringAttribute("key");
  230. keyToNode.put(key, this);
  231. if (uidl.hasAttribute("al")) {
  232. actionKeys = uidl.getStringArrayAttribute("al");
  233. }
  234. if (uidl.getTag().equals("node")) {
  235. if (uidl.getChildCount() == 0) {
  236. childNodeContainer.setVisible(false);
  237. } else {
  238. renderChildNodes(uidl.getChildIterator());
  239. childrenLoaded = true;
  240. }
  241. } else {
  242. addStyleName(CLASSNAME + "-leaf");
  243. }
  244. addStyleName(CLASSNAME);
  245. if (uidl.getBooleanAttribute("expanded") && !getState()) {
  246. setState(true, false);
  247. }
  248. if (uidl.getBooleanAttribute("selected")) {
  249. setSelected(true);
  250. }
  251. if (uidl.hasAttribute("icon")) {
  252. if (icon == null) {
  253. icon = new Icon(client);
  254. DOM.insertBefore(DOM.getFirstChild(nodeCaptionDiv), icon
  255. .getElement(), nodeCaptionSpan);
  256. }
  257. icon.setUri(uidl.getStringAttribute("icon"));
  258. } else {
  259. if (icon != null) {
  260. DOM.removeChild(DOM.getFirstChild(nodeCaptionDiv), icon
  261. .getElement());
  262. icon = null;
  263. }
  264. }
  265. if (BrowserInfo.get().isIE6() && isAttached()) {
  266. fixWidth();
  267. }
  268. }
  269. private void setState(boolean state, boolean notifyServer) {
  270. if (open == state) {
  271. return;
  272. }
  273. if (state) {
  274. if (!childrenLoaded && notifyServer) {
  275. client.updateVariable(paintableId, "requestChildTree",
  276. true, false);
  277. }
  278. if (notifyServer) {
  279. client.updateVariable(paintableId, "expand",
  280. new String[] { key }, true);
  281. }
  282. addStyleName(CLASSNAME + "-expanded");
  283. childNodeContainer.setVisible(true);
  284. } else {
  285. removeStyleName(CLASSNAME + "-expanded");
  286. childNodeContainer.setVisible(false);
  287. if (notifyServer) {
  288. client.updateVariable(paintableId, "collapse",
  289. new String[] { key }, true);
  290. }
  291. }
  292. open = state;
  293. if (!rendering) {
  294. Util.notifyParentOfSizeChange(VTree.this, false);
  295. }
  296. }
  297. private boolean getState() {
  298. return open;
  299. }
  300. private void setText(String text) {
  301. DOM.setInnerText(nodeCaptionSpan, text);
  302. }
  303. private void renderChildNodes(Iterator i) {
  304. childNodeContainer.clear();
  305. childNodeContainer.setVisible(true);
  306. while (i.hasNext()) {
  307. final UIDL childUidl = (UIDL) i.next();
  308. // actions are in bit weird place, don't mix them with children,
  309. // but current node's actions
  310. if ("actions".equals(childUidl.getTag())) {
  311. updateActionMap(childUidl);
  312. continue;
  313. }
  314. final TreeNode childTree = new TreeNode();
  315. childNodeContainer.add(childTree);
  316. childTree.updateFromUIDL(childUidl, client);
  317. }
  318. childrenLoaded = true;
  319. }
  320. public boolean isChildrenLoaded() {
  321. return childrenLoaded;
  322. }
  323. public Action[] getActions() {
  324. if (actionKeys == null) {
  325. return new Action[] {};
  326. }
  327. final Action[] actions = new Action[actionKeys.length];
  328. for (int i = 0; i < actions.length; i++) {
  329. final String actionKey = actionKeys[i];
  330. final TreeAction a = new TreeAction(this, String.valueOf(key),
  331. actionKey);
  332. a.setCaption(getActionCaption(actionKey));
  333. a.setIconUrl(getActionIcon(actionKey));
  334. actions[i] = a;
  335. }
  336. return actions;
  337. }
  338. public ApplicationConnection getClient() {
  339. return client;
  340. }
  341. public String getPaintableId() {
  342. return paintableId;
  343. }
  344. /**
  345. * Adds/removes IT Mill Toolkit specific style name. This method ought
  346. * to be called only from VTree.
  347. *
  348. * @param selected
  349. */
  350. protected void setSelected(boolean selected) {
  351. // add style name to caption dom structure only, not to subtree
  352. setStyleName(nodeCaptionDiv, "i-tree-node-selected", selected);
  353. }
  354. protected boolean isSelected() {
  355. return VTree.this.isSelected(this);
  356. }
  357. public void showContextMenu(Event event) {
  358. if (!readonly && !disabled) {
  359. if (actionKeys != null) {
  360. int left = event.getClientX();
  361. int top = event.getClientY();
  362. top += Window.getScrollTop();
  363. left += Window.getScrollLeft();
  364. client.getContextMenu().showAt(this, left, top);
  365. }
  366. event.cancelBubble(true);
  367. event.preventDefault();
  368. }
  369. }
  370. /*
  371. * We need to fix the width of TreeNodes so that the float in
  372. * ie6compatNode does not wrap (see ticket #1245)
  373. */
  374. private void fixWidth() {
  375. nodeCaptionDiv.getStyle().setProperty("styleFloat", "left");
  376. nodeCaptionDiv.getStyle().setProperty("display", "inline");
  377. nodeCaptionDiv.getStyle().setProperty("marginLeft", "0");
  378. final int captionWidth = ie6compatnode.getOffsetWidth()
  379. + nodeCaptionDiv.getOffsetWidth();
  380. setWidth(captionWidth + "px");
  381. }
  382. @Override
  383. public void onAttach() {
  384. super.onAttach();
  385. if (BrowserInfo.get().isIE6()) {
  386. fixWidth();
  387. }
  388. }
  389. }
  390. }