1 10 11 package org.netbeans.modules.piagetproject.treestructure; 12 13 import java.awt.event.ActionEvent ; 14 import java.beans.PropertyChangeEvent ; 15 import java.beans.PropertyChangeListener ; 16 import java.io.File ; 17 import java.io.FilenameFilter ; 18 import java.util.ArrayList ; 19 import javax.swing.Action ; 20 import org.netbeans.modules.piagetproject.PiagetLogicalView; 21 import org.netbeans.modules.piagetproject.PiagetNode; 22 import org.netbeans.modules.piagetproject.PiagetProject; 23 import org.netbeans.modules.piagetproject.PiagetProjectFactory; 24 import org.openide.filesystems.FileObject; 25 import org.openide.filesystems.FileUtil; 26 import org.openide.loaders.DataObject; 27 import org.openide.nodes.Children; 28 import org.openide.nodes.Node; 29 30 31 35 public class NodeRetriever { 36 37 public final static String USER = "user"; 38 public final static String PARSED = "parsed"; 39 public final static String LAYOUT = "layout"; 40 41 static ArrayList listeners; 42 43 44 public NodeRetriever() { 45 } 46 47 public static Node[] getUserFiles(Node parent) { 48 ArrayList list = new ArrayList (); 49 listeners = new ArrayList (); 50 DataObject data = (DataObject) parent.getLookup().lookup(DataObject.class); 51 FileObject object = data.getPrimaryFile(); 52 File f = FileUtil.toFile(object); 53 54 FilenameFilter filter = new FilenameFilter () { 55 public boolean accept(File dir, String name) { 56 return name.endsWith(USER); 57 } 58 }; 59 60 File user [] = f.listFiles(filter); 61 for(int i=0; i<user.length; i++) { 62 FileObject fo = FileUtil.toFileObject(user [i]); 63 try{ 64 DataObject dataObj = DataObject.find(fo); 65 Node n = dataObj.getNodeDelegate(); 66 DestroyChangeListener dl = new DestroyChangeListener(); 67 n.addPropertyChangeListener(dl); 68 list.add(n); 69 listeners.add(dl); 70 }catch (Exception e) { 71 e.printStackTrace(System.out); 72 } 73 } 74 75 if(list.isEmpty()) return null; 76 77 Node [] result = new Node [list.size()]; 78 for(int i=0; i<result.length; i++) { 79 result [i] = (Node)list.get(i); 80 } 81 82 83 return result; 84 } 85 86 public static void initListeners(PiagetNode node){ 87 for(int i=0; i<listeners.size(); i++){ 88 DestroyChangeListener dl = (DestroyChangeListener)listeners.get(i); 89 dl.targetNode(node); 90 } 91 } 92 93 public static Node[] getLevels() { 94 95 LevelNode levels [] = new LevelNode [3]; 96 for(int i=1; i<4; i++) { 97 levels [i-1] = new LevelNode(i); 98 } 99 100 return levels; 101 } 102 103 public static Children getUsers(int level) { 105 File workdir = new File (PiagetProject.workPath); 106 String myLevel = ""; 107 switch(level) { 108 case 1: 109 myLevel = PiagetProjectFactory.LEVEL1; 110 break; 111 case 2: 112 myLevel = PiagetProjectFactory.LEVEL2; 113 break; 114 case 3: 115 myLevel = PiagetProjectFactory.LEVEL3; 116 break; 117 } 118 119 final String LEVEL = myLevel; 120 121 FilenameFilter filter = new FilenameFilter () { 122 public boolean accept(File dir, String name) { 123 return name.lastIndexOf(LEVEL)!=-1; 124 } 125 }; 126 127 File f [] = workdir.listFiles(filter); 128 129 Node users [] = new Node [f.length]; 130 for(int i=0; i<f.length; i++) { 131 String username = f[i].getName(); 132 int dotIndex = username.indexOf("."); 133 if(dotIndex!=-1) username = username.substring(dotIndex+1, username.length()); 134 users[i] = new UserNode(f[i].getAbsolutePath(), username, LEVEL); 135 } 136 137 if(f.length == 0) return Children.LEAF; 138 Children.Array c = new Children.Array(); 139 c.add(users); 140 return c; 141 } 142 143 public static Children getSessions(String userpath, String username, String level) { 145 File workdir = new File (userpath); 146 FilenameFilter filter = new FilenameFilter () { 147 public boolean accept(File dir, String name) { 148 return name.lastIndexOf(PARSED)!=-1; 149 } 150 }; 151 152 File f [] = workdir.listFiles(filter); 153 154 SessionNode sessions [] = new SessionNode [f.length]; 155 for(int i=0; i<f.length; i++) { 156 sessions[i] = new SessionNode(f[i].getName(), f[i].getAbsolutePath(), username, level); 157 } 158 159 Children.Array c = new Children.Array(); 160 c.add(sessions); 161 162 filter = new FilenameFilter () { 163 public boolean accept(File dir, String name) { 164 return name.endsWith(LAYOUT); 165 } 166 }; 167 168 File layout [] = workdir.listFiles(filter); 169 if(layout!=null && layout.length>0){ 170 LayoutNode node = new LayoutNode(layout[0].getAbsolutePath(), username, level); 171 c.add(new Node [] {node}); 172 } 173 174 if(c.getNodesCount()==0) return Children.LEAF; 175 return c; 176 } 177 178 private static class DestroyChangeListener implements PropertyChangeListener { 179 180 PiagetNode toRefresh; 181 182 public void targetNode(PiagetNode toRefresh){ 183 this.toRefresh = toRefresh; 184 } 185 186 public void propertyChange(PropertyChangeEvent evt){ 187 if(evt.getPropertyName().equals("myDestroy")){ 188 Action refresh = toRefresh.getPreferredAction(); 189 if(refresh.getValue(Action.NAME).equals("refresh")) 190 refresh.actionPerformed(new ActionEvent (this, 0, null)); 191 } 192 } 193 194 } 195 196 } 197 | Popular Tags |