慢TreeModel问题

时间:2014-06-28 10:12:28

标签: java performance treemodel

我正在开发一个在主窗口侧面有一个文件树的应用程序,它适用于不包含大量文件的文件夹,但它会慢一点。这是型号代码:

public class FileTreeModel implements TreeModel {
private File root;
private List<TreeModelListener> listeners;

public FileTreeModel(File root) {
    this.root = root;
    this.listeners = new ArrayList<TreeModelListener>();
}

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

@Override
public Object getChild(Object parent, int index) {
    File directory = (File) parent;
    String[] children = getSortedChildren(directory);
    return new File(directory, children[index]) {
        private static final long serialVersionUID = 1L;

        @Override
        public String toString() { // This is so the node only shows the file name instead of the whole path
            return getName();
        }
    };
}

@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 int getIndexOfChild(Object parent, Object child) {
    File directory = (File) parent;
    File file = (File) child;
    String[] children = getSortedChildren(directory);
    for (int i = 0; i < children.length; i++) {
        if (file.getName().equals(children[i])) {
            return i;
        }
    }
    return -1;
}

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

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

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

@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);
    for (TreeModelListener listener : this.listeners) {
        listener.treeNodesChanged(event);
    }
}

private String[] getSortedChildren(File directory) {
    File[] directoryChildren = directory.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory();
        }
    });
    Arrays.sort(directoryChildren);
    File[] fileChildren = directory.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isFile();
        }
    });
    Arrays.sort(fileChildren);
    int arraySize = directoryChildren.length + fileChildren.length;
    String[] sortedChildren = new String[arraySize];
    for (int i = 0; i < directoryChildren.length; i++) {
        sortedChildren[i] = directoryChildren[i].getName();
    }
    for (int i = directoryChildren.length; i < arraySize; i++) {
        sortedChildren[i] = fileChildren[i - directoryChildren.length].getName();
    }
    return sortedChildren;
}
}

我怀疑瓶颈在getSortedChildren方法中,我需要这个方法来给我一个目录的内容,首先是文件夹,按字母顺序排序,然后是按字母顺序排序的文件。

有没有办法针对包含大量文件的目录优化我的代码?

由于

0 个答案:

没有答案