/*
 * ConfigurationListForm.java
 *
 * Copyright 2013 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This class represents a form used to display and select from lists of providers and extensions
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
abstract class ConfigurationListForm extends AddressBookWindow
{

  protected ExtensionSelector extensionSelector;
  private JPanel listPanel;
  int numEntries = 0;
  
  public ConfigurationListForm (String title, String itemType, AddressBookConfiguration configuration, 
                                JFrame mainWindow, ExtensionSelector extensionSelector)
  {
    super (title, mainWindow);
    this.extensionSelector = extensionSelector;
    
    // Create panel for list of items
    listPanel = new JPanel (new GridBagLayout ());
    
    // Get list of items currently configured
    java.util.List<ConfigurationRecord> items = configuration.getElementRecords (itemType);
    
    // Create row for each item      
    for (ConfigurationRecord item : items)
    {
      
      // Add entry to panel
      listPanel.add (createEntry (item),
                     new GridBagConstraints (0, numEntries++, 1, 1, 0, 0, 
                                             GridBagConstraints.WEST, 
                                             GridBagConstraints.NONE, 
                                             new Insets (0, 0, 0, 0), 
                                             0, 0));
    }
    
    // Create buttons for creating new entry and closing window
    JButton addButton = new JButton ("Add");
    JButton closeButton = new JButton ("Close");
    
    // Make buttons same size
    Dimension buttonSize = closeButton.getPreferredSize ();
    addButton.setPreferredSize (buttonSize);
    addButton.setMinimumSize (buttonSize);
    
    // Set action for close button
    final ConfigurationListForm form = this;
    closeButton.addActionListener (new ActionListener ()
    {
      public void actionPerformed (ActionEvent e)
      {
        form.dispose ();
      }
    });
                                             
    // Set action for new button
    addButton.addActionListener (new ActionListener ()
    {
      public void actionPerformed (ActionEvent e)
      {
        displayEntry (null);
      }
    });
                                             
    // Add buttons
    JPanel buttonPanel = new JPanel (new GridBagLayout ());
    buttonPanel.add (addButton,
                     new GridBagConstraints (0, 0, 1, 1, 0, 0, 
                                             GridBagConstraints.EAST, 
                                             GridBagConstraints.NONE, 
                                             new Insets (5, 5, 5, 5), 
                                             0, 0));
                                         
    buttonPanel.add (closeButton,
                     new GridBagConstraints (0, 1, 1, 1, 0, 0, 
                                             GridBagConstraints.EAST, 
                                             GridBagConstraints.NONE, 
                                             new Insets (5, 5, 5, 5), 
                                             0, 0));
    // Add panels to content
    Container content = getContentPane ();
    content.setLayout (new GridBagLayout ());
    content.add (listPanel,
                 new GridBagConstraints (0, 0, 1, 1, 0, 0, 
                                         GridBagConstraints.NORTH, 
                                         GridBagConstraints.NONE, 
                                         new Insets (0, 0, 0, 0), 
                                         0, 0));
    content.add (buttonPanel,
                 new GridBagConstraints (1, 0, 1, 1, 0, 0, 
                                         GridBagConstraints.NORTH, 
                                         GridBagConstraints.NONE, 
                                         new Insets (0, 0, 0, 0), 
                                         0, 0));
    
                                         
    // Pack and make visible 
    this.pack ();
    this.setVisible (true);

  }
  
  // Add a new entry
  public void add (ConfigurationRecord item)
  {
    listPanel.add (createEntry (item),
                   new GridBagConstraints (0, numEntries++, 1, 1, 0, 0, 
                                           GridBagConstraints.WEST, 
                                           GridBagConstraints.NONE, 
                                           new Insets (0, 0, 0, 0), 
                                           0, 0));
    pack ();
  }  
  
  // Remove an entry from list
  public void remove (ConfigurationListEntry entry)
  {
    listPanel.remove (entry);
    pack ();
  }  
    
  protected abstract ConfigurationListEntry createEntry (ConfigurationRecord item);
  
  public abstract void displayEntry (ConfigurationListEntry entry);
  
}