KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > browser > config > BrowserConfig


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.browser.config;
9
10 import org.gjt.jclasslib.browser.config.classpath.*;
11 import org.gjt.jclasslib.mdi.MDIConfig;
12
13 import javax.swing.tree.DefaultTreeModel JavaDoc;
14 import java.io.File JavaDoc;
15 import java.util.*;
16 import java.util.regex.Matcher JavaDoc;
17 import java.util.regex.Pattern JavaDoc;
18
19
20 /**
21     Workspace configuration object.
22
23     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
24     @version $Revision: 1.2 $ $Date: 2003/08/21 14:40:45 $
25 */

26 public class BrowserConfig implements ClasspathComponent {
27
28     private MDIConfig mdiConfig;
29     private List classpath = new ArrayList();
30     private Set mergedEntries = new HashSet();
31     private Set changeListeners = new HashSet();
32
33     /**
34      * Get the associated MDI configuration object.
35      * @return the <tt>MDIConfig</tt> object.
36      */

37     public MDIConfig getMDIConfig() {
38         return mdiConfig;
39     }
40
41     /**
42      * Set the associated MDI configuration object.
43      * @param mdiConfig the <tt>MDIConfig</tt> object.
44      */

45     public void setMDIConfig(MDIConfig mdiConfig) {
46         this.mdiConfig = mdiConfig;
47     }
48
49     /**
50      * Get the list of <tt>ClasspathEntry</tt> objects that define the classpath.
51      * @return the list
52      */

53     public List getClasspath() {
54         return classpath;
55     }
56
57     /**
58      * Set the list of <tt>ClasspathEntry</tt> objects that define the classpath.
59      * @param classpath the list
60      */

61     public void setClasspath(List classpath) {
62         this.classpath = classpath;
63     }
64
65     public void addClasspathChangeListener(ClasspathChangeListener listener) {
66         changeListeners.add(listener);
67     }
68
69     public void removeClasspathChangeListener(ClasspathChangeListener listener) {
70         changeListeners.remove(listener);
71     }
72
73     /**
74      * Add a classpath entry for a directory.
75      * Has no effect of the classpath entry is already present.
76      * @param directoryName the name of the directory.
77      */

78     public void addClasspathDirectory(String JavaDoc directoryName) {
79         ClasspathDirectoryEntry entry = new ClasspathDirectoryEntry();
80         entry.setFileName(directoryName);
81         if (classpath.indexOf(entry) < 0) {
82             classpath.add(entry);
83             fireClasspathChanged(false);
84         }
85     }
86
87     /**
88      * Add a classpath entry for an archive.
89      * Has no effect of the classpath entry is already present.
90      * @param archiveName the path of he archive.
91      */

92     public void addClasspathArchive(String JavaDoc archiveName) {
93         ClasspathArchiveEntry entry = new ClasspathArchiveEntry();
94         entry.setFileName(archiveName);
95         if (classpath.indexOf(entry) < 0) {
96             classpath.add(entry);
97             fireClasspathChanged(false);
98         }
99     }
100
101     /**
102      * Add a classpath entry.
103      * Has no effect of the classpath entry is already present.
104      * @param entry the entry.
105      */

106     public void addClasspathEntry(ClasspathEntry entry) {
107         if (classpath.indexOf(entry) < 0) {
108             classpath.add(entry);
109             fireClasspathChanged(false);
110         }
111     }
112
113     /**
114      * Remove a classpath entry.
115      * @param entry the entry.
116      */

117     public void removeClasspathEntry(ClasspathEntry entry) {
118         if (classpath.remove(entry)) {
119             fireClasspathChanged(true);
120         }
121     }
122
123     /**
124      * Add the <tt>rt.jar</tt> archive of the JRE used by the bytecode browser to the classpath.
125      */

126     public void addRuntimeLib() {
127
128         String JavaDoc fileName = String JavaDoc.class.getResource("String.class").toExternalForm();
129         Matcher JavaDoc matcher = Pattern.compile("jar:file:/(.*)!.*").matcher(fileName);
130         if (matcher.matches()) {
131             String JavaDoc path = matcher.group(1);
132             if (path.indexOf(':') == -1) {
133                 path = "/" + path;
134             }
135             addClasspathArchive(new File JavaDoc(path).getPath());
136             fireClasspathChanged(false);
137         }
138     }
139
140     public FindResult findClass(String JavaDoc className) {
141
142         Iterator it = classpath.iterator();
143         while (it.hasNext()) {
144             ClasspathEntry entry = (ClasspathEntry)it.next();
145             FindResult findResult = entry.findClass(className);
146             if (findResult != null) {
147                 return findResult;
148             }
149         }
150         return null;
151     }
152
153     public void mergeClassesIntoTree(DefaultTreeModel JavaDoc model, boolean reset) {
154
155         Iterator it = classpath.iterator();
156         while (it.hasNext()) {
157             ClasspathEntry entry = (ClasspathEntry)it.next();
158             if (reset || !mergedEntries.contains(entry)) {
159                 entry.mergeClassesIntoTree(model, reset);
160                 mergedEntries.add(entry);
161             }
162         }
163     }
164
165     private void fireClasspathChanged(boolean removal) {
166         Iterator it = changeListeners.iterator();
167         ClasspathChangeEvent event = new ClasspathChangeEvent(this, removal);
168         while (it.hasNext()) {
169             ClasspathChangeListener listener = (ClasspathChangeListener)it.next();
170             listener.classpathChanged(event);
171         }
172     }
173
174 }
175
Popular Tags