001 /** 002 * jline - Java console input library 003 * Copyright (c) 2002,2003 Marc Prud'hommeaux marc@apocalypse.org 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 package jline; 020 021 import java.io.*; 022 import java.util.*; 023 024 025 /** 026 * <p> 027 * A simple {@link Completor} implementation that handles a pre-defined 028 * list of completion words. 029 * </p> 030 * 031 * <p> 032 * Example usage: 033 * </p> 034 * <pre> 035 * myConsoleReader.addCompletor (new SimpleCompletor (new String [] { "now", "yesterday", "tomorrow" })); 036 * </pre> 037 * 038 * @author <a href="mailto:marc@apocalypse.org">Marc Prud'hommeaux</a> 039 */ 040 public class SimpleCompletor 041 implements Completor 042 { 043 /** 044 * The list of candidates that will be completed. 045 */ 046 String [] candidateStrings; 047 048 049 /** 050 * Create a new SimpleCompletor with a single possible completion 051 * values. 052 */ 053 public SimpleCompletor (String candidateString) 054 { 055 this (new String [] { candidateString }); 056 } 057 058 059 /** 060 * Create a new SimpleCompletor with a list of possible completion 061 * values. 062 */ 063 public SimpleCompletor (String [] candidateStrings) 064 { 065 this.candidateStrings = candidateStrings; 066 } 067 068 069 /** 070 * Complete candidates using the contents of the specified Reader. 071 */ 072 public SimpleCompletor (Reader reader) 073 throws IOException 074 { 075 this (getStrings (reader)); 076 } 077 078 079 /** 080 * Complete candidates using the whitespearated values in 081 * read from the specified Reader. 082 */ 083 public SimpleCompletor (InputStream in) 084 throws IOException 085 { 086 this (getStrings (new InputStreamReader (in))); 087 } 088 089 090 private static String [] getStrings (Reader reader) 091 throws IOException 092 { 093 if (!(reader instanceof BufferedReader)) 094 reader = new BufferedReader (reader); 095 096 List words = new LinkedList (); 097 String line; 098 while ((line = ((BufferedReader)reader).readLine ()) != null) 099 { 100 for (StringTokenizer tok = new StringTokenizer (line); 101 tok.hasMoreTokens (); words.add (tok.nextToken ())); 102 } 103 104 return (String [])words.toArray (new String [words.size ()]); 105 } 106 107 108 public int complete (String buffer, int cursor, List candidates) 109 { 110 for (int i = 0; i < candidateStrings.length; i++) 111 { 112 if (buffer == null || buffer.length () == 0 || 113 candidateStrings [i].startsWith (buffer)) 114 { 115 candidates.add (candidateStrings [i]); 116 } 117 } 118 119 if (candidates.size () == 1) 120 candidates.set (0, ((String)candidates.get (0)) + " "); 121 122 // the index of the completion is always from the beginning of 123 // the buffer. 124 return candidates.size () == 0 ? -1 : 0; 125 } 126 127 128 /** 129 * Set the list of candidate Strings. 130 */ 131 public void setCandidateStrings (String [] candidateStrings) 132 { 133 this.candidateStrings = candidateStrings; 134 } 135 136 137 /** 138 * Returns the list of candidate Strings. 139 */ 140 public String [] getCandidateStrings () 141 { 142 return this.candidateStrings; 143 } 144 145 146 public void addCandidateString (String candidateString) 147 { 148 List cand = new LinkedList (Arrays.asList (candidateStrings)); 149 cand.add (candidateString); 150 candidateStrings = (String [])cand.toArray (new String [cand.size ()]); 151 } 152 }