Changeset 1243
- Timestamp:
- 06/28/13 14:38:46 (12 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-ui-swt/src/main/java/de/ugoe/cs/autoquest/ui/swt/ConsoleTabComposite.java
r1236 r1243 35 35 import org.eclipse.swt.events.KeyEvent; 36 36 37 import de.ugoe.cs.util.FileTools; 38 import de.ugoe.cs.util.StringTools; 37 39 import de.ugoe.cs.util.console.CommandExecuter; 38 40 import de.ugoe.cs.util.console.Console; 41 import de.ugoe.cs.util.console.GlobalDataContainer; 39 42 import de.ugoe.cs.util.console.listener.ICommandListener; 40 43 … … 143 146 144 147 private void autoCompleteCommand() { 145 String commandPrefix = textCommand.getText().trim(); 146 String completedPrefix = CommandExecuter.getInstance().autoCompleteCommand(commandPrefix); 147 148 textCommand.setText(completedPrefix.toString()); 149 textCommand.setSelection(completedPrefix.length()); 148 String prefix = textCommand.getText(); 149 int end = textCommand.getCaretPosition() - 1; 150 int start = 0; 151 152 for (int i = end; i >= 0; i--) { 153 if (Character.isWhitespace(prefix.charAt(i))) { 154 start = i + 1; 155 break; 156 } 157 } 158 159 String textPrefix = prefix.substring(0, start); 160 String textSuffix = prefix.substring(end + 1); 161 prefix = prefix.substring(start, end + 1); 162 163 String completedPrefix = null; 164 if ("".equals(textCommand.getText().substring(0, start).trim())) { 165 // search for a command completion 166 completedPrefix = CommandExecuter.getInstance().autoCompleteCommand(prefix); 167 } 168 else { 169 // complete an object in the data store or a file name 170 completedPrefix = FileTools.autoCompletePath(prefix); 171 172 if (prefix.equals(completedPrefix)) { 173 // file completion wasn't possible, so try to complete using data objects 174 String[] completions = 175 GlobalDataContainer.getInstance().getAllKeys().toArray(new String[0]); 176 completedPrefix = StringTools.autocomplete(prefix, completions); 177 } 178 } 179 180 textCommand.setText(textPrefix + completedPrefix + textSuffix); 181 textCommand.setSelection(textPrefix.length() + completedPrefix.length()); 150 182 } 151 183 -
trunk/java-utils/src/main/java/de/ugoe/cs/util/FileTools.java
r1240 r1243 99 99 return (new String(buffer)).split(splitString); 100 100 } 101 102 /** 103 * <p> 104 * Autocompletes a give path. The path must be absolute. Otherwise, autocompletion 105 * is not possible. 106 * </p> 107 * 108 * @param prefix 109 * the prefix to be complete 110 * @return the auto completed path 111 */ 112 public static String autoCompletePath(String prefix) { 113 File prefixFile = new File(prefix); 114 File parentDir = prefixFile.getParentFile(); 115 116 if (parentDir == null) { 117 // the prefix does not denote a path or denotes one of the root directories. 118 // this can not be auto completed 119 return prefix; 120 } 121 122 String[] completions = null; 123 124 if (parentDir.exists()) { 125 completions = parentDir.list(); 126 } 127 128 if (completions == null) { 129 completions = new String[0]; 130 } 131 132 String completedPrefix; 133 134 completedPrefix = StringTools.autocomplete(prefixFile.getName(), completions); 135 136 File completedFile = new File(parentDir, completedPrefix); 137 138 if (completedFile.exists() && completedFile.isDirectory()) { 139 return completedFile.getAbsolutePath() + File.separator; 140 } 141 else { 142 return (parentDir.getAbsolutePath() + File.separator + completedPrefix) 143 .replaceAll("//", "/"); 144 } 145 } 101 146 102 147 } -
trunk/java-utils/src/main/java/de/ugoe/cs/util/StringTools.java
r1241 r1243 14 14 15 15 package de.ugoe.cs.util; 16 17 import java.util.ArrayList; 18 import java.util.List; 16 19 17 20 /** … … 63 66 return result; 64 67 } 68 69 /** 70 * <p> 71 * Performs an auto completion of the provided prefix using the given potential completions. 72 * The method searches for all completions that start with the given prefix and stores them in 73 * a subset. It then extends the prefix with characters of the subset of completions as long as 74 * the prefix still prefixes all completions in the subset. The result is then returned. If 75 * there is no matching completion or if there are several matching completions differing 76 * in the succeeding letter, the prefix is returned as is. If there is only one matching 77 * completion this completion is returned. 78 * </p> 79 * 80 * @param prefix the prefix to be further completed 81 * @param completions the potential completions of the prefix 82 * 83 * @return as described 84 */ 85 public static String autocomplete(String prefix, String[] completions) { 86 List<String> matchingCompletions = new ArrayList<String>(); 87 for (String completion : completions) { 88 if (completion.startsWith(prefix)) { 89 matchingCompletions.add(completion); 90 } 91 } 92 93 StringBuffer completedPrefix = new StringBuffer(prefix); 94 95 boolean foundCompletion = false; 96 97 while (!foundCompletion) { 98 char nextCompletionChar = 0; 99 for (String completion : matchingCompletions) { 100 if (completion.length() > completedPrefix.length()) { 101 if (nextCompletionChar == 0) { 102 nextCompletionChar = completion.charAt(completedPrefix.length()); 103 } 104 else if (nextCompletionChar != completion.charAt(completedPrefix.length())) { 105 foundCompletion = true; 106 } 107 } 108 else { 109 foundCompletion = true; 110 } 111 } 112 113 if (!foundCompletion && (nextCompletionChar != 0)) { 114 completedPrefix.append(nextCompletionChar); 115 } 116 else { 117 foundCompletion = true; 118 } 119 } 120 121 return completedPrefix.toString(); 122 } 65 123 } -
trunk/java-utils/src/main/java/de/ugoe/cs/util/console/CommandExecuter.java
r1238 r1243 30 30 import java.util.jar.JarInputStream; 31 31 import java.util.logging.Level; 32 33 import de.ugoe.cs.util.StringTools; 32 34 33 35 /** … … 356 358 Command[] commands = getAvailableCommands(); 357 359 358 List<Command> matchingCommands = new ArrayList<Command>(); 359 for (Command command : commands) { 360 String commandName = command.getClass().getSimpleName().substring(3); 361 if (commandName.startsWith(commandPrefix)) { 362 matchingCommands.add(command); 363 } 364 } 360 String[] completions = new String[commands.length]; 365 361 366 StringBuffer completedPrefix = new StringBuffer(commandPrefix); 362 for (int i = 0; i < commands.length; i++) { 363 completions[i] = commands[i].getClass().getSimpleName().substring(3); 364 } 367 365 368 boolean foundCompletion = false; 369 370 while (!foundCompletion) { 371 char nextCompletionChar = 0; 372 for (Command command : matchingCommands) { 373 String commandName = command.getClass().getSimpleName().substring(3); 374 if (commandName.length() > completedPrefix.length()) { 375 if (nextCompletionChar == 0) { 376 nextCompletionChar = commandName.charAt(completedPrefix.length()); 377 } 378 else if (nextCompletionChar != commandName.charAt(completedPrefix.length())) { 379 foundCompletion = true; 380 } 381 } 382 else { 383 foundCompletion = true; 384 } 385 } 386 387 if (!foundCompletion && (nextCompletionChar != 0)) { 388 completedPrefix.append(nextCompletionChar); 389 } 390 else { 391 foundCompletion = true; 392 } 393 } 394 395 return completedPrefix.toString(); 366 return StringTools.autocomplete(commandPrefix, completions); 396 367 } 397 368 }
Note: See TracChangeset
for help on using the changeset viewer.