refreshing the tree without collapsing the nodes

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);
 
B

Babu Kalakrishnan

sgane said:
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,

Haven't looked at the code, but one comment regarding the use of the
fireTreeStructureChanged method to "refresh" a tree.

The fireTreeStructureChanged event should be fired only if the structure
of the tree has changed in such a manner that cannot be described by a
narrower set of changes (such as nodes added / nodes removed etc). In
such a scenario, I wonder if remembering the previously expanded state
of a treepath (which may or may not be present in the new structure) is
really relevant. If a lot of previously expanded paths are still present
in the new structure (the only case where this code would be useful), it
might also mean that one probably didn't really need to fire a
nodeStructureChanged event at all, but could have instead fired one or
more smaller nodesInserted/removed/changed events which wouldn't have
changed the display state of other branches in the first place.

BK
 
S

sgane

Yeah,
u'r exactly right. But what i'm doing is, getting the new nodes
of the path using the preordertraversal function and comparing this
with the old nodes. I'm going to fire only the sub-paths which have
been changed. This won't affect the rest of the paths. It wld be
better if u review the part of code.
If u have a better solution, u r always welcome.

Thanks,
Ganesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top