KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > metainf > ServicesFileListener


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.apisupport.project.metainf;
21
22 import java.io.IOException JavaDoc;
23 import org.netbeans.api.project.FileOwnerQuery;
24 import org.netbeans.api.project.Project;
25 import org.netbeans.modules.apisupport.project.NbModuleProject;
26 import org.netbeans.modules.apisupport.project.Util;
27 import org.openide.filesystems.FileAttributeEvent;
28 import org.openide.filesystems.FileChangeListener;
29 import org.openide.filesystems.FileEvent;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileRenameEvent;
32
33 /**
34  * FileChangeListener for src/META-INF/services
35  */

36 final class ServicesFileListener implements FileChangeListener {
37     private static ServicesFileListener instance ;
38     
39     private ServicesFileListener() {
40         
41     }
42     public void fileFolderCreated(FileEvent fe) {
43         FileObject fo = fe.getFile();
44         if (fo.getName().equals("services")) { // NOI18N
45
if (fo.getParent().getName().equals("META-INF")) { // NOI18N
46
fo.removeFileChangeListener(this);
47                 fo.addFileChangeListener(this);
48             }
49         } else if (fo.getName().equals("META-INF")) { // NOI18N
50
// check if the folder is really /src/META-INF
51
Project prj = FileOwnerQuery.getOwner(fo);
52            
53             if (prj != null && prj instanceof NbModuleProject && ((NbModuleProject)prj).getSourceDirectory().equals(fo.getParent())) {
54                  fo.removeFileChangeListener(this);
55                  fo.addFileChangeListener(this);
56             }
57         }
58     }
59     public void fileDataCreated(FileEvent fe) {
60         updateFile(fe.getFile()) ;
61     }
62     
63     public void fileChanged(FileEvent fe) {
64         updateFile(fe.getFile());
65     }
66     
67     public void fileDeleted(FileEvent fe) {
68         try {
69             removeFile(fe.getFile());
70         } catch (IOException JavaDoc ex) {
71             Util.err.notify();
72         }
73     }
74     
75     public void fileRenamed(FileRenameEvent fe) {
76         try {
77             removeFile((FileObject)fe.getSource());
78         } catch (IOException JavaDoc ex) {
79             Util.err.notify();
80         }
81         updateFile(fe.getFile());
82     }
83     
84     public void fileAttributeChanged(FileAttributeEvent fe) {
85     }
86
87     private ServiceNodeHandler getHandler(FileObject fo) {
88         FileObject parent = fo.getParent();
89         if (parent != null && parent.getName().equals("services") ) {
90             parent = parent.getParent();
91             if (parent != null && parent.getName().equals("META-INF")) { // NOI18N
92
Project prj = FileOwnerQuery.getOwner(fo);
93                 return (prj == null) ? null :
94                     (ServiceNodeHandler)prj.getLookup().lookup(ServiceNodeHandler.class);
95             }
96         }
97         return null;
98     }
99
100     private void updateFile(FileObject fo) {
101         ServiceNodeHandler handler = getHandler (fo) ;
102         if (handler != null) {
103             handler.updateFile(fo);
104         }
105     }
106     
107     public void removeFile(FileObject fo) throws IOException JavaDoc {
108         ServiceNodeHandler handler = getHandler(fo);
109         if (handler != null) {
110             handler.removeFile(fo);
111         }
112     }
113     
114     public static ServicesFileListener getInstance() {
115         if (instance == null) {
116             instance = new ServicesFileListener();
117         }
118         return instance;
119     }
120     
121
122 }
123
Popular Tags