KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Completion.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: Completion.java,v 1.5 2003/09/16 00:29:43 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.List JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 public final class Completion
28 {
29     private String JavaDoc input;
30     private String JavaDoc toBeCompleted;
31     private boolean ignoreCase;
32     private boolean cygnify;
33
34     private ArrayList JavaDoc list = new ArrayList JavaDoc();
35
36     public Completion(File dir, String JavaDoc input, String JavaDoc shellCommand)
37     {
38         if (Platform.isPlatformWindows()) {
39             if (shellCommand != null && shellCommand.toLowerCase().indexOf("cmd.exe") < 0)
40                 cygnify = true;
41         }
42         final char separatorChar = getSeparatorChar();
43         this.input = input;
44         toBeCompleted = input;
45         if (Platform.isPlatformWindows()) {
46             if (cygnify) {
47                 if (toBeCompleted.startsWith("~/")) {
48                     String JavaDoc home = Utilities.getUserHome();
49                     if (!home.startsWith("/"))
50                         home = Utilities.cygnify(home);
51                     toBeCompleted = Utilities.uncygnify(home + toBeCompleted.substring(1));
52                 } else if (toBeCompleted.startsWith("../"))
53                     toBeCompleted = File.normalize(toBeCompleted);
54                 else
55                     toBeCompleted = Utilities.uncygnify(toBeCompleted);
56             } else {
57                 if (toBeCompleted.startsWith("~/")) {
58                     String JavaDoc home = Utilities.getUserHome();
59                     toBeCompleted = home + toBeCompleted.substring(1);
60                 } else
61                     toBeCompleted = File.normalize(toBeCompleted);
62             }
63             ignoreCase = true;
64         }
65         FilenameCompletion c = new FilenameCompletion(dir, toBeCompleted, null,
66             ignoreCase);
67         List JavaDoc files = c.listFiles();
68         if (files != null) {
69             String JavaDoc home = Utilities.getUserHome() + "/";
70             String JavaDoc prefix = dir.canonicalPath() + LocalFile.getSeparator();
71             int skip = prefix.length();
72             String JavaDoc toBeAdded;
73             int limit = files.size();
74             for (int i = 0; i < limit; i++) {
75                 File file = (File) files.get(i);
76                 toBeAdded = file.getAbsolutePath();
77                 if (!Utilities.isFilenameAbsolute(toBeCompleted)) {
78                     if (toBeAdded.startsWith(prefix)) {
79                         toBeAdded = toBeAdded.substring(skip);
80                         if (input.startsWith("./"))
81                             toBeAdded = "./" + toBeAdded;
82                     }
83                 }
84                 if (cygnify)
85                     toBeAdded = Utilities.cygnify(toBeAdded);
86                 if (input.startsWith("~/")) {
87                     if (toBeAdded.startsWith(home)) {
88                         toBeAdded = "~/" + toBeAdded.substring(home.length());
89                     }
90                 } else if (input.startsWith("..")) {
91                     String JavaDoc remaining = input;
92                     File parentDir = dir;
93                     String JavaDoc parentPrefix = "";
94                     while (remaining.startsWith("../")) {
95                         parentDir = parentDir.getParentFile();
96                         parentPrefix += "../";
97                         remaining = remaining.substring(3);
98                     }
99                     String JavaDoc parentDirName = parentDir.canonicalPath();
100                     if (cygnify)
101                         parentDirName = Utilities.cygnify(parentDirName);
102                     if (!parentDirName.endsWith("/"))
103                         parentDirName += "/";
104                     if (toBeAdded.startsWith(parentDirName))
105                         toBeAdded = parentPrefix + toBeAdded.substring(parentDirName.length());
106                 }
107                 toBeAdded = escapeSpaces(toBeAdded);
108                 if (file.isDirectory())
109                     toBeAdded += separatorChar;
110                 list.add(toBeAdded);
111             }
112         }
113     }
114
115     // Converts "this is a test" into "this\ is\ a\ test".
116
private static final String JavaDoc escapeSpaces(String JavaDoc s)
117     {
118         final int length = s.length();
119         FastStringBuffer sb = new FastStringBuffer(length * 2);
120         for (int i = 0; i < length; i++) {
121             char c = s.charAt(i);
122             if (c == ' ')
123                 sb.append('\\');
124             sb.append(c);
125         }
126         return sb.toString();
127     }
128
129     private final char getSeparatorChar()
130     {
131         if (Platform.isPlatformWindows() && !cygnify)
132             return '\\';
133         return '/';
134     }
135
136     public final List JavaDoc getCompletions()
137     {
138         return list;
139     }
140
141     private boolean isUnique()
142     {
143         return list.size() == 1;
144     }
145
146     private String JavaDoc getLongestCommonPrefix()
147     {
148         String JavaDoc s = input;
149         if (list.size() != 0) {
150             if (list.size() == 1) {
151                 s = (String JavaDoc) list.get(0);
152             } else {
153                 String JavaDoc first = (String JavaDoc) list.get(0);
154                 int length = toBeCompleted.length() + 1;
155                 while (true) {
156                     if (length > first.length())
157                         return s;
158                     String JavaDoc maybe = first.substring(0, length);
159                     for (int i = 1; i < list.size(); i++) {
160                         String JavaDoc toBeChecked = (String JavaDoc) list.get(i);
161                         if (!maybe.regionMatches(ignoreCase, 0, toBeChecked, 0, length)) {
162                             if (cygnify)
163                                 s = Utilities.cygnify(s);
164                             return s;
165                         }
166                     }
167                     s = maybe;
168                     ++length;
169                 }
170             }
171         }
172         if (cygnify)
173             s = Utilities.cygnify(s);
174         return s;
175     }
176
177     public String JavaDoc toString()
178     {
179         if (isUnique()) {
180             String JavaDoc s = (String JavaDoc) list.get(0);
181             // Directories have file separator already appended.
182
if (!s.endsWith("/") && !s.endsWith("\\"))
183                 s += ' ';
184             return s;
185         }
186         return getLongestCommonPrefix();
187     }
188 }
189
Popular Tags