From: Matti Tahvonen Date: Mon, 13 Aug 2007 12:49:42 +0000 (+0000) Subject: tree now renders only subtree on expand event, not whole component X-Git-Tag: 6.7.0.beta1~6115 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c4c0b6da880476d228ae5e934256bb6927e25287;p=vaadin-framework.git tree now renders only subtree on expand event, not whole component svn changeset:1989/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITree.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITree.java index cbf5bba8eb..a4b517ba97 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITree.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITree.java @@ -25,6 +25,8 @@ public class ITree extends Tree implements Paintable { private boolean selectable; private boolean multiselect; + private HashMap keyToNode = new HashMap(); + /** * This map contains captions and icon urls for * actions like: @@ -69,6 +71,12 @@ public class ITree extends Tree implements Paintable { return; this.client = client; + + if(uidl.hasAttribute("partialUpdate")) { + handleUpdate(uidl); + return; + } + this.paintableId = uidl.getId(); clear(); @@ -89,6 +97,17 @@ public class ITree extends Tree implements Paintable { addTreeListener(new TreeListener() { public void onTreeItemStateChanged(TreeItem item) { + if (item instanceof TreeNode) { + TreeNode tn = (TreeNode) item; + if(item.getState()) { + if(!tn.isChildrenLoaded()) { + String key = tn.key; + ITree.this.client.updateVariable(paintableId, "expand", new String[] {key}, true); + } + } else { + // TODO collapse + } + } } public void onTreeItemSelected(TreeItem item) { @@ -109,11 +128,23 @@ public class ITree extends Tree implements Paintable { } + private void handleUpdate(UIDL uidl) { + TreeNode rootNode = (TreeNode) keyToNode.get(uidl.getStringAttribute("rootKey")); + if(rootNode != null) { + rootNode.renderChildNodes(uidl.getChildIterator()); + } + + } + private class TreeNode extends TreeItem implements IActionOwner { String key; + boolean isLeaf = false; + private String[] actionKeys = null; + + private boolean childrenLoaded; public TreeNode() { super(); @@ -125,17 +156,50 @@ public class ITree extends Tree implements Paintable { this.setText(uidl.getStringAttribute("caption")); key = uidl.getStringAttribute("key"); + keyToNode.put(key, this); + if(uidl.hasAttribute("al")) actionKeys = uidl.getStringArrayAttribute("al"); - for (Iterator i = uidl.getChildIterator(); i.hasNext();) { + if(uidl.getTag().equals("node")) { + isLeaf = false; + if(uidl.getChidlCount() == 0) { + TreeNode childTree = new TreeNode(); + childTree.setText("Loading..."); + childrenLoaded = false; + this.addItem(childTree); + } else { + renderChildNodes(uidl.getChildIterator()); + } + } else { + isLeaf = true; + } + + if(uidl.getBooleanAttribute("expanded") && !getState()) { + setState(true); + } + + setSelected(uidl.getBooleanAttribute("selected")); + + } + + private void renderChildNodes(Iterator i) { + removeItems(); + while (i.hasNext()) { UIDL childUidl = (UIDL)i.next(); + if("actions".equals(childUidl.getTag())) { + updateActionMap(childUidl); + continue; + } TreeNode childTree = new TreeNode(); this.addItem(childTree); childTree.updateFromUIDL(childUidl, client); } - setState(uidl.getBooleanAttribute("expanded")); - setSelected(uidl.getBooleanAttribute("selected")); + childrenLoaded = true; + } + + public boolean isChildrenLoaded() { + return childrenLoaded; } public IAction[] getActions() { @@ -177,7 +241,6 @@ public class ITree extends Tree implements Paintable { return false; }; }-*/; - } } diff --git a/src/com/itmill/toolkit/ui/Tree.java b/src/com/itmill/toolkit/ui/Tree.java index 0d43a34acb..503a03609d 100644 --- a/src/com/itmill/toolkit/ui/Tree.java +++ b/src/com/itmill/toolkit/ui/Tree.java @@ -101,6 +101,16 @@ public class Tree extends Select implements Container.Hierarchical, * Is the tree selectable . */ private boolean selectable = true; + + /** + * Flag to indicate sub-tree loading + */ + private boolean partialUpdate = false; + + /** + * Holds a itemId which was recently expanded + */ + private Object expandedItemId; /* Tree constructors ************************************************** */ @@ -162,11 +172,22 @@ public class Tree extends Select implements Container.Hierarchical, // Expands expanded.add(itemId); - requestRepaint(); + + expandedItemId = itemId; + requestPartialRepaint(); fireExpandEvent(itemId); return true; } + + public void requestRepaint() { + super.requestRepaint(); + partialUpdate = false; + } + private void requestPartialRepaint() { + super.requestRepaint(); + partialUpdate = true; + } /** * Expands the items recursively @@ -349,25 +370,32 @@ public class Tree extends Select implements Container.Hierarchical, * @see com.itmill.toolkit.ui.AbstractComponent#paintContent(PaintTarget) */ public void paintContent(PaintTarget target) throws PaintException { - - // Focus control id - if (this.getFocusableId() > 0) { - target.addAttribute("focusid", this.getFocusableId()); + + if(partialUpdate) { + target.addAttribute("partialUpdate", true); + target.addAttribute("rootKey", itemIdMapper.key(expandedItemId)); + } else { + + // Focus control id + if (this.getFocusableId() > 0) { + target.addAttribute("focusid", this.getFocusableId()); + } + + // The tab ordering number + if (this.getTabIndex() > 0) + target.addAttribute("tabindex", this.getTabIndex()); + + // Paint tree attributes + if (isSelectable()) + target.addAttribute("selectmode", (isMultiSelect() ? "multi" + : "single")); + else + target.addAttribute("selectmode", "none"); + if (isNewItemsAllowed()) + target.addAttribute("allownewitem", true); + } - // The tab ordering number - if (this.getTabIndex() > 0) - target.addAttribute("tabindex", this.getTabIndex()); - - // Paint tree attributes - if (isSelectable()) - target.addAttribute("selectmode", (isMultiSelect() ? "multi" - : "single")); - else - target.addAttribute("selectmode", "none"); - if (isNewItemsAllowed()) - target.addAttribute("allownewitem", true); - // Initialize variables Set actionSet = new LinkedHashSet(); String[] selectedKeys; @@ -380,9 +408,15 @@ public class Tree extends Select implements Container.Hierarchical, // Iterates through hierarchical tree using a stack of iterators Stack iteratorStack = new Stack(); - Collection ids = rootItemIds(); + Collection ids; + if(partialUpdate) + ids = getChildren(expandedItemId); + else + ids = rootItemIds(); + if (ids != null) iteratorStack.push(ids.iterator()); + while (!iteratorStack.isEmpty()) { // Gets the iterator for current tree level @@ -475,15 +509,19 @@ public class Tree extends Select implements Container.Hierarchical, target.endTag("actions"); } - // Selected - target.addVariable(this, "selected", selectedKeys); + if(partialUpdate) { + partialUpdate = false; + } else { + // Selected + target.addVariable(this, "selected", selectedKeys); - // Expand and collapse - target.addVariable(this, "expand", new String[] {}); - target.addVariable(this, "collapse", new String[] {}); + // Expand and collapse + target.addVariable(this, "expand", new String[] {}); + target.addVariable(this, "collapse", new String[] {}); - // New items - target.addVariable(this, "newitem", new String[] {}); + // New items + target.addVariable(this, "newitem", new String[] {}); + } } /* Container.Hierarchical API ***************************************** */