From: SVN by r. <sv...@ca...> - 2008-08-02 10:11:31
|
Author: roy Date: 2008-08-02 12:11:20 +0200 (Sat, 02 Aug 2008) New Revision: 277 Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java src/main/java/nl/improved/sqlclient/Screen.java src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java Log: finally (really??) fix paint issues added some comments code cleanup Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java =================================================================== --- src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-07-31 07:59:52 UTC (rev 276) +++ src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-08-02 10:11:20 UTC (rev 277) @@ -54,8 +54,8 @@ import org.w3c.dom.NodeList; /** - * The SQLShell main class. - * This class provides a (textbased) window for entering sql commands. + * The SQLShell abstract main class. + * This class provides a the basic implementation for entering sql commands. A super implementation is required to show the actual data. */ public abstract class AbstractSQLShellWindow { @@ -89,16 +89,28 @@ */ protected CommandManager commands = new CommandManager(); - private ArrayBlockingQueue<CommandInfo> commandQueue = new ArrayBlockingQueue<CommandInfo>(10, true); // true means fair + /** + * The command list of commands to execute. + */ + private ArrayBlockingQueue<CommandInfo> commandQueue = new ArrayBlockingQueue<CommandInfo>(99, true); // true means fair + /** + * The screen information. + */ private Screen screen; /** * 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>(); + /** + * The special (home, end, up, down, etc) key actions. + */ private Map<InputKey.SpecialKey, KeyAction> specialActionKeys = new LinkedHashMap<InputKey.SpecialKey, KeyAction>(); - private boolean paint = true; + /** + * The programm runs while this var is set to true. + * All threads should stop when this is set to false + */ private boolean run = true; // set to false on quit /** @@ -370,10 +382,13 @@ output("Welcome to the SQLShell client."); output("Type 'help' to get a list of available commands other then the default sql input."); + // + // Thread for executing the sql commands in the background + // Runnable r = new Runnable() { @Override public void run() { - while ( run ) { + while ( isRunning() ) { final CommandInfo cmd; try { cmd = commandQueue.poll(5, TimeUnit.SECONDS); @@ -408,7 +423,6 @@ }; commandThread.start(); } - } } }; @@ -418,24 +432,45 @@ } - // TODO move to Screen??? + /** + * Return the visible screen width. + * @return the visible screen width. + */ public abstract int getScreenWidth(); + /** + * Return the visible screen height. + * @return the visible screen height. + */ public abstract int getScreenHeight(); + /** + * Repaint the screen. + */ protected void repaint() { - if (paint) { - try { - synchronized(this) { - paint(screen); - } - } catch(Throwable t) { - error(t); - } + if (isRunning()) { + paint(getScreen()); } } + + /** + * Paints the screen. + * @param screen the screen to be painted + */ public abstract void paint(Screen screen); + /** + * Make a beep sound. + */ public abstract void beep(); + /** + * Print a debug string. + * @param debug the string to print in the debug line + */ public abstract void debug(String debug); + /** + * Shows a popupwindow to allow the user to make a selection from the list of possible matches. + * @param items the list of possible matches + * @param p the character x/y location to show the popupscreen on + */ public abstract String select(List<String> items, Point p); /** * Returns the connection to the database @@ -445,9 +480,10 @@ return DBConnector.getInstance().getConnection(); } - public void close() { - debug("CLOSE HERE"); - System.out.println("CLOSE HERE"); + /** + * Close all writers. + */ + public void close() { // TODO move to someplace else where it's less likely to forget if (spoolWriter != null) { try { spoolWriter.close(); @@ -1111,7 +1147,6 @@ } @Override public CharSequence execute(SQLCommand command) { - paint = false; run = false; close(); return "Application terminated."; @@ -1771,6 +1806,15 @@ cursorPosition.x = lineBuffer.length(); lineBuffer.append(line); } + + protected boolean isRunning() { + return run; + } + + protected Screen getScreen() { + return screen; + } + private interface KeyAction { void execute(); CharSequence getHelp(); Modified: src/main/java/nl/improved/sqlclient/Screen.java =================================================================== --- src/main/java/nl/improved/sqlclient/Screen.java 2008-07-31 07:59:52 UTC (rev 276) +++ src/main/java/nl/improved/sqlclient/Screen.java 2008-08-02 10:11:20 UTC (rev 277) @@ -19,7 +19,7 @@ import nl.improved.sqlclient.util.LimitedArrayList; /** - * + * Screen class holding information that needs to be painted in the sql window. * @author roy */ public class Screen { @@ -28,6 +28,9 @@ * The (default) maximum matches to show in a selection dialog. */ public static final int MAX_MATCH_SIZE = 15; + /** + * The maximum line length. + */ public int MAX_LINE_LENGTH; // not static and final.. since it can change on window resize /** * The prompt to show when entering sql commands. @@ -60,35 +63,63 @@ */ private Point cursorPosition = new Point(0,0); + /** + * Returns the position of the blinking cursor. + * @return the position of the blinking cursor. + */ public Point getCursorPosition() { return cursorPosition; } + /** + * Returns the screen buffer. + * @return the screen buffer. + */ public List<CharSequence> getScreenBuffer() { return screenBuffer; } + /** + * Returns the pageup count used for viewing history. + * @return the pageup count used for viewing history. + */ public int getPageUpCount() { return pageUpCount; } + /** + * Changes the pageup count used for viewing history. + * @param pageUpCount the pageup count used for viewing history. + */ public void setPageUpCount(int pageUpCount) { this.pageUpCount = pageUpCount; } + /** + * Returns true if the prompt should be visible. + * @return true if the prompt should be visible. + */ public boolean getShowPrompt() { return showPrompt; } + /** + * Set to true if the prompt should be visible. + * @param showPrompt true if the prompt should be visible. + */ public void setShowPrompt(boolean showPrompt) { this.showPrompt = showPrompt; } + /** + * Returns a full empty line string. + * @return a full empty line string. + */ public String getEmptyLine() { return emptyLine; } - public void setEmptyLine(String emptyLine) { + void setEmptyLine(String emptyLine) { this.emptyLine = emptyLine; } } Modified: src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java =================================================================== --- src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java 2008-07-31 07:59:52 UTC (rev 276) +++ src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java 2008-08-02 10:11:20 UTC (rev 277) @@ -32,27 +32,43 @@ import nl.improved.sqlclient.commands.Command; /** - * + * SQLShell window based on the jcurses library. + * @see http://jcurses.sourceforge.net/ * @author roy */ public class SQLShellWindow extends AbstractSQLShellWindow { private Window window; + private boolean dontRepaint = false; + private boolean repaint = true; + /** + * Default constructor. + */ public SQLShellWindow() { - //commands.register("WINDOW[\\s]+[A-Z]+", new WindowCommand()); } - + /** + * Return the screen width. + * @return the screen width. + */ @Override public int getScreenWidth() { return Toolkit.getScreenWidth(); } + /** + * Return the screen height. + * @return the screen height. + */ @Override public int getScreenHeight() { return Toolkit.getScreenHeight(); } + /** + * Write a debug string. + * @param debug the debug to output + */ @Override public void debug(String debug) { synchronized(this) { @@ -62,6 +78,9 @@ } + /** + * Set the window to be visible. + */ public void show() { if (window == null) { window = new Window(getScreenWidth(), getScreenHeight(), true, "SQLShell") { @@ -116,8 +135,26 @@ }; } window.setVisible(true); + while (isRunning()) { + Screen screen = getScreen(); + if (!dontRepaint && repaint) { + try { + synchronized(this) { + paint(screen); + } + } catch(Throwable t) { + error(t); + } + } + try { + Thread.sleep(200); + } catch(Exception e) {} + } } + /** + * Close the window. + */ @Override public void close() { debug("CLOSE CALLED"); @@ -125,61 +162,32 @@ window.close(); } + /** - * Some basic window settings like resize. + * Shows a popupwindow to allow the user to make a selection from the list of possible matches. + * @param items the list of possible matches + * @param p the character x/y location to show the popupscreen on */ - private class WindowCommand implements Command { - @Override - public CharSequence execute(SQLCommand cmd) { - String command = cmd.getCommandString(); - String argument = command.trim().substring("window".length()).trim(); - if (argument.equalsIgnoreCase("resize")) { - //window.resize(getScreenWidth(),getScreenHeight()); // TODO - return "Window resized to " + getScreenWidth() +"x"+getScreenHeight(); - } - return "Uknown command '"+ argument+"'"; - } - - @Override - public CharSequence getCommandString() { - return "window"; - } - - /** - * Returns some tab completion info for the specified command. - * @param commandInfo the command lines - * @param commandPoint the cursor position - * @return some tab completion info for the specified command. - */ - @Override - public TabCompletionInfo getTabCompletionInfo(SQLCommand command, Point commandPoint) { - return null; - } - @Override - public CharSequence getHelp() { - return "window resize: notify the sql client of a screen resize"; - } - @Override - public boolean abort() { - return false;// not implemented - } - @Override - public boolean backgroundProcessSupported() { - return false; - } - } - @Override public String select(List<String> items, Point p) { - PopUpMenu menu = new PopUpMenu(p.x,Math.max(2, p.y-items.size()), "Find match"); - Iterator<String> iMatches = items.iterator(); - while (iMatches.hasNext()) { - menu.add(iMatches.next()); + try { + PopUpMenu menu = new PopUpMenu(p.x,Math.max(2, p.y-items.size()), "Find match"); + Iterator<String> iMatches = items.iterator(); + while (iMatches.hasNext()) { + menu.add(iMatches.next()); + } + dontRepaint = true; + menu.show(); + return menu.getSelectedItem(); + } finally { + dontRepaint = false; } - menu.show(); - return menu.getSelectedItem(); } + /** + * Paint the screen. + * @param screen the screen information to be painted + */ @Override public void paint(Screen screen) { synchronized(this) { @@ -240,6 +248,24 @@ } } + /** + * Override to allow painting on the main thread to overcome painting errors. + */ + @Override + protected void repaint() { + if (isRunning()) { + repaint = true; + } + } + + /** + * Beep. + */ + @Override + public void beep() { + Toolkit.beep(); + } + public static void main(String[] args) { SQLShellWindow shell = new SQLShellWindow(); shell.show(); @@ -250,9 +276,4 @@ } */ } - - @Override - public void beep() { - Toolkit.beep(); - } } |