Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.unicorn.co226.ui.patient; import com.unicorn.co226.controller.StudentController; import com.unicorn.co226.model.Patient; import com.unicorn.co226.model.Student; import com.unicorn.co226.other.IdGen; import com.unicorn.co226.ui.WaitingList; import java.awt.Frame; import java.io.File; import java.io.IOException; import java.sql.SQLException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.MaskFormatter; import org.apache.commons.io.FileUtils; /** * * @author HP */ public class AddStudentForm extends javax.swing.JDialog { private Map<String, MaskFormatter> maskFormatterMap; private DateFormat dateFormat; private JFileChooser fileChooser; private File[] medicalPhotos; private Frame parent; /** * Creates new form AddStudentForm */ public AddStudentForm(java.awt.Frame parent, boolean modal) { super(parent, modal); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { Logger.getLogger(WaitingList.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(WaitingList.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(WaitingList.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(WaitingList.class.getName()).log(Level.SEVERE, null, ex); } this.parent = parent; dateFormat = new SimpleDateFormat("yyyy-MM-dd"); initComponents(); setLocationRelativeTo(parent); medicalPhotos = new File[] {}; maskFormatterMap = new HashMap<>(); try { maskFormatterMap.put("ENG", new MaskFormatter("E-##-###")); MaskFormatter maskFormatter = new MaskFormatter("A-##-###"); maskFormatter.setValidCharacters("A"); maskFormatterMap.put("ART", maskFormatter); maskFormatterMap.put("SCI", new MaskFormatter("S-##-###")); maskFormatterMap.put("MED", new MaskFormatter("M-##-###")); maskFormatterMap.put("AHS", new MaskFormatter("AHS-##-###")); maskFormatterMap.put("MGT", new MaskFormatter("MGT-##-###")); maskFormatterMap.put("VET", new MaskFormatter("V-##-###")); maskFormatterMap.put("AGR", new MaskFormatter("AG-##-###")); maskFormatterMap.put("DEN", new MaskFormatter("D-##-###")); } catch (ParseException ex) { Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex); } //dobDatePicker.setDate(new Date(90, 1, 1)); dobDatePicker.setFormats(dateFormat, new SimpleDateFormat("yyyy"), new SimpleDateFormat("yyyy/MM/dd")); fileChooser = new JFileChooser(); fileChooser.setMultiSelectionEnabled(true); fileChooser.setFileFilter(new FileNameExtensionFilter("Images", "jpg", "png")); try { String nextId = IdGen.getNextId("Patient", "id", "P"); idTextField.setText(nextId); } catch (ClassNotFoundException ex) { Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex); } } /** * 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() { jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); nameText = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); facultyComboBox = new javax.swing.JComboBox(); jLabel4 = new javax.swing.JLabel(); regNoTextField = new javax.swing.JFormattedTextField(); jLabel5 = new javax.swing.JLabel(); dobDatePicker = new org.jdesktop.swingx.JXDatePicker(); jLabel6 = new javax.swing.JLabel(); addressTextField = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); idTextField = new javax.swing.JTextField(); saveButton = new javax.swing.JButton(); cancelButton = new javax.swing.JButton(); jLabel8 = new javax.swing.JLabel(); openButton = new javax.swing.JButton(); medicalTextField = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setResizable(false); jPanel1.setBackground(java.awt.Color.white); jLabel1.setBackground(new java.awt.Color(51, 153, 255)); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N jLabel1.setForeground(new java.awt.Color(255, 255, 255)); jLabel1.setLabelFor(this); jLabel1.setText(" New Student"); jLabel1.setOpaque(true); jLabel2.setLabelFor(nameText); jLabel2.setText("Name"); jLabel3.setLabelFor(facultyComboBox); jLabel3.setText("Faculty"); facultyComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "ENG - Facutly of Enginnering", "AGR - Faculty of Agriculture", "AHS - Faculty of Allied Health Sciences", "ART - Faculty of Arts", "DET - Faculty of Dental Sciences", "MGT - Faculty of Management", "MED - Faculty of Medicine", "SCI - Faculty of Science", "VET - Faculty of Veterinary Medicine & Animal Science" })); facultyComboBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { facultyComboBoxActionPerformed(evt); } }); jLabel4.setText("Reg. No"); try { regNoTextField.setFormatterFactory( new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("E-##-###"))); } catch (java.text.ParseException ex) { ex.printStackTrace(); } jLabel5.setText("Address"); jLabel6.setText("Date of Birth"); jLabel7.setText("ID"); idTextField.setEditable(false); idTextField.setBorder(null); idTextField.setSelectionColor(new java.awt.Color(255, 102, 51)); saveButton.setText("Save"); saveButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveButtonActionPerformed(evt); } }); cancelButton.setText("Cancel"); cancelButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButtonActionPerformed(evt); } }); jLabel8.setText("Medical"); openButton.setText("Open"); openButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { openButtonActionPerformed(evt); } }); medicalTextField.setEditable(false); medicalTextField.setText("No files selected"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(28, 28, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(24, 24, 24).addComponent(cancelButton)) .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2).addComponent(jLabel3) .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel7)).addGap(21, 21, 21) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(nameText, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(dobDatePicker, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(regNoTextField, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(facultyComboBox, javax.swing.GroupLayout.Alignment.LEADING, 0, 234, Short.MAX_VALUE)) .addComponent(idTextField)) .addComponent(addressTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout .createSequentialGroup().addComponent(medicalTextField) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(openButton))))) .addContainerGap(31, Short.MAX_VALUE)) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel7).addComponent(idTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(nameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(facultyComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(regNoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(addressTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(dobDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(openButton) .addComponent(medicalTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(saveButton).addComponent(cancelButton)) .addContainerGap())); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); pack(); }// </editor-fold>//GEN-END:initComponents private void facultyComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_facultyComboBoxActionPerformed String selectedItem = (String) facultyComboBox.getSelectedItem(); String key = selectedItem.split(" - ")[0]; regNoTextField.setFormatterFactory(new DefaultFormatterFactory(maskFormatterMap.get(key))); // System.out.println(key); }//GEN-LAST:event_facultyComboBoxActionPerformed private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed String date = dateFormat.format(dobDatePicker.getDate()); Patient patient = new Patient(idTextField.getText(), nameText.getText(), addressTextField.getText(), date); String fac = (String) facultyComboBox.getSelectedItem(); Student student = new Student(patient, regNoTextField.getText(), fac.substring(0, 3), medicalPhotos.length); try { int res = StudentController.addStudent(student); if (res > 0) { JOptionPane.showMessageDialog(this, "Student Saved..."); dispose(); new AddStudentForm(parent, true).setVisible(true); } } catch (SQLException ex) { Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_saveButtonActionPerformed private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed dispose(); }//GEN-LAST:event_cancelButtonActionPerformed private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed int res = fileChooser.showOpenDialog(this); if (res == JFileChooser.APPROVE_OPTION) { medicalPhotos = fileChooser.getSelectedFiles(); String regNo = regNoTextField.getText(); regNo = regNo.replaceAll("-", "_"); medicalTextField.setText(medicalPhotos.length + " photo(s) selected."); for (int i = 0; i < medicalPhotos.length; i++) { File medicalPhoto = medicalPhotos[i]; File dest = new File("./Medicals/" + regNo + "_" + i + ".jpg"); try { // dest.createNewFile(); FileUtils.copyFile(medicalPhoto, dest); } catch (IOException ex) { Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex); } } } }//GEN-LAST:event_openButtonActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { try { /* 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(AddStudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } catch (InstantiationException ex) { // java.util.logging.Logger.getLogger(AddStudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } catch (IllegalAccessException ex) { // java.util.logging.Logger.getLogger(AddStudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } catch (javax.swing.UnsupportedLookAndFeelException ex) { // java.util.logging.Logger.getLogger(AddStudentForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); // } // //</editor-fold> UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(AddStudentForm.class.getName()).log(Level.SEVERE, null, ex); } /* Create and display the dialog */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { AddStudentForm dialog = new AddStudentForm(new javax.swing.JFrame(), true); 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.JTextField addressTextField; private javax.swing.JButton cancelButton; private org.jdesktop.swingx.JXDatePicker dobDatePicker; private javax.swing.JComboBox facultyComboBox; private javax.swing.JTextField idTextField; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JPanel jPanel1; private javax.swing.JTextField medicalTextField; private javax.swing.JTextField nameText; private javax.swing.JButton openButton; private javax.swing.JFormattedTextField regNoTextField; private javax.swing.JButton saveButton; // End of variables declaration//GEN-END:variables }