1 19 package jline; 20 21 import java.io.*; 22 import java.text.*; 23 import java.util.*; 24 25 36 public class CandidateListCompletionHandler 37 implements CompletionHandler 38 { 39 private static ResourceBundle loc = ResourceBundle.getBundle ( 40 CandidateListCompletionHandler.class.getName ()); 41 42 43 public boolean complete (ConsoleReader reader, List candidates, int pos) 44 throws IOException 45 { 46 CursorBuffer buf = reader.getCursorBuffer (); 47 48 if (candidates.size () == 1) 50 { 51 String value = candidates.get (0).toString (); 52 53 if (value.equals (buf.toString ())) 55 return false; 56 setBuffer (reader, value, pos); 57 return true; 58 } 59 else if (candidates.size () > 1) 60 { 61 String value = getUnambiguousCompletions (candidates); 62 String bufString = buf.toString (); 63 setBuffer (reader, value, pos); 64 65 if (bufString.length () - pos + 1 != value.length ()) 68 return true; 69 } 70 71 reader.printNewline (); 72 printCandidates (reader, candidates); 73 74 reader.drawLine (); 76 77 return true; 78 } 79 80 81 private static void setBuffer (ConsoleReader reader, 82 String value, int offset) 83 throws IOException 84 { 85 while (reader.getCursorBuffer ().cursor >= offset 86 && reader.backspace ()); 87 reader.putString (value); 88 reader.setCursorPosition (offset + value.length ()); 89 } 90 91 92 99 private final void printCandidates (ConsoleReader reader, 100 Collection candidates) 101 throws IOException 102 { 103 Set distinct = new HashSet (candidates); 104 105 if (distinct.size () > reader.getAutoprintThreshhold ()) 106 { 107 reader.printString (MessageFormat.format ( 108 loc.getString ("display-candidates"), 109 new Object [] { new Integer (candidates.size ()) } ) + " "); 110 111 reader.flushConsole (); 112 113 int c; 114 115 String noOpt = loc.getString ("display-candidates-no"); 116 String yesOpt = loc.getString ("display-candidates-yes"); 117 118 while ((c = reader.readCharacter ( 119 new char[] { yesOpt.charAt (0), noOpt.charAt (0) })) != -1) 120 { 121 if (noOpt.startsWith (new String (new char[] {(char)c}))) 122 { 123 reader.printNewline (); 124 return; 125 } 126 else if (yesOpt.startsWith (new String (new char[] {(char)c}))) 127 { 128 break; 129 } 130 else 131 { 132 reader.beep (); 133 } 134 } 135 } 136 137 if (distinct.size () != candidates.size ()) 140 { 141 Collection copy = new ArrayList (); 142 for (Iterator i = candidates.iterator (); i.hasNext (); ) 143 { 144 Object next = i.next (); 145 if (!(copy.contains (next))) 146 copy.add (next); 147 } 148 149 candidates = copy; 150 } 151 152 reader.printNewline (); 153 reader.printColumns (candidates); 154 } 155 156 157 158 159 166 private final String getUnambiguousCompletions (List candidates) 167 { 168 if (candidates == null || candidates.size () == 0) 169 return null; 170 171 String [] strings = (String [])candidates.toArray ( 173 new String [candidates.size ()]); 174 175 String first = strings [0]; 176 StringBuffer candidate = new StringBuffer (); 177 for (int i = 0; i < first.length (); i++) 178 { 179 if (startsWith (first.substring (0, i + 1), strings)) 180 candidate.append (first.charAt (i)); 181 else 182 break; 183 } 184 185 return candidate.toString (); 186 } 187 188 189 193 private final boolean startsWith (String starts, String [] candidates) 194 { 195 for (int i = 0; i < candidates.length; i++) 196 { 197 if (!candidates [i].startsWith (starts)) 198 return false; 199 } 200 201 return true; 202 } 203 } 204 205 | Popular Tags |