IDEMPIERE-186 Zk6: Rendering of organization tree throw NPE

This commit is contained in:
Heng Sin Low 2012-03-12 11:51:31 +08:00
parent 2e6f20aabd
commit acedf65ff7
4 changed files with 46 additions and 18 deletions

View File

@ -12,6 +12,7 @@
*****************************************************************************/
package org.adempiere.webui.component;
import org.adempiere.webui.util.TreeUtils;
import org.adempiere.webui.window.FDialog;
import org.compiere.model.MTree;
import org.compiere.model.MTreeNode;
@ -146,6 +147,10 @@ public class ADTreeOnDropListener implements EventListener {
treeModel.addNode(newParent, movingNode, index);
int path[] = treeModel.getPath(movingNode);
if (TreeUtils.isOnInitRenderPosted(tree))
{
tree.onInitRender();
}
Treeitem movingItem = tree.renderItemByPath(path);
tree.setSelectedItem(movingItem);
Events.sendEvent(tree, new Event(Events.ON_SELECT, tree));

View File

@ -88,7 +88,7 @@ public class SimpleTreeModel extends org.zkoss.zul.DefaultTreeModel implements T
treeCols.appendChild(treeCol);
tree.setPageSize(-1);
try {
tree.setTreeitemRenderer(treeModel);
tree.setItemRenderer(treeModel);
tree.setModel(treeModel);
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to setup tree");
@ -106,10 +106,11 @@ public class SimpleTreeModel extends org.zkoss.zul.DefaultTreeModel implements T
SimpleTreeModel model = null;
Enumeration nodeEnum = root.children();
DefaultTreeNode stRoot = new DefaultTreeNode(root, new ArrayList());
DefaultTreeNode stRoot = new DefaultTreeNode(root, nodeEnum.hasMoreElements() ? new ArrayList() : null);
while(nodeEnum.hasMoreElements()) {
MTreeNode childNode = (MTreeNode)nodeEnum.nextElement();
DefaultTreeNode stNode = new DefaultTreeNode(childNode, new ArrayList());
DefaultTreeNode stNode = childNode.getChildCount() > 0 ? new DefaultTreeNode(childNode, new ArrayList())
: new DefaultTreeNode(childNode);
stRoot.getChildren().add(stNode);
if (childNode.getChildCount() > 0) {
populate(stNode, childNode);
@ -123,7 +124,8 @@ public class SimpleTreeModel extends org.zkoss.zul.DefaultTreeModel implements T
Enumeration nodeEnum = root.children();
while(nodeEnum.hasMoreElements()) {
MTreeNode childNode = (MTreeNode)nodeEnum.nextElement();
DefaultTreeNode stChildNode = new DefaultTreeNode(childNode, new ArrayList());
DefaultTreeNode stChildNode = childNode.getChildCount() > 0 ? new DefaultTreeNode(childNode, new ArrayList())
: new DefaultTreeNode(childNode);
stNode.getChildren().add(stChildNode);
if (childNode.getChildCount() > 0) {
populate(stChildNode, childNode);
@ -135,7 +137,8 @@ public class SimpleTreeModel extends org.zkoss.zul.DefaultTreeModel implements T
* @param ti
* @param node
*/
public void render(Treeitem ti, Object node) {
@Override
public void render(Treeitem ti, Object node, int index) {
Treecell tc = new Treecell(Objects.toString(node));
Treerow tr = null;
if(ti.getTreerow()==null){
@ -164,7 +167,7 @@ public class SimpleTreeModel extends org.zkoss.zul.DefaultTreeModel implements T
public void addNode(DefaultTreeNode newNode) {
DefaultTreeNode root = (DefaultTreeNode) getRoot();
root.getChildren().add(newNode);
fireEvent(root, root.getChildCount() - 1, root.getChildCount() - 1, TreeDataEvent.INTERVAL_ADDED);
fireEvent(TreeDataEvent.INTERVAL_ADDED, getPath(root), root.getChildCount() - 1, root.getChildCount() - 1);
}
@Override
@ -191,8 +194,8 @@ public class SimpleTreeModel extends org.zkoss.zul.DefaultTreeModel implements T
}
parentNode.getChildren().remove(path[index]);
fireEvent(parentNode, path[index], path[index], TreeDataEvent.INTERVAL_REMOVED);
parentNode.getChildren().remove(path[index]);
fireEvent(TreeDataEvent.INTERVAL_REMOVED, getPath(parentNode), path[index], path[index]);
}
}
@ -256,8 +259,15 @@ public class SimpleTreeModel extends org.zkoss.zul.DefaultTreeModel implements T
*/
public void addNode(DefaultTreeNode newParent, DefaultTreeNode newNode,
int index) {
newParent.getChildren().add(index, newNode);
fireEvent(newParent, index, index, TreeDataEvent.INTERVAL_ADDED);
DefaultTreeNode parent = newParent;
if (newParent.getChildren() == null) {
parent = new DefaultTreeNode(newParent.getData(), new ArrayList());
newParent.getParent().insert(parent, newParent.getParent().getIndex(newParent));
removeNode(newParent);
}
parent.getChildren().add(index, newNode);
fireEvent(TreeDataEvent.INTERVAL_ADDED, getPath(parent), index, index);
}
/**
@ -290,13 +300,7 @@ public class SimpleTreeModel extends org.zkoss.zul.DefaultTreeModel implements T
DefaultTreeNode parent = getParent(node);
if (parent != null) {
int i = parent.getChildren().indexOf(node);
fireEvent(parent, i, i, TreeDataEvent.CONTENTS_CHANGED);
fireEvent(TreeDataEvent.CONTENTS_CHANGED, getPath(parent), i, i);
}
}
@Override
public void render(Treeitem arg0, Object arg1, int arg2) throws Exception {
// TODO Auto-generated method stub
}
}

View File

@ -47,6 +47,7 @@ import org.adempiere.webui.editor.WEditorPopupMenu;
import org.adempiere.webui.editor.WebEditorFactory;
import org.adempiere.webui.event.ContextMenuListener;
import org.adempiere.webui.util.GridTabDataBinder;
import org.adempiere.webui.util.TreeUtils;
import org.adempiere.webui.window.FDialog;
import org.compiere.model.DataStatusEvent;
import org.compiere.model.DataStatusListener;
@ -982,6 +983,11 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
private void setSelectedNode(int recordId) {
if (recordId <= 0) return;
//force on init render
if (TreeUtils.isOnInitRenderPosted(treePanel.getTree())) {
treePanel.getTree().onInitRender();
}
if (treePanel.getTree().getSelectedItem() != null) {
DefaultTreeNode treeNode = (DefaultTreeNode) treePanel.getTree().getSelectedItem().getValue();
@ -994,7 +1000,7 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
if (treeNode != null) {
int[] path = model.getPath(treeNode);
Treeitem ti = treePanel.getTree().renderItemByPath(path);
treePanel.getTree().setSelectedItem(ti);
treePanel.getTree().selectItem(ti);
} else {
addNewNode();
}

View File

@ -28,6 +28,12 @@ import org.zkoss.zul.Treeitem;
*/
public class TreeUtils {
/**
* copy from Tree.java
*/
private static final String ATTR_ON_INIT_RENDER_POSTED =
"org.zkoss.zul.Tree.onInitLaterPosted";
/**
* Collapse all nodes
* @param tree
@ -126,4 +132,11 @@ public class TreeUtils {
traverse(model, child, action);
}
}
public static boolean isOnInitRenderPosted(Tree tree) {
if (tree.getAttribute(ATTR_ON_INIT_RENDER_POSTED) != null) {
return ((Boolean)tree.getAttribute(ATTR_ON_INIT_RENDER_POSTED)).booleanValue();
}
return false;
}
}