viernes, 13 de mayo de 2011

Arbol de directorios con objeto Tree de java IceFaces.

Saludos, en esta ocación y de manera muy rapida tengo para ustedes un breve ejemplo de como llenar un objeto Tree en JavaIceFaces.



public class Page3 extends AbstractPageBean {
private static final String DIR_OPEN_ICON = "./xmlhttp/css/xp/css-images/tree_folder_open.gif";
private static final String DIR_CLOSE_ICON = "./xmlhttp/css/xp/css-images/tree_folder_close.gif";
private static final String DIR_LEAF_ICON = "./xmlhttp/css/xp/css-images/tree_document.gif";

private void get_tree(String dir, DefaultMutableTreeNode parent){
//Apertura del directorio
File a = new File(dir);
//Conseguimos un listado de los archivos y subdirectorios
String[] ficheros = a.list();
int i = 0;

//Agregamos los nodos al arbol, por cada archivo encontrado
try{
while ( i < ficheros.length ){
File tmp = new File( a.getCanonicalPath() +"/"+ ficheros[i]);

DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
IceUserObject branchObject = new IceUserObject(branchNode);
branchObject.setText( ficheros[i] );
branchObject.setBranchContractedIcon(DIR_CLOSE_ICON);
branchObject.setBranchExpandedIcon(DIR_OPEN_ICON);
branchObject.setLeafIcon(DIR_LEAF_ICON);
branchObject.setExpanded(false);

//Si el archivo es un directorio volvemos a invocar el metodo.
if ( tmp.isDirectory() ){
branchObject.setLeaf( false );
this.get_tree( a.getCanonicalPath() +"/"+ ficheros[i] , branchNode);
}
else
branchObject.setLeaf( true );

branchNode.setUserObject(branchObject);
parent.add(branchNode);
i++;
}
}
catch (IOException e){
this.setLblCurrentDir("Error: " + e.getMessage());
}
}

private void lectura2(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
IceUserObject itemx = new IceUserObject(root);
itemx.setText("Directorios");
itemx.setExpanded(true);
itemx.setLeaf(false);
itemx.setBranchContractedIcon(DIR_CLOSE_ICON);
itemx.setBranchExpandedIcon(DIR_OPEN_ICON);
itemx.setLeafIcon(DIR_LEAF_ICON);
root.setUserObject(itemx);

//Directorio a visualizar
this.get_tree("..", root);

DefaultTreeModel model = new DefaultTreeModel(root);
this.tree1Model.setModel(model);
}
}