From: SVN by r. <sv...@ca...> - 2008-09-16 23:01:04
|
Author: roy Date: 2008-09-17 08:00:52 +0200 (Wed, 17 Sep 2008) New Revision: 307 Modified: src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java Log: some more code refactoring Modified: src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java =================================================================== --- src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java 2008-09-15 19:11:19 UTC (rev 306) +++ src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java 2008-09-17 06:00:52 UTC (rev 307) @@ -19,19 +19,10 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import jcurses.event.ActionEvent; -import jcurses.event.ActionListener; import jcurses.system.CharColor; import jcurses.system.InputChar; import jcurses.system.Toolkit; -import jcurses.widgets.Button; -import jcurses.widgets.Dialog; -import jcurses.widgets.GridLayoutManager; -import jcurses.widgets.Label; -import jcurses.widgets.PasswordField; import jcurses.widgets.PopUpMenu; -import jcurses.widgets.TextField; -import jcurses.widgets.WidgetsConstants; import jcurses.widgets.Window; import nl.improved.sqlclient.AbstractSQLShellWindow; import nl.improved.sqlclient.InputKey; @@ -150,7 +141,7 @@ if (!dontRepaint && repaint) { try { synchronized(this) { - _paint(screen); + paintImpl(screen); } } catch(Throwable t) { error(t); @@ -217,32 +208,31 @@ } } - private void _paint(Screen screen) { + /** + * Actual implementation of the paint screen method. + * This is different from the normal paint method since this will only + * (and should be) called from the main thread to overcome painting errors. + * @param screen the screen to paint + */ + private void paintImpl(Screen screen) { if (!isRunning()) { return; } synchronized(lockObject) { CharColor color = new CharColor(CharColor.BLACK, CharColor.WHITE, CharColor.BOLD, CharColor.BOLD); - List<CharSequence> tmpList = new ArrayList<CharSequence>(); - List<CharSequence> screenBuffer = screen.getScreenBuffer(); - tmpList.addAll(screenBuffer); + List<CharSequence> tmpList = clone(screen.getScreenBuffer()); //add prompt - List<SQLCommand> commands = getUnprocessedCommands(); - commands.add(getCommand()); - for (SQLCommand commandLines : commands) { - //SQLCommand commandLines = getCommand(); - List<? extends CharSequence> currentLines = commandLines.getLines(); - for (int i = 0; i < currentLines.size(); i++) { - if (i == 0 && screen.getShowPrompt()) { - tmpList.add(Screen.PROMPT+"> "+currentLines.get(i)); - } else { - String nrI = Integer.toString(i+1); - tmpList.add(screen.getEmptyLine().substring(0,Screen.PROMPT.length() - nrI.length()) + nrI+"> "+currentLines.get(i)); - } - } + List<SQLCommand> commandList = getUnprocessedCommands(); + //commandList.add(getCommand()); + boolean showPrompt = screen.getShowPrompt(); + for (SQLCommand commandLines : commandList) { + List<CharSequence> currentLines = clone((List<CharSequence>) commandLines.getLines()); + tmpList.addAll(formatCommandLines(showPrompt, screen.getEmptyLine(), currentLines)); } + List<CharSequence> currentCommandLines = clone((List<CharSequence>) getCommand().getLines()); + tmpList.addAll(formatCommandLines(showPrompt, screen.getEmptyLine(), currentCommandLines)); int startLine; if (tmpList.size() > Toolkit.getScreenHeight()-1) { startLine = tmpList.size() - (Toolkit.getScreenHeight()-1); @@ -270,9 +260,9 @@ color = new CharColor(CharColor.BLACK, CharColor.WHITE, CharColor.REVERSE, CharColor.REVERSE); String cursorChar = " "; Point cursorPosition = screen.getCursorPosition(); - SQLCommand commandLines = getCommand(); - if (commandLines.getLines().size() > 0) { - String tmp = commandLines.getLines().get(cursorPosition.y).toString(); + //SQLCommand commandLines = getCommand(); + if (currentCommandLines.size() > 0) { + String tmp = currentCommandLines.get(cursorPosition.y).toString(); if (cursorPosition.x < 0) { debug("Cursor position was: "+ cursorPosition +" fixing"); cursorPosition.x = 0; @@ -281,7 +271,7 @@ cursorChar = tmp.substring(cursorPosition.x, cursorPosition.x+1); } } - Toolkit.printString(cursorChar, Screen.PROMPT.length() +"> ".length() + cursorPosition.x, lineNr-(commandLines.getLines().size() -cursorPosition.y)-startLine, color); + Toolkit.printString(cursorChar, Screen.PROMPT.length() +"> ".length() + cursorPosition.x, lineNr-(currentCommandLines.size() -cursorPosition.y)-startLine, color); if (debugString != null) { if (debugString.length() > Toolkit.getScreenWidth()) { debugString = debugString.substring(0, Toolkit.getScreenWidth()-1); @@ -293,6 +283,13 @@ } } + /** + * Fetch the login detail information. + * @param username the default username + * @param password the default password + * @return the login credential string array + * @throws java.sql.SQLException + */ @Override protected String[] getLoginCredentials(String username, String password) throws SQLException { LoginDialog diag = new LoginDialog(username, password); @@ -329,6 +326,39 @@ Toolkit.beep(); } + /** + * Clone a list of charsequence to make sure it isn't modified later (during paint). + * @param screenBuffer the screenBuffer to clone + * @return a cloned list of charsequence objects. + */ + private static List<CharSequence> clone(List<CharSequence> screenBuffer) { + List<CharSequence> result = new ArrayList<CharSequence>(screenBuffer.size()); + for(CharSequence cs : screenBuffer) { + result.add(cs.toString()); + } + return result; + } + + /** + * Format the command lines to fit on the screen and start with a line id. + * @param showPrompt true if the prompt is shown + * @param emptyLine an empty line string + * @param currentLines the current command lines to be formatted + * @return a formatted list of strings + */ + private List<String> formatCommandLines(boolean showPrompt, String emptyLine, List<CharSequence> currentLines) { + List<String> tmpList = new ArrayList<String>(); + for (int i = 0; i < currentLines.size(); i++) { + if (i == 0 && showPrompt) { + tmpList.add(Screen.PROMPT+"> "+currentLines.get(i)); + } else { + String nrI = Integer.toString(i+1); + tmpList.add(emptyLine.substring(0,Screen.PROMPT.length() - nrI.length()) + nrI+"> "+currentLines.get(i)); + } + } + return tmpList; + } + public static void main(String[] args) { SQLShellWindow shell = new SQLShellWindow(); shell.show(); @@ -339,4 +369,5 @@ } */ } + } |