283 lines
13 KiB
Java
283 lines
13 KiB
Java
package ch.psi.mxsc;
|
|
|
|
|
|
import ch.psi.pshell.swing.StandardDialog;
|
|
import ch.psi.pshell.swing.SwingUtils;
|
|
import java.util.Arrays;
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
*
|
|
*/
|
|
public class PasswordDialog extends StandardDialog {
|
|
final List numbers;
|
|
final long secret;
|
|
|
|
public PasswordDialog(java.awt.Frame parent, boolean modal, long secret) {
|
|
super(parent, modal);
|
|
initComponents();
|
|
numbers = Arrays.asList(0,1,2,3,4,5,6,7,8,9);
|
|
this.secret = secret;
|
|
Collections.shuffle(numbers);
|
|
setTitle("Password");
|
|
|
|
button1.setText(numbers.get(0) + " or " + numbers.get(1));
|
|
button2.setText(numbers.get(2) + " or " + numbers.get(3));
|
|
button3.setText(numbers.get(4) + " or " + numbers.get(5));
|
|
button4.setText(numbers.get(6) + " or " + numbers.get(7));
|
|
button5.setText(numbers.get(8) + " or " + numbers.get(9));
|
|
}
|
|
|
|
|
|
|
|
boolean checkPassword(){
|
|
char[] password = textPassword.getPassword();
|
|
int length = password.length;
|
|
String pass = String.valueOf(secret);
|
|
if ((length==0) || (length != pass.length())) {
|
|
return false;
|
|
}
|
|
|
|
for (int i=0; i<length; i++){
|
|
char c = password[i];
|
|
int digit = Integer.valueOf(pass.substring(i, i+1));
|
|
int index = c - '0';
|
|
if ((!numbers.get(index*2).equals(digit)) && (!numbers.get(index*2+1).equals(digit))){
|
|
return false;
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 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() {
|
|
|
|
button2 = new javax.swing.JButton();
|
|
button1 = new javax.swing.JButton();
|
|
button3 = new javax.swing.JButton();
|
|
button4 = new javax.swing.JButton();
|
|
button5 = new javax.swing.JButton();
|
|
buttonCancel = new javax.swing.JButton();
|
|
buttonOk = new javax.swing.JButton();
|
|
buttonClear = new javax.swing.JButton();
|
|
textPassword = new javax.swing.JPasswordField();
|
|
|
|
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
|
|
|
|
button2.setText("X or Y");
|
|
button2.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
button2ActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
button1.setText("X or Y");
|
|
button1.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
button1ActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
button3.setText("X or Y");
|
|
button3.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
button3ActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
button4.setText("X or Y");
|
|
button4.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
button4ActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
button5.setText("X or Y");
|
|
button5.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
button5ActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
buttonCancel.setText("Cancel");
|
|
buttonCancel.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
buttonCancelActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
buttonOk.setText("Ok");
|
|
buttonOk.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
buttonOkActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
buttonClear.setText("Clear");
|
|
buttonClear.addActionListener(new java.awt.event.ActionListener() {
|
|
public void actionPerformed(java.awt.event.ActionEvent evt) {
|
|
buttonClearActionPerformed(evt);
|
|
}
|
|
});
|
|
|
|
textPassword.setFont(new java.awt.Font("Lucida Grande", 0, 36)); // NOI18N
|
|
textPassword.setHorizontalAlignment(javax.swing.JTextField.CENTER);
|
|
|
|
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(40, Short.MAX_VALUE)
|
|
.addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addGap(18, 18, 18)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
|
|
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
|
.addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addGap(18, 18, 18)
|
|
.addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addGap(18, 18, 18)
|
|
.addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addGap(18, 18, 18)
|
|
.addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
|
.addComponent(textPassword)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
|
|
.addComponent(buttonClear, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)))
|
|
.addContainerGap(40, Short.MAX_VALUE))
|
|
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
|
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
|
.addComponent(buttonCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
|
|
.addComponent(buttonOk, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
|
|
);
|
|
|
|
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonCancel, buttonOk});
|
|
|
|
layout.setVerticalGroup(
|
|
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
|
|
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
|
|
.addGap(40, 40, 40)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
|
|
.addComponent(button2, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
|
|
.addComponent(button5, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
.addGap(44, 44, 44)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
|
|
.addComponent(buttonClear)
|
|
.addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
|
|
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
|
|
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
|
|
.addComponent(buttonOk)
|
|
.addComponent(buttonCancel))
|
|
.addContainerGap())
|
|
);
|
|
|
|
pack();
|
|
}// </editor-fold>//GEN-END:initComponents
|
|
|
|
private void buttonClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClearActionPerformed
|
|
textPassword.setText("");
|
|
}//GEN-LAST:event_buttonClearActionPerformed
|
|
|
|
private void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
|
|
cancel();
|
|
}//GEN-LAST:event_buttonCancelActionPerformed
|
|
|
|
private void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
|
|
if (!checkPassword()){
|
|
SwingUtils.showMessage(this, "Error", "Invalid password");
|
|
buttonClearActionPerformed(null);
|
|
return;
|
|
}
|
|
accept();
|
|
}//GEN-LAST:event_buttonOkActionPerformed
|
|
|
|
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
|
|
textPassword.setText(textPassword.getText() + "0");
|
|
}//GEN-LAST:event_button1ActionPerformed
|
|
|
|
private void button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button2ActionPerformed
|
|
textPassword.setText(textPassword.getText() + "1");
|
|
}//GEN-LAST:event_button2ActionPerformed
|
|
|
|
private void button3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button3ActionPerformed
|
|
textPassword.setText(textPassword.getText() + "2");
|
|
}//GEN-LAST:event_button3ActionPerformed
|
|
|
|
private void button4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button4ActionPerformed
|
|
textPassword.setText(textPassword.getText() + "3");
|
|
}//GEN-LAST:event_button4ActionPerformed
|
|
|
|
private void button5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button5ActionPerformed
|
|
textPassword.setText(textPassword.getText() + "4");
|
|
}//GEN-LAST:event_button5ActionPerformed
|
|
|
|
/**
|
|
* @param args the command line arguments
|
|
*/
|
|
public static void main(String args[]) {
|
|
/* Set the Nimbus look and feel */
|
|
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
|
|
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
|
|
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
|
|
*/
|
|
try {
|
|
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
|
|
if ("Nimbus".equals(info.getName())) {
|
|
javax.swing.UIManager.setLookAndFeel(info.getClassName());
|
|
break;
|
|
}
|
|
}
|
|
} catch (ClassNotFoundException ex) {
|
|
java.util.logging.Logger.getLogger(PasswordDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
|
} catch (InstantiationException ex) {
|
|
java.util.logging.Logger.getLogger(PasswordDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
|
} catch (IllegalAccessException ex) {
|
|
java.util.logging.Logger.getLogger(PasswordDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
|
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
|
|
java.util.logging.Logger.getLogger(PasswordDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
|
|
}
|
|
//</editor-fold>
|
|
|
|
/* Create and display the dialog */
|
|
java.awt.EventQueue.invokeLater(new Runnable() {
|
|
public void run() {
|
|
PasswordDialog dialog = new PasswordDialog(new javax.swing.JFrame(), true, 1234);
|
|
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
|
|
@Override
|
|
public void windowClosing(java.awt.event.WindowEvent e) {
|
|
System.exit(0);
|
|
}
|
|
});
|
|
dialog.setVisible(true);
|
|
}
|
|
});
|
|
}
|
|
|
|
// Variables declaration - do not modify//GEN-BEGIN:variables
|
|
private javax.swing.JButton button1;
|
|
private javax.swing.JButton button2;
|
|
private javax.swing.JButton button3;
|
|
private javax.swing.JButton button4;
|
|
private javax.swing.JButton button5;
|
|
private javax.swing.JButton buttonCancel;
|
|
private javax.swing.JButton buttonClear;
|
|
private javax.swing.JButton buttonOk;
|
|
private javax.swing.JPasswordField textPassword;
|
|
// End of variables declaration//GEN-END:variables
|
|
}
|