[ adempiere-ZK Web Client-2713719 ] Tree doesn't have expand and lookup

This commit is contained in:
Heng Sin Low 2009-03-26 08:42:26 +00:00
parent 3aa9d77dfe
commit 21db3e1d31
7 changed files with 478 additions and 80 deletions

View File

@ -76,7 +76,6 @@ import org.zkoss.zul.Groupfoot;
import org.zkoss.zul.Separator;
import org.zkoss.zul.SimpleTreeNode;
import org.zkoss.zul.Space;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treeitem;
/**
@ -132,7 +131,7 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
private Component formComponent = null;
private Tree tree = null;
private ADTreePanel treePanel = null;
private GridTabDataBinder dataBinder;
@ -202,10 +201,9 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
layout.setParent(this);
layout.setStyle("width: 100%; height: 100%; position: absolute;");
tree = new Tree();
tree.setStyle("border: none");
treePanel = new ADTreePanel();
West west = new West();
west.appendChild(tree);
west.appendChild(treePanel);
west.setWidth("300px");
west.setCollapsible(true);
west.setSplittable(true);
@ -218,7 +216,7 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
layout.appendChild(center);
formComponent = layout;
tree.addEventListener(Events.ON_SELECT, this);
treePanel.getTree().addEventListener(Events.ON_SELECT, this);
}
else
{
@ -492,10 +490,10 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
}
//create tree
if (gridTab.isTreeTab() && tree != null) {
if (gridTab.isTreeTab() && treePanel != null) {
int AD_Tree_ID = MTree.getDefaultAD_Tree_ID (
Env.getAD_Client_ID(Env.getCtx()), gridTab.getKeyColumnName());
SimpleTreeModel.initADTree(tree, AD_Tree_ID, windowNo);
treePanel.initTree(AD_Tree_ID, windowNo);
}
if (!gridTab.isSingleRow() && !isGridView())
@ -779,8 +777,8 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
{
this.switchRowPresentation();
}
else if (event.getTarget() == tree) {
Treeitem item = tree.getSelectedItem();
else if (event.getTarget() == treePanel.getTree()) {
Treeitem item = treePanel.getTree().getSelectedItem();
navigateTo((SimpleTreeNode)item.getValue());
}
}
@ -863,7 +861,7 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
dynamicDisplay(col);
//sync tree
if (tree != null) {
if (treePanel != null) {
if ("Deleted".equalsIgnoreCase(e.getAD_Message()))
if (e.Record_ID != null
&& e.Record_ID instanceof Integer
@ -890,10 +888,10 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
private void deleteNode(int recordId) {
if (recordId <= 0) return;
SimpleTreeModel model = (SimpleTreeModel) tree.getModel();
SimpleTreeModel model = (SimpleTreeModel) treePanel.getTree().getModel();
if (tree.getSelectedItem() != null) {
SimpleTreeNode treeNode = (SimpleTreeNode) tree.getSelectedItem().getValue();
if (treePanel.getTree().getSelectedItem() != null) {
SimpleTreeNode treeNode = (SimpleTreeNode) treePanel.getTree().getSelectedItem().getValue();
MTreeNode data = (MTreeNode) treeNode.getData();
if (data.getNode_ID() == recordId) {
model.removeNode(treeNode);
@ -914,7 +912,7 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
boolean summary = gridTab.getValueAsBoolean("IsSummary");
String imageIndicator = (String)gridTab.getValue("Action"); // Menu - Action
//
SimpleTreeModel model = (SimpleTreeModel) tree.getModel();
SimpleTreeModel model = (SimpleTreeModel) treePanel.getTree().getModel();
SimpleTreeNode treeNode = model.getRoot();
MTreeNode root = (MTreeNode) treeNode.getData();
MTreeNode node = new MTreeNode (gridTab.getRecord_ID(), 0, name, description,
@ -922,26 +920,26 @@ DataStatusListener, IADTabpanel, VetoableChangeListener
SimpleTreeNode newNode = new SimpleTreeNode(node, new ArrayList<Object>());
model.addNode(newNode);
int[] path = model.getPath(model.getRoot(), newNode);
Treeitem ti = tree.renderItemByPath(path);
tree.setSelectedItem(ti);
Treeitem ti = treePanel.getTree().renderItemByPath(path);
treePanel.getTree().setSelectedItem(ti);
}
}
private void setSelectedNode(int recordId) {
if (recordId <= 0) return;
if (tree.getSelectedItem() != null) {
SimpleTreeNode treeNode = (SimpleTreeNode) tree.getSelectedItem().getValue();
if (treePanel.getTree().getSelectedItem() != null) {
SimpleTreeNode treeNode = (SimpleTreeNode) treePanel.getTree().getSelectedItem().getValue();
MTreeNode data = (MTreeNode) treeNode.getData();
if (data.getNode_ID() == recordId) return;
}
SimpleTreeModel model = (SimpleTreeModel) tree.getModel();
SimpleTreeModel model = (SimpleTreeModel) treePanel.getTree().getModel();
SimpleTreeNode treeNode = model.find(null, recordId);
if (treeNode != null) {
int[] path = model.getPath(model.getRoot(), treeNode);
Treeitem ti = tree.renderItemByPath(path);
tree.setSelectedItem(ti);
Treeitem ti = treePanel.getTree().renderItemByPath(path);
treePanel.getTree().setSelectedItem(ti);
} else {
addNewNode();
}

View File

@ -0,0 +1,148 @@
/******************************************************************************
* Copyright (C) 2009 Low Heng Sin *
* Copyright (C) 2009 Idalica Corporation *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.adempiere.webui.panel;
import org.adempiere.webui.component.Checkbox;
import org.adempiere.webui.component.SimpleTreeModel;
import org.adempiere.webui.util.TreeUtils;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Panel;
import org.zkoss.zul.Panelchildren;
import org.zkoss.zul.Toolbar;
import org.zkoss.zul.Tree;
/**
*
* @author hengsin
*
*/
public class ADTreePanel extends Panel implements EventListener
{
private static final long serialVersionUID = 1L;
private TreeSearchPanel pnlSearch;
private Tree tree;
private Checkbox chkExpand; // Elaine 2009/02/27 - expand tree
public ADTreePanel()
{
init();
}
/**
* @param AD_Tree_ID
* @param windowNo
*/
public void initTree(int AD_Tree_ID, int windowNo)
{
SimpleTreeModel.initADTree(tree, AD_Tree_ID, windowNo);
pnlSearch.initialise();
}
private void init()
{
this.setWidth("100%");
this.setHeight("100%");
tree = new Tree();
tree.setMultiple(false);
tree.setWidth("100%");
tree.setVflex(true);
tree.setPageSize(-1); // Due to bug in the new paging functionality
tree.setStyle("border: none");
pnlSearch = new TreeSearchPanel(tree, Events.ON_SELECT);
Toolbar toolbar = new Toolbar();
toolbar.appendChild(pnlSearch);
this.appendChild(toolbar);
Panelchildren pc = new Panelchildren();
this.appendChild(pc);
pc.appendChild(tree);
// Elaine 2009/02/27 - expand tree
toolbar = new Toolbar();
chkExpand = new Checkbox();
chkExpand.setText(Msg.getMsg(Env.getCtx(), "ExpandTree"));
chkExpand.addEventListener(Events.ON_CHECK, this);
toolbar.appendChild(chkExpand);
this.appendChild(toolbar);
}
/**
* @param event
* @see EventListener#onEvent(Event)
*/
public void onEvent(Event event)
{
String eventName = event.getName();
// Elaine 2009/02/27 - expand tree
if (eventName.equals(Events.ON_CHECK) && event.getTarget() == chkExpand)
{
expandOnCheck();
}
//
}
/**
* @return tree
*/
public Tree getTree()
{
return tree;
}
/**
* expand all node
*/
public void expandAll()
{
if (!chkExpand.isChecked())
chkExpand.setChecked(true);
TreeUtils.expandAll(tree);
}
/**
* collapse all node
*/
public void collapseAll()
{
if (chkExpand.isChecked())
chkExpand.setChecked(false);
TreeUtils.collapseAll(tree);
}
/**
* On check event for the expand checkbox
*/
private void expandOnCheck()
{
if (chkExpand.isChecked())
expandAll();
else
collapseAll();
}
//
}

View File

@ -26,6 +26,7 @@ import org.adempiere.webui.component.Checkbox;
import org.adempiere.webui.event.MenuListener;
import org.adempiere.webui.exception.ApplicationException;
import org.adempiere.webui.session.SessionManager;
import org.adempiere.webui.util.TreeUtils;
import org.compiere.model.MTree;
import org.compiere.model.MTreeNode;
import org.compiere.util.DB;
@ -56,7 +57,7 @@ public class MenuPanel extends Panel implements EventListener
private static final long serialVersionUID = 1L;
private Properties ctx;
private MenuSearchPanel pnlSearch;
private TreeSearchPanel pnlSearch;
private Tree menuTree;
private ArrayList<MenuListener> menuListeners = new ArrayList<MenuListener>();
@ -95,7 +96,7 @@ public class MenuPanel extends Panel implements EventListener
menuTree.setStyle("border: none");
pnlSearch = new MenuSearchPanel(this);
pnlSearch = new TreeSearchPanel(menuTree);
Toolbar toolbar = new Toolbar();
toolbar.appendChild(pnlSearch);
@ -173,7 +174,6 @@ public class MenuPanel extends Panel implements EventListener
treeitem.setImage("/images/mWindow.png");
treeitem.getTreerow().setDraggable("favourite"); // Elaine 2008/07/24
pnlSearch.addTreeItem(treeitem);
treeitem.getTreerow().addEventListener(Events.ON_CLICK, this);
}
@ -209,10 +209,7 @@ public class MenuPanel extends Panel implements EventListener
// Elaine 2009/02/27 - expand tree
else if (eventName.equals(Events.ON_CHECK) && event.getTarget() == chkExpand)
{
if(comp.equals(chkExpand))
{
expandTree();
}
expandOnCheck();
}
//
}
@ -242,37 +239,6 @@ public class MenuPanel extends Panel implements EventListener
return menuTree;
}
// Elaine 2009/02/27 - expand tree
private void expand(Treechildren treechildren, boolean expand)
{
List<?> list = treechildren.getChildren();
for(int index = 0; index < list.size(); index++)
{
Object o = list.get(index);
if(o instanceof Treechildren)
{
Treechildren treechild = (Treechildren) o;
expand(treechild, expand);
}
else if(o instanceof Treeitem)
{
Treeitem treeitem = (Treeitem) o;
treeitem.setOpen(expand);
List<?> treeitemChildren = treeitem.getChildren();
for(int childIndex = 0; childIndex < treeitemChildren.size(); childIndex++)
{
Object child = treeitemChildren.get(childIndex);
if(child instanceof Treechildren)
{
Treechildren treechild = (Treechildren) child;
expand(treechild, expand);
}
}
}
}
}
/**
* expand all node
*/
@ -281,7 +247,7 @@ public class MenuPanel extends Panel implements EventListener
if (!chkExpand.isChecked())
chkExpand.setChecked(true);
expandTree();
TreeUtils.expandAll(menuTree);
}
/**
@ -292,16 +258,18 @@ public class MenuPanel extends Panel implements EventListener
if (chkExpand.isChecked())
chkExpand.setChecked(false);
expandTree();
TreeUtils.collapseAll(menuTree);
}
/**
* Clicked on Expand All
* On check event for the expand checkbox
*/
private void expandTree()
private void expandOnCheck()
{
Treechildren treechildren = menuTree.getTreechildren();
expand(treechildren, chkExpand.isChecked());
if (chkExpand.isChecked())
expandAll();
else
collapseAll();
}
//
}

View File

@ -22,13 +22,21 @@ import java.util.TreeMap;
import org.adempiere.webui.component.AutoComplete;
import org.adempiere.webui.component.Label;
import org.adempiere.webui.component.Panel;
import org.adempiere.webui.util.TreeItemAction;
import org.adempiere.webui.util.TreeNodeAction;
import org.adempiere.webui.util.TreeUtils;
import org.compiere.model.MTreeNode;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.SimpleTreeNode;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.event.TreeDataEvent;
import org.zkoss.zul.event.TreeDataListener;
/**
*
@ -36,24 +44,38 @@ import org.zkoss.zul.Treeitem;
* @date Mar 3, 2007
* @version $Revision: 0.10 $
*/
public class MenuSearchPanel extends Panel implements EventListener
public class TreeSearchPanel extends Panel implements EventListener, TreeDataListener
{
private static final long serialVersionUID = 1L;
private TreeMap<String, Treeitem> treeNodeItemMap = new TreeMap<String, Treeitem>();
private TreeMap<String, Object> treeNodeItemMap = new TreeMap<String, Object>();
private String[] treeValues;
private String[] treeDescription;
private Label lblSearch;
private AutoComplete cmbSearch;
private MenuPanel menuPanel;
private Tree tree;
private String eventToFire;
/**
* @param tree
*/
public TreeSearchPanel(Tree tree)
{
this(tree, Events.ON_CLICK);
}
public MenuSearchPanel(MenuPanel menuPanel)
/**
* @param tree
* @param event
*/
public TreeSearchPanel(Tree tree, String event)
{
super();
this.menuPanel = menuPanel;
this.tree = tree;
this.eventToFire = event;
init();
}
@ -71,38 +93,96 @@ public class MenuSearchPanel extends Panel implements EventListener
this.appendChild(lblSearch);
this.appendChild(cmbSearch);
}
public void addTreeItem(Treeitem treeItem)
private void addTreeItem(Treeitem treeItem)
{
String key = treeItem.getLabel();
treeNodeItemMap.put(key, treeItem);
}
private void addTreeItem(SimpleTreeNode node) {
Object data = node.getData();
if (data instanceof MTreeNode) {
MTreeNode mNode = (MTreeNode) data;
treeNodeItemMap.put(mNode.getName(), node);
}
}
/**
* populate the searchable list
*/
public void initialise()
{
refreshSearchList();
if (tree.getModel() != null)
{
tree.getModel().addTreeDataListener(this);
}
}
private void refreshSearchList() {
treeNodeItemMap.clear();
if (tree.getModel() == null) {
TreeUtils.traverse(tree, new TreeItemAction() {
public void run(Treeitem treeItem) {
addTreeItem(treeItem);
}
});
} else {
TreeUtils.traverse(tree.getModel(), new TreeNodeAction() {
public void run(SimpleTreeNode treeNode) {
addTreeItem(treeNode);
}
});
}
treeValues = new String[treeNodeItemMap.size()];
treeDescription = new String[treeNodeItemMap.size()];
int i = -1;
for (Treeitem treeItem: treeNodeItemMap.values())
for (Object value : treeNodeItemMap.values())
{
i++;
treeValues[i] = treeItem.getLabel();
treeDescription[i] = treeItem.getTooltiptext();
if (value instanceof Treeitem)
{
Treeitem treeItem = (Treeitem) value;
treeValues[i] = treeItem.getLabel();
treeDescription[i] = treeItem.getTooltiptext();
}
else if (value instanceof SimpleTreeNode)
{
SimpleTreeNode sNode = (SimpleTreeNode) value;
MTreeNode mNode = (MTreeNode) sNode.getData();
treeValues[i] = mNode.getName();
treeDescription[i] = mNode.getDescription();
}
}
cmbSearch.setDescription(treeDescription);
cmbSearch.setDict(treeValues);
}
}
/**
* @param event
* @see EventListener#onEvent(Event)
*/
public void onEvent(Event event)
{
if (cmbSearch.equals(event.getTarget()) && (event.getName().equals(Events.ON_CHANGE)))
{
String value = cmbSearch.getValue();
Treeitem treeItem = treeNodeItemMap.get(value);
Object node = treeNodeItemMap.get(value);
Treeitem treeItem = null;
if (node instanceof Treeitem) {
treeItem = (Treeitem) node;
} else {
SimpleTreeNode sNode = (SimpleTreeNode) node;
int[] path = tree.getModel().getPath(tree.getModel().getRoot(), sNode);
treeItem = tree.renderItemByPath(path);
tree.setSelectedItem(treeItem);
}
if (treeItem != null)
{
select(treeItem);
@ -112,9 +192,16 @@ public class MenuSearchPanel extends Panel implements EventListener
}
}
/**
* don't call this directly, use internally for post selection event
*/
public void onPostSelect() {
Clients.showBusy(null, false);
Event event = new Event(Events.ON_CLICK, menuPanel.getMenuTree().getSelectedItem().getTreerow());
Event event = null;
if (eventToFire.equals(Events.ON_CLICK))
event = new Event(Events.ON_CLICK, tree.getSelectedItem().getTreerow());
else
event = new Event(eventToFire, tree);
Events.postEvent(event);
}
@ -128,4 +215,12 @@ public class MenuSearchPanel extends Panel implements EventListener
}
selectedItem.getTree().setSelectedItem(selectedItem);
}
/**
* @param event
* @see TreeDataListener#onChange(TreeDataEvent)
*/
public void onChange(TreeDataEvent event) {
refreshSearchList();
}
}

View File

@ -0,0 +1,30 @@
/******************************************************************************
* Copyright (C) 2009 Low Heng Sin *
* Copyright (C) 2009 Idalica Corporation *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.adempiere.webui.util;
import org.zkoss.zul.Treeitem;
/**
*
* @author hengsin
*
*/
public interface TreeItemAction {
/**
*
* @param treeItem
*/
public void run(Treeitem treeItem);
}

View File

@ -0,0 +1,30 @@
/******************************************************************************
* Copyright (C) 2009 Low Heng Sin *
* Copyright (C) 2009 Idalica Corporation *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.adempiere.webui.util;
import org.zkoss.zul.SimpleTreeNode;
/**
*
* @author hengsin
*
*/
public interface TreeNodeAction {
/**
*
* @param treeNode
*/
public void run(SimpleTreeNode treeNode);
}

View File

@ -0,0 +1,129 @@
/******************************************************************************
* Copyright (C) 2009 Low Heng Sin *
* Copyright (C) 2009 Idalica Corporation *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program is distributed in the hope *
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
*****************************************************************************/
package org.adempiere.webui.util;
import java.util.List;
import org.zkoss.zul.SimpleTreeNode;
import org.zkoss.zul.Tree;
import org.zkoss.zul.TreeModel;
import org.zkoss.zul.Treechildren;
import org.zkoss.zul.Treeitem;
/**
*
* @author hengsin
*
*/
public class TreeUtils {
/**
* Collapse all nodes
* @param tree
*/
public static void collapseAll(Tree tree) {
traverse(tree, new TreeItemAction() {
public void run(Treeitem treeItem) {
treeItem.setOpen(false);
}
});
}
/**
* Expand all nodes
* @param tree
*/
public static void expandAll(Tree tree) {
traverse(tree, new TreeItemAction() {
public void run(Treeitem treeItem) {
treeItem.setOpen(true);
}
});
}
/**
* Traverse tree and execution action on Treeitem
* @param tree
* @param action
*/
public static void traverse(Tree tree, TreeItemAction action)
{
Treechildren treechildren = tree.getTreechildren();
traverse(treechildren, action);
}
/**
* Traverse treechildren and execution action on Treeitem
* @param treechildren
* @param action
*/
public static void traverse(Treechildren treechildren, TreeItemAction action)
{
List<?> list = treechildren.getChildren();
for(int index = 0; index < list.size(); index++)
{
Object o = list.get(index);
if(o instanceof Treechildren)
{
Treechildren treechild = (Treechildren) o;
traverse(treechild, action);
}
else if(o instanceof Treeitem)
{
Treeitem treeitem = (Treeitem) o;
action.run(treeitem);
List<?> treeitemChildren = treeitem.getChildren();
for(int childIndex = 0; childIndex < treeitemChildren.size(); childIndex++)
{
Object child = treeitemChildren.get(childIndex);
if(child instanceof Treechildren)
{
Treechildren treechild = (Treechildren) child;
traverse(treechild, action);
}
}
}
}
}
/**
* Traverse tree model and execution action on tree node
* @param model
* @param action
*/
public static void traverse(TreeModel model, TreeNodeAction action) {
traverse(model, model.getRoot(), action);
}
/**
* Traverse tree model from parent and execution action on tree node
* @param model
* @param parent
* @param action
*/
public static void traverse(TreeModel model, Object parent,
TreeNodeAction action) {
int count = model.getChildCount(parent);
for(int i = 0; i < count; i++) {
Object child = model.getChild(parent, i);
if (child instanceof SimpleTreeNode) {
action.run((SimpleTreeNode) child);
}
traverse(model, child, action);
}
}
}