KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > classpath > CPImpl


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.bluej.classpath;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import org.netbeans.bluej.BluejProject;
30 import org.netbeans.bluej.options.BlueJSettings;
31 import org.netbeans.spi.java.classpath.ClassPathImplementation;
32 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
33 import org.openide.filesystems.FileChangeAdapter;
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.FileUtil;
39 import org.openide.filesystems.URLMapper;
40 import org.openide.util.WeakListeners;
41
42 /**
43  *
44  * @author mkleint
45  */

46 public class CPImpl implements ClassPathImplementation {
47     
48     private List JavaDoc resources = null;
49     private List JavaDoc listeners = new ArrayList JavaDoc();
50
51     private BluejProject project;
52     private FileObject userLib;
53     private PropertyChangeListener JavaDoc settingsListener = new PropertyChangeListener JavaDoc() {
54         public void propertyChange(PropertyChangeEvent JavaDoc arg0) {
55             fireChange();
56         }
57     };
58     
59     private FileChangeListener fileListener = new FileChangeAdapter() {
60         public void fileDataCreated(FileEvent arg0) {
61             fireChange();
62         }
63
64         public void fileDeleted(FileEvent arg0) {
65             fireChange();
66         }
67
68         public void fileRenamed(FileRenameEvent arg0) {
69             fireChange();
70         }
71     };
72     /** Creates a new instance of CPImpl */
73     public CPImpl(BluejProject prj) {
74         project = prj;
75         BlueJSettings.getDefault().addPropertyChangeListener(
76                 WeakListeners.propertyChange(settingsListener, BlueJSettings.getDefault()));
77     }
78
79     public synchronized List JavaDoc getResources() {
80         if (resources == null) {
81             resources = new ArrayList JavaDoc();
82             FileObject libs = project.getProjectDirectory().getFileObject("+libs"); // NOI18N
83
if (libs != null) {
84                 FileObject[] fos = libs.getChildren();
85                 for (int i = 0; i < fos.length; i++) {
86                     if (FileUtil.isArchiveFile(fos[i])) {
87                         resources.add(ClassPathSupport.createResource(URLMapper.findURL(FileUtil.getArchiveRoot(fos[i]), URLMapper.INTERNAL)));
88                     }
89                 }
90             }
91             File JavaDoc home = BlueJSettings.getDefault().getHome();
92             if (home != null) {
93                 FileObject fo = FileUtil.toFileObject(BluejProject.getUserLibPath(home));
94                 if (fo != null) {
95                     FileObject[] fos = fo.getChildren();
96                     for (int i = 0; i < fos.length; i++) {
97                         if (FileUtil.isArchiveFile(fos[i])) {
98                             resources.add(ClassPathSupport.createResource(URLMapper.findURL(FileUtil.getArchiveRoot(fos[i]), URLMapper.INTERNAL)));
99                         }
100                     }
101                 }
102                 if (userLib != null && !userLib.equals(fo)) {
103                     //remove
104
userLib.removeFileChangeListener(fileListener);
105                 }
106                 userLib = fo;
107                 //add
108
userLib.addFileChangeListener(fileListener);
109             } else if (userLib != null) {
110                 //remove listener
111
userLib.removeFileChangeListener(fileListener);
112                 userLib = null;
113             }
114             String JavaDoc userPath = BlueJSettings.getDefault().getUserLibrariesAsClassPath();
115             if (userPath.length() > 0) {
116                 StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(userPath, ":", false); // NOI18N
117
while (tokens.hasMoreTokens()) {
118                     File JavaDoc fil = new File JavaDoc(tokens.nextToken());
119                     FileObject fo = FileUtil.toFileObject(fil);
120                     if (fo != null && FileUtil.isArchiveFile(fo)) {
121                         resources.add(ClassPathSupport.createResource(URLMapper.findURL(FileUtil.getArchiveRoot(fo), URLMapper.INTERNAL)));
122                     }
123                 }
124             }
125         }
126         return resources;
127     }
128
129     public synchronized void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
130         listeners.add(listener);
131     }
132
133     public synchronized void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
134         listeners.remove(listener);
135     }
136     
137     public synchronized void fireChange() {
138         resources = null;
139         List JavaDoc lst = new ArrayList JavaDoc();
140         lst.addAll(listeners);
141         Iterator JavaDoc it = lst.iterator();
142         PropertyChangeEvent JavaDoc evnt = new PropertyChangeEvent JavaDoc(this, ClassPathImplementation.PROP_RESOURCES, null, null);
143         while (it.hasNext()) {
144             PropertyChangeListener JavaDoc listener = (PropertyChangeListener JavaDoc)it.next();
145             listener.propertyChange(evnt);
146         }
147     }
148 }
149
Popular Tags