Author: nbelaevski
Date: 2009-03-23 20:29:41 -0400 (Mon, 23 Mar 2009)
New Revision: 13129
Modified:
trunk/framework/api/src/main/java/org/richfaces/model/AbstractTreeDataModel.java
trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeNodeImpl.java
trunk/framework/api/src/main/java/org/richfaces/model/TreeDataModel.java
trunk/framework/api/src/main/java/org/richfaces/model/TreeDataModelNodeAdaptor.java
Log:
https://jira.jboss.org/jira/browse/RF-6560
Modified:
trunk/framework/api/src/main/java/org/richfaces/model/AbstractTreeDataModel.java
===================================================================
---
trunk/framework/api/src/main/java/org/richfaces/model/AbstractTreeDataModel.java 2009-03-23
20:46:03 UTC (rev 13128)
+++
trunk/framework/api/src/main/java/org/richfaces/model/AbstractTreeDataModel.java 2009-03-24
00:29:41 UTC (rev 13129)
@@ -162,6 +162,10 @@
return ((TreeRowKey) key).getParentKey();
}
+ public void addNode(Object parentRowKey, Object newNode, Object id) {
+ throw new UnsupportedOperationException();
+ }
+
public void addNode(Object parentRowKey, TreeNode newNode, Object id) {
throw new UnsupportedOperationException();
}
Modified: trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeNodeImpl.java
===================================================================
---
trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeNodeImpl.java 2009-03-23
20:46:03 UTC (rev 13128)
+++
trunk/framework/api/src/main/java/org/richfaces/model/SwingTreeNodeImpl.java 2009-03-24
00:29:41 UTC (rev 13129)
@@ -36,7 +36,7 @@
* @author Nick Belaevski
* @since 3.2
*/
-
+@Deprecated
public class SwingTreeNodeImpl implements TreeNode {
private List<TreeNode> children = new ArrayList<TreeNode>();
@@ -93,18 +93,7 @@
* @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode)
*/
public int getIndex(TreeNode node) {
- if (node != null) {
- int counter = 0;
- for (Iterator<TreeNode> iterator = children.iterator(); iterator.hasNext();) {
- if (node.equals(iterator.next())) {
- return counter;
- }
-
- counter++;
- }
- }
-
- return -1;
+ return children.indexOf(node);
}
/* (non-Javadoc)
@@ -144,5 +133,13 @@
public void setData(Object data) {
this.data = data;
}
+
+ public void removeChild(TreeNode node) {
+ this.children.remove(node);
+ }
+ public void removeChildByKey(Object key) {
+ this.children.remove(((Integer) key).intValue());
+ }
+
}
Modified: trunk/framework/api/src/main/java/org/richfaces/model/TreeDataModel.java
===================================================================
--- trunk/framework/api/src/main/java/org/richfaces/model/TreeDataModel.java 2009-03-23
20:46:03 UTC (rev 13128)
+++ trunk/framework/api/src/main/java/org/richfaces/model/TreeDataModel.java 2009-03-24
00:29:41 UTC (rev 13129)
@@ -361,25 +361,41 @@
public Object getChildNodeId(Object childNode) {
return getNodeAdaptor().getChildNodeId((T) childNode);
}
-
+
@Override
public void addNode(Object parentRowKey, TreeNode newNode, Object id) {
- TreeNode parentNode = (parentRowKey != null) ? getTreeNode() : (TreeNode)
getWrappedData();
- if (parentNode == null) {
- throw new IllegalArgumentException("Parent node required!");
- }
-
- parentNode.addChild(id, newNode);
- }
+ addNode(parentRowKey, newNode, id);
+ }
+ public void addNode(Object parentRowKey, Object newNode, Object id) {
+ Object initialRowKey = getRowKey();
+ try {
+ T parentTreeNode = locateTreeNode((TreeRowKey) parentRowKey);
+
+ if (parentTreeNode == null) {
+ throw new IllegalArgumentException("Parent node not found!");
+ }
+
+ getNodeAdaptor().appendChild(parentTreeNode, id, clazz.cast(newNode));
+ } finally {
+ setRowKey(initialRowKey);
+ }
+ }
+
@Override
public void removeNode(Object rowKey) {
- TreeNode node = getTreeNode();
- TreeNode parentNode = node.getParent();
-
- // 1. remove node from data model
- Object id = getChildNodeId(node);
- parentNode.removeChild(id);
+ Object initialRowKey = getRowKey();
+ try {
+ T treeNode = locateTreeNode((TreeRowKey) rowKey);
+
+ if (treeNode == null) {
+ throw new IllegalArgumentException("Node not fond!");
+ }
+
+ getNodeAdaptor().removeFromParent(treeNode);
+ } finally {
+ setRowKey(initialRowKey);
+ }
}
}
Modified:
trunk/framework/api/src/main/java/org/richfaces/model/TreeDataModelNodeAdaptor.java
===================================================================
---
trunk/framework/api/src/main/java/org/richfaces/model/TreeDataModelNodeAdaptor.java 2009-03-23
20:46:03 UTC (rev 13128)
+++
trunk/framework/api/src/main/java/org/richfaces/model/TreeDataModelNodeAdaptor.java 2009-03-24
00:29:41 UTC (rev 13129)
@@ -26,6 +26,7 @@
import java.util.Map;
import java.util.Map.Entry;
+import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
/**
@@ -57,6 +58,10 @@
*/
public Object getChildNodeId(T childNode);
+ public void appendChild(T parentNode, Object key, T childNode);
+
+ public void removeFromParent(T childNode);
+
/**
* Instance of {@link TreeDataModelNodeAdaptor} for {@link org.richfaces.model.TreeNode}
nodes handling
*/
@@ -104,6 +109,20 @@
}
return null;
}
+
+ public void appendChild(org.richfaces.model.TreeNode parentNode,
+ Object childKey, org.richfaces.model.TreeNode childNode) {
+
+ parentNode.addChild(childKey, childNode);
+ }
+
+ public void removeFromParent(org.richfaces.model.TreeNode childNode) {
+ org.richfaces.model.TreeNode parent = childNode.getParent();
+ Object childNodeId = getChildNodeId(childNode);
+ if (childNodeId != null) {
+ parent.removeChild(childNodeId);
+ }
+ }
};
/**
@@ -202,15 +221,58 @@
public Object getChildNodeId(javax.swing.tree.TreeNode childNode) {
javax.swing.tree.TreeNode parentNode = getParent(childNode);
if (childNode != null && parentNode != null) {
- Iterator<Map.Entry<Object, TreeNode>> iter =
getChildren(parentNode);
- while (iter != null && iter.hasNext()) {
- Map.Entry<Object, TreeNode> entry = iter.next();
- if (entry != null && childNode.equals(entry.getValue())) {
- return entry.getKey();
- }
- }
+ int idx = parentNode.getIndex(childNode);
+ if (idx != -1) {
+ return idx;
+ } else {
+ return null;
+ }
+ } else {
+ return null;
}
- return null;
}
+
+ @SuppressWarnings("deprecation")
+ public void appendChild(javax.swing.tree.TreeNode parentNode, Object key,
+ javax.swing.tree.TreeNode childNode) {
+
+ if (childNode instanceof MutableTreeNode) {
+ MutableTreeNode mutableChildNode = (MutableTreeNode) childNode;
+ MutableTreeNode mutableParentNode = (MutableTreeNode) parentNode;
+ int idx;
+
+ if (key != null) {
+ idx = (Integer) key;
+ } else {
+ idx = mutableParentNode.getChildCount();
+ }
+
+ mutableParentNode.insert(mutableChildNode, idx);
+ } else {
+ SwingTreeNodeImpl swingTreeNodeImpl = (SwingTreeNodeImpl) childNode;
+ SwingTreeNodeImpl parent = (SwingTreeNodeImpl) parentNode;
+
+ if (key != null) {
+ parent.addChild(key, swingTreeNodeImpl);
+ } else {
+ parent.addChild(swingTreeNodeImpl);
+ }
+
+ swingTreeNodeImpl.setParent(parent);
+ }
+ }
+
+ @SuppressWarnings("deprecation")
+ public void removeFromParent(javax.swing.tree.TreeNode node) {
+ if (node instanceof MutableTreeNode) {
+ ((MutableTreeNode) node).removeFromParent();
+ } else {
+ SwingTreeNodeImpl swingTreeNodeImpl = (SwingTreeNodeImpl) node;
+ SwingTreeNodeImpl parent = (SwingTreeNodeImpl) swingTreeNodeImpl.getParent();
+
+ parent.removeChild(swingTreeNodeImpl);
+ swingTreeNodeImpl.setParent(null);
+ }
+ }
};
}