package ringdb.ui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import ringdb.database.Database;
import ringdb.database.Ringer;
import ringdb.ui.validation.IFormValidationListener;
import ringdb.ui.validation.NotEmptyValidator;
public class RingerScreen extends JInternalFrame implements IFormValidationListener {
/** Version ID */
private static final long serialVersionUID = 8498217752258805827L;
private static final int FRAME_WIDTH = 400;
private static final int FRAME_HEIGHT = 200;
private MainWindow m_MainWindow;
private Ringer m_Ringer;
private JButton m_OKButton;
public RingerScreen(final MainWindow pr_MainWindow) {
super ("Ringer", true, true, true);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
m_Ringer = new Ringer();
m_MainWindow = pr_MainWindow;
createContents();
}
private void createContents() {
setLayout(new GridBagLayout());
GridBagConstraints labels = new GridBagConstraints();
labels.gridx = 0;
labels.anchor = GridBagConstraints.LINE_END;
labels.fill = GridBagConstraints.NONE;
labels.insets = new Insets(5, 10, 10, 5);
GridBagConstraints fields = new GridBagConstraints();
fields.gridx = 1;
fields.anchor = GridBagConstraints.LINE_START;
fields.fill = GridBagConstraints.HORIZONTAL;
fields.insets = new Insets(0, 0, 0, 10);
fields.weightx = 1.0;
labels.gridy = 0;
add(new JLabel(Database.camelToWords(Ringer.DB_COLUMN_FORENAME)), labels);
fields.gridy = 0;
final JTextField forenameTextField = new JTextField();
forenameTextField.setText(m_Ringer.getForename());
forenameTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent pr_Event) {
m_Ringer.setForename(forenameTextField.getText());
}
});
forenameTextField.setInputVerifier(new NotEmptyValidator(this, forenameTextField, "You must specify a forename."));
add(forenameTextField, fields);
labels.gridy = 1;
add(new JLabel(Database.camelToWords(Ringer.DB_COLUMN_MIDDLE_INITIALS)), labels);
fields.gridy = 1;
final JTextField middleInitialsTextField = new JTextField();
middleInitialsTextField.setText(m_Ringer.getMiddleInitials());
middleInitialsTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent pr_Event) {
m_Ringer.setMiddleInitials(middleInitialsTextField.getText());
}
});
add(middleInitialsTextField, fields);
labels.gridy = 2;
add(new JLabel(Database.camelToWords(Ringer.DB_COLUMN_SURNAME)), labels);
fields.gridy = 2;
final JTextField surnameTextField = new JTextField();
surnameTextField.setText(m_Ringer.getSurname());
surnameTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent pr_Event) {
m_Ringer.setSurname(surnameTextField.getText());
}
});
surnameTextField.setInputVerifier(new NotEmptyValidator(this, surnameTextField, "You must specify a forename."));
add(surnameTextField, fields);
JPanel buttonPanel = new JPanel();
JButton cancelButton = new JButton("Cancel");
final JInternalFrame thisObj = this;
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent pr_Event) {
try {
thisObj.setClosed(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
m_MainWindow.removeFrame(thisObj);
}
});
buttonPanel.add(cancelButton);
m_OKButton = new JButton("OK");
m_OKButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent pr_Event) {
try {
m_Ringer.commit();
} catch (SQLException e) {
e.printStackTrace();
}
try {
thisObj.setClosed(true);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
m_MainWindow.removeFrame(thisObj);
}
});
buttonPanel.add(m_OKButton);
labels.gridx = 0;
labels.gridy = 3;
labels.gridwidth = 2;
labels.anchor = GridBagConstraints.LINE_END;
add(buttonPanel, labels);
}
@Override
public void validationFailed() {
m_OKButton.setEnabled(false);
}
@Override
public void validationPassed() {
m_OKButton.setEnabled(true);
}
}