From: SVN by r. <sv...@ca...> - 2008-09-15 12:11:28
|
Author: roy Date: 2008-09-15 21:11:19 +0200 (Mon, 15 Sep 2008) New Revision: 306 Added: src/main/java/nl/improved/sqlclient/jcurses/LoginDialog.java Modified: src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java Log: small cleanup code step Added: src/main/java/nl/improved/sqlclient/jcurses/LoginDialog.java =================================================================== --- src/main/java/nl/improved/sqlclient/jcurses/LoginDialog.java 2008-09-15 19:01:58 UTC (rev 305) +++ src/main/java/nl/improved/sqlclient/jcurses/LoginDialog.java 2008-09-15 19:11:19 UTC (rev 306) @@ -0,0 +1,138 @@ +package nl.improved.sqlclient.jcurses; + +import jcurses.event.ActionEvent; +import jcurses.event.ActionListener; +import jcurses.system.InputChar; +import jcurses.widgets.Button; +import jcurses.widgets.Dialog; +import jcurses.widgets.GridLayoutManager; +import jcurses.widgets.Label; +import jcurses.widgets.PasswordField; +import jcurses.widgets.TextField; +import jcurses.widgets.WidgetsConstants; + +/** + * Simple login dialog used for jcurses implementation. + * It allows a user to enter the username/pwd + */ +public class LoginDialog extends Dialog { + + private boolean exitOk = false; + private TextField userfield; + private PasswordField passfield; + + /** + * Constructor. + * @param username the default value in the username field + * @param password the default value in the password field + */ + public LoginDialog(final String username, final String password) { + super(10, 10, 50, 7, true, "Connect"); + userfield = new TextField(); + setUsername(username); + passfield = new PasswordField() { + + @Override + protected void focus() { + super.focus(); + } + + @Override + protected boolean handleInput(InputChar ch) { + if (!ch.isSpecialCode() && ch.getCharacter() == '\n') { + okButtonPressedSlot(); + return false; + } + return super.handleInput(ch); + } + }; + setPassword(password); + Button okButton = new Button("Ok"); + okButton.addListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent event) { + okButtonPressedSlot(); + } + }); + Button cancelButton = new Button("Cancel"); + cancelButton.addListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent arg0) { + LoginDialog.this.exitOk = false; + LoginDialog.this.close(); + } + }); + GridLayoutManager glm = new GridLayoutManager(4, 3); + getRootPanel().setLayoutManager(glm); + glm.addWidget(new Label("Username"), 0, 0, 1, 1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_LEFT); + glm.addWidget(userfield, 1, 0, 3, 1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_LEFT); + glm.addWidget(new Label("Password"), 0, 1, 1, 1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_LEFT); + glm.addWidget(passfield, 1, 1, 3, 1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_LEFT); + + glm.addWidget(okButton, 1, 2, 1, 1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_CENTER); + glm.addWidget(cancelButton, 2, 2, 1, 1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_CENTER); + } + + public void okButtonPressedSlot() { + exitOk = true; + close(); + } + + + /** + * Override to force focus on the correct input fields. + */ + @Override + protected void activate() { + super.activate(); + if (userfield.getText().length() == 0) { + userfield.getFocus(); + } else if (passfield.getText().length() == 0) { + passfield.getFocus(); + } else { + throw new IllegalStateException("We have login data, but get a login dailog anyway."); + } + } + + 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; + } +} Modified: src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java =================================================================== --- src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java 2008-09-15 19:01:58 UTC (rev 305) +++ src/main/java/nl/improved/sqlclient/jcurses/SQLShellWindow.java 2008-09-15 19:11:19 UTC (rev 306) @@ -299,7 +299,7 @@ dontRepaint = true; try { diag.show(); - if (!diag.exitOk) { + if (!diag.endedSuccessfully()) { throw new SQLException("Connect cancelled."); } return new String[] {diag.getUsername(), diag.getPassword() }; @@ -329,104 +329,6 @@ Toolkit.beep(); } - private static class LoginDialog extends Dialog { - private boolean exitOk = false; - private TextField userfield; - private PasswordField passfield; - - public LoginDialog(final String username, final String password) { - super(10,10, 50, 7, true,"Connect"); - userfield = new TextField(); - setUsername(username); - passfield = new PasswordField() { - - @Override - protected void focus() { - super.focus(); - } - - @Override - protected boolean handleInput(InputChar ch) { - if (!ch.isSpecialCode() && ch.getCharacter() == '\n') { - okButtonPressedSlot(); - return false; - } - return super.handleInput(ch); - } - }; - setPassword(password); - - Button okButton = new Button("Ok"); - okButton.addListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent event) { - okButtonPressedSlot(); - } - }); - Button cancelButton = new Button("Cancel"); - cancelButton.addListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent arg0) { - LoginDialog.this.exitOk = false; - LoginDialog.this.close(); - } - }); - - GridLayoutManager glm = new GridLayoutManager(4,3); - getRootPanel().setLayoutManager(glm); - - glm.addWidget(new Label("Username"), 0,0,1,1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_LEFT); - glm.addWidget(userfield, 1,0,3,1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_LEFT); - glm.addWidget(new Label("Password"), 0,1,1,1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_LEFT); - glm.addWidget(passfield, 1,1,3,1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_LEFT); - - glm.addWidget(okButton, 1,2,1,1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_CENTER); - glm.addWidget(cancelButton, 2,2,1,1, WidgetsConstants.ALIGNMENT_CENTER, WidgetsConstants.ALIGNMENT_CENTER); - - } - public void okButtonPressedSlot() { - exitOk = true; - close(); - } - - @Override - protected void activate() { - super.activate(); - - if (userfield.getText().length() == 0) { - userfield.getFocus(); - } else if (passfield.getText().length() == 0) { - passfield.getFocus(); - } else { - throw new IllegalStateException("We have login data, but get a login dailog anyway."); - } - } - - public void setUsername(String username) { - if (username == null) { - userfield.setText(""); - } else { - userfield.setText(username); - } - } - - public String getUsername() { - return userfield.getText(); - } - - public void setPassword(String password) { - if (password == null) { - passfield.setText(""); - } else { - passfield.setText(password); - } - } - - public String getPassword() { - return passfield.getText(); - } - } - public static void main(String[] args) { SQLShellWindow shell = new SQLShellWindow(); shell.show(); |