KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > piagetproject > treestructure > NodeRetriever


1 /*
2  * FileRetriever.java
3  *
4  * Created on June 25, 2005, 11:10 AM
5  *
6  * To change this template, choose Tools | Options and locate the template under
7  * the Source Creation and Management node. Right-click the template and choose
8  * Open. You can then make changes to the template in the Source Editor.
9  */

10
11 package org.netbeans.modules.piagetproject.treestructure;
12
13 import java.awt.event.ActionEvent JavaDoc;
14 import java.beans.PropertyChangeEvent JavaDoc;
15 import java.beans.PropertyChangeListener JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FilenameFilter JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import javax.swing.Action JavaDoc;
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 /**
32  *
33  * @author loicsegapelli
34  */

35 public class NodeRetriever {
36     
37     public final static String JavaDoc USER = "user";
38     public final static String JavaDoc PARSED = "parsed";
39     public final static String JavaDoc LAYOUT = "layout";
40     
41     static ArrayList JavaDoc listeners;
42     
43     /** Creates a new instance of FileRetriever */
44     public NodeRetriever() {
45     }
46     
47     public static Node[] getUserFiles(Node parent) {
48         ArrayList JavaDoc list = new ArrayList JavaDoc();
49         listeners = new ArrayList JavaDoc();
50         DataObject data = (DataObject) parent.getLookup().lookup(DataObject.class);
51         FileObject object = data.getPrimaryFile();
52         File JavaDoc f = FileUtil.toFile(object);
53         
54         FilenameFilter JavaDoc filter = new FilenameFilter JavaDoc() {
55             public boolean accept(File JavaDoc dir, String JavaDoc name) {
56                 return name.endsWith(USER);
57             }
58         };
59         
60         File JavaDoc 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 JavaDoc 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     //
104
public static Children getUsers(int level) {
105         File JavaDoc workdir = new File JavaDoc(PiagetProject.workPath);
106         String JavaDoc 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 JavaDoc LEVEL = myLevel;
120         
121         FilenameFilter JavaDoc filter = new FilenameFilter JavaDoc() {
122             public boolean accept(File JavaDoc dir, String JavaDoc name) {
123                 return name.lastIndexOf(LEVEL)!=-1;
124             }
125         };
126         
127         File JavaDoc f [] = workdir.listFiles(filter);
128         
129         Node users [] = new Node [f.length];
130         for(int i=0; i<f.length; i++) {
131             String JavaDoc 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     //
144
public static Children getSessions(String JavaDoc userpath, String JavaDoc username, String JavaDoc level) {
145         File JavaDoc workdir = new File JavaDoc(userpath);
146         FilenameFilter JavaDoc filter = new FilenameFilter JavaDoc() {
147             public boolean accept(File JavaDoc dir, String JavaDoc name) {
148                 return name.lastIndexOf(PARSED)!=-1;
149             }
150         };
151         
152         File JavaDoc 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 JavaDoc() {
163             public boolean accept(File JavaDoc dir, String JavaDoc name) {
164                 return name.endsWith(LAYOUT);
165             }
166         };
167         
168         File JavaDoc 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 JavaDoc{
179         
180         PiagetNode toRefresh;
181         
182         public void targetNode(PiagetNode toRefresh){
183             this.toRefresh = toRefresh;
184         }
185         
186         public void propertyChange(PropertyChangeEvent JavaDoc evt){
187             if(evt.getPropertyName().equals("myDestroy")){
188                 Action JavaDoc refresh = toRefresh.getPreferredAction();
189                 if(refresh.getValue(Action.NAME).equals("refresh"))
190                     refresh.actionPerformed(new ActionEvent JavaDoc(this, 0, null));
191             }
192         }
193         
194     }
195     
196 }
197
Popular Tags