S
sgane
Hi friends,
Hope this will be useful for some of you. While refreshing
the tree, the methode fireTreeStructureChanged will collapse the
exoanded nodes. to avoid this you can follow this,
Note : I have done this for nodes of type <NodeName> <fileCount>
[inbox (10)]
------------------------------------
...
...
...
...
...
...
DefaultMutableTreeNode rootNodeForRefresh =
getLastMatchedNodeForPath(
(DefaultMutableTreeNode) null,
new ModifiableBoolean(true),
curPath);
FolderTreeModel ftm = (FolderTreeModel) getModel();
rootNodeForRefresh.removeAllChildren();
Collections.sort(folderNames);
ftm.eatLastSlash(folderNames);
ftm.makeChildren(rootNodeForRefresh, 0, folderNames);
ftm.nodeStructureChanged(rootNodeForRefresh);
/** @author sganesh
* The NodeStructureChanged medhod will collapse the expanded
* nodes. But we don't want it to collapse. So I'm getting the
expanded tree paths
* and comparing with the new tree Paths that are created after
collapsing.
* (I think the address of the tree nodes are changing after
collapsing, So doing so)
* Further while comparing we have to ignore the mailcounts. so
replacing them using RegEx
* comparision.
*/
Enumeration newNodes = rootNodeForRefresh.preorderEnumeration();
Vector newNodeVector = new Vector();
while(newNodes.hasMoreElements()){
newNodeVector.addElement(getPath((DefaultMutableTreeNode)newNodes.nextElement()));
}
if((value.size() != 0) && (newNodeVector.size() != 0)){
String newPaths = newNodeVector.toString();
String regex = "\\([^)]*\\)";
newPaths = newPaths.replaceAll(regex, "");
newPaths = newPaths.substring(1,newPaths.length()-1);
String oldPaths = value.toString();
oldPaths = oldPaths.replaceAll(regex, "");
oldPaths = oldPaths.substring(1,oldPaths.length()-1);
regex = "\\s\\]\\,";
newPaths = newPaths.replaceAll(regex, "^");
newPaths = newPaths.replaceAll("\\s\\[","");
newPaths = newPaths.substring(1,newPaths.length()-1).trim();
oldPaths = oldPaths.replaceAll(regex, "^");
oldPaths = oldPaths.replaceAll("\\s\\[","");
oldPaths = oldPaths.substring(1,oldPaths.length()-1).trim();
StringTokenizer token = new StringTokenizer(newPaths,"^");
ArrayList newList = new ArrayList();
ArrayList oldList = new ArrayList();
while(token.hasMoreElements()){
newList.add(token.nextToken());
}
token = new StringTokenizer(oldPaths,"^");
while(token.hasMoreElements()){
oldList.add(token.nextToken());
}
int[] row = new int[oldList.size()];
int j = 0;
for(int i = 0; i < newList.size(); i++){
if(oldList.contains(newList.get(i))){
row[j] = i;
j++;
}
}
Object temp = null;
collapsePath(path);
for(int i = 0; i < row.length; i++){
temp = newNodeVector.get(row);
setExpandedState((TreePath)temp,true);
}
}
clearSelection();
setSelectionPath(path);
Hope this will be useful for some of you. While refreshing
the tree, the methode fireTreeStructureChanged will collapse the
exoanded nodes. to avoid this you can follow this,
Note : I have done this for nodes of type <NodeName> <fileCount>
[inbox (10)]
------------------------------------
...
...
...
...
...
...
DefaultMutableTreeNode rootNodeForRefresh =
getLastMatchedNodeForPath(
(DefaultMutableTreeNode) null,
new ModifiableBoolean(true),
curPath);
FolderTreeModel ftm = (FolderTreeModel) getModel();
rootNodeForRefresh.removeAllChildren();
Collections.sort(folderNames);
ftm.eatLastSlash(folderNames);
ftm.makeChildren(rootNodeForRefresh, 0, folderNames);
ftm.nodeStructureChanged(rootNodeForRefresh);
/** @author sganesh
* The NodeStructureChanged medhod will collapse the expanded
* nodes. But we don't want it to collapse. So I'm getting the
expanded tree paths
* and comparing with the new tree Paths that are created after
collapsing.
* (I think the address of the tree nodes are changing after
collapsing, So doing so)
* Further while comparing we have to ignore the mailcounts. so
replacing them using RegEx
* comparision.
*/
Enumeration newNodes = rootNodeForRefresh.preorderEnumeration();
Vector newNodeVector = new Vector();
while(newNodes.hasMoreElements()){
newNodeVector.addElement(getPath((DefaultMutableTreeNode)newNodes.nextElement()));
}
if((value.size() != 0) && (newNodeVector.size() != 0)){
String newPaths = newNodeVector.toString();
String regex = "\\([^)]*\\)";
newPaths = newPaths.replaceAll(regex, "");
newPaths = newPaths.substring(1,newPaths.length()-1);
String oldPaths = value.toString();
oldPaths = oldPaths.replaceAll(regex, "");
oldPaths = oldPaths.substring(1,oldPaths.length()-1);
regex = "\\s\\]\\,";
newPaths = newPaths.replaceAll(regex, "^");
newPaths = newPaths.replaceAll("\\s\\[","");
newPaths = newPaths.substring(1,newPaths.length()-1).trim();
oldPaths = oldPaths.replaceAll(regex, "^");
oldPaths = oldPaths.replaceAll("\\s\\[","");
oldPaths = oldPaths.substring(1,oldPaths.length()-1).trim();
StringTokenizer token = new StringTokenizer(newPaths,"^");
ArrayList newList = new ArrayList();
ArrayList oldList = new ArrayList();
while(token.hasMoreElements()){
newList.add(token.nextToken());
}
token = new StringTokenizer(oldPaths,"^");
while(token.hasMoreElements()){
oldList.add(token.nextToken());
}
int[] row = new int[oldList.size()];
int j = 0;
for(int i = 0; i < newList.size(); i++){
if(oldList.contains(newList.get(i))){
row[j] = i;
j++;
}
}
Object temp = null;
collapsePath(path);
for(int i = 0; i < row.length; i++){
temp = newNodeVector.get(row);
setExpandedState((TreePath)temp,true);
}
}
clearSelection();
setSelectionPath(path);