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 org.smart.migrate.ui; import java.awt.event.ItemEvent; import java.sql.Connection; import java.util.List; import javax.swing.JComboBox; import org.apache.commons.lang3.StringUtils; import org.smart.migrate.dao.MetaDao; import org.smart.migrate.dao.MetaDaoFactory; import org.smart.migrate.model.Field; import org.smart.migrate.setting.MigratePlan; import org.smart.migrate.setting.TableRelation; import org.smart.migrate.util.ConnectionUtils; /** * * @author Sandy Duan */ public class RelationDialog extends javax.swing.JDialog { private TableRelation tableRelation = new TableRelation(); private MigratePlan migratePlan; private MetaDao metaDao; private Connection connection; private int modalResult; /** * Creates new form RelationDialog * @param parent * @param modal */ public RelationDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); } //? private void initTables(JComboBox comboBox) { comboBox.removeAllItems(); List<String> tables = migratePlan.getSourceTableNames(); for (String table : tables) { comboBox.addItem(table); } } //? private void initFields(JComboBox comboBox, String table) { comboBox.removeAllItems(); List<Field> fields = metaDao.getFieldsOfTable(connection, table); for (Field field : fields) { comboBox.addItem(field.getName()); } } /** * 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() { jComboBox4 = new javax.swing.JComboBox(); jLabel11 = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); cbxFKTable = new javax.swing.JComboBox(); jLabel2 = new javax.swing.JLabel(); cbxFK = new javax.swing.JComboBox(); cbxPKTable = new javax.swing.JComboBox(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); btnCancel = new javax.swing.JButton(); btnOK = new javax.swing.JButton(); cbxPK = new javax.swing.JComboBox(); jLabel5 = new javax.swing.JLabel(); jComboBox4.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jComboBox4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jComboBox4ActionPerformed(evt); } }); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); jLabel11.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/smart/migrate/ui/Bundle"); // NOI18N jLabel11.setText(bundle.getString("RelationDialog.jLabel11.text")); // NOI18N jLabel1.setText(bundle.getString("RelationDialog.jLabel1.text")); // NOI18N cbxFKTable.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); cbxFKTable.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { cbxFKTableItemStateChanged(evt); } }); jLabel2.setText(bundle.getString("RelationDialog.jLabel2.text")); // NOI18N cbxFK.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); cbxPKTable.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); cbxPKTable.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { cbxPKTableItemStateChanged(evt); } }); jLabel3.setText(bundle.getString("RelationDialog.jLabel3.text")); // NOI18N jLabel4.setText(bundle.getString("RelationDialog.jLabel4.text")); // NOI18N btnCancel.setText(bundle.getString("RelationDialog.btnCancel.text")); // NOI18N btnCancel.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelActionPerformed(evt); } }); btnOK.setText(bundle.getString("RelationDialog.btnOK.text")); // NOI18N btnOK.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnOKActionPerformed(evt); } }); cbxPK.setModel( new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" })); jLabel5.setForeground(new java.awt.Color(153, 153, 153)); jLabel5.setText(bundle.getString("RelationDialog.jLabel5.text")); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createSequentialGroup().addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING, false) .addGroup(layout.createSequentialGroup().addGap(126, 126, 126) .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout .createSequentialGroup().addContainerGap() .addComponent(jLabel11) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel5)) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup().addGap(81, 81, 81) .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel4).addComponent( jLabel1) .addComponent(jLabel3).addComponent( jLabel2)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(cbxPK, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(cbxFKTable, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( cbxPKTable, javax.swing.GroupLayout.Alignment.TRAILING, 0, 281, Short.MAX_VALUE) .addComponent(cbxFK, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))) .addGap(0, 83, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel5)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(cbxFKTable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(cbxFK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(26, 26, 26) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(cbxPKTable, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(cbxPK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnCancel).addComponent(btnOK)) .addContainerGap(20, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jComboBox4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox4ActionPerformed }//GEN-LAST:event_jComboBox4ActionPerformed private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed this.modalResult = 1; this.dispose(); }//GEN-LAST:event_btnCancelActionPerformed private void cbxFKTableItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxFKTableItemStateChanged if (evt.getStateChange() == ItemEvent.SELECTED) { initFields(cbxFK, (String) evt.getItem()); if (StringUtils.isNotBlank(tableRelation.getForeignKey())) { cbxFK.setSelectedItem(tableRelation.getForeignKey()); } } }//GEN-LAST:event_cbxFKTableItemStateChanged private void cbxPKTableItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxPKTableItemStateChanged if (evt.getStateChange() == ItemEvent.SELECTED) { initFields(cbxPK, (String) evt.getItem()); if (StringUtils.isNotBlank(tableRelation.getPrimaryKey())) { cbxPK.setSelectedItem(tableRelation.getPrimaryKey()); } } }//GEN-LAST:event_cbxPKTableItemStateChanged private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed // TODO add your handling code here: if (StringUtils.isBlank((String) cbxFKTable.getSelectedItem()) || StringUtils.isBlank((String) cbxPKTable.getSelectedItem()) || StringUtils.isBlank((String) cbxFK.getSelectedItem()) || StringUtils.isBlank((String) cbxPK.getSelectedItem())) { this.modalResult = 1; } else { this.modalResult = 0; } this.dispose(); }//GEN-LAST:event_btnOKActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* 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(RelationDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(RelationDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(RelationDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(RelationDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the dialog */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { RelationDialog dialog = new RelationDialog(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.JButton btnCancel; private javax.swing.JButton btnOK; private javax.swing.JComboBox cbxFK; private javax.swing.JComboBox cbxFKTable; private javax.swing.JComboBox cbxPK; private javax.swing.JComboBox cbxPKTable; private javax.swing.JComboBox jComboBox4; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; // End of variables declaration//GEN-END:variables /** * @return the tableRelation */ public TableRelation getTableRelation() { tableRelation.setForeignKey((String) cbxFK.getSelectedItem()); tableRelation.setForeignTable((String) cbxFKTable.getSelectedItem()); tableRelation.setPrimaryKey((String) cbxPK.getSelectedItem()); tableRelation.setPrimaryTable((String) cbxPKTable.getSelectedItem()); return tableRelation; } /** * @param tableRelation the tableRelation to set */ public void setTableRelation(TableRelation tableRelation) { this.tableRelation = tableRelation; cbxFKTable.setSelectedItem(tableRelation.getForeignTable()); cbxFK.setSelectedItem(tableRelation.getForeignKey()); cbxPKTable.setSelectedItem(tableRelation.getPrimaryTable()); cbxPK.setSelectedItem(tableRelation.getPrimaryKey()); } /** * @return the migratePlan */ public MigratePlan getMigratePlan() { return migratePlan; } /** * @param migratePlan the migratePlan to set */ public void setMigratePlan(MigratePlan migratePlan) { this.migratePlan = migratePlan; metaDao = MetaDaoFactory.createMetaDao(migratePlan.getSourceDB().getdBType(), migratePlan.getSourceDB().getDatabase()); connection = ConnectionUtils.connect(migratePlan.getSourceDB()); initTables(cbxFKTable); initTables(cbxPKTable); } /** * @return the modalResult */ public int getModalResult() { return modalResult; } }