KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > impl > FolderChildren


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.mimelookup.impl;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33 import org.openide.filesystems.FileAttributeEvent;
34 import org.openide.filesystems.FileChangeAdapter;
35 import org.openide.filesystems.FileEvent;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileRenameEvent;
38 import org.openide.filesystems.Repository;
39
40 /**
41  *
42  * @author vita
43  */

44 public final class FolderChildren {
45
46     public static final String JavaDoc PROP_CHILDREN = "FolderChildren.PROP_CHILDREN"; //NOI18N
47
public static final String JavaDoc PROP_CHILD_CHANGED = "FolderChildren.PROP_CHILD_CHANGED"; //NOI18N
48
public static final String JavaDoc PROP_ATTRIBUTES = "FolderChildren.PROP_ATTRIBUTES"; //NOI18N
49

50     private static final Logger JavaDoc LOG = Logger.getLogger(FolderChildren.class.getName());
51     
52     private String JavaDoc [] pathElement;
53     private boolean includeSubfolders;
54     
55     private final String JavaDoc LOCK = new String JavaDoc("FolderChildren.LOCK"); //NOI18N
56

57     private FileObject currentFolder = null;
58     private boolean isTargetFolder = false;
59     
60     private FolderListener listener = new FolderListener(this);
61
62     private List JavaDoc children = Collections.EMPTY_LIST;
63     
64     private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
65
66     public FolderChildren(String JavaDoc path) {
67         this(path, true);
68     }
69     
70     /** Creates a new instance of FolderPathLookup */
71     public FolderChildren(String JavaDoc path, boolean includeSubfolders) {
72         this.pathElement = path.split("/"); //NOI18N
73
this.includeSubfolders = includeSubfolders;
74         rebuild();
75     }
76     
77     public List JavaDoc getChildren() {
78         synchronized (LOCK) {
79             return children;
80         }
81     }
82     
83     public Map JavaDoc getFolderAttributes() {
84         synchronized (LOCK) {
85             if (!isTargetFolder) {
86                 return Collections.EMPTY_MAP;
87             }
88             
89             HashMap JavaDoc attrs = new HashMap JavaDoc();
90             for (Enumeration JavaDoc e = currentFolder.getAttributes(); e.hasMoreElements(); ) {
91                 String JavaDoc attrName = (String JavaDoc) e.nextElement();
92                 Object JavaDoc attrValue = currentFolder.getAttribute(attrName);
93                 
94                 if (!attrs.containsKey(attrName)) {
95                     attrs.put(attrName, attrValue);
96                 }
97             }
98             
99             return attrs;
100         }
101     }
102     
103     public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
104         pcs.addPropertyChangeListener(l);
105     }
106     
107     public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
108         pcs.removePropertyChangeListener(l);
109     }
110     
111     private void rebuild() {
112         PropertyChangeEvent JavaDoc event = null;
113         
114         synchronized (LOCK) {
115             Object JavaDoc [] result = findTarget(pathElement);
116             FileObject f = (FileObject) result[0];
117             boolean isTarget = ((Boolean JavaDoc) result[1]).booleanValue();
118
119             // The target folder has been created/deleted
120
if (f != currentFolder) {
121                 // Stop listening the current folder is going to change
122
if (currentFolder != null) {
123                     currentFolder.removeFileChangeListener(listener);
124                 }
125
126                 // Set the current folder and its is-target-flag
127
currentFolder = f;
128                 isTargetFolder = isTarget;
129
130                 LOG.finest("Setting currentFolder = '" + currentFolder.getPath() + "'"); //NOI18N
131
LOG.finest("Setting isTargetFolder = '" + isTargetFolder + "'"); //NOI18N
132

133                 // Start listening again
134
currentFolder.addFileChangeListener(listener);
135             }
136
137             // Refresh the children
138
List JavaDoc newChildren;
139             if (isTargetFolder) {
140                 FileObject [] files = currentFolder.getChildren();
141                 newChildren = new ArrayList JavaDoc(files.length);
142                 
143                 for (int i = 0; i < files.length; i++) {
144                     if (includeSubfolders || !files[i].isFolder()) {
145                         newChildren.add(files[i]);
146                     }
147                 }
148                 
149                 newChildren = Collections.unmodifiableList(newChildren);
150             } else {
151                 newChildren = Collections.EMPTY_LIST;
152             }
153             
154             if (!children.equals(newChildren)) {
155                 event = new PropertyChangeEvent JavaDoc(this, PROP_CHILDREN, children, newChildren);
156                 children = newChildren;
157             }
158         }
159         
160         if (event != null) {
161             pcs.firePropertyChange(event);
162         }
163     }
164
165     private void fileContentChanged(FileObject file) {
166         PropertyChangeEvent JavaDoc event = null;
167         
168         synchronized (LOCK) {
169             if (isTargetFolder) {
170                 event = new PropertyChangeEvent JavaDoc(this, PROP_CHILD_CHANGED, file, file);
171             }
172         }
173         
174         if (event != null) {
175             pcs.firePropertyChange(event);
176         }
177     }
178
179     private void attributeChanged(FileObject file, String JavaDoc attribName, Object JavaDoc oldValue, Object JavaDoc newValue) {
180         PropertyChangeEvent JavaDoc event = null;
181         
182         synchronized (LOCK) {
183             if (isTargetFolder && currentFolder == file) {
184                 event = new PropertyChangeEvent JavaDoc(this, PROP_ATTRIBUTES, null, null);
185             }
186         }
187         
188         if (event != null) {
189             pcs.firePropertyChange(event);
190         }
191     }
192     
193     private Object JavaDoc [] findTarget(String JavaDoc [] path) {
194         FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot();
195         boolean isTarget = 0 == path.length;
196         
197         for (int i = 0; i < path.length; i++) {
198             FileObject f = target.getFileObject(path[i]);
199
200             if (f == null || !f.isFolder() || !f.isValid() || f.isVirtual()) {
201                 break;
202             } else {
203                 target = f;
204                 isTarget = i + 1 == path.length;
205             }
206         }
207         
208         return new Object JavaDoc [] { target, Boolean.valueOf(isTarget) };
209     }
210
211     private static class FolderListener extends FileChangeAdapter {
212         
213         private WeakReference JavaDoc ref = null;
214         
215         public FolderListener(FolderChildren folderChildren) {
216             this.ref = new WeakReference JavaDoc(folderChildren);
217         }
218         
219         public void fileRenamed(FileRenameEvent fe) {
220             handleRebuildEvent(fe);
221         }
222
223         public void fileDeleted(FileEvent fe) {
224             handleRebuildEvent(fe);
225         }
226
227         public void fileFolderCreated(FileEvent fe) {
228             handleRebuildEvent(fe);
229         }
230         
231         public void fileAttributeChanged(FileAttributeEvent fe) {
232             FolderChildren folderChildren = (FolderChildren) ref.get();
233             if (folderChildren != null) {
234                 folderChildren.attributeChanged(fe.getFile(), fe.getName(), fe.getOldValue(), fe.getNewValue());
235             } else {
236                 ((FileObject) fe.getSource()).removeFileChangeListener(this);
237             }
238         }
239
240         public void fileDataCreated(FileEvent fe) {
241             handleRebuildEvent(fe);
242         }
243
244         public void fileChanged(FileEvent fe) {
245             FolderChildren folderChildren = (FolderChildren) ref.get();
246             if (folderChildren != null) {
247                 folderChildren.fileContentChanged(fe.getFile());
248             } else {
249                 ((FileObject) fe.getSource()).removeFileChangeListener(this);
250             }
251         }
252         
253         private void handleRebuildEvent(FileEvent fe) {
254             FolderChildren folderChildren = (FolderChildren) ref.get();
255             if (folderChildren != null) {
256                 folderChildren.rebuild();
257             } else {
258                 ((FileObject) fe.getSource()).removeFileChangeListener(this);
259             }
260         }
261
262     } // End of FolderListener class
263
}
264
Popular Tags