KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > classpath > JspSourcePathImplementation


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.web.project.classpath;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyChangeSupport JavaDoc;
27 import org.netbeans.modules.web.project.ui.customizer.WebProjectProperties;
28 import org.netbeans.spi.java.classpath.ClassPathImplementation;
29 import org.netbeans.spi.java.classpath.PathResourceImplementation;
30 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
31 import org.netbeans.spi.project.support.ant.AntProjectHelper;
32 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
33 import org.openide.ErrorManager;
34 import org.openide.filesystems.FileChangeListener;
35 import org.openide.filesystems.FileEvent;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileRenameEvent;
38 import org.openide.filesystems.FileStateInvalidException;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.util.WeakListeners;
41
42 /**
43  * Implementation of ClassPathImplementation which represents the Web Pages folder.
44  *
45  * @author Andrei Badea
46  */

47 final class JspSourcePathImplementation implements ClassPathImplementation, PropertyChangeListener JavaDoc {
48
49     private PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
50     private List JavaDoc resources;
51     private AntProjectHelper helper;
52     private PropertyEvaluator evaluator;
53     private ProjectDirectoryListener projectDirListener;
54
55     /**
56      * Construct the implementation.
57      */

58     public JspSourcePathImplementation(AntProjectHelper helper, PropertyEvaluator eval) {
59         assert helper != null;
60         assert eval != null;
61         this.helper = helper;
62         this.evaluator = eval;
63         eval.addPropertyChangeListener(WeakListeners.propertyChange(this, eval));
64         FileObject projectDir = helper.getProjectDirectory();
65         projectDirListener = new ProjectDirectoryListener();
66         projectDir.addFileChangeListener(FileUtil.weakFileChangeListener(projectDirListener, projectDir));
67     }
68
69     public List JavaDoc /*<PathResourceImplementation>*/ getResources() {
70         synchronized (this) {
71             if (this.resources != null) {
72                 return resources;
73             }
74         }
75         PathResourceImplementation webDocbaseDirRes = null;
76         String JavaDoc webDocbaseDir = evaluator.getProperty(WebProjectProperties.WEB_DOCBASE_DIR);
77         if (webDocbaseDir != null) {
78             FileObject webDocbaseDirFO = helper.resolveFileObject(webDocbaseDir);
79             if (webDocbaseDirFO != null) {
80                 try {
81                     webDocbaseDirRes = ClassPathSupport.createResource(webDocbaseDirFO.getURL());
82                 } catch (FileStateInvalidException e) {
83                     ErrorManager.getDefault().notify(e);
84                 }
85             }
86         }
87         synchronized (this) {
88             if (this.resources == null) {
89                 if (webDocbaseDirRes != null) {
90                     this.resources = Collections.singletonList(webDocbaseDirRes);
91                 } else {
92                     this.resources = Collections.EMPTY_LIST;
93                 }
94             }
95         }
96         return this.resources;
97     }
98
99     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
100         support.addPropertyChangeListener (listener);
101     }
102
103     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
104         support.removePropertyChangeListener (listener);
105     }
106
107
108     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
109         if (WebProjectProperties.WEB_DOCBASE_DIR.equals(evt.getPropertyName())) {
110             fireChange();
111         }
112     }
113     
114     private void fireChange() {
115         synchronized (this) {
116             this.resources = null;
117         }
118         this.support.firePropertyChange (PROP_RESOURCES,null,null);
119     }
120     
121     private final class ProjectDirectoryListener implements FileChangeListener {
122
123         public void fileAttributeChanged(org.openide.filesystems.FileAttributeEvent fe) {
124         }
125
126         public void fileChanged(FileEvent fe) {
127         }
128
129         public void fileDataCreated(FileEvent fe) {
130         }
131
132         public void fileDeleted(FileEvent fe) {
133             if (isWatchedFile(getFileName(fe))) {
134                 fireChange();
135             }
136         }
137
138         public void fileFolderCreated(FileEvent fe) {
139             if (isWatchedFile(getFileName(fe))) {
140                 fireChange();
141             }
142         }
143
144         public void fileRenamed(org.openide.filesystems.FileRenameEvent fe) {
145             if (isWatchedFile(getFileName(fe)) || isWatchedFile(getOldFileName(fe))) {
146                 fireChange();
147             }
148         }
149
150         private boolean isWatchedFile(String JavaDoc fileName) {
151             String JavaDoc webDir = evaluator.getProperty(WebProjectProperties.WEB_DOCBASE_DIR);
152             return fileName.equals(webDir);
153         }
154
155         private String JavaDoc getFileName(FileEvent fe) {
156             return fe.getFile().getNameExt();
157         }
158
159         private String JavaDoc getOldFileName(FileRenameEvent fe) {
160             String JavaDoc result = fe.getName();
161             if (!(fe.getExt()).equals("")) { // NOI18N
162
result = result + "." + fe.getExt(); // NOI18N
163
}
164             return result;
165         }
166     }
167 }
168
Popular Tags