From: SVN by r. <sv...@ca...> - 2008-08-19 11:00:19
|
Author: roy Date: 2008-08-19 13:00:11 +0200 (Tue, 19 Aug 2008) New Revision: 287 Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java Log: add more comments add tabcompletion to spool command some code cleanups Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java =================================================================== --- src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-08-19 10:59:06 UTC (rev 286) +++ src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-08-19 11:00:11 UTC (rev 287) @@ -1486,6 +1486,68 @@ } /** + * Returns tabcompletion information based on a filename prefix input string. + * @param fileNamePrefix the filename prefix to search for matches + * @param otherMatches an optional list of other matches that are not filenames + * @return the tabcompletion information found based on the filename prefix and other matches (optional) + */ + private static TabCompletionInfo getFileNameTabCompletionInfo(String fileNamePrefix, String... otherMatches) { + String dirName; + if (fileNamePrefix.equals("")) { + fileNamePrefix = "."; + dirName = "."; + } else { + fileNamePrefix = toFileName(fileNamePrefix); + if (fileNamePrefix.indexOf('/') >= 0) { + File file = new File(fileNamePrefix); + if (file.isDirectory()) { + fileNamePrefix = ""; + dirName = file.getAbsolutePath()+"/"; + } else { + fileNamePrefix = file.getName(); + dirName = file.getParent(); + } + } else { + dirName = "."; + } + } + List<String> matches = new ArrayList<String>(10); + for (String otherMatch : otherMatches) { + matches.add(otherMatch); + } + matches.addAll(Arrays.asList(new File(dirName).list())); + return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER, matches, fileNamePrefix); + } + + /** + * Join two lines. + */ + private void joinLine() { + Point cursorPosition = screen.getCursorPosition(); + StringBuffer line = getEditableCommand().getEditableLines().remove(cursorPosition.y); + cursorPosition.y--; + StringBuffer lineBuffer = commandLines.getEditableLines().get(cursorPosition.y); + cursorPosition.x = lineBuffer.length(); + lineBuffer.append(line); + } + + /** + * Return true if the programm is still running. + * @return true if the program is still running + */ + protected boolean isRunning() { + return run; + } + + /** + * Returns the screen implementation. + * @return the screen. + */ + protected Screen getScreen() { + return screen; + } + + /** * Writes in/output to a file. */ private class SpoolCommand implements Command { @@ -1506,6 +1568,17 @@ return "No spool to close."; } } else { + StringBuffer returnValue = new StringBuffer(100); + if (spoolWriter != null) { + returnValue.append("Closing previous spool to : "); + returnValue.append(fileName); + returnValue.append('\n'); + try { + spoolWriter.close(); + } catch (IOException ex) { + /* ignore */ + } + } try { File f = new File(toFileName(nextPart.trim())); fileName = f.getAbsolutePath(); @@ -1516,7 +1589,8 @@ } catch (IOException e) { throw new IllegalStateException("Failed to create spool ("+fileName+"): " + e.toString(), e); } - return "Spool to "+fileName+" created."; + returnValue.append("Spool to "+fileName+" created."); + return returnValue.toString(); } } @@ -1533,6 +1607,11 @@ */ @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { + String cmd = command.getCommandString(); + if (cmd.startsWith("spool")) { + String fn = command.getCommandString().substring("spool".length()).trim(); // cutoff 'spool' + return getFileNameTabCompletionInfo(fn, "off"); + } return null; } @Override @@ -1875,26 +1954,7 @@ @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { String fileName = command.getCommandString().substring(1).trim(); // cutoff '@' - String dirName; - if (fileName.equals("")) { - fileName = "."; - dirName = "."; - } else { - fileName = toFileName(fileName); - if (fileName.indexOf('/') >= 0) { - File file = new File(fileName); - if (file.isDirectory()) { - fileName = ""; - dirName = file.getAbsolutePath()+"/"; - } else { - fileName = file.getName(); - dirName = file.getParent(); - } - } else { - dirName = "."; - } - } - return new TabCompletionInfo(TabCompletionInfo.MatchType.OTHER,Arrays.asList(new File(dirName).list()) , fileName); + return getFileNameTabCompletionInfo(fileName); } @Override @@ -1950,12 +2010,23 @@ } } + /** + * Command thread responsible for executing commands in the background. + * It holds a reference to the current command that is being executed. + */ private abstract class CommandThread extends Thread { private Command cmd; + /** + * Constructor. + * @param cmd the command to be executed in the background + */ public CommandThread(Command cmd) { this.cmd = cmd; } + /** + * Execute the command and disable prompt. + */ @Override public final void run() { screen.setShowPrompt(false); @@ -1968,20 +2039,35 @@ } } + /** + * Override to implement the execution of the command. + */ abstract void execute(); + /** + * Returns the command that is being executed by this thread. + * @return the command that is being executed by this thread. + */ Command getCommand() { return cmd; } } + /** + * Query command responsible for executing a sql query. + */ private class QueryCommand implements Command { /** - * Executor for SQL Statements + * Executor for SQL Statements. */ private StatementExecutor statementExecutor; + /** + * Execute the sql query. + * @param cmd the sqlcommand containing the sql query + * @return the result of the sql query. + */ @Override public CharSequence execute(SQLCommand cmd) { try { @@ -2010,6 +2096,7 @@ @Override public TabCompletionInfo getTabCompletionInfo(SQLCommand commandInfo, Point commandPoint) { // TODO call SQLUTil.. + debug("GET TAB COMPLETION OF QUERY COMMAND"); return null; } @@ -2031,23 +2118,7 @@ } - private void joinLine() { - Point cursorPosition = screen.getCursorPosition(); - StringBuffer line = getEditableCommand().getEditableLines().remove(cursorPosition.y); - cursorPosition.y--; - StringBuffer lineBuffer = commandLines.getEditableLines().get(cursorPosition.y); - cursorPosition.x = lineBuffer.length(); - lineBuffer.append(line); - } - protected boolean isRunning() { - return run; - } - - protected Screen getScreen() { - return screen; - } - private interface KeyAction { void execute(); CharSequence getHelp(); |