KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > sharedfolder > gui > FolderTreeModel


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2005 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
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 JavaDoc;
27 import javax.swing.tree.TreePath JavaDoc;
28 import javax.swing.event.TreeModelListener JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ArrayList JavaDoc;
33
34 public class FolderTreeModel implements TreeModel JavaDoc
35 {
36     private SharedFolderPlugin plugin;
37     private Map JavaDoc cache;
38
39     public FolderTreeModel(SharedFolderPlugin plugin)
40     {
41         this.plugin = plugin;
42         this.cache = new HashMap JavaDoc();
43     }
44
45
46     public Object JavaDoc getRoot() {
47         try {
48             return plugin.getFolder(FolderInfo.ROOT_ID);
49         } catch (Exception JavaDoc e) {
50             e.printStackTrace();
51         }
52
53         return null;
54     }
55
56     public int getChildCount(Object JavaDoc parent) {
57         FolderInfo info = (FolderInfo)parent;
58         return getChildrenFromCache(info).size();
59     }
60
61     public boolean isLeaf(Object JavaDoc node) {
62         FolderInfo info = (FolderInfo)node;
63         return !info.isReadable();
64     }
65
66     public Object JavaDoc getChild(Object JavaDoc parent, int index) {
67         FolderInfo info = (FolderInfo)parent;
68         return getChildrenFromCache(info).get(index);
69     }
70
71     public int getIndexOfChild(Object JavaDoc parent, Object JavaDoc child) {
72         FolderInfo info = (FolderInfo)parent;
73         return getChildrenFromCache(info).indexOf(child);
74     }
75
76     //-- private helpers
77

78     private List JavaDoc getChildrenFromCache(FolderInfo info)
79     {
80         List JavaDoc children = (List JavaDoc)cache.get(info);
81         if(children == null)
82         {
83             try {
84                 children = plugin.listFolders(info.getId());
85             } catch (Exception JavaDoc e) {
86                 Logging.getLogger().warning("Unable to list folders : " + e);
87                 children = new ArrayList JavaDoc();
88             }
89             cache.put(info, children);
90         }
91         return children;
92     }
93
94     //-- not implemented
95
public void addTreeModelListener(TreeModelListener JavaDoc l) {}
96     public void removeTreeModelListener(TreeModelListener JavaDoc l) {}
97     public void valueForPathChanged(TreePath JavaDoc path, Object JavaDoc newValue) {}
98 }
Popular Tags