Sunday, March 3, 2013

FileSystemModel.java File to be used with jTree


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.File;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author ProgrammingKnowledge
 */
public class FileSystemModel implements TreeModel {

    private File root;
    private Vector listeners = new Vector();

    public FileSystemModel(File rootDirectory) {
        root = rootDirectory;
    }

    @Override
    public Object getRoot() {
        return root;
    }

    @Override
    public Object getChild(Object parent, int index) {
        File directory = (File) parent;
        String[] children = directory.list();
 /*       for (int j = 0; j< children.length; j++ ){
            System.out.println(children[j]);
        }       */
        
        return new FileSystemModel.TreeFile(directory, children[index]);
    }

    @Override
    public int getChildCount(Object parent) {
        File file = (File) parent;
        if (file.isDirectory()) {
            String[] fileList = file.list();
          
            if (fileList != null) {
                return file.list().length;
            }
        }
        return 0;
    }

    @Override
    public boolean isLeaf(Object node) {
        File file = (File) node;
        return file.isFile();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        File directory = (File) parent;
        File file = (File) child;
        String[] children = directory.list();
        for (int i = 0; i < children.length; i++) {
            if (file.getName().equals(children[i])) {
                return i;
            }
        }
        return -1;

    }

    @Override
    public void valueForPathChanged(TreePath path, Object value) {
        File oldFile = (File) path.getLastPathComponent();
        String fileParentPath = oldFile.getParent();
        String newFileName = (String) value;
        File targetFile = new File(fileParentPath, newFileName);
        oldFile.renameTo(targetFile);
        File parent = new File(fileParentPath);
        int[] changedChildrenIndices = {getIndexOfChild(parent, targetFile)};
        Object[] changedChildren = {targetFile};
        fireTreeNodesChanged(path.getParentPath(), changedChildrenIndices, changedChildren);

    }

    private void fireTreeNodesChanged(TreePath parentPath, int[] indices, Object[] children) {
        TreeModelEvent event = new TreeModelEvent(this, parentPath, indices, children);
        Iterator iterator = listeners.iterator();
        TreeModelListener listener = null;
        while (iterator.hasNext()) {
            listener = (TreeModelListener) iterator.next();
            listener.treeNodesChanged(event);
        }
    }

    @Override
    public void addTreeModelListener(TreeModelListener listener) {
        listeners.add(listener);
    }

    @Override
    public void removeTreeModelListener(TreeModelListener listener) {
        listeners.remove(listener);
    }

    private class TreeFile extends File {

        public TreeFile(File parent, String child) {
            super(parent, child);
        }

        @Override
        public String toString() {
            return getName();
        }
    }
}

12 comments:

  1. Thanks a lot for the tutorial and code!
    when I run this on Linux, the jTree shows even the hidden files.. what should I need to do to get only visible directories & file (NOT hidden)

    ReplyDelete
  2. Thanks a lot for the tutorial and code!
    but how can save it to database

    ReplyDelete
  3. How can i list only the folders in the Jtree?

    ReplyDelete
    Replies
    1. Hello!
      Did you know now how to list just the folder??

      Delete
  4. thank for tutorial and code mr :)

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Thanks a lot, but I can't figure out what is the meaning of the code.

    ReplyDelete
  7. hello sir please in Java prog video # 132 I still have the second message "This file does not exist" despite that I copy your source code that is-I can do !!

    ReplyDelete
  8. sir plzz tell how to make a tree by retrieving data form database....thanx..

    ReplyDelete
  9. Hello!
    Do you know how i can only show the folder of the desire directory and what shall i put directory if i want to access the drives in my computer, like Local Disk(D) using Jtree
    Local Disk(C)

    ReplyDelete