KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > FileSystemListener


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.core.registry;
21
22 import org.netbeans.api.registry.AttributeEvent;
23 import org.netbeans.api.registry.BindingEvent;
24 import org.netbeans.api.registry.ContextEvent;
25 import org.netbeans.api.registry.SubcontextEvent;
26 import org.netbeans.spi.registry.BasicContext;
27 import org.netbeans.spi.registry.SpiUtils;
28 import org.openide.filesystems.FileChangeListener;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 class FileSystemListener implements FileChangeListener {
36     
37     private ContextImpl rootContext;
38     
39
40     public FileSystemListener(ContextImpl rootContext) {
41         this.rootContext =rootContext;
42     }
43     
44     public void fileAttributeChanged(org.openide.filesystems.FileAttributeEvent fe) {
45         FileObject fo = fe.getFile();
46         if (!acceptFileObject(rootContext.getFolder(), fo)) {
47             return;
48         }
49         String JavaDoc attrName = fe.getName();
50         if (attrName != null && attrName.startsWith(ContextBindings.PRIMITIVE_BINDING_PREFIX)) {
51             BindingEvent be = null;
52             BasicContext ctx1 = rootContext.getCtx(fo);
53             String JavaDoc bindingName = attrName.substring(ContextBindings.PRIMITIVE_BINDING_PREFIX.length());
54             if (fe.getNewValue() == null) {
55                 be = SpiUtils.createBindingEvent(ctx1, bindingName, BindingEvent.BINDING_REMOVED);
56             } else if (fe.getOldValue() == null) {
57                 be = SpiUtils.createBindingEvent(ctx1, bindingName, BindingEvent.BINDING_ADDED);
58             } else {
59                 be = SpiUtils.createBindingEvent(ctx1, bindingName, BindingEvent.BINDING_MODIFIED);
60             }
61             fireContextEvent(fo, be);
62             return;
63         }
64         
65         BasicContext ctx;
66         String JavaDoc bindingName = null;
67         if (!fo.isFolder()) {
68             bindingName = fo.getName();
69             ctx = rootContext.getCtx(fo.getParent());
70         } else {
71             // #34156 - attribute name can be null
72
if (attrName != null && attrName.startsWith(ContextImpl.PRIMITIVE_BINDING_ATTR_PREFIX)) {
73                int index = attrName.indexOf('/');
74                if (index != -1) {
75                    bindingName = attrName.substring(ContextImpl.PRIMITIVE_BINDING_ATTR_PREFIX.length(), index);
76                    attrName = attrName.substring(index+1);
77                }
78             }
79             ctx = rootContext.getCtx(fo);
80         }
81         Object JavaDoc v = fe.getNewValue();
82         AttributeEvent ae = null;
83         if (fe.getNewValue() == null) {
84             ae = SpiUtils.createAttributeEvent(ctx, bindingName, attrName, AttributeEvent.ATTRIBUTE_REMOVED);
85         } else if (fe.getOldValue() == null) {
86             ae = SpiUtils.createAttributeEvent(ctx, bindingName, attrName, AttributeEvent.ATTRIBUTE_ADDED);
87         } else {
88             ae = SpiUtils.createAttributeEvent(ctx, bindingName, attrName, AttributeEvent.ATTRIBUTE_MODIFIED);
89         }
90         fireContextEvent(fo, ae);
91     }
92     
93     public void fileChanged(org.openide.filesystems.FileEvent fe) {
94         FileObject fo = fe.getFile();
95         if (!acceptFileObject(rootContext.getFolder(), fo)) {
96             return;
97         }
98         boolean contextExisted = (rootContext.getContextCache().retrieveContextFromCache(fo.getParent()) != null);
99         ContextImpl ctx = rootContext.getCtx(fo.getParent());
100         if(fe.firedFrom(ContextBindings.referenceAction)) {
101             if (ctx.getContextBindings().isBindingFile(fo)) {
102                 fireContextEvent(fo, SpiUtils.createBindingEvent(ctx, fo.getName(), BindingEvent.BINDING_MODIFIED));
103             }
104         } else {
105             // change from outside of core/registry:
106
if (ctx.getContextBindings().isRelevantChange(fo, false)) {
107                 fireContextEvent(fo, SpiUtils.createBindingEvent(ctx, fo.getName(), BindingEvent.BINDING_MODIFIED));
108             }
109         }
110     }
111     
112     public void fileDataCreated(org.openide.filesystems.FileEvent fe) {
113         FileObject fo = fe.getFile();
114         if (!acceptFileObject(rootContext.getFolder(), fo)) {
115             return;
116         }
117         boolean contextExisted = (rootContext.getContextCache().retrieveContextFromCache(fo.getParent()) != null);
118         ContextImpl ctx = rootContext.getCtx(fo.getParent());
119         if (fo.isFolder()) {
120             fireContextEvent(fo, SpiUtils.createSubcontextEvent(ctx, fo.getName(), SubcontextEvent.SUBCONTEXT_ADDED));
121         } else {
122             if(fe.firedFrom(ContextBindings.referenceAction)) {
123                 if (ctx.getContextBindings().isBindingFile(fo)) {
124                     fireContextEvent(fo, SpiUtils.createBindingEvent(ctx, fo.getName(), BindingEvent.BINDING_ADDED));
125                 }
126             } else {
127                 // change from outside of core/registry:
128
if (ctx.getContextBindings().isRelevantChange(fo, false)) {
129                     fireContextEvent(fo, SpiUtils.createBindingEvent(ctx, fo.getName(), BindingEvent.BINDING_ADDED));
130                 }
131             }
132         }
133     }
134     
135     public void fileDeleted(org.openide.filesystems.FileEvent fe) {
136         FileObject fo = fe.getFile();
137         if (!acceptFileObject(rootContext.getFolder(), fo)) {
138             return;
139         }
140         boolean contextExisted = (rootContext.getContextCache().retrieveContextFromCache(fo.getParent()) != null);
141         ContextImpl ctx = rootContext.getCtx(fo.getParent());
142         if (fo.isFolder()) {
143             // folder fo was deleted, so start firing events from its parent
144
fireContextEvent(fo.getParent(), SpiUtils.createSubcontextEvent(ctx, fo.getName(), SubcontextEvent.SUBCONTEXT_REMOVED));
145         } else {
146             if(fe.firedFrom(ContextBindings.referenceAction)) {
147                 if (isBindingFile(fo)) {
148                     fireContextEvent(fo, SpiUtils.createBindingEvent(ctx, fo.getName(), BindingEvent.BINDING_REMOVED));
149                 }
150             } else {
151                 // change from outside of core/registry:
152
if (ctx.getContextBindings().isRelevantChange(fo, true)) {
153                     fireContextEvent(fo, SpiUtils.createBindingEvent(ctx, fo.getName(), BindingEvent.BINDING_REMOVED));
154                 }
155             }
156         }
157     }
158     
159     public static boolean isBindingFile(FileObject fo) {
160         String JavaDoc fileExt = fo.getExt();
161         Collection JavaDoc exts = ObjectBinding.getFileExtensions();
162         for (Iterator JavaDoc iterator = exts.iterator(); iterator.hasNext();) {
163             String JavaDoc ext = (String JavaDoc) iterator.next();
164             if (fileExt.equals(ext)) return true;
165         }
166         return false;
167     }
168     
169     public void fileFolderCreated(org.openide.filesystems.FileEvent fe) {
170         FileObject fo = fe.getFile();
171         if (!acceptFileObject(rootContext.getFolder(), fo)) {
172             return;
173         }
174         BasicContext ctx = rootContext.getCtx(fo.getParent());
175         fireContextEvent(fo, SpiUtils.createSubcontextEvent(ctx, fo.getName(), SubcontextEvent.SUBCONTEXT_ADDED));
176     }
177     
178     public void fileRenamed(org.openide.filesystems.FileRenameEvent fe) {
179         //TODO: does it need to be handled?
180
}
181
182     private void fireContextEvent(FileObject fo, ContextEvent ce) {
183         fireContextEvent(rootContext, fo, ce);
184     }
185     
186     private static boolean acceptFileObject(FileObject root, FileObject child) {
187         return (FileUtil.isParentOf(root, child) || root.equals(child));
188     }
189     
190     public static void fireContextEvent(ContextImpl rootContext, FileObject fo, ContextEvent ce) {
191         // go throught the hierarchy and check whether the context exist for
192
// the fileobject. if yes then fire events on the context
193
while (fo != null && acceptFileObject(rootContext.getFolder(), fo)) {
194             ContextImpl ctx = rootContext.getContextCache().retrieveContextFromCache(fo);
195             if (ctx != null) {
196                 if (ce instanceof AttributeEvent) {
197                     ctx.fireAttributeEvent((AttributeEvent)ce);
198                 } else if (ce instanceof BindingEvent) {
199                     ctx.fireBindingEvent((BindingEvent)ce);
200                 } else if (ce instanceof SubcontextEvent) {
201                     ctx.fireSubcontextEvent((SubcontextEvent)ce);
202                 }
203             }
204             fo = fo.getParent();
205         }
206     }
207
208     
209 }
210     
211
Popular Tags