/*
* CheckDigitCalculator.java
*
* Created on September 3, 2002, 5:18 PM
*/
import javax.swing.*;
/**
*
* @author alex
*/
public class CheckDigitCalculator extends javax.swing.JFrame {
private JTextField parent = null;
/** Creates new form CheckDigitCalculator */
public CheckDigitCalculator() {
double width = this.getGraphicsConfiguration().getBounds().getWidth();
double height = this.getGraphicsConfiguration().getBounds().getHeight();
this.setLocation(((int)width-300)/2,((int)height-300)/2);
initComponents();
}
public CheckDigitCalculator(JTextField parent){
this();
this.parent = parent;
String all = parent.getText();
if(all.length()==11){
all = all.substring(0,10);
}
inputTextField.setText(all);
}
/** 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.
*/
private void initComponents() {//GEN-BEGIN:initComponents
java.awt.GridBagConstraints gridBagConstraints;
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
inputTextField = new JTextField(new AllCapsLimitDocument(10),null,0);
jLabel3 = new javax.swing.JLabel();
goButton = new javax.swing.JButton();
resultLabel = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
getContentPane().setLayout(new java.awt.GridBagLayout());
setTitle("CheckDigit Calculator");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jLabel1.setText("Container Number CheckDigit");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = 3;
gridBagConstraints.insets = new java.awt.Insets(15, 15, 1, 15);
getContentPane().add(jLabel1, gridBagConstraints);
jLabel2.setText("Input 10 Characters:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(15, 10, 15, 10);
getContentPane().add(jLabel2, gridBagConstraints);
inputTextField.setFont(new java.awt.Font("Courier New", 0, 12));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.ipadx = 100;
gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
getContentPane().add(inputTextField, gridBagConstraints);
jLabel3.setText("Result is:");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(15, 10, 15, 10);
getContentPane().add(jLabel3, gridBagConstraints);
goButton.setText("CRC?");
goButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
goButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 2;
getContentPane().add(goButton, gridBagConstraints);
resultLabel.setFont(new java.awt.Font("Courier New", 1, 12));
resultLabel.setText("-----------");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10);
getContentPane().add(resultLabel, gridBagConstraints);
jLabel4.setFont(new java.awt.Font("Dialog", 2, 12));
jLabel4.setText("(ISO 6346)");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.gridwidth = 3;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 0);
getContentPane().add(jLabel4, gridBagConstraints);
pack();
}//GEN-END:initComponents
private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goButtonActionPerformed
// Add your handling code here:
if(inputTextField.getText()==null){
return;
}
if(inputTextField.getText().length()!=10){
JOptionPane.showMessageDialog(this,"Input must be 10 characters",
"ERROR",JOptionPane.ERROR_MESSAGE);
resultLabel.setText("-----------");
return;
}
int crc = ContCheckDigit.getCRC(inputTextField.getText());
if(crc == -1){
JOptionPane.showMessageDialog(this,"Please use this Format: ABCD123456",
"ERROR",JOptionPane.ERROR_MESSAGE);
resultLabel.setText("-----------");
return;
}
if(crc == 10){
JOptionPane.showMessageDialog(this,"It is recomended not to use this number because the CRC is 10 ",
"ERROR",JOptionPane.INFORMATION_MESSAGE);
crc=0;
}
resultLabel.setText(inputTextField.getText() + crc);
}//GEN-LAST:event_goButtonActionPerformed
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
if(parent != null){ // if attached to a parent text field
parent.setText(resultLabel.getText());
this.hide();
}
else{
System.exit(0); // if run as standalone program
}
}//GEN-LAST:event_exitForm
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new CheckDigitCalculator().show();
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton goButton;
private javax.swing.JTextField inputTextField;
private javax.swing.JLabel resultLabel;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel1;
// End of variables declaration//GEN-END:variables
}