From: SVN by r. <sv...@ca...> - 2008-10-11 12:29:37
|
Author: roy Date: 2008-10-11 14:29:29 +0200 (Sat, 11 Oct 2008) New Revision: 310 Added: src/main/java/nl/improved/sqlclient/charva/ src/main/java/nl/improved/sqlclient/charva/CharvaLoginDialog.java src/main/java/nl/improved/sqlclient/charva/CharvaSQLShellWindow.java src/main/java/nl/improved/sqlclient/charva/SQLShellComponent.java Modified: pom.xml src/main/java/nl/improved/sqlclient/ src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java src/main/java/nl/improved/sqlclient/jcurses/ src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java Log: code split introduction to charva based sqlshell window Modified: pom.xml =================================================================== --- pom.xml 2008-10-11 11:20:20 UTC (rev 309) +++ pom.xml 2008-10-11 12:29:29 UTC (rev 310) @@ -43,6 +43,13 @@ <systemPath>${basedir}/lib/jcurses/jcurses.jar</systemPath> </dependency> <dependency> + <groupId>charva</groupId> + <artifactId>charva</artifactId> + <version>1.1.4</version> + <scope>system</scope> + <systemPath>${basedir}/lib/charva/charva.jar</systemPath> + </dependency> + <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> Property changes on: src/main/java/nl/improved/sqlclient ___________________________________________________________________ Name: svn:ignore - .SQLPlusPlus.java.swp .SQLPlus.java.swp .DBConnector.java.swp .SQLUtil.java.swp .Point.java.swp .SQLLineWrapper.java.swp .SQLOutput.java.swp .QueryExecutor.java.swp .SQLShell.java.swp + .SQLPlusPlus.java.swp .SQLPlus.java.swp .DBConnector.java.swp .SQLUtil.java.swp .Point.java.swp .SQLLineWrapper.java.swp .SQLOutput.java.swp .QueryExecutor.java.swp .SQLShell.java.swp .AbstractSQLShellWindow.java.swo .AbstractSQLShellWindow.java.swp Modified: src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java =================================================================== --- src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-10-11 11:20:20 UTC (rev 309) +++ src/main/java/nl/improved/sqlclient/AbstractSQLShellWindow.java 2008-10-11 12:29:29 UTC (rev 310) @@ -1567,6 +1567,26 @@ } /** + * 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 + */ + protected 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; + } + + /** * Writes in/output to a file. */ private class SpoolCommand implements Command { Added: src/main/java/nl/improved/sqlclient/charva/CharvaLoginDialog.java =================================================================== --- src/main/java/nl/improved/sqlclient/charva/CharvaLoginDialog.java 2008-10-11 11:20:20 UTC (rev 309) +++ src/main/java/nl/improved/sqlclient/charva/CharvaLoginDialog.java 2008-10-11 12:29:29 UTC (rev 310) @@ -0,0 +1,136 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package nl.improved.sqlclient.charva; + +import charva.awt.GridBagConstraints; +import charva.awt.GridBagLayout; +import charva.awt.event.ActionEvent; +import charva.awt.event.ActionListener; +import charva.awt.event.KeyEvent; +import charva.awt.event.KeyListener; +import charvax.swing.JButton; +import charvax.swing.JDialog; +import charvax.swing.JLabel; +import charvax.swing.JPasswordField; +import charvax.swing.JTextField; + +/** + * + * @author roy + */ +public class CharvaLoginDialog extends JDialog { + + private boolean exitOk = false; + private JTextField userfield; + private JPasswordField passfield; + + /** + * Constructor. + * @param username the default value in the username field + * @param password the default value in the password field + */ + public CharvaLoginDialog(final String username, final String password) { + setModal(true); + userfield = new JTextField(15); + setUsername(username); + passfield = new JPasswordField(15); + passfield.addKeyListener(new KeyListener() { + + public void keyPressed(KeyEvent arg0) { } + + public void keyTyped(KeyEvent evt) { + if(evt.isActionKey() && evt.getKeyCode() == KeyEvent.VK_ENTER) { + okButtonPressedSlot(); + } + } + public void keyReleased(KeyEvent arg0) { } + }); + + setPassword(password); + JButton okButton = new JButton("Ok"); + + okButton.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent event) { + okButtonPressedSlot(); + } + }); + JButton cancelButton = new JButton("Cancel"); + cancelButton.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent arg0) { + CharvaLoginDialog.this.exitOk = false; + CharvaLoginDialog.this.hide(); + } + }); + setLayout(new GridBagLayout()); + GridBagConstraints gbc = new GridBagConstraints(); + gbc.fill = GridBagConstraints.HORIZONTAL; + gbc.gridx = 0; + add(new JLabel("Login:"), gbc); + gbc.gridx = 1; + add(userfield, gbc); + gbc.gridx = 0; + gbc.gridy = 1; + add(new JLabel("Password:"), gbc); + gbc.gridx = 1; + add(passfield, gbc); + gbc.gridx = 0; + gbc.gridy = 2; + add(cancelButton, gbc); + gbc.gridx = 1; + add(okButton, gbc); + } + + public void okButtonPressedSlot() { + exitOk = true; + hide(); + } + + + + public void setUsername(String username) { + if (username == null) { + userfield.setText(""); + } else { + userfield.setText(username); + } + } + + /** + * Returns the username entered. + * @return the username entered. + */ + public String getUsername() { + return userfield.getText(); + } + + public void setPassword(String password) { + if (password == null) { + passfield.setText(""); + } else { + passfield.setText(password); + } + } + + /** + * Returns the password entered. + * @return the password entered. + */ + public String getPassword() { + return passfield.getText(); + } + + /** + * Returns true if the dialog has been closed to accept the input variables. + * @return true if the dialog has been closed to accept the input variables.n + */ + public boolean endedSuccessfully() { + return exitOk; + } +} \ No newline at end of file Added: src/main/java/nl/improved/sqlclient/charva/CharvaSQLShellWindow.java =================================================================== --- src/main/java/nl/improved/sqlclient/charva/CharvaSQLShellWindow.java 2008-10-11 11:20:20 UTC (rev 309) +++ src/main/java/nl/improved/sqlclient/charva/CharvaSQLShellWindow.java 2008-10-11 12:29:29 UTC (rev 310) @@ -0,0 +1,230 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package nl.improved.sqlclient.charva; + +import charva.awt.Toolkit; +import charva.awt.event.ActionEvent; +import charva.awt.event.ActionListener; +import charva.awt.event.KeyEvent; +import charvax.swing.JMenuItem; +import charvax.swing.JPopupMenu; +import java.sql.SQLException; +import java.util.List; +import java.util.Vector; +import nl.improved.sqlclient.AbstractSQLShellWindow; +import nl.improved.sqlclient.InputKey; +import nl.improved.sqlclient.Point; +import nl.improved.sqlclient.SQLCommand; +import nl.improved.sqlclient.Screen; + +/** + * + * @author roy + */ +class CharvaSQLShellWindow extends AbstractSQLShellWindow { + private SQLShellComponent textComponent; + + public CharvaSQLShellWindow(SQLShellComponent textComponent) { + this.textComponent = textComponent; + } + + @Override + public int getScreenWidth() { + return Toolkit.getDefaultToolkit().getScreenColumns()-1; + } + + @Override + public int getScreenHeight() { + return Toolkit.getDefaultToolkit().getScreenRows()-1; + } + + void show() { + paint(getScreen()); + } + + @Override + public void paint(Screen screen) { + int totalLineCount = 0; + StringBuilder newText = new StringBuilder(); + for (CharSequence seq: screen.getScreenBuffer()) { + newText.append(seq.toString()); + newText.append("\n"); + totalLineCount++; + } + for (SQLCommand s : getUnprocessedCommands()) { + for (CharSequence seq: s.getLines()) { + newText.append(screen.getEmptyLine().substring(0, Screen.PROMPT.length()) + ">"); + newText.append(seq.toString()); + newText.append("\n"); + totalLineCount++; + } + } + List<String> commandLines = formatCommandLines(screen.getShowPrompt() + , screen.getEmptyLine(),(List<CharSequence>) getCommand().getLines()); + for (int i = 0; i < commandLines.size(); i++) { + CharSequence seq = commandLines.get(i); + newText.append(seq.toString()); + if (i < commandLines.size()-1) { + newText.append("\n"); + totalLineCount++; + } + } + if (totalLineCount > getScreenHeight()-1) { + totalLineCount = getScreenHeight()-1; + } + String trimmed = trim(newText); + if (trimmed.startsWith(textComponent.getText())) { + textComponent.append(trimmed.substring(textComponent.getText().length())); + } else { + textComponent.replaceRange(trim(newText), 0, textComponent.getText().length()); + } + Point cursorPos = screen.getCursorPosition(); + try { + int start = textComponent.getLineStartOffset(totalLineCount - (commandLines.size() - cursorPos.y)+1); + textComponent.setCaretPosition(start + cursorPos.x + (Screen.PROMPT+" >").length()); + } catch(Exception e) { + debug("ERROR: failed to calculate line end: "+ totalLineCount +"-"+ cursorPos.y); + } + // TODO fix caretposition + textComponent.draw(); + } + + @Override + public void close() { + super.close(); + Toolkit.getDefaultToolkit().getTopWindow().hide(); + } + + + @Override + public void beep() { + Toolkit.getDefaultToolkit().beep(); + } + + @Override + public void debug(String debug) { + + } + + @Override + public String select(List<String> items, Point p) { + Vector v = new Vector(items.size()); + for(String item : items) { + JMenuItem mi = new JMenuItem(item); + mi.setActionCommand(item); + v.add(mi); + } + MyPopupMenu menu = new MyPopupMenu(v); + charva.awt.Point point = new charva.awt.Point(p.x, Math.max(2, p.y-items.size())); + menu.setLocation(point); + return menu.select(); + } + + @Override + protected String[] getLoginCredentials(String username, String password) throws SQLException { + + CharvaLoginDialog charvaLoginDialog = new CharvaLoginDialog(username, password); + charvaLoginDialog.setLocationRelativeTo(textComponent); + charvaLoginDialog.show(); + if (charvaLoginDialog.endedSuccessfully()) { + return new String[]{charvaLoginDialog.getUsername(), charvaLoginDialog.getPassword()}; + } + return null; + } + + public void keyTyped(KeyEvent evt) { + if (evt.isConsumed()) { + return; + } + if (evt.isActionKey()) { + if (evt.getKeyCode() == evt.VK_ENTER) { + handleInput(new InputKey('\n')); + evt.consume(); + } else if (evt.getKeyCode() == KeyEvent.VK_BACK_SPACE) { + handleInput(new InputKey(InputKey.SpecialKey.BACKSPACE)); + evt.consume(); + } else if (evt.getKeyCode() == KeyEvent.VK_DELETE) { + handleInput(new InputKey(InputKey.SpecialKey.DELETE)); + evt.consume(); + } else if (evt.getKeyCode() == KeyEvent.VK_PAGE_UP) { + handleInput(new InputKey(InputKey.SpecialKey.PAGE_DOWN)); + evt.consume(); + } else if (evt.getKeyCode() == KeyEvent.VK_PAGE_DOWN) { + handleInput(new InputKey(InputKey.SpecialKey.PAGE_UP)); + evt.consume(); + } else if (evt.getKeyCode() == KeyEvent.VK_LEFT) { + handleInput(new InputKey(InputKey.SpecialKey.LEFT)); + evt.consume(); + } else if (evt.getKeyCode() == KeyEvent.VK_RIGHT) { + handleInput(new InputKey(InputKey.SpecialKey.RIGHT)); + evt.consume(); + } else if (evt.getKeyCode() == KeyEvent.VK_DOWN) { + handleInput(new InputKey(InputKey.SpecialKey.DOWN)); + evt.consume(); + } else if (evt.getKeyCode() == KeyEvent.VK_UP) { + handleInput(new InputKey(InputKey.SpecialKey.UP)); + evt.consume(); + } else if (evt.getKeyCode() == KeyEvent.VK_HOME) { + handleInput(new InputKey(InputKey.SpecialKey.HOME)); + evt.consume(); + } else if (evt.getKeyCode() == KeyEvent.VK_END) { + handleInput(new InputKey(InputKey.SpecialKey.END)); + evt.consume(); + } + return; + } + handleInput(new InputKey((char)evt.getKeyCode())); + evt.consume(); + } + + private String trim(StringBuilder text) { + String strText = text.toString(); + if (strText.indexOf('\n') < 0) { + return strText; + } + int maxHeight = getScreenHeight()-1; + String[] lines = strText.split("\n"); + if (lines.length <= maxHeight) { + return strText; + } + StringBuilder newString = new StringBuilder(); + boolean endsWithReturn = strText.endsWith("\n"); + for (int i = lines.length - maxHeight; i < lines.length; i++) { + newString.append(lines[i]); + if (i < lines.length-1 || endsWithReturn) { + newString.append('\n'); + } + } + return newString.toString(); + } + + private static class MyPopupMenu extends JPopupMenu { + private String selectedValue; + public MyPopupMenu(Vector items) { + super(items); + + ActionListener acl = new ActionListener() { + + public void actionPerformed(ActionEvent arg0) { + selectedValue = arg0.getActionCommand(); + MyPopupMenu.super.hide(); + } + }; + for (int i = 0; i < items.size(); i++) { + ((JMenuItem)items.get(i)).addActionListener(acl); + } + } + + + @Override + public void hide() {} + + private String select() { + show(); + return selectedValue; + } + } +} Added: src/main/java/nl/improved/sqlclient/charva/SQLShellComponent.java =================================================================== --- src/main/java/nl/improved/sqlclient/charva/SQLShellComponent.java 2008-10-11 11:20:20 UTC (rev 309) +++ src/main/java/nl/improved/sqlclient/charva/SQLShellComponent.java 2008-10-11 12:29:29 UTC (rev 310) @@ -0,0 +1,42 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package nl.improved.sqlclient.charva; + +import charva.awt.Dimension; +import charva.awt.Point; +import charva.awt.Toolkit; +import charva.awt.event.KeyEvent; +import charvax.swing.JFrame; +import charvax.swing.JTextArea; + +/** + * + * @author roy + */ +public class SQLShellComponent extends JTextArea { + + private CharvaSQLShellWindow sqlshellWindow = new CharvaSQLShellWindow(this); + + @Override + public void processKeyEvent(KeyEvent arg0) { + //super.processKeyEvent(arg0); + sqlshellWindow.keyTyped(arg0); + } + + public static void main(String[] args) { + JFrame frame = new JFrame(); + SQLShellComponent component = new SQLShellComponent(); + frame.add(component); + //frame.pack(); + component.setBounds(new Point(0,0), new Dimension(Toolkit.getDefaultToolkit().getScreenColumns()-2 + , Toolkit.getDefaultToolkit().getScreenRows()-2)); + frame.add(component); + component.sqlshellWindow.show(); + frame.pack(); + frame.show(); + } + +} Property changes on: src/main/java/nl/improved/sqlclient/jcurses ___________________________________________________________________ Name: svn:ignore + .SQLShellWindow.java.swo .SQLShellWindow.java.swp Modified: src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java =================================================================== --- src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java 2008-10-11 11:20:20 UTC (rev 309) +++ src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java 2008-10-11 12:29:29 UTC (rev 310) @@ -339,26 +339,6 @@ 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(); |