/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ua.pp.bizon.cripto.gui;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import ua.pp.bizon.cripto.authorization.Credentials;
import ua.pp.bizon.cripto.authorization.UI;
import ua.pp.bizon.cripto.configuration.Configuration;
import ua.pp.bizon.cripto.keystore.CryptoException;
import ua.pp.bizon.cripto.keystore.CryptoUtil;
/**
*
* @author roman
*/
public class UIDialog extends javax.swing.JDialog implements UI {
/**
*
*/
private static final long serialVersionUID = 1231785838254487474L;
/**
* 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;
/**
* Creates new form UIDialog
*/
public UIDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
// Close the dialog when Esc is pressed
String cancelName = "cancel";
InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
ActionMap actionMap = getRootPane().getActionMap();
actionMap.put(cancelName, new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = 1820594207832189538L;
public void actionPerformed(ActionEvent e) {
doClose(RET_CANCEL);
}
});
}
/**
* @return the return status of this dialog - one of RET_OK or RET_CANCEL
*/
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.
*/
// <editor-fold defaultstate="collapsed"
// desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
usernameLabel = new javax.swing.JLabel();
usernameTextField = new javax.swing.JTextField();
passwordLabel = new javax.swing.JLabel();
passwordField = new javax.swing.JPasswordField();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
okButton.setText("OK");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
cancelButton.setText("Cancel");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
usernameLabel.setText("Input username:");
passwordLabel.setText("Input password:");
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(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGap(0, 262, Short.MAX_VALUE)
.addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 67,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(cancelButton))
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(usernameLabel).addComponent(passwordLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(usernameTextField).addComponent(passwordField)))).addContainerGap()));
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { cancelButton, okButton });
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(usernameLabel)
.addComponent(usernameTextField, 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(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(passwordLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(cancelButton).addComponent(okButton))
.addContainerGap()));
getRootPane().setDefaultButton(okButton);
pack();
}// </editor-fold>//GEN-END:initComponents
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_okButtonActionPerformed
doClose(RET_OK);
}// GEN-LAST:event_okButtonActionPerformed
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_cancelButtonActionPerformed
doClose(RET_CANCEL);
}// GEN-LAST:event_cancelButtonActionPerformed
/**
* Closes the dialog
*/
private void closeDialog(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_closeDialog
doClose(RET_CANCEL);
}// GEN-LAST:event_closeDialog
private void doClose(int retStatus) {
returnStatus = retStatus;
setVisible(false);
dispose();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cancelButton;
private javax.swing.JButton okButton;
private javax.swing.JPasswordField passwordField;
private javax.swing.JLabel passwordLabel;
private javax.swing.JLabel usernameLabel;
private javax.swing.JTextField usernameTextField;
// End of variables declaration//GEN-END:variables
private int returnStatus = RET_CANCEL;
@Override
public void login(Credentials credentials) throws IOException, CryptoException {
setVisible(true);
if (returnStatus == RET_OK) {
credentials.setUsername(usernameTextField.getText());
credentials.setHashedPassword(CryptoUtil.cryptPassword(passwordField.getPassword()));
} else {
throw new CryptoException("user canseled login");
}
}
@Override
public Configuration configure(Configuration c) {
SelectSourceDialog frame = new SelectSourceDialog(null, true);
if (c != null) {
frame.getSelectFromPathPanel().select(c.getFrom());
frame.getSelectToPathPanel().select(c.getTo());
}
c = new Configuration();
frame.setVisible(true);
c.setFrom(frame.getSelectFromPathPanel().getSelected());
c.setTo(frame.getSelectToPathPanel().getSelected());
c.setDirection(frame.getDirection());
return c;
}
@Override
public void done() {
JOptionPane.showMessageDialog(null, "done");
}
}