package javaforce;
/*
* GetPassword.java
*
* Created on August 3, 2007, 1:57 PM
*
* @author pquiring
*/
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class GetPassword extends javax.swing.JDialog {
/**
* Creates new form GetPassword
*/
public GetPassword(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
setComponentOrientation(((parent == null) ? javax.swing.JOptionPane.getRootFrame() : parent).getComponentOrientation());
if (parent != null) {
setLocationRelativeTo(parent); //doesn't work
}
setPosition();
}
/**
* 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
label = new javax.swing.JLabel();
password = new javax.swing.JPasswordField();
bOk = new javax.swing.JButton();
bCancel = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Enter Password");
setResizable(false);
label.setText("Password");
password.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
passwordKeyPressed(evt);
}
});
bOk.setText("OK");
bOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bOkActionPerformed(evt);
}
});
bCancel.setText("Cancel");
bCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bCancelActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(label)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(password, javax.swing.GroupLayout.DEFAULT_SIZE, 148, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(bOk)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
.addComponent(bCancel)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(label)
.addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bOk)
.addComponent(bCancel))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void passwordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordKeyPressed
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
bOkActionPerformed(null);
}
if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
bCancelActionPerformed(null);
}
}//GEN-LAST:event_passwordKeyPressed
private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
retValue = null;
setVisible(false);
}//GEN-LAST:event_bCancelActionPerformed
private void bOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOkActionPerformed
retValue = new String(password.getPassword());
if (retValue.length() == 0) {
return;
}
setVisible(false);
}//GEN-LAST:event_bOkActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton bCancel;
private javax.swing.JButton bOk;
private javax.swing.JLabel label;
private javax.swing.JPasswordField password;
// End of variables declaration//GEN-END:variables
private String retValue = null;
public static String getPassword(Frame parent) {
return getPassword(parent, null);
}
public static String getPassword(Frame parent, String prompt) {
GetPassword dialog = new GetPassword(parent, true);
if (prompt != null) {
dialog.label.setText(prompt);
dialog.pack();
}
dialog.setVisible(true); //does not return until dialog is closed
return dialog.retValue;
}
private void setPosition() {
Rectangle s = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
Dimension d = getPreferredSize();
setLocation(s.width / 2 - d.width / 2, s.height / 2 - (d.height / 2));
}
}