001: /*
002: * FilenameCompletion.java
003: *
004: * Copyright (C) 2000-2002 Peter Graves
005: * $Id: FilenameCompletion.java,v 1.7 2003/01/06 04:04:31 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: public final class FilenameCompletion {
028: private final File currentDirectory;
029: private final String sourcePath;
030: private final boolean ignoreCase;
031:
032: private String prefix;
033: private ArrayList list;
034:
035: public FilenameCompletion(File directory, String prefix,
036: String sourcePath, boolean ignoreCase) {
037: currentDirectory = directory;
038: this .prefix = prefix;
039: this .sourcePath = sourcePath;
040: this .ignoreCase = ignoreCase;
041: initialize();
042: }
043:
044: // Returns list of File objects.
045: public List listFiles() {
046: return list;
047: }
048:
049: private void initialize() {
050: list = new ArrayList();
051: if (Utilities.isFilenameAbsolute(prefix)) {
052: File file = File.getInstance(currentDirectory, prefix);
053: if (file == null)
054: return;
055: if (file.isDirectory()
056: && prefix.endsWith(LocalFile.getSeparator()))
057: addCompletionsFromDirectory(list, file, null);
058: else {
059: File directory = file.getParentFile();
060: if (directory != null) {
061: prefix = file.getName();
062: addCompletionsFromDirectory(list, directory, prefix);
063: }
064: }
065: } else if (prefix.indexOf(LocalFile.getSeparatorChar()) >= 0) {
066: // Prefix specifies a directory.
067: String dirName;
068: if (prefix.endsWith(LocalFile.getSeparator())) {
069: dirName = prefix.substring(0, prefix.length() - 1);
070: prefix = null;
071: } else {
072: int index = prefix.lastIndexOf(LocalFile
073: .getSeparatorChar());
074: dirName = prefix.substring(0, index);
075: prefix = prefix.substring(index + 1);
076: }
077: // First try relative to current directory.
078: File dir = File.getInstance(currentDirectory, dirName);
079: if (dir != null && dir.isDirectory()) {
080: addCompletionsFromDirectory(list, dir, prefix);
081: } else {
082: // No such directory relative to current directory.
083: // Look in source path.
084: if (sourcePath != null) {
085: List sourcePathDirectories = Utilities
086: .getDirectoriesInPath(sourcePath);
087: for (int i = 0; i < sourcePathDirectories.size(); i++) {
088: File sourcePathDirectory = File
089: .getInstance((String) sourcePathDirectories
090: .get(i));
091: dir = File.getInstance(sourcePathDirectory,
092: dirName);
093: if (dir != null && dir.isDirectory())
094: addCompletionsFromDirectory(list, dir,
095: prefix);
096: }
097: }
098: }
099: } else {
100: // Short name.
101: // Current directory.
102: addCompletionsFromDirectory(list, currentDirectory, prefix);
103: // Source path.
104: if (sourcePath != null) {
105: List sourcePathDirectories = Utilities
106: .getDirectoriesInPath(sourcePath);
107: for (int i = 0; i < sourcePathDirectories.size(); i++) {
108: File sourcePathDirectory = File
109: .getInstance((String) sourcePathDirectories
110: .get(i));
111: if (sourcePathDirectory != null)
112: addCompletionsFromDirectory(list,
113: sourcePathDirectory, prefix);
114: }
115: }
116: }
117: }
118:
119: private void addCompletionsFromDirectory(List list, File directory,
120: String prefix) {
121: File[] files = directory.listFiles();
122: if (files != null) {
123: final int limit = files.length;
124: if (prefix != null && prefix.length() > 0) {
125: final int prefixLength = prefix.length();
126: for (int i = 0; i < limit; i++) {
127: final File file = files[i];
128: final String name = file.getName();
129: final boolean isMatch;
130: if (ignoreCase)
131: isMatch = name.regionMatches(true, 0, prefix,
132: 0, prefixLength);
133: else
134: isMatch = name.startsWith(prefix);
135: if (isMatch)
136: list.add(file);
137: }
138: } else {
139: for (int i = 0; i < limit; i++)
140: list.add(files[i]);
141: }
142: }
143: }
144: }
|