繁体   English   中英

我可以停止JTree缓存节点(在文件系统树中)吗?

[英]Can I stop JTree caching nodes (in a file system tree)?

我在文件系统中有一个用于目录的JTree实现。 我的问题是,当目录被删除或重命名时,树仍在其父目录中显示目录。 节点扩展后,似乎不会针对任何更改更新父树。

我编写的模型不缓存(我已经注释掉了它的有限缓存)-就像JTree本身已经缓存了节点。

代码(模型是嵌套的子类):

public class FileSystemTree
extends JTree
{

// *****************************************************************************
// INSTANCE CREATE/DELETE
// *****************************************************************************

public FileSystemTree() {
    this(new Model(null,null,null));
    }

public FileSystemTree(String startPath) {
    this(new Model(startPath,null,null));
    }

public FileSystemTree(FileSelector inc, FileSelector exc) {
    this(new Model(null,inc,exc));
    }

public FileSystemTree(String startPath, FileSelector inc, FileSelector exc) {
    this(new Model(startPath,inc,exc));
    }

private FileSystemTree(Model model) {
    super(model);

    //setLargeModel(true);
    setRootVisible(false);
    setShowsRootHandles(true);
    putClientProperty("JTree.lineStyle","Angled");
    }

// *****************************************************************************
// INSTANCE METHODS - ACCESSORS
// *****************************************************************************

public Object getRoot() {
    return getModel().getRoot();
    }

// *****************************************************************************
// INSTANCE METHODS
// *****************************************************************************

public String convertValueToText(Object value,boolean selected,boolean expanded,boolean leaf,int row,boolean hasFocus) {
    File                                fil=(File)value;

    return (fil.getName().length()!=0 ? fil.getName() : fil.getPath());
    }

// *****************************************************************************
// STATIC NESTED CLASSES - SUPPORTING MODEL
// *****************************************************************************

    static class Model
    extends Object
    implements TreeModel, FilenameFilter
    {

    private File                        root;                                   // tree root
    //ivate Map                         cache;                                  // caches child counts for directories
    private File[]                      fsRoots;                                // copy of file system roots
    private FileSelector                include;                                // inclusion selector
    private FileSelector                exclude;                                // exclusion selector
    private java.util.List              listeners=new ArrayList();

    public Model(String roo, FileSelector inc, FileSelector exc) {
        super();

        root=(roo==null ? DRIVES : new File(roo));
        //cache=new HashMap();
        fsRoots=(root==DRIVES ? rootList() : null);
        include=inc;
        exclude=exc;
        }

    // *****************************************************************************
    // METHODS - MODEL
    // *****************************************************************************

    public Object getRoot() {
        return root;
        }

    public Object getChild(Object parent, int index) {
        File                            dir=(File)parent;

        if(dir==DRIVES) {
            File[]   chl=fsRoots; //rootList();
            return (index<chl.length ? chl[index] : null);
            }
        else {
            String[] chl=dirList(dir);
            return (index<chl.length ? new File(dir,chl[index]) : null);
            }
        }

    public int getChildCount(Object parent) {
        File                            dir=(File)parent;
        //Integer                         cch=(Integer)cache.get(dir);

        //if(cch!=null) {
        //    return cch.intValue();
        //    }

        if(dir==DRIVES) {
            return fsRoots.length; //rootList().length;
            }
        else if(dir.isDirectory()) {
            return dirList(dir).length;
            }
        else {
            return 0;
            }
        }

    public boolean isLeaf(Object node) {
        return((File)node).isFile();
        }

    public void valueForPathChanged(TreePath path, Object newValue) {
        }

    public int getIndexOfChild(Object parent, Object child) {
        File                            dir=(File)parent;
        File                            fse=(File)child;

        if(dir==DRIVES) {
            File[] ca=fsRoots; //rootList();
            for(int xa=0; xa<ca.length; xa++) {
                if(fse.equals(ca[xa])) { return xa; }
                }
            }
        else {
            String[] ca=dirList(dir);
            for(int xa=0; xa<ca.length; ++xa) {
                if(fse.getName().equals(ca[xa])) { return xa; }
                }
            }
        return -1;
        }

     private File[] rootList() {
        File[]                          lst=File.listRoots();

        if(lst==null) { lst=new File[0]; }
        //cache.put(DRIVES,new Integer(lst.length));
        return lst;
        }

     private String[] dirList(File dir) {
        String[]                        lst=dir.list(this);

        if(lst==null) { lst=new String[0]; }
        //cache.put(dir,new Integer(lst.length));
        return lst;
        }

    // *****************************************************************************
    // METHODS - FILENAME FILTER
    // *****************************************************************************

    public boolean accept(File dir, String nam)  {
        return ((include==null || include.accept(dir,nam)) && (exclude==null || !exclude.accept(dir,nam)));
        }

    // *****************************************************************************
    // METHODS - LISTENER
    // *****************************************************************************

    public void addTreeModelListener(TreeModelListener listener) {
        if(listener != null && !listeners.contains(listener)) {
            listeners.add(listener);
            }
        }

    public void removeTreeModelListener(TreeModelListener listener) {
        if(listener != null) {
            listeners.remove(listener);
            }
        }

    public void fireTreeNodesChanged(TreeModelEvent evt) {
        Iterator                        itr=listeners.iterator();

        while(itr.hasNext()) {
            TreeModelListener listener=(TreeModelListener)itr.next();
            listener.treeNodesChanged(evt);
            }
        }

    public void fireTreeNodesInserted(TreeModelEvent evt) {
        Iterator                        itr=listeners.iterator();

        while(itr.hasNext()) {
            TreeModelListener listener=(TreeModelListener)itr.next();
            listener.treeNodesInserted(evt);
            }
        }

    public void fireTreeNodesRemoved(TreeModelEvent evt) {
        Iterator                        itr=listeners.iterator();

        while(itr.hasNext()) {
            TreeModelListener listener=(TreeModelListener)itr.next();
            listener.treeNodesRemoved(evt);
            }
        }

    public void fireTreeStructureChanged(TreeModelEvent evt) {
        Iterator                        itr=listeners.iterator();

        while(itr.hasNext()) {
            TreeModelListener listener=(TreeModelListener)itr.next();
            listener.treeStructureChanged(evt);
            }
        }
    } // END INNER CLASS

// *****************************************************************************
// STATIC PROPERTIES
// *****************************************************************************

static private final File               DRIVES=new File("*DRIVES");             // marker for listing file system drives

} // END PUBLIC CLASS

该模型负责将树结构的任何修改通知给侦听器。 如果没有此类通知,树将不会刷新自身。

您可以编写一些后台线程来查找文件系统中的更改,并在检测到目录已更改时触发一些模型修改事件。

在没有看到模型代码的情况下,您确定正在调用它,例如getChild方法吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM