/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.eas.client.login; import static com.eas.client.login.ConnectionsSelector.SETTINGS_NODE; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.util.ResourceBundle; import java.util.prefs.Preferences; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.KeyStroke; /** * * @author mg */ public class CredentialsSelector extends javax.swing.JDialog { /** * A return status code - returned if Cancel button has been pressed */ public static final int RET_CANCEL = 0; /** * A return status code - returned if OK button has been pressed */ public static final int RET_OK = 1; // private static final String LOGIN_SETTING = "login"; private static final String PASSWORD_SETTING = "password"; // private static final String CANCEL_ACTION_ID = "cancel"; private static final String OK_ACTION_ID = "ok"; private static final ResourceBundle bundle = ResourceBundle.getBundle("com/eas/client/login/Bundle"); private int returnStatus = RET_CANCEL; private final Action okAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { returnStatus = RET_OK; CredentialsSelector.this.dispose(); if (checkRememberPassword.isSelected()) { Preferences.userRoot().node(SETTINGS_NODE).put(LOGIN_SETTING, String.valueOf(tfUserName.getText())); Preferences.userRoot().node(SETTINGS_NODE).put(PASSWORD_SETTING, String.valueOf(tfPassword.getPassword())); } } }; private final Action cancelAction = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { returnStatus = RET_CANCEL; CredentialsSelector.this.dispose(); } }; /** * Creates new form LoginDialog * */ public CredentialsSelector() { super((java.awt.Frame) null, true); initComponents(); tfUserName.getActionMap().put(OK_ACTION_ID, okAction); tfUserName.getActionMap().put(CANCEL_ACTION_ID, cancelAction); tfPassword.getActionMap().put(OK_ACTION_ID, okAction); tfPassword.getActionMap().put(CANCEL_ACTION_ID, cancelAction); tfUserName.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), OK_ACTION_ID); tfUserName.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL_ACTION_ID); tfPassword.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), OK_ACTION_ID); tfPassword.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL_ACTION_ID); String userName = Preferences.userRoot().node(SETTINGS_NODE).get(LOGIN_SETTING, ""); if (userName != null && !userName.isEmpty()) { tfUserName.setText(userName); String password = Preferences.userRoot().node(SETTINGS_NODE).get(PASSWORD_SETTING, ""); tfPassword.setText(password); checkRememberPassword.setSelected(true); } } public String getUserName() { return tfUserName.getText(); } public String getPassword() { return new String(tfPassword.getPassword()); } public int getReturnStatus() { return returnStatus; } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { pnlAppLogin = new javax.swing.JPanel(); tfUserName = new javax.swing.JTextField(); lblUserName = new javax.swing.JLabel(); tfPassword = new javax.swing.JPasswordField(); lblPassword = new javax.swing.JLabel(); checkRememberPassword = new javax.swing.JCheckBox(); btnOk = new javax.swing.JButton(); btnCancel = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle(bundle.getString("LoginDialog.authenticationRequired")); // NOI18N setIconImage(new javax.swing.ImageIcon(getClass().getResource("/com/eas/client/login/key.png")).getImage()); addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { formKeyTyped(evt); } }); lblUserName.setText(bundle.getString("LoginDialog.lblUserName.text")); // NOI18N lblPassword.setText(bundle.getString("LoginDialog.lblPassword.text")); // NOI18N checkRememberPassword.setText(bundle.getString("checkRememberPassword")); // NOI18N javax.swing.GroupLayout pnlAppLoginLayout = new javax.swing.GroupLayout(pnlAppLogin); pnlAppLogin.setLayout(pnlAppLoginLayout); pnlAppLoginLayout.setHorizontalGroup( pnlAppLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlAppLoginLayout.createSequentialGroup() .addContainerGap() .addGroup(pnlAppLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlAppLoginLayout.createSequentialGroup() .addGroup(pnlAppLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(lblPassword, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblUserName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(pnlAppLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(tfUserName) .addComponent(tfPassword)) .addContainerGap()) .addGroup(pnlAppLoginLayout.createSequentialGroup() .addComponent(checkRememberPassword) .addGap(17, 265, Short.MAX_VALUE)))) ); pnlAppLoginLayout.setVerticalGroup( pnlAppLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlAppLoginLayout.createSequentialGroup() .addContainerGap() .addGroup(pnlAppLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblUserName) .addComponent(tfUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(pnlAppLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblPassword) .addComponent(tfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(checkRememberPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(10, 10, 10)) ); btnOk.setAction(okAction); btnOk.setText(bundle.getString("Dialog.OKButton.text")); // NOI18N btnCancel.setAction(cancelAction); btnCancel.setText(bundle.getString("Dialog.CancelButton.text")); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(pnlAppLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnOk, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnCancel) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(pnlAppLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnOk) .addComponent(btnCancel)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void formKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyTyped if (evt.getKeyCode() == KeyEvent.VK_ENTER) { okAction.actionPerformed(new ActionEvent(this, 0, null)); } else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) { cancelAction.actionPerformed(new ActionEvent(this, 0, null)); } }//GEN-LAST:event_formKeyTyped // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnCancel; private javax.swing.JButton btnOk; private javax.swing.JCheckBox checkRememberPassword; private javax.swing.JLabel lblPassword; private javax.swing.JLabel lblUserName; private javax.swing.JPanel pnlAppLogin; private javax.swing.JPasswordField tfPassword; private javax.swing.JTextField tfUserName; // End of variables declaration//GEN-END:variables }