From: SVN by r. <sv...@ca...> - 2008-07-28 20:20:35
|
Author: roy Date: 2008-07-28 22:20:24 +0200 (Mon, 28 Jul 2008) New Revision: 272 Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java Log: initial rewrite for non blocking pasting of commands Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java =================================================================== --- src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-07-27 11:40:00 UTC (rev 271) +++ src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-07-28 20:20:24 UTC (rev 272) @@ -15,7 +15,15 @@ */ package nl.improved.sqlclient; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.Writer; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; @@ -32,6 +40,8 @@ import java.util.List; import java.util.Map; import java.util.LinkedHashMap; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.parsers.DocumentBuilder; @@ -113,12 +123,15 @@ */ protected CommandManager commands = new CommandManager(); + private ArrayBlockingQueue<CommandInfo> commandQueue = new ArrayBlockingQueue<CommandInfo>(10, true); // true means fair + /** * A map of string key representation to a action that should be executed when a specific key is pressed. */ private Map<String, KeyAction> actionKeys = new LinkedHashMap<String, KeyAction>(); private Map<InputKey.SpecialKey, KeyAction> specialActionKeys = new LinkedHashMap<InputKey.SpecialKey, KeyAction>(); private boolean paint = true; + private boolean run = true; // set to false on quit /** * Constructor. @@ -152,6 +165,7 @@ // keys specialActionKeys.put(InputKey.SpecialKey.LEFT, new KeyAction() { + @Override public void execute() { if (cursorPosition.x > 0) { cursorPosition.x--; @@ -160,11 +174,13 @@ cursorPosition.x = commandLines.getLines().get(cursorPosition.y).length(); } } + @Override public CharSequence getHelp() { return "Arrow Left:\tMove cursor to the left"; } }); specialActionKeys.put(InputKey.SpecialKey.RIGHT, new KeyAction() { + @Override public void execute() { CharSequence tmp = commandLines.getLines().get(cursorPosition.y); if (cursorPosition.x < tmp.length()) { @@ -174,11 +190,13 @@ cursorPosition.y++; } } + @Override public CharSequence getHelp() { return "Arrow Right:\tMove cursor to the right"; } }); specialActionKeys.put(InputKey.SpecialKey.UP, new KeyAction() { + @Override public void execute() { if (commandIndex > 0) { commandLines = commandHistory.get(--commandIndex); @@ -187,11 +205,13 @@ cursorPosition.x = lineBuffer.length(); } } + @Override public CharSequence getHelp() { return "Arrow Up:\tBrowse to previous command in the history"; } }); specialActionKeys.put(InputKey.SpecialKey.DOWN, new KeyAction() { + @Override public void execute() { if (commandIndex < commandHistory.size()-1) { commandLines = commandHistory.get(++commandIndex); @@ -200,11 +220,13 @@ cursorPosition.x = lineBuffer.length(); } } + @Override public CharSequence getHelp() { return "Arrow Down:\tBrowse to next command in the history"; } }); specialActionKeys.put(InputKey.SpecialKey.END,new KeyAction() { + @Override public void execute() { int curLineEnd = commandLines.getLines().get(cursorPosition.y).length(); if (cursorPosition.x == curLineEnd) { @@ -215,22 +237,26 @@ cursorPosition.x = curLineEnd; } } + @Override public CharSequence getHelp() { return "End:\tMove the cursor to the end of the line, of if already there to the end of the command"; } }); specialActionKeys.put(InputKey.SpecialKey.HOME, new KeyAction() { + @Override public void execute() { if (cursorPosition.x == 0) { cursorPosition.y = 0; } cursorPosition.x = 0; } + @Override public CharSequence getHelp() { return "Home:\tMove the cursor to the start of the line, of if already there to the start of the command"; } }); specialActionKeys.put(InputKey.SpecialKey.BACKSPACE, new KeyAction() { + @Override public void execute() { if (cursorPosition.x == 0) { if (cursorPosition.y > 0) { @@ -244,11 +270,13 @@ } } } + @Override public CharSequence getHelp() { return "Backspace:\tRemove the character before the cursor position"; } }); specialActionKeys.put(InputKey.SpecialKey.DELETE, new KeyAction() { + @Override public void execute() { StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y); if (cursorPosition.x < lineBuffer.length()) { @@ -256,6 +284,7 @@ tmp.deleteCharAt(cursorPosition.x); } } + @Override public CharSequence getHelp() { return "Del:\tDelete the charactor at the current cursor position"; } @@ -269,6 +298,7 @@ } } + @Override public CharSequence getHelp() { return "PageUp:\tMove back in screen history"; } @@ -280,11 +310,13 @@ pageUpCount--; } } + @Override public CharSequence getHelp() { return "PageDown:\tMove forward in screen history"; } }); actionKeys.put("", new KeyAction() { + @Override public void execute() { // ctrl+w if (cursorPosition.x == 0) { if (cursorPosition.y > 0) { @@ -300,11 +332,13 @@ lineBuffer.delete(previousBreak, cursorPosition.x); cursorPosition.x = previousBreak; } + @Override public CharSequence getHelp() { return "Control-W:\tRemove word before cursor position"; } }); actionKeys.put("", new KeyAction() { // ctrl+u + @Override public void execute() { if (cursorPosition.x > 0) { StringBuilder lineBuffer = commandLines.getEditableLines().get(cursorPosition.y); @@ -320,6 +354,7 @@ lineBuffer.delete(0, lineBuffer.length()); } } + @Override public CharSequence getHelp() { return "Control-U:\tRemove all characters before the cursor position"; } @@ -354,6 +389,51 @@ output("Welcome to the SQLShell client."); output("Type 'help' to get a list of available commands other then the default sql input."); + + Runnable r = new Runnable() { + @Override + public void run() { + while ( run ) { + final CommandInfo cmd; + try { + cmd = commandQueue.poll(5, TimeUnit.SECONDS); + } catch (InterruptedException ex) { + Logger.getLogger(AbstractSQLShellWindow.class.getName()).log(Level.SEVERE, null, ex); + continue; + } + if (cmd == null) { + // TODO update waiting dot or something + continue; + } + // make sure only one command is run at once + if (commandThread != null && commandThread.isAlive()) { + try { + commandThread.join(); + } catch (InterruptedException ex) { + Logger.getLogger(AbstractSQLShellWindow.class.getName()).log(Level.SEVERE, null, ex); + } + } + output(cmd.sql.getLines()); + repaint(); + if (/*direct ||*/ !cmd.cmd.backgroundProcessSupported()) { + output(cmd.cmd.execute(cmd.sql)); + repaint(); + } else { + commandThread = new CommandThread(cmd.cmd) { + @Override + void execute() { + output(getCommand().execute(cmd.sql)); + } + }; + commandThread.start(); + } + + } + } + }; + Thread t = new Thread(r); + t.setDaemon(true); + t.start(); } @@ -758,7 +838,7 @@ * @param command the command to try and execute * @return true if it succeeded. */ - protected boolean executeCommand(SQLCommand sqlCommand) { + private boolean executeCommand(SQLCommand sqlCommand) { return executeCommand(sqlCommand, false); } @@ -779,26 +859,7 @@ if (command == null) { return false; } - // make sure only one command is run at once - if (commandThread != null && commandThread.isAlive()) { - try { - commandThread.join(); - } catch (InterruptedException ex) { - Logger.getLogger(AbstractSQLShellWindow.class.getName()).log(Level.SEVERE, null, ex); - } - } - output(sqlCommand.getLines()); - if (direct || !command.backgroundProcessSupported()) { - output(command.execute(sqlCommand)); - } else { - commandThread = new CommandThread(command) { - @Override - void execute() { - output(getCommand().execute(sqlCommand)); - } - }; - commandThread.start(); - } + commandQueue.add(new CommandInfo(sqlCommand, command)); return true; } private Command createCommand(String commandString) { @@ -1077,6 +1138,7 @@ @Override public CharSequence execute(SQLCommand command) { paint = false; + run = false; close(); return "Application terminated."; } @@ -1731,4 +1793,14 @@ void execute(); CharSequence getHelp(); } + + private class CommandInfo { + private SQLCommand sql; + private Command cmd; + + public CommandInfo(SQLCommand sql, Command cmd) { + this.sql = sql; + this.cmd = cmd; + } + } } |