001: /*
002: This library is free software; you can redistribute it and/or
003: modify it under the terms of the GNU General Public
004: License as published by the Free Software Foundation; either
005: version 2 of the license, or (at your option) any later version.
006: */
007:
008: package org.gjt.jclasslib.browser.config;
009:
010: import org.gjt.jclasslib.browser.config.classpath.*;
011: import org.gjt.jclasslib.mdi.MDIConfig;
012:
013: import javax.swing.tree.DefaultTreeModel;
014: import java.io.File;
015: import java.util.*;
016: import java.util.regex.Matcher;
017: import java.util.regex.Pattern;
018:
019: /**
020: Workspace configuration object.
021:
022: @author <a href="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
023: @version $Revision: 1.2 $ $Date: 2003/08/21 14:40:45 $
024: */
025: public class BrowserConfig implements ClasspathComponent {
026:
027: private MDIConfig mdiConfig;
028: private List classpath = new ArrayList();
029: private Set mergedEntries = new HashSet();
030: private Set changeListeners = new HashSet();
031:
032: /**
033: * Get the associated MDI configuration object.
034: * @return the <tt>MDIConfig</tt> object.
035: */
036: public MDIConfig getMDIConfig() {
037: return mdiConfig;
038: }
039:
040: /**
041: * Set the associated MDI configuration object.
042: * @param mdiConfig the <tt>MDIConfig</tt> object.
043: */
044: public void setMDIConfig(MDIConfig mdiConfig) {
045: this .mdiConfig = mdiConfig;
046: }
047:
048: /**
049: * Get the list of <tt>ClasspathEntry</tt> objects that define the classpath.
050: * @return the list
051: */
052: public List getClasspath() {
053: return classpath;
054: }
055:
056: /**
057: * Set the list of <tt>ClasspathEntry</tt> objects that define the classpath.
058: * @param classpath the list
059: */
060: public void setClasspath(List classpath) {
061: this .classpath = classpath;
062: }
063:
064: public void addClasspathChangeListener(
065: ClasspathChangeListener listener) {
066: changeListeners.add(listener);
067: }
068:
069: public void removeClasspathChangeListener(
070: ClasspathChangeListener listener) {
071: changeListeners.remove(listener);
072: }
073:
074: /**
075: * Add a classpath entry for a directory.
076: * Has no effect of the classpath entry is already present.
077: * @param directoryName the name of the directory.
078: */
079: public void addClasspathDirectory(String directoryName) {
080: ClasspathDirectoryEntry entry = new ClasspathDirectoryEntry();
081: entry.setFileName(directoryName);
082: if (classpath.indexOf(entry) < 0) {
083: classpath.add(entry);
084: fireClasspathChanged(false);
085: }
086: }
087:
088: /**
089: * Add a classpath entry for an archive.
090: * Has no effect of the classpath entry is already present.
091: * @param archiveName the path of he archive.
092: */
093: public void addClasspathArchive(String archiveName) {
094: ClasspathArchiveEntry entry = new ClasspathArchiveEntry();
095: entry.setFileName(archiveName);
096: if (classpath.indexOf(entry) < 0) {
097: classpath.add(entry);
098: fireClasspathChanged(false);
099: }
100: }
101:
102: /**
103: * Add a classpath entry.
104: * Has no effect of the classpath entry is already present.
105: * @param entry the entry.
106: */
107: public void addClasspathEntry(ClasspathEntry entry) {
108: if (classpath.indexOf(entry) < 0) {
109: classpath.add(entry);
110: fireClasspathChanged(false);
111: }
112: }
113:
114: /**
115: * Remove a classpath entry.
116: * @param entry the entry.
117: */
118: public void removeClasspathEntry(ClasspathEntry entry) {
119: if (classpath.remove(entry)) {
120: fireClasspathChanged(true);
121: }
122: }
123:
124: /**
125: * Add the <tt>rt.jar</tt> archive of the JRE used by the bytecode browser to the classpath.
126: */
127: public void addRuntimeLib() {
128:
129: String fileName = String.class.getResource("String.class")
130: .toExternalForm();
131: Matcher matcher = Pattern.compile("jar:file:/(.*)!.*").matcher(
132: fileName);
133: if (matcher.matches()) {
134: String path = matcher.group(1);
135: if (path.indexOf(':') == -1) {
136: path = "/" + path;
137: }
138: addClasspathArchive(new File(path).getPath());
139: fireClasspathChanged(false);
140: }
141: }
142:
143: public FindResult findClass(String className) {
144:
145: Iterator it = classpath.iterator();
146: while (it.hasNext()) {
147: ClasspathEntry entry = (ClasspathEntry) it.next();
148: FindResult findResult = entry.findClass(className);
149: if (findResult != null) {
150: return findResult;
151: }
152: }
153: return null;
154: }
155:
156: public void mergeClassesIntoTree(DefaultTreeModel model,
157: boolean reset) {
158:
159: Iterator it = classpath.iterator();
160: while (it.hasNext()) {
161: ClasspathEntry entry = (ClasspathEntry) it.next();
162: if (reset || !mergedEntries.contains(entry)) {
163: entry.mergeClassesIntoTree(model, reset);
164: mergedEntries.add(entry);
165: }
166: }
167: }
168:
169: private void fireClasspathChanged(boolean removal) {
170: Iterator it = changeListeners.iterator();
171: ClasspathChangeEvent event = new ClasspathChangeEvent(this ,
172: removal);
173: while (it.hasNext()) {
174: ClasspathChangeListener listener = (ClasspathChangeListener) it
175: .next();
176: listener.classpathChanged(event);
177: }
178: }
179:
180: }
|