FolderTreeModel.java :  » Groupware » lucane » org » lucane » applications » sharedfolder » gui » Java Open Source

Java Open Source » Groupware » lucane 
lucane » org » lucane » applications » sharedfolder » gui » FolderTreeModel.java
/*
 * Lucane - a collaborative platform
 * Copyright (C) 2005  Vincent Fiack <vfiack@mail15.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.lucane.applications.sharedfolder.gui;

import org.lucane.applications.sharedfolder.SharedFolderPlugin;
import org.lucane.applications.sharedfolder.model.FolderInfo;
import org.lucane.common.Logging;

import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.event.TreeModelListener;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

public class FolderTreeModel implements TreeModel
{
    private SharedFolderPlugin plugin;
    private Map cache;

    public FolderTreeModel(SharedFolderPlugin plugin)
    {
        this.plugin = plugin;
        this.cache = new HashMap();
    }


    public Object getRoot() {
        try {
            return plugin.getFolder(FolderInfo.ROOT_ID);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public int getChildCount(Object parent) {
        FolderInfo info = (FolderInfo)parent;
        return getChildrenFromCache(info).size();
    }

    public boolean isLeaf(Object node) {
        FolderInfo info = (FolderInfo)node;
        return !info.isReadable();
    }

    public Object getChild(Object parent, int index) {
        FolderInfo info = (FolderInfo)parent;
        return getChildrenFromCache(info).get(index);
    }

    public int getIndexOfChild(Object parent, Object child) {
        FolderInfo info = (FolderInfo)parent;
        return getChildrenFromCache(info).indexOf(child);
    }

    //-- private helpers

    private List getChildrenFromCache(FolderInfo info)
    {
        List children = (List)cache.get(info);
        if(children == null)
        {
            try {
                children = plugin.listFolders(info.getId());
            } catch (Exception e) {
                Logging.getLogger().warning("Unable to list folders : " + e);
                children = new ArrayList();
            }
            cache.put(info, children);
        }
        return children;
    }

    //-- not implemented
    public void addTreeModelListener(TreeModelListener l) {}
    public void removeTreeModelListener(TreeModelListener l) {}
    public void valueForPathChanged(TreePath path, Object newValue) {}
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.