KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > FilenameCompletion


1 /*
2  * FilenameCompletion.java
3  *
4  * Copyright (C) 2000-2002 Peter Graves
5  * $Id: FilenameCompletion.java,v 1.7 2003/01/06 04:04:31 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 public final class FilenameCompletion
28 {
29     private final File currentDirectory;
30     private final String JavaDoc sourcePath;
31     private final boolean ignoreCase;
32
33     private String JavaDoc prefix;
34     private ArrayList JavaDoc list;
35
36     public FilenameCompletion(File directory, String JavaDoc prefix,
37         String JavaDoc sourcePath, boolean ignoreCase)
38     {
39         currentDirectory = directory;
40         this.prefix = prefix;
41         this.sourcePath = sourcePath;
42         this.ignoreCase = ignoreCase;
43         initialize();
44     }
45
46     // Returns list of File objects.
47
public List JavaDoc listFiles()
48     {
49         return list;
50     }
51
52     private void initialize()
53     {
54         list = new ArrayList JavaDoc();
55         if (Utilities.isFilenameAbsolute(prefix)) {
56             File file = File.getInstance(currentDirectory, prefix);
57             if (file == null)
58                 return;
59             if (file.isDirectory() && prefix.endsWith(LocalFile.getSeparator()))
60                 addCompletionsFromDirectory(list, file, null);
61             else {
62                 File directory = file.getParentFile();
63                 if (directory != null) {
64                     prefix = file.getName();
65                     addCompletionsFromDirectory(list, directory, prefix);
66                 }
67             }
68         } else if (prefix.indexOf(LocalFile.getSeparatorChar()) >= 0) {
69             // Prefix specifies a directory.
70
String JavaDoc dirName;
71             if (prefix.endsWith(LocalFile.getSeparator())) {
72                 dirName = prefix.substring(0, prefix.length() - 1);
73                 prefix = null;
74             } else {
75                 int index = prefix.lastIndexOf(LocalFile.getSeparatorChar());
76                 dirName = prefix.substring(0, index);
77                 prefix = prefix.substring(index + 1);
78             }
79             // First try relative to current directory.
80
File dir = File.getInstance(currentDirectory, dirName);
81             if (dir != null && dir.isDirectory()) {
82                 addCompletionsFromDirectory(list, dir, prefix);
83             } else {
84                 // No such directory relative to current directory.
85
// Look in source path.
86
if (sourcePath != null) {
87                     List JavaDoc sourcePathDirectories = Utilities.getDirectoriesInPath(sourcePath);
88                     for (int i = 0; i < sourcePathDirectories.size(); i++) {
89                         File sourcePathDirectory =
90                             File.getInstance((String JavaDoc) sourcePathDirectories.get(i));
91                         dir = File.getInstance(sourcePathDirectory, dirName);
92                         if (dir != null && dir.isDirectory())
93                             addCompletionsFromDirectory(list, dir, prefix);
94                     }
95                 }
96             }
97         } else {
98             // Short name.
99
// Current directory.
100
addCompletionsFromDirectory(list, currentDirectory, prefix);
101             // Source path.
102
if (sourcePath != null) {
103                 List JavaDoc sourcePathDirectories = Utilities.getDirectoriesInPath(sourcePath);
104                 for (int i = 0; i < sourcePathDirectories.size(); i++) {
105                     File sourcePathDirectory =
106                         File.getInstance((String JavaDoc) sourcePathDirectories.get(i));
107                     if (sourcePathDirectory != null)
108                         addCompletionsFromDirectory(list, sourcePathDirectory,
109                             prefix);
110                 }
111             }
112         }
113     }
114
115     private void addCompletionsFromDirectory(List JavaDoc list, File directory,
116         String JavaDoc prefix)
117     {
118         File[] files = directory.listFiles();
119         if (files != null) {
120             final int limit = files.length;
121             if (prefix != null && prefix.length() > 0) {
122                 final int prefixLength = prefix.length();
123                 for (int i = 0; i < limit; i++) {
124                     final File file = files[i];
125                     final String JavaDoc name = file.getName();
126                     final boolean isMatch;
127                     if (ignoreCase)
128                         isMatch = name.regionMatches(true, 0, prefix, 0,
129                             prefixLength);
130                     else
131                         isMatch = name.startsWith(prefix);
132                     if (isMatch)
133                         list.add(file);
134                 }
135             } else {
136                 for (int i = 0; i < limit; i++)
137                     list.add(files[i]);
138             }
139         }
140     }
141 }
142
Popular Tags