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 contactsdirectory.frontend; import contactsdirectory.backend.Contact; import contactsdirectory.backend.ContactManager; import contactsdirectory.backend.ContactManagerImpl; import static contactsdirectory.backend.ContactType.MAIL; import static contactsdirectory.backend.ContactType.PHONE; import contactsdirectory.backend.DirectoryManager; import contactsdirectory.backend.DirectoryManagerImpl; import contactsdirectory.backend.MailContact; import contactsdirectory.backend.Person; import contactsdirectory.backend.PersonManager; import contactsdirectory.backend.PersonManagerImpl; import contactsdirectory.backend.PhoneContact; import java.awt.Color; import java.awt.Component; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; //import java.awt.Color; import java.util.ResourceBundle; import java.util.concurrent.ExecutionException; import java.util.logging.Level; import java.util.logging.Logger; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import javax.swing.DefaultListModel; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPopupMenu; import javax.swing.ListCellRenderer; import javax.swing.ListModel; import javax.swing.SwingWorker; import org.apache.commons.dbcp.BasicDataSource; /** * * @author Martin */ public class MainJFrame extends javax.swing.JFrame { private static final ContactManager contactManager = new ContactManagerImpl(); private static final PersonManager personManager = new PersonManagerImpl(); private static final DirectoryManager directoryManager = new DirectoryManagerImpl(); private final ResourceBundle localizedTexts; private static DataSource dataSource; private void initDataSource() { try (InputStream is = this.getClass().getResourceAsStream("resources/DBConnection.properties"))//new FileInputStream("../contactsdirectory/frontend/resources/DBConnection.properties")) { Properties props = new Properties(); props.load(is); dataSource = new BasicDataSource(); ((BasicDataSource) dataSource).setUrl(props.getProperty("DB_URL")); ((BasicDataSource) dataSource).setUsername(props.getProperty("DB_USERNAME")); ((BasicDataSource) dataSource).setPassword(props.getProperty("DB_PASSWORD")); } catch (FileNotFoundException ex) { Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex); } } private void asignDataSource() { contactManager.setDataSource(dataSource); personManager.setDataSource(dataSource); directoryManager.setDataSource(dataSource); } /** * Creates new form MainJFrame */ public MainJFrame() { localizedTexts = ResourceBundle.getBundle("contactsdirectory.frontend.resources.mainForm"); initDataSource(); asignDataSource(); initComponents(); setPersonsToListModel(); jProgressBar.setVisible(false); } /** * 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() { jProgressBar = new javax.swing.JProgressBar(); jPanel1 = new javax.swing.JPanel(); jSplitPane2 = new javax.swing.JSplitPane(); jSplitPane1 = new javax.swing.JSplitPane(); jScrollPane1 = new javax.swing.JScrollPane(); jListPerson = new javax.swing.JList(); jScrollPane2 = new javax.swing.JScrollPane(); jListContact = new javax.swing.JList(); jPanelDetail = new javax.swing.JPanel(); jLabelTypeLabel = new javax.swing.JLabel(); jLabelType = new javax.swing.JLabel(); jLabelDataLabel = new javax.swing.JLabel(); jLabelData = new javax.swing.JLabel(); jLabelNoteLabelContact = new javax.swing.JLabel(); jLabelNoteContact = new javax.swing.JLabel(); jLabelSurnameLabel = new javax.swing.JLabel(); jLabelSurname = new javax.swing.JLabel(); jLabelNameLabel = new javax.swing.JLabel(); jLabelName = new javax.swing.JLabel(); jLabelPersonNote = new javax.swing.JLabel(); jLabelNotePerson = new javax.swing.JLabel(); jToolBar2 = new javax.swing.JToolBar(); jButtonAddPerson = new javax.swing.JButton(); jButtonEditPerson = new javax.swing.JButton(); jButtonRemovePerson = new javax.swing.JButton(); jSeparator3 = new javax.swing.JToolBar.Separator(); jButtonAddContact = new javax.swing.JButton(); jButtonEditContact = new javax.swing.JButton(); jButtonRemoveContact = new javax.swing.JButton(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItemAddPerson = new javax.swing.JMenuItem(); jMenuItemEditPerson = new javax.swing.JMenuItem(); jMenuItemRemovePerson = new javax.swing.JMenuItem(); jSeparator1 = new javax.swing.JPopupMenu.Separator(); jMenuItemAddContact = new javax.swing.JMenuItem(); jMenuItemEditContact = new javax.swing.JMenuItem(); jMenuItemRemoveContact = new javax.swing.JMenuItem(); jSeparator2 = new javax.swing.JPopupMenu.Separator(); jMenuItemExit = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Contacts Directory"); setLocationByPlatform(true); setMinimumSize(new java.awt.Dimension(815, 570)); jPanel1.setLayout(new java.awt.BorderLayout()); jSplitPane2.setResizeWeight(0.5); jSplitPane1.setResizeWeight(0.5); jSplitPane1.setMinimumSize(new java.awt.Dimension(420, 409)); jSplitPane1.setPreferredSize(new java.awt.Dimension(476, 500)); jScrollPane1.setMinimumSize(new java.awt.Dimension(124, 178)); jListPerson.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N jListPerson.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jListPerson.setCellRenderer(new PersonCellRenderer()); jListPerson.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { jListPersonValueChanged(evt); } }); jScrollPane1.setViewportView(jListPerson); jSplitPane1.setLeftComponent(jScrollPane1); jScrollPane2.setMinimumSize(new java.awt.Dimension(124, 178)); jListContact.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N jListContact.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); jListContact.setCellRenderer(new ContactCellRenderer()); jListContact.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { jListContactValueChanged(evt); } }); jScrollPane2.setViewportView(jListContact); jSplitPane1.setRightComponent(jScrollPane2); jSplitPane2.setLeftComponent(jSplitPane1); jPanelDetail.setMinimumSize(new java.awt.Dimension(300, 202)); jPanelDetail.setPreferredSize(new java.awt.Dimension(300, 202)); jLabelTypeLabel.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelTypeLabel.setText(localizedTexts.getString("type") + ":"); jLabelType.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelType.setText("-"); jLabelDataLabel.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelDataLabel.setText(localizedTexts.getString("mailData") + ":"); jLabelData.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelData.setText("-"); jLabelNoteLabelContact.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelNoteLabelContact.setText(localizedTexts.getString("note") + ":"); jLabelNoteContact.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelNoteContact.setText("<html>-</html>"); jLabelSurnameLabel.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelSurnameLabel.setText(localizedTexts.getString("surname") + ":"); jLabelSurname.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelSurname.setText("-"); jLabelNameLabel.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelNameLabel.setText(localizedTexts.getString("name") + ":"); jLabelName.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelName.setText("-"); jLabelPersonNote.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelPersonNote.setText(localizedTexts.getString("note") + ":"); jLabelNotePerson.setFont(new java.awt.Font("Calibri", 0, 16)); // NOI18N jLabelNotePerson.setText("<html>-</html>"); javax.swing.GroupLayout jPanelDetailLayout = new javax.swing.GroupLayout(jPanelDetail); jPanelDetail.setLayout(jPanelDetailLayout); jPanelDetailLayout.setHorizontalGroup(jPanelDetailLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelDetailLayout.createSequentialGroup().addContainerGap() .addGroup(jPanelDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelSurnameLabel).addComponent(jLabelNameLabel) .addComponent(jLabelPersonNote).addComponent(jLabelTypeLabel) .addComponent(jLabelDataLabel).addComponent(jLabelNoteLabelContact)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup( jPanelDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabelSurname, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE) .addComponent(jLabelName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabelNotePerson) .addComponent(jLabelType, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabelData, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabelNoteContact)) .addContainerGap())); jPanelDetailLayout.setVerticalGroup(jPanelDetailLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanelDetailLayout.createSequentialGroup().addContainerGap() .addGroup(jPanelDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabelSurnameLabel).addComponent(jLabelSurname)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanelDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabelNameLabel).addComponent(jLabelName)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanelDetailLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabelPersonNote).addComponent(jLabelNotePerson)) .addGap(44, 44, 44) .addGroup(jPanelDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabelTypeLabel).addComponent(jLabelType)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanelDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabelDataLabel).addComponent(jLabelData)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanelDetailLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabelNoteLabelContact).addComponent(jLabelNoteContact)) .addGap(257, 257, 257))); jSplitPane2.setRightComponent(jPanelDetail); jPanel1.add(jSplitPane2, java.awt.BorderLayout.CENTER); jToolBar2.setRollover(true); jButtonAddPerson.setIcon(new javax.swing.ImageIcon( getClass().getResource("/contactsdirectory/frontend/resources/images/new.gif"))); // NOI18N jButtonAddPerson.setToolTipText(localizedTexts.getString("addPerson")); jButtonAddPerson.setFocusable(false); jButtonAddPerson.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButtonAddPerson.setName("addContact"); // NOI18N jButtonAddPerson.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButtonAddPerson.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonAddPersonActionPerformed(evt); } }); jToolBar2.add(jButtonAddPerson); jButtonEditPerson.setIcon(new javax.swing.ImageIcon( getClass().getResource("/contactsdirectory/frontend/resources/images/edit.gif"))); // NOI18N jButtonEditPerson.setToolTipText(localizedTexts.getString("editPerson")); jButtonEditPerson.setFocusable(false); jButtonEditPerson.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButtonEditPerson.setName("editContact"); // NOI18N jButtonEditPerson.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButtonEditPerson.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonEditPersonActionPerformed(evt); } }); jToolBar2.add(jButtonEditPerson); jButtonRemovePerson.setIcon(new javax.swing.ImageIcon( getClass().getResource("/contactsdirectory/frontend/resources/images/delete.gif"))); // NOI18N jButtonRemovePerson.setToolTipText(localizedTexts.getString("removePerson")); jButtonRemovePerson.setFocusable(false); jButtonRemovePerson.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButtonRemovePerson.setName("removeContact"); // NOI18N jButtonRemovePerson.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButtonRemovePerson.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonRemovePersonActionPerformed(evt); } }); jToolBar2.add(jButtonRemovePerson); jToolBar2.add(jSeparator3); jButtonAddContact.setIcon(new javax.swing.ImageIcon( getClass().getResource("/contactsdirectory/frontend/resources/images/new.gif"))); // NOI18N jButtonAddContact.setToolTipText(localizedTexts.getString("addContact")); jButtonAddContact.setFocusable(false); jButtonAddContact.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButtonAddContact.setName("addContact"); // NOI18N jButtonAddContact.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButtonAddContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonAddContactActionPerformed(evt); } }); jToolBar2.add(jButtonAddContact); jButtonEditContact.setIcon(new javax.swing.ImageIcon( getClass().getResource("/contactsdirectory/frontend/resources/images/edit.gif"))); // NOI18N jButtonEditContact.setToolTipText(localizedTexts.getString("editContact")); jButtonEditContact.setFocusable(false); jButtonEditContact.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButtonEditContact.setName("editContact"); // NOI18N jButtonEditContact.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButtonEditContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonEditContactActionPerformed(evt); } }); jToolBar2.add(jButtonEditContact); jButtonRemoveContact.setIcon(new javax.swing.ImageIcon( getClass().getResource("/contactsdirectory/frontend/resources/images/delete.gif"))); // NOI18N jButtonRemoveContact.setToolTipText(localizedTexts.getString("removeContact")); jButtonRemoveContact.setFocusable(false); jButtonRemoveContact.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButtonRemoveContact.setName("removeContact"); // NOI18N jButtonRemoveContact.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButtonRemoveContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButtonRemoveContactActionPerformed(evt); } }); jToolBar2.add(jButtonRemoveContact); jPanel1.add(jToolBar2, java.awt.BorderLayout.NORTH); jMenu1.setText(localizedTexts.getString("file")); jMenuItemAddPerson.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.SHIFT_MASK)); jMenuItemAddPerson.setText(localizedTexts.getString("addPerson")); jMenuItemAddPerson.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemAddPersonActionPerformed(evt); } }); jMenu1.add(jMenuItemAddPerson); jMenuItemEditPerson.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, java.awt.event.InputEvent.SHIFT_MASK)); jMenuItemEditPerson.setText(localizedTexts.getString("editPerson")); jMenuItemEditPerson.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemEditPersonActionPerformed(evt); } }); jMenu1.add(jMenuItemEditPerson); jMenuItemRemovePerson.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DELETE, java.awt.event.InputEvent.SHIFT_MASK)); jMenuItemRemovePerson.setText(localizedTexts.getString("removePerson")); jMenuItemRemovePerson.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemRemovePersonActionPerformed(evt); } }); jMenu1.add(jMenuItemRemovePerson); jMenu1.add(jSeparator1); jMenuItemAddContact.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK)); jMenuItemAddContact.setText(localizedTexts.getString("addContact")); jMenuItemAddContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemAddContactActionPerformed(evt); } }); jMenu1.add(jMenuItemAddContact); jMenuItemEditContact.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0)); jMenuItemEditContact.setText(localizedTexts.getString("editContact")); jMenuItemEditContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemEditContactActionPerformed(evt); } }); jMenu1.add(jMenuItemEditContact); jMenuItemRemoveContact .setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DELETE, 0)); jMenuItemRemoveContact.setText(localizedTexts.getString("removeContact")); jMenuItemRemoveContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemRemoveContactActionPerformed(evt); } }); jMenu1.add(jMenuItemRemoveContact); jMenu1.add(jSeparator2); jMenuItemExit.setText(localizedTexts.getString("exit")); jMenuItemExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItemExitActionPerformed(evt); } }); jMenu1.add(jMenuItemExit); jMenuBar1.add(jMenu1); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 704, Short.MAX_VALUE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap(565, Short.MAX_VALUE).addComponent( jProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE) .addGap(19, 19, 19)))); pack(); setLocationRelativeTo(null); }// </editor-fold>//GEN-END:initComponents private void jButtonAddContactActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonAddContactActionPerformed {//GEN-HEADEREND:event_jButtonAddContactActionPerformed addContact(); }//GEN-LAST:event_jButtonAddContactActionPerformed private void jMenuItemEditPersonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItemEditPersonActionPerformed {//GEN-HEADEREND:event_jMenuItemEditPersonActionPerformed editPerson(); }//GEN-LAST:event_jMenuItemEditPersonActionPerformed private void jMenuItemAddContactActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItemAddContactActionPerformed {//GEN-HEADEREND:event_jMenuItemAddContactActionPerformed addContact(); }//GEN-LAST:event_jMenuItemAddContactActionPerformed private void jMenuItemRemoveContactActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItemRemoveContactActionPerformed {//GEN-HEADEREND:event_jMenuItemRemoveContactActionPerformed removeContact(); }//GEN-LAST:event_jMenuItemRemoveContactActionPerformed private void jMenuItemEditContactActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItemEditContactActionPerformed {//GEN-HEADEREND:event_jMenuItemEditContactActionPerformed editContact(); }//GEN-LAST:event_jMenuItemEditContactActionPerformed private void jButtonEditContactActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonEditContactActionPerformed {//GEN-HEADEREND:event_jButtonEditContactActionPerformed editContact(); }//GEN-LAST:event_jButtonEditContactActionPerformed private void jButtonRemoveContactActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonRemoveContactActionPerformed {//GEN-HEADEREND:event_jButtonRemoveContactActionPerformed removeContact(); }//GEN-LAST:event_jButtonRemoveContactActionPerformed private void jMenuItemExitActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItemExitActionPerformed {//GEN-HEADEREND:event_jMenuItemExitActionPerformed System.exit(0); }//GEN-LAST:event_jMenuItemExitActionPerformed private void jButtonEditPersonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonEditPersonActionPerformed {//GEN-HEADEREND:event_jButtonEditPersonActionPerformed editPerson(); }//GEN-LAST:event_jButtonEditPersonActionPerformed private void jButtonRemovePersonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonRemovePersonActionPerformed {//GEN-HEADEREND:event_jButtonRemovePersonActionPerformed removePerson(); }//GEN-LAST:event_jButtonRemovePersonActionPerformed private void jButtonAddPersonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonAddPersonActionPerformed {//GEN-HEADEREND:event_jButtonAddPersonActionPerformed addPerson(); }//GEN-LAST:event_jButtonAddPersonActionPerformed private void jMenuItemAddPersonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItemAddPersonActionPerformed {//GEN-HEADEREND:event_jMenuItemAddPersonActionPerformed addPerson(); }//GEN-LAST:event_jMenuItemAddPersonActionPerformed private void jMenuItemRemovePersonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItemRemovePersonActionPerformed {//GEN-HEADEREND:event_jMenuItemRemovePersonActionPerformed removePerson(); }//GEN-LAST:event_jMenuItemRemovePersonActionPerformed private void jListContactValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_jListContactValueChanged {//GEN-HEADEREND:event_jListContactValueChanged if (!evt.getValueIsAdjusting()) { updateContactDetail(); } }//GEN-LAST:event_jListContactValueChanged private void jListPersonValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_jListPersonValueChanged {//GEN-HEADEREND:event_jListPersonValueChanged Person person = getSelectedPerson(); setContactsToListModel(person); updatePersonDetail(); }//GEN-LAST:event_jListPersonValueChanged /** * @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(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new MainJFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButtonAddContact; private javax.swing.JButton jButtonAddPerson; private javax.swing.JButton jButtonEditContact; private javax.swing.JButton jButtonEditPerson; private javax.swing.JButton jButtonRemoveContact; private javax.swing.JButton jButtonRemovePerson; private javax.swing.JLabel jLabelData; private javax.swing.JLabel jLabelDataLabel; private javax.swing.JLabel jLabelName; private javax.swing.JLabel jLabelNameLabel; private javax.swing.JLabel jLabelNoteContact; private javax.swing.JLabel jLabelNoteLabelContact; private javax.swing.JLabel jLabelNotePerson; private javax.swing.JLabel jLabelPersonNote; private javax.swing.JLabel jLabelSurname; private javax.swing.JLabel jLabelSurnameLabel; private javax.swing.JLabel jLabelType; private javax.swing.JLabel jLabelTypeLabel; private javax.swing.JList jListContact; private javax.swing.JList jListPerson; private javax.swing.JMenu jMenu1; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItemAddContact; private javax.swing.JMenuItem jMenuItemAddPerson; private javax.swing.JMenuItem jMenuItemEditContact; private javax.swing.JMenuItem jMenuItemEditPerson; private javax.swing.JMenuItem jMenuItemExit; private javax.swing.JMenuItem jMenuItemRemoveContact; private javax.swing.JMenuItem jMenuItemRemovePerson; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanelDetail; private javax.swing.JProgressBar jProgressBar; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JPopupMenu.Separator jSeparator1; private javax.swing.JPopupMenu.Separator jSeparator2; private javax.swing.JToolBar.Separator jSeparator3; private javax.swing.JSplitPane jSplitPane1; private javax.swing.JSplitPane jSplitPane2; private javax.swing.JToolBar jToolBar2; // End of variables declaration//GEN-END:variables private Person getSelectedPerson() { if (jListPerson.getSelectedIndex() < 0) { return null; } return (Person) jListPerson.getSelectedValue(); } private void addContact() { final ContactJDialog dialog = new ContactJDialog(this, true); if (JOptionPane.OK_OPTION == dialog.showDialog())//contact != null) { Contact contact = dialog.getContact(); Person person = getSelectedPerson(); try { jProgressBar.setIndeterminate(true); jProgressBar.setVisible(true); NewContactSwingWorker worker = new NewContactSwingWorker(contact, person); worker.execute(); ((DefaultListModel<Contact>) jListContact.getModel()).addElement(contact); jProgressBar.setVisible(false); jProgressBar.setIndeterminate(false); } catch (RuntimeException e) { JOptionPane.showMessageDialog(rootPane, localizedTexts.getString("addContactErrMsg"), localizedTexts.getString("errorMsgTitle"), JOptionPane.ERROR_MESSAGE); } } } private void removeContact() { if (jListContact.getSelectedIndex() < 0) { JOptionPane.showMessageDialog(rootPane, localizedTexts.getString("noContactSelected"), "", JOptionPane.INFORMATION_MESSAGE); return; } if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(rootPane, localizedTexts.getString("deleteContactMsg"), "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)) { try { Contact contact = (Contact) jListContact.getSelectedValue(); jProgressBar.setIndeterminate(true); jProgressBar.setVisible(true); RemoveContactSwingWorker worker = new RemoveContactSwingWorker(contact); worker.execute(); ((DefaultListModel<Contact>) jListContact.getModel()).removeElement(contact); jProgressBar.setVisible(false); jProgressBar.setIndeterminate(false); } catch (RuntimeException e) { JOptionPane.showMessageDialog(this, localizedTexts.getString("removeContactErrMsg"), localizedTexts.getString("errorMsgTitle"), JOptionPane.ERROR_MESSAGE); } } } private void editContact() { if (jListContact.getSelectedIndex() < 0) { JOptionPane.showMessageDialog(rootPane, localizedTexts.getString("noContactSelected"), "", JOptionPane.INFORMATION_MESSAGE); return; } final ContactJDialog dialog = new ContactJDialog(this, true); dialog.setContact((Contact) jListContact.getSelectedValue());//.getContact()); if (JOptionPane.OK_OPTION == dialog.showDialog())//contact != null) { Contact contact = dialog.getContact(); try { jProgressBar.setIndeterminate(true); jProgressBar.setVisible(true); EditContactSwingWorker worker = new EditContactSwingWorker(contact); worker.execute(); updateContactDetail(); jProgressBar.setVisible(false); jProgressBar.setIndeterminate(false); } catch (RuntimeException e) { JOptionPane.showMessageDialog(this, localizedTexts.getString("editContactErrMsg"), localizedTexts.getString("errorMsgTitle"), JOptionPane.ERROR_MESSAGE); } } } private void setContactsToListModel(Person person) { jProgressBar.setIndeterminate(true); jProgressBar.setVisible(true); ContactsToListModelSwingWorker sw = new ContactsToListModelSwingWorker(person); sw.execute(); jProgressBar.setVisible(false); jProgressBar.setIndeterminate(false); } private void setPersonsToListModel() { jProgressBar.setIndeterminate(true); jProgressBar.setVisible(true); PersonsToListModelSwingWorker sw = new PersonsToListModelSwingWorker(); sw.execute(); jProgressBar.setVisible(false); jProgressBar.setIndeterminate(false); } private void updateContactDetail() { if (jListContact.getSelectedIndex() < 0) { jLabelData.setText("-"); jLabelNoteContact.setText("-"); jLabelType.setText("-"); return; } Contact contact = (Contact) jListContact.getSelectedValue(); switch (contact.getType()) { case MAIL: jLabelData.setText(((MailContact) contact).getMailAddress()); break; case PHONE: jLabelData.setText(((PhoneContact) contact).getPhoneNumber()); break; } switch (contact.getType()) { case MAIL: jLabelType.setText(localizedTexts.getString("mailType")); jLabelDataLabel.setText(localizedTexts.getString("mailData")); break; case PHONE: jLabelType.setText(localizedTexts.getString("phoneType")); jLabelDataLabel.setText(localizedTexts.getString("phoneData")); break; } if (contact.getNote().isEmpty()) { jLabelNoteContact.setText("-"); } else { jLabelNoteContact.setText(convertInternalTextFormToHtml(contact.getNote())); } } private void updatePersonDetail() { if (jListPerson.getSelectedIndex() < 0) { jLabelName.setText("-"); jLabelSurname.setText("-"); jLabelPersonNote.setText("-"); return; } Person person = (Person) jListPerson.getSelectedValue(); jLabelName.setText(person.getName()); jLabelSurname.setText(person.getLastName()); if (person.getNote().isEmpty()) { jLabelNotePerson.setText("-"); } else { jLabelNotePerson.setText(convertInternalTextFormToHtml(person.getNote())); } } private void editPerson() { if (jListPerson.getSelectedIndex() < 0) { JOptionPane.showMessageDialog(this, localizedTexts.getString("noPersonSelected"), "", JOptionPane.INFORMATION_MESSAGE); return; } final PersonJDialog dialog = new PersonJDialog(this, true); dialog.setPerson(getSelectedPerson());//.getContact()); if (JOptionPane.OK_OPTION == dialog.showDialog())//contact != null) { Person person = dialog.getPerson(); try { jProgressBar.setIndeterminate(true); jProgressBar.setVisible(true); EditPersonSwingWorker worker = new EditPersonSwingWorker(person); worker.execute(); jProgressBar.setVisible(false); jProgressBar.setIndeterminate(false); updatePersonDetail(); } catch (RuntimeException e) { JOptionPane.showMessageDialog(this, localizedTexts.getString("noPersonEdited"), localizedTexts.getString("errorMsgTitle"), JOptionPane.ERROR_MESSAGE); } } } private void removePerson() { if (jListPerson.getSelectedIndex() < 0) { JOptionPane.showMessageDialog(this, localizedTexts.getString("noPersonSelected"), "", JOptionPane.INFORMATION_MESSAGE); return; } if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(rootPane, localizedTexts.getString("deletePersonMsg"), "", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)) { try { Person person = getSelectedPerson(); jProgressBar.setIndeterminate(true); jProgressBar.setVisible(true); RemovePersonSwingWorker worker = new RemovePersonSwingWorker(person); worker.execute(); ((DefaultListModel<Contact>) jListPerson.getModel()).removeElement(person); jProgressBar.setVisible(false); jProgressBar.setIndeterminate(false); } catch (RuntimeException e) { JOptionPane.showMessageDialog(this, localizedTexts.getString("noPersonRemoved"), localizedTexts.getString("errorMsgTitle"), JOptionPane.ERROR_MESSAGE); } } } private void addPerson() { final PersonJDialog dialog = new PersonJDialog(this, true); if (JOptionPane.OK_OPTION == dialog.showDialog())//contact != null) { Person person = dialog.getPerson(); try { jProgressBar.setIndeterminate(true); jProgressBar.setVisible(true); NewPersonSwingWorker worker = new NewPersonSwingWorker(person); worker.execute(); ((DefaultListModel<Person>) jListPerson.getModel()).addElement(person); jProgressBar.setVisible(false); jProgressBar.setIndeterminate(false); } catch (RuntimeException e) { JOptionPane.showMessageDialog(this, localizedTexts.getString("noPersonAdded"), localizedTexts.getString("errorMsgTitle"), JOptionPane.ERROR_MESSAGE); } } } private static class ContactCellRenderer extends JLabel implements ListCellRenderer<Contact> { public ContactCellRenderer() { setOpaque(true); setVerticalAlignment(CENTER); } @Override public Component getListCellRendererComponent(JList<? extends Contact> list, Contact value, int index, boolean isSelected, boolean cellHasFocus) { String data;// = value.getType()//.toString(); switch (value.getType())//((Contact)value).getType()) { case MAIL: data = ((MailContact) value).getMailAddress(); break; case PHONE: data = ((PhoneContact) value).getPhoneNumber(); break; default: data = "unknown"; } if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(Color.WHITE);//list.getBackground()); setForeground(list.getForeground()); } setText(" " + data); setFont(list.getFont()); return this; } } private static class PersonCellRenderer extends JLabel implements ListCellRenderer<Person> { public PersonCellRenderer() { setOpaque(true); setVerticalAlignment(CENTER); } @Override public Component getListCellRendererComponent(JList<? extends Person> list, Person value, int index, boolean isSelected, boolean cellHasFocus) { StringBuilder data = new StringBuilder(value.getLastName().toUpperCase(getDefaultLocale())); data.append(", "); data.append(value.getName()); if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(Color.WHITE);//list.getBackground()); setForeground(list.getForeground()); } setText(" " + data.toString()); setFont(list.getFont()); return this; } } private String convertInternalTextFormToHtml(String text) { String converted = text.replaceAll("\n", "<br />"); return "<html>" + converted + "</html>"; } private static class RemoveContactSwingWorker extends SwingWorker<Void, Void> { Contact contact; public RemoveContactSwingWorker(Contact contact) { this.contact = contact; } @Override protected Void doInBackground() throws Exception { setProgress(0); contactManager.removeContact(contact); setProgress(100); return null; } } private static class EditContactSwingWorker extends SwingWorker<Void, Void> { Contact contact; public EditContactSwingWorker(Contact contact) { this.contact = contact; } @Override protected Void doInBackground() throws Exception { setProgress(0); contactManager.editContact(contact); setProgress(100); return null; } } private static class NewContactSwingWorker extends SwingWorker<Void, Void> { Contact contact; Person person; public NewContactSwingWorker(Contact contact, Person person) { this.contact = contact; this.person = person; } @Override protected Void doInBackground() throws Exception { setProgress(0); contactManager.createContact(contact); setProgress(50); directoryManager.addContactToPerson(person, contact); setProgress(100); return null; } } private static class RemovePersonSwingWorker extends SwingWorker<Void, Void> { Person person; public RemovePersonSwingWorker(Person person) { this.person = person; } @Override protected Void doInBackground() throws Exception { setProgress(0); personManager.removePerson(person); setProgress(100); return null; } } private static class EditPersonSwingWorker extends SwingWorker<Void, Void> { Person person; public EditPersonSwingWorker(Person person) { this.person = person; } @Override protected Void doInBackground() throws Exception { setProgress(0); personManager.editPerson(person); setProgress(100); return null; } } private static class NewPersonSwingWorker extends SwingWorker<Void, Void> { Person person; public NewPersonSwingWorker(Person person) { this.person = person; } @Override protected Void doInBackground() throws Exception { setProgress(0); personManager.createPerson(person); setProgress(100); return null; } } private class PersonsToListModelSwingWorker extends SwingWorker<ListModel, Void> { public PersonsToListModelSwingWorker() { } @Override protected ListModel doInBackground() throws Exception { setProgress(0); DefaultListModel model = new DefaultListModel(); for (Person person : personManager.getAllPeople()) { model.addElement(person); } setProgress(100); return model; } @Override protected void done() { try { jListPerson.setModel(get()); } catch (InterruptedException | ExecutionException ex) { JOptionPane.showMessageDialog(rootPane, localizedTexts.getString("noPersonMsg"), localizedTexts.getString("errorMsgTitle"), JOptionPane.ERROR_MESSAGE); jListPerson.setModel(new DefaultListModel()); } } } private class ContactsToListModelSwingWorker extends SwingWorker<ListModel, Void> { Person person; public ContactsToListModelSwingWorker(Person person) { this.person = person; } @Override protected ListModel doInBackground() throws Exception { setProgress(0); DefaultListModel model = new DefaultListModel(); if (person != null) { for (Contact contact : directoryManager.findAllContactsOfPerson(person)) { model.addElement(contact); } } setProgress(100); return model; } @Override protected void done() { try { jListContact.setModel(get()); } catch (InterruptedException | ExecutionException ex) { JOptionPane.showMessageDialog(rootPane, localizedTexts.getString("noContactMsg"), localizedTexts.getString("errorMsgTitle"), JOptionPane.ERROR_MESSAGE); jListContact.setModel(new DefaultListModel()); } } } }