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.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import ringdb.database.Database;
import ringdb.database.Tower;
import ringdb.ui.validation.IFormValidationListener;
import ringdb.ui.validation.NotEmptyValidator;
public class TowerScreen 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 = 300;
private MainWindow m_MainWindow;
private Tower m_Tower;
private JButton m_OKButton;
public TowerScreen(final MainWindow pr_MainWindow) {
this (pr_MainWindow, new Tower());
}
public TowerScreen(final MainWindow pr_MainWindow,
final Tower pr_Tower) {
super ("Tower", true, true, true);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
m_Tower = pr_Tower;
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(Tower.DB_COLUMN_TOWN)), labels);
fields.gridy = 0;
final JTextField townTextField = new JTextField();
townTextField.setText(m_Tower.getTown());
townTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent pr_Event) {
m_Tower.setTown(townTextField.getText());
}
});
townTextField.setInputVerifier(new NotEmptyValidator(this, townTextField, "You must specify a town."));
add(townTextField, fields);
labels.gridy = 1;
add(new JLabel("Area"), labels);
fields.gridy = 1;
final JTextField areaTextField = new JTextField();
areaTextField.setText(m_Tower.getArea());
areaTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent pr_Event) {
m_Tower.setArea(areaTextField.getText());
}
});
add(areaTextField, fields);
labels.gridy = 2;
add(new JLabel(Database.camelToWords(Tower.DB_COLUMN_COUNTY)), labels);
fields.gridy = 2;
final JTextField countyTextField = new JTextField();
countyTextField.setText(m_Tower.getCounty());
countyTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent pr_Event) {
m_Tower.setCounty(countyTextField.getText());
}
});
countyTextField.setInputVerifier(new NotEmptyValidator(this, countyTextField, "You must specify a county."));
add(countyTextField, fields);
labels.gridy = 3;
add(new JLabel(Database.camelToWords(Tower.DB_COLUMN_DEDICATION)), labels);
fields.gridy = 3;
final JTextField dedicationTextField = new JTextField();
dedicationTextField.setText(m_Tower.getDedication());
dedicationTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent pr_Event) {
m_Tower.setDedication(dedicationTextField.getText());
}
});
add(dedicationTextField, fields);
labels.gridy = 4;
add(new JLabel(Database.camelToWords(Tower.DB_COLUMN_BELLS)), labels);
fields.gridy = 4;
fields.fill = GridBagConstraints.NONE;
final JSpinner numberOfBellsSpinner = new JSpinner(new SpinnerNumberModel(m_Tower.getBells(), 2, 16, 1));
numberOfBellsSpinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
m_Tower.setBells(Integer.parseInt(numberOfBellsSpinner.getValue().toString()));
}
});
add(numberOfBellsSpinner, fields);
labels.gridy = 5;
add(new JLabel(Database.camelToWords(Tower.DB_COLUMN_TENOR_WEIGHT)), labels);
fields.gridy = 5;
fields.fill = GridBagConstraints.NONE;
final JTextField tenorWeightTextField = new JTextField();
tenorWeightTextField.setText(m_Tower.getTenorWeight());
tenorWeightTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent pr_Event) {
m_Tower.setTenorWeight(tenorWeightTextField.getText());
}
});
tenorWeightTextField.setColumns(10);
tenorWeightTextField.setInputVerifier(new NotEmptyValidator(this, tenorWeightTextField, "You must specify a tenor weight."));
add(tenorWeightTextField, 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_Tower.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 = 6;
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);
}
}