/*
 * ConfigurationListEntry.java
 *
 * Copyright 2013 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This class represents an entry in a list of configuration items as a tool bar
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
abstract class ConfigurationListEntry extends JToolBar implements ActionListener
{

  private static final String EDIT_IMAGE = "images/Edit16.gif";
  private static final String DELETE_IMAGE = "images/Delete16.gif";
  private JLabel name;
  private String itemType;
  protected ConfigurationRecord record;
  private ConfigurationListForm list;
  private JButton editButton;
  private JButton deleteButton;

  public ConfigurationListEntry (String itemType, ConfigurationRecord record, ConfigurationListForm list)
  {
    this.record = record;
    this.list = list;
    this.itemType = itemType;
    
    // Add spacer
    add (Box.createHorizontalStrut (5));
    
    // 1st column is edit button
    editButton = new JButton (new ImageIcon (getClass().getResource (EDIT_IMAGE)));  
    editButton.addActionListener (this);
    add (editButton); 

    // 2nd column is delete button
    deleteButton = new JButton (new ImageIcon (getClass().getResource (DELETE_IMAGE))); 
    deleteButton.addActionListener (this);
    add (deleteButton);
    
    // Add spacer
    add (Box.createHorizontalStrut (5));
                                              
    // 3rd column is entry name
    name = new JLabel (record.toString ());
    add (name);
    
    // Add spacer
    add (Box.createHorizontalStrut (5));
                                              
    // Add spring to end
    add (Box.createHorizontalGlue ());

    // Bar can't be undocked      
    setFloatable (false );
    
  }
  
  public void update ()
  {
    name.setText (record.toString ());
    list.pack ();
  }
  
  public ConfigurationRecord getRecord ()
  {
    return record;
  }
    
  private void delete ()
  {
    // Confirm deletion
    if (JOptionPane.showConfirmDialog (list, 
                                       "Do you really want to delete the " + itemType + " " + record.toString (),
                                       "Confirm delete " + itemType,
                                       JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
    {
      // Remove record from configuration
      record.remove ();
      
      // Perform any specific delete actions
      deleteActions ();
      
      // Remove entry from list
      list.remove (this);
    }
  }
  
  public void actionPerformed (ActionEvent event)
  {
    if (event.getSource() == editButton)
    {
      // Display form to edit entry
      list.displayEntry (this);
    }
    else if (event.getSource() == deleteButton)
    {
      delete ();
    }
  }
  
  protected abstract void deleteActions ();
   
 }    
