/* * Copyright 2011 Research In Motion Limited. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package eclserver.panels; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.event.EventListenerList; import javax.swing.JList; import java.util.List; import javax.swing.JOptionPane; import eclserver.db.objects.AddressObject; /** * * @author rbalsewich */ public class ContactsPanel extends javax.swing.JPanel { /** Creates new form ContactsPanel */ public ContactsPanel() { listeners = new EventListenerList(); initComponents(); contactPanel.setEditable(false); contactPanel.setEnabled(false); contactPanel.clear(); } /** 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(); btnAddContact = new javax.swing.JButton(); btnDeleteContact = new javax.swing.JButton(); btnSaveContact = new javax.swing.JButton(); btnCancelContact = new javax.swing.JButton(); contactPanel = new eclserver.panels.ContactPanel(); contactsList = new eclserver.panels.ContactsListPanel(); btnAddContact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eclserver/images/action_add.png"))); // NOI18N btnAddContact.setText("Add"); btnAddContact.setActionCommand("ADD_CONTACT"); btnAddContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnAddContactActionPerformed(evt); } }); btnDeleteContact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eclserver/images/action_delete.png"))); // NOI18N btnDeleteContact.setText("Delete"); btnDeleteContact.setActionCommand("DELETE_CONTACT"); btnDeleteContact.setDoubleBuffered(true); btnDeleteContact.setEnabled(false); btnDeleteContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDeleteContactActionPerformed(evt); } }); btnSaveContact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eclserver/images/action_save.png"))); // NOI18N btnSaveContact.setText("Save"); btnSaveContact.setActionCommand("SAVE_CONTACT"); btnSaveContact.setEnabled(false); btnSaveContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSaveContactActionPerformed(evt); } }); btnCancelContact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eclserver/images/action_cancel.png"))); // NOI18N btnCancelContact.setText("Clear"); btnCancelContact.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelContactActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(btnCancelContact, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnAddContact, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE) .addComponent(btnDeleteContact, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE) .addComponent(btnSaveContact, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addComponent(btnAddContact, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btnDeleteContact, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btnSaveContact, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(btnCancelContact, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(38, Short.MAX_VALUE)) ); contactsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() { public void valueChanged(javax.swing.event.ListSelectionEvent evt) { contactsListValueChanged(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(contactsList, javax.swing.GroupLayout.DEFAULT_SIZE, 698, Short.MAX_VALUE) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(10, 10, 10) .addComponent(contactPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18)))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(contactsList, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE) .addGap(11, 11, 11) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(contactPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); }// </editor-fold>//GEN-END:initComponents private void btnAddContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddContactActionPerformed // TODO add your handling code here: //contactPanel.clear(); contactPanel.setEnabled(true); contactPanel.setEditable(true); btnSaveContact.setEnabled(true); btnAddContact.setEnabled(false); }//GEN-LAST:event_btnAddContactActionPerformed private void btnDeleteContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteContactActionPerformed // TODO add your handling code here: try{ int id = contactsList.getSelectedIndex(); if (id != -1) { fireActionEvent(evt); } else { JOptionPane.showMessageDialog(this, "Select a list item to delete."); } }catch (Exception ex){ JOptionPane.showMessageDialog(this, "Delete Failed: " + ex.getMessage()); } }//GEN-LAST:event_btnDeleteContactActionPerformed private void btnSaveContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveContactActionPerformed // TODO add your handling code here: try{ // System.out.println("Validating required fields..."); if ((contactPanel.getGroupName().length() > 0 ) && (contactPanel.getLastName().length() > 0 ) && (contactPanel.getFirstName().length() > 0 ) && (contactPanel.getEmail().length() > 0)) { fireActionEvent(evt); } else { JOptionPane.showMessageDialog(this, "Complete the Mandatory Fields in RED"); } }catch (Exception ex){ System.out.println("Error Saving Contact:" + ex.getMessage()); } }//GEN-LAST:event_btnSaveContactActionPerformed private void btnCancelContactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelContactActionPerformed // TODO add your handling code here: try{ resetButtons(); clearPanel(); }catch (Exception ex){ JOptionPane.showMessageDialog(this, "Clear Failed: " + ex.getMessage()); } }//GEN-LAST:event_btnCancelContactActionPerformed private void contactsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_contactsListValueChanged // TODO add your handling code here: if (evt.getValueIsAdjusting() == false) { JList entryList = (JList) evt.getSource(); // System.out.println("List item selected: " + entryList.getSelectedIndex()); selectedEntry = entryList.getSelectedIndex(); int selList = contactsList.setSelectedIndex(selectedEntry); // System.out.println("getting selList " + selList); if (contactsList.getSelectedIndex() == -1) { //No selection, disable fire button. btnDeleteContact.setEnabled(false); } else { //Selection, enable the fire button.\ // setListSelection(contactsList.getSelectedIndex()); // System.out.println("Clicked " + contactsList.getSelectedListEntry().getId() + // " LastName: " + contactsList.getSelectedListEntry().getLastName()); contactPanel.setAddress(contactsList.getSelectedListEntry()); contactPanel.setEditable(true); btnDeleteContact.setEnabled(true); btnSaveContact.setEnabled(true); btnAddContact.setEnabled(false); } } }//GEN-LAST:event_contactsListValueChanged private void fireActionEvent(ActionEvent evt) { ActionListener[] listenerList = listeners.getListeners(ActionListener.class); for (int i = listenerList.length-1; i>=0; --i) { listenerList[i].actionPerformed(evt); } } public void addActionListener(ActionListener listener) { listeners.add(ActionListener.class, listener); } public void removeActionListener(ActionListener listener) { if (listeners != null) { listeners.remove(ActionListener.class, listener); } } public void setContactsListEntries(List<AddressObject> objects){ contactsList.addListEntries(objects); } public AddressObject getSelectedListEntry(){ return contactsList.getSelectedListEntry(); } public void addContactsListEntry(AddressObject object){ contactsList.addListEntry(object); } public AddressObject getPanelEntry(){ return contactPanel.getAddress(); } public void removeListEntry(){ contactsList.deleteSelectedEntry(); } public void removeAllEntries(){ contactsList.deleteAllEntries(); } public void updateListEntry(AddressObject obj, int value) { contactsList.updateIndexEntryAt(obj, value); } public void clearPanel(){ contactPanel.clear(); } public void resetButtons(){ btnAddContact.setEnabled(true); btnSaveContact.setEnabled(false); btnDeleteContact.setEnabled(false); contactPanel.setEnabled(false); contactPanel.setEditable(false); } public void setListSelection(int value){ this.selectedEntry = value; } public int getListSelection(){ return selectedEntry; } //CUSTOM VARIABLES EventListenerList listeners; private int selectedEntry = -1; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnAddContact; private javax.swing.JButton btnCancelContact; private javax.swing.JButton btnDeleteContact; private javax.swing.JButton btnSaveContact; private eclserver.panels.ContactPanel contactPanel; private eclserver.panels.ContactsListPanel contactsList; private javax.swing.JPanel jPanel1; // End of variables declaration//GEN-END:variables }