001: package tide.sources;
002:
003: import tide.editor.MainEditorFrame;
004: import snow.texteditor.SimpleDocument;
005: import java.util.*;
006: import java.io.*;
007: import javax.swing.tree.*;
008:
009: public class SourceFileUtils {
010: public SourceFileUtils() {
011: }
012:
013: /** All name.class and name$*.class
014: * TODO: parse the source to look at private types ! " public class XXX {} private class A {} "
015: */
016: public static List<File> getClassFilesFor(SourceFile sf,
017: File classesBase) {
018: List<File> cf = new ArrayList<File>();
019:
020: String packagePath = getPackageName(sf).replace('.', '/');
021: final String className = sf.javaPartName; // Hello
022: File packageFile = new File(classesBase, packagePath);
023:
024: if (!packageFile.exists()) {
025: //System.out.println("Package "+packageFile.getAbsolutePath()+" not found");
026: return cf;
027: }
028:
029: File[] cfs = packageFile.listFiles(new FilenameFilter() {
030: public boolean accept(File dir, String name) {
031: if (!name.endsWith(".class"))
032: return false;
033: if (name.startsWith(className + "."))
034: return true;
035: if (name.startsWith(className + "$"))
036: return true;
037: return false;
038: }
039: });
040:
041: for (File f : cfs) {
042: cf.add(f);
043: }
044:
045: return cf;
046: }
047:
048: public static String getPackageName(SourceFile sf) {
049: if (sf.isDirectory)
050: return sf.getJavaName();
051:
052: // just remove the last name
053: String pn = sf.getJavaName();
054: int pos = pn.lastIndexOf('.');
055: if (pos == -1)
056: return ""; // base package (unnamed)
057:
058: return pn.substring(0, pos);
059: }
060:
061: static FileItemNameComparator fileItemNameComparator = new FileItemNameComparator();
062:
063: /**
064: * Should be called in the edt.
065: */
066: public static void insertSorted(final FileItem item,
067: final FileItem parent, final DefaultTreeModel tm) {
068: // determine the insert position
069: int pos = 0;
070:
071: fl: for (pos = 0; pos < parent.getChildCount(); pos++) {
072: if (fileItemNameComparator.compare((FileItem) parent
073: .getChildAt(pos), item) > 0)
074: break fl;
075: }
076:
077: // bad if not edt call !(?)
078: tm.insertNodeInto((DefaultMutableTreeNode) item,
079: (DefaultMutableTreeNode) parent, pos);
080:
081: }
082:
083: /*
084: public static void main(String[] a)
085: {
086: System.out.println("a".compareTo("b"));
087: }*/
088:
089: /** Ordering according to
090: 1) directories first, alphabetically sorted
091: 2) sources files
092: 3) resource files
093:
094: note: "a".compareTo("b") is -1
095: */
096: static class FileItemNameComparator implements Comparator<FileItem> {
097: public int compare(FileItem fi1, FileItem fi2) {
098: // treat the cases of different types,
099: if (fi1.isDirectory()) {
100: if (!fi2.isDirectory())
101: return -1;
102: } else {
103: // fi1 is a file
104: if (fi2.isDirectory())
105: return 1;
106: }
107:
108: // ok, they are of same type
109: return fi1.getJavaPartName().compareTo(
110: fi2.getJavaPartName());
111: }
112: }
113:
114: /** null if not already existing.
115: */
116: public static SourceFile getDirectPackage(final SourceFile parent,
117: String name) {
118: int pos = 0;
119:
120: for (pos = 0; pos < parent.getChildCount(); pos++) {
121: SourceFile ci = parent.getChildFileAt(pos);
122: if (ci.isDirectory() && ci.getPackageName().equals(name)) {
123: return ci;
124: }
125: }
126:
127: return null;
128: }
129:
130: public static void writeDependencies(SourceFile src,
131: SimpleDocument doc) {
132: if (src.isDirectory()) {
133: List<SourceFile> files = new ArrayList<SourceFile>();
134: MainEditorFrame.instance.sourcesTreePanel.getTreeModel()
135: .getAllSourceFilesRecurse(src, files, false, false);
136: for (SourceFile sfi : files) {
137: sfi.sourceFileDependencies.writeDependencies(doc);
138: }
139: } else {
140: src.sourceFileDependencies.writeDependencies(doc);
141: }
142: }
143: /*
144: public static void writeDependenciesSrc(SourceFile src, SimpleDocument doc)
145: {
146: doc.append("=== Source "+src.getJavaName()+" ===\r\n");
147: if(src.sourceFileDependencies.getClassesUsedBy_REF().isEmpty())
148: {
149: doc.appendLine("Uses no other classes in the project");
150: }
151: else
152: {
153: doc.appendLine("Uses "+src.sourceFileDependencies.getClassesUsedBy_REF().size()
154: +" class"+(src.sourceFileDependencies.getClassesUsedBy_REF().size()==1?"":"es")+":");
155: for(SourceFile sf: src.sourceFileDependencies.getClassesUsedBy_REF())
156: {
157: doc.appendLine(" "+sf.getJavaName()+".java:");
158: }
159: }
160:
161: doc.appendLine("");
162:
163: if(src.sourceFileDependencies.getClassesUsingThis_REF().isEmpty())
164: {
165: doc.appendLine("Is used by no other classes in the project");
166: }
167: else
168: {
169: doc.appendLine("Is used by "+src.sourceFileDependencies.getClassesUsingThis_REF().size()+" class"+(src.sourceFileDependencies.getClassesUsingThis_REF().size()==1?"":"es")+":");
170: for(SourceFile sf: src.sourceFileDependencies.getClassesUsingThis_REF())
171: {
172: doc.appendLine(" "+sf.getJavaName()+".java:");
173: }
174: }
175:
176: doc.appendLine("");
177: }*/
178:
179: }
|