import java.io.File;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
/**
*
* @author Student
*/
public class MyTreeModel implements TreeModel {
private final String rootPath = "C://WINDOWS";
public Object getRoot() { // call first time
MyFile root = new MyFile(rootPath);
return root;
}
public Object getChild(Object parent, int index) { //it run asmany times as the value return by getChildCount()
MyFile myParent = (MyFile) parent;
File[] childFiles = myParent.listFiles();
MyFile child = new MyFile(childFiles[index]);
return child;
}
public int getChildCount(Object parent) { //
MyFile myParent = (MyFile) parent;
File[] childFiles = myParent.listFiles();
if (childFiles != null) {
return childFiles.length;
} else {
return 0;
}
}
public boolean isLeaf(Object node) { //2nd time and accept object send by getRoot() here node return type is MyFile
MyFile myNode = (MyFile) node;
return myNode.isFile();
}
public void valueForPathChanged(TreePath path, Object newValue) {
System.out.println("path="+path);
System.out.println("newvalue"+newValue);
}
public int getIndexOfChild(Object parent, Object child) {
int index;
MyFile childParent= (MyFile)parent;
MyFile Child1=(MyFile)child;
File[]listFiles=childParent.listFiles();
for( index=0;index<listFiles.length;index++){
if(listFiles[index].equals(Child1.getPath())){
return (index);
}
}
return 0;
}
public void addTreeModelListener(TreeModelListener l) { //not used by tree models
}
public void removeTreeModelListener(TreeModelListener l) { //not used by tree models
}
}
No comments:
Post a Comment