import javax.swing.JOptionPane; /** * EntryDialog is used to view, add or update entry details. * @author Lars Appel * @version 0.2 */ public class EntryDialog extends javax.swing.JDialog { /** Initializes the Form */ public EntryDialog(java.awt.Frame parent, boolean modal) { super (parent, modal); initComponents (); pack (); } /** * The dataset this dialog is to interact with. */ MasterSet myMaster; /** * In add mode we make the keyValue read only. */ boolean addMode; /** * In modify mode we need the row for updating the list view. */ int myRow; /** * This customized contructor is used by the application. */ public EntryDialog( java.awt.Frame parent, boolean modal, MasterSet master, boolean addMode ) { super (parent, modal); initComponents (); myMaster = master; this.addMode = addMode; initGridComponents(); pack (); setLocationRelativeTo(parent); } // keep track of the field name/value pairs of an entry javax.swing.JLabel[] gridLabels; javax.swing.JTextField[] gridFields; /** * Builds the entry labels and input fields dynamically. */ private void initGridComponents() { String[] fields = myMaster.getFieldNames(); int[] widths = myMaster.getFieldWidths(); java.awt.GridBagConstraints myConstraints = new java.awt.GridBagConstraints (); gridLabels = new javax.swing.JLabel[fields.length]; gridFields = new javax.swing.JTextField[fields.length]; for (int k=0; k < fields.length; k++) { // add the label gridLabels[k] = new javax.swing.JLabel(fields[k]); myConstraints.gridwidth = 1; myConstraints.anchor = java.awt.GridBagConstraints.CENTER; gridPanel.add (gridLabels[k], myConstraints); // add the field gridFields[k] = new javax.swing.JTextField(widths[k]); myConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; myConstraints.anchor = java.awt.GridBagConstraints.WEST; gridPanel.add (gridFields[k], myConstraints); } if (!addMode) gridFields[0].setEditable(false); } /** * Retrieve an entry from the dataset and populate the GUI fields. */ void fetchData(int row, String keyValue) { myRow = row; // remember in case of update String[] tuple = myMaster.fetchData(keyValue); for (int k=0; k < gridFields.length; k++) { gridFields[k].setText(tuple[k]); } } /** * Add an entry based on the GUI field contents, show error msg if any. */ void addEntry() { String[] tuple = new String[gridFields.length]; for (int k=0; k < tuple.length; k++) tuple[k] = gridFields[k].getText(); String msg = myMaster.addEntry(tuple); if (msg != null) JOptionPane.showMessageDialog(null, msg); } /** * Update dataset entry based on the GUI fields or show error msg. */ void updateEntry() { String[] tuple = new String[gridFields.length]; for (int k=0; k < tuple.length; k++) tuple[k] = gridFields[k].getText(); String msg = myMaster.updateEntry(myRow, tuple); if (msg != null) JOptionPane.showMessageDialog(null, msg); } /** 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 FormEditor. */ private void initComponents () {//GEN-BEGIN:initComponents setTitle ("Entry Details"); addWindowListener (new java.awt.event.WindowAdapter () { public void windowClosing (java.awt.event.WindowEvent evt) { closeDialog (evt); } } ); getContentPane ().setLayout (new java.awt.BorderLayout ()); bottomPanel = new javax.swing.JPanel (); bottomPanel.setLayout (new java.awt.FlowLayout ()); saveButton = new javax.swing.JButton (); saveButton.setText ("Save"); saveButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { saveButtonActionPerformed (evt); } } ); bottomPanel.add (saveButton); closeButton = new javax.swing.JButton (); closeButton.setText ("Close"); closeButton.addActionListener (new java.awt.event.ActionListener () { public void actionPerformed (java.awt.event.ActionEvent evt) { closeButtonActionPerformed (evt); } } ); bottomPanel.add (closeButton); getContentPane ().add (bottomPanel, "South"); gridPanel = new javax.swing.JPanel (); gridPanel.setLayout (new java.awt.GridBagLayout ()); java.awt.GridBagConstraints gridBagConstraints1; getContentPane ().add (gridPanel, "Center"); }//GEN-END:initComponents private void closeButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed // Add your handling code here: setVisible (false); // dispose (); -- maybe we can reuse the dialog (eg screen position) }//GEN-LAST:event_closeButtonActionPerformed private void saveButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed // Add your handling code here: if (addMode) addEntry(); else updateEntry(); setVisible (false); // dispose (); -- maybe we can reuse the dialog (eg screen position) }//GEN-LAST:event_saveButtonActionPerformed /** Closes the dialog */ private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog setVisible (false); // dispose (); -- maybe we can reuse the dialog (eg screen position) }//GEN-LAST:event_closeDialog // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPanel bottomPanel; private javax.swing.JPanel gridPanel; private javax.swing.JButton saveButton; private javax.swing.JButton closeButton; // End of variables declaration//GEN-END:variables public static void main(java.lang.String[] args) { new EntryDialog (new java.awt.Frame (), false).show (); } }