KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jline > FileNameCompletor


1 /**
2  * jline - Java console input library
3  * Copyright (c) 2002,2003 Marc Prud'hommeaux mwp1@cornell.edu
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jline;
20
21 import java.io.*;
22 import java.util.*;
23
24
25 /**
26  * A file name completor takes the buffer and issues a list of
27  * potential completions.
28  *
29  * <p>
30  * This completor tries to behave as similar as possible to
31  * <i>bash</i>'s file name completion (using GNU readline)
32  * with the following exceptions:
33  *
34  * <ul>
35  * <li>Candidates that are directories will end with "/"</li>
36  * <li>Wildcard regular expressions are not evaluated or replaced</li>
37  * <li>The "~" character can be used to represent the user's home,
38  * but it cannot complete to other users' homes, since java does
39  * not provide any way of determining that easily</li>
40  * </ul>
41  *
42  * <p>TODO</p>
43  * <ul>
44  * <li>Handle files with spaces in them</li>
45  * <li>Have an option for file type color highlighting</li>
46  * </ul>
47  *
48  * @author <a HREF="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
49  */

50 public class FileNameCompletor
51     implements Completor
52 {
53     public int complete (String JavaDoc buffer, int cursor, List candidates)
54     {
55         if (buffer == null)
56             buffer = "";
57
58         String JavaDoc translated = buffer;
59
60         // special character: ~ maps to the user's home directory
61
if (translated.startsWith ("~" + File.separator))
62         {
63             translated = System.getProperty ("user.home")
64                 + translated.substring (1);
65         }
66         else if (translated.startsWith ("~"))
67         {
68             translated = new File (System.getProperty ("user.home"))
69                 .getParentFile ().getAbsolutePath ();
70         }
71         else if (!(translated.startsWith (File.separator)))
72         {
73             translated = new File ("").getAbsolutePath ()
74                 + File.separator + translated;
75         }
76
77         File f = new File (translated);
78
79         final File dir;
80         
81         if (translated.endsWith (File.separator))
82             dir = f;
83         else
84             dir = f.getParentFile ();
85         
86         final File [] entries = dir == null ? new File [0] : dir.listFiles ();
87
88         try
89         {
90             return matchFiles (buffer, translated, entries, candidates);
91         }
92         finally
93         {
94             // we want to output a sorted list of files
95
sortFileNames (candidates);
96         }
97     }
98
99
100     protected void sortFileNames (List fileNames)
101     {
102         Collections.sort (fileNames);
103     }
104
105
106     /**
107      * Match the specified <i>buffer</i> to the array of <i>entries</i>
108      * and enter the matches into the list of <i>candidates</i>. This method
109      * can be overridden in a subclass that wants to do more
110      * sophisticated file name completion.
111      *
112      * @param buffer the untranslated buffer
113      * @param translated the buffer with common characters replaced
114      * @param entries the list of files to match
115      * @param candidates the list of candidates to populate
116      *
117      * @return the offset of the match
118      */

119     public int matchFiles (String JavaDoc buffer, String JavaDoc translated,
120         File [] entries, List candidates)
121     {
122         if (entries == null)
123             return -1;
124
125         int matches = 0;
126
127         // first pass: just count the matches
128
for (int i = 0; i < entries.length; i++)
129         {
130             if (entries [i].getAbsolutePath ().startsWith (translated))
131             {
132                 matches++;
133             }
134         }
135
136         // green - executable
137
// blue - directory
138
// red - compressed
139
// cyan - symlink
140
for (int i = 0; i < entries.length; i++)
141         {
142             if (entries [i].getAbsolutePath ().startsWith (translated))
143             {
144                 String JavaDoc name = entries [i].getName ()
145                     + (matches == 1 && entries [i].isDirectory ()
146                         ? File.separator : " ");
147
148                 /*
149                 if (entries [i].isDirectory ())
150                 {
151                     name = new ANSIBuffer ().blue (name).toString ();
152                 }
153                 */

154
155                 candidates.add (name);
156             }
157         }
158
159
160         final int index = buffer.lastIndexOf (File.separator);
161         return index + File.separator.length ();
162     }
163 }
164
165
Popular Tags