1 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 { 36 private SharedFolderPlugin plugin; 37 private Map cache; 38 39 public FolderTreeModel(SharedFolderPlugin plugin) 40 { 41 this.plugin = plugin; 42 this.cache = new HashMap (); 43 } 44 45 46 public Object getRoot() { 47 try { 48 return plugin.getFolder(FolderInfo.ROOT_ID); 49 } catch (Exception e) { 50 e.printStackTrace(); 51 } 52 53 return null; 54 } 55 56 public int getChildCount(Object parent) { 57 FolderInfo info = (FolderInfo)parent; 58 return getChildrenFromCache(info).size(); 59 } 60 61 public boolean isLeaf(Object node) { 62 FolderInfo info = (FolderInfo)node; 63 return !info.isReadable(); 64 } 65 66 public Object getChild(Object parent, int index) { 67 FolderInfo info = (FolderInfo)parent; 68 return getChildrenFromCache(info).get(index); 69 } 70 71 public int getIndexOfChild(Object parent, Object child) { 72 FolderInfo info = (FolderInfo)parent; 73 return getChildrenFromCache(info).indexOf(child); 74 } 75 76 78 private List getChildrenFromCache(FolderInfo info) 79 { 80 List children = (List )cache.get(info); 81 if(children == null) 82 { 83 try { 84 children = plugin.listFolders(info.getId()); 85 } catch (Exception e) { 86 Logging.getLogger().warning("Unable to list folders : " + e); 87 children = new ArrayList (); 88 } 89 cache.put(info, children); 90 } 91 return children; 92 } 93 94 public void addTreeModelListener(TreeModelListener l) {} 96 public void removeTreeModelListener(TreeModelListener l) {} 97 public void valueForPathChanged(TreePath path, Object newValue) {} 98 } | Popular Tags |