01: /*
02: * Lucane - a collaborative platform
03: * Copyright (C) 2005 Vincent Fiack <vfiack@mail15.com>
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: */
19:
20: package org.lucane.applications.sharedfolder.gui;
21:
22: import org.lucane.applications.sharedfolder.SharedFolderPlugin;
23: import org.lucane.applications.sharedfolder.model.FolderInfo;
24: import org.lucane.common.Logging;
25:
26: import javax.swing.tree.TreeModel;
27: import javax.swing.tree.TreePath;
28: import javax.swing.event.TreeModelListener;
29: import java.util.Map;
30: import java.util.HashMap;
31: import java.util.List;
32: import java.util.ArrayList;
33:
34: public class FolderTreeModel implements TreeModel {
35: private SharedFolderPlugin plugin;
36: private Map cache;
37:
38: public FolderTreeModel(SharedFolderPlugin plugin) {
39: this .plugin = plugin;
40: this .cache = new HashMap();
41: }
42:
43: public Object getRoot() {
44: try {
45: return plugin.getFolder(FolderInfo.ROOT_ID);
46: } catch (Exception e) {
47: e.printStackTrace();
48: }
49:
50: return null;
51: }
52:
53: public int getChildCount(Object parent) {
54: FolderInfo info = (FolderInfo) parent;
55: return getChildrenFromCache(info).size();
56: }
57:
58: public boolean isLeaf(Object node) {
59: FolderInfo info = (FolderInfo) node;
60: return !info.isReadable();
61: }
62:
63: public Object getChild(Object parent, int index) {
64: FolderInfo info = (FolderInfo) parent;
65: return getChildrenFromCache(info).get(index);
66: }
67:
68: public int getIndexOfChild(Object parent, Object child) {
69: FolderInfo info = (FolderInfo) parent;
70: return getChildrenFromCache(info).indexOf(child);
71: }
72:
73: //-- private helpers
74:
75: private List getChildrenFromCache(FolderInfo info) {
76: List children = (List) cache.get(info);
77: if (children == null) {
78: try {
79: children = plugin.listFolders(info.getId());
80: } catch (Exception e) {
81: Logging.getLogger().warning(
82: "Unable to list folders : " + e);
83: children = new ArrayList();
84: }
85: cache.put(info, children);
86: }
87: return children;
88: }
89:
90: //-- not implemented
91: public void addTreeModelListener(TreeModelListener l) {
92: }
93:
94: public void removeTreeModelListener(TreeModelListener l) {
95: }
96:
97: public void valueForPathChanged(TreePath path, Object newValue) {
98: }
99: }
|