/*
 * ControlPanel.java
 *
 * Copyright 2011 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This class represents the panel containing the buttons which control the program
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
import java.util.logging.*;
public class ControlPanel extends JPanel implements ActionListener
{
  static Logger log = Logger.getLogger ("LdapAddressBook");
  private Dimension largestButtonSize = new Dimension ();
  private Map<String, JButton> buttons = new HashMap<String, JButton>();
  private EditPanel editPanel;
  private JTextField searchBox;
  private LdapSearchList searchList;
  
  public ControlPanel ()
  {

    setLayout (new BoxLayout (this, BoxLayout.PAGE_AXIS));
    
    // Add the buttons
    addButton ("New");
    addButton ("Copy");
    addButton ("Save");
    addButton ("Undo");
    addButton ("Delete");
    
    // Make the buttons all the same width
    for (JButton button : buttons.values())
    {
      button.setMaximumSize (largestButtonSize);
    }
 
  }
  
  public void initialise (EditPanel editPanel)
  {
    this.editPanel = editPanel;
  }
  
  private void addButton (String label)
  {
    // Create and add button    
    JButton button = new JButton (label);
    add (button);
    
    // All buttons are initially disabled
    button.setEnabled (false);
    
    // Add a spacer
    add (Box.createRigidArea (new Dimension(0, 10)));

    // Is this the widest button
    if (button.getPreferredSize().getWidth() > largestButtonSize.getWidth())
    {
      largestButtonSize = button.getPreferredSize();
    }

    // Store ref to button in table    
    buttons.put (label, button);
    
    // Set up handler for button click
    button.addActionListener (this);
  }
  
  public void enableButton (String label)
  {
    buttons.get (label).setEnabled (true);
  }
  
  public void disableButton (String label)
  {
    buttons.get (label).setEnabled (false);
  }
  
  public void disableAllButtons ()
  {
    for (JButton button : buttons.values ())
    {
      button.setEnabled (false);
    }
  }
  
  public void actionPerformed (ActionEvent event)
  {
    // Check which button was clicked
    AbstractButton source = (AbstractButton) event.getSource ();
    
    if (source.getText().equals ("New"))
    {
      // Clear the edit panel
      editPanel.newEntry ();
    }
    else if (source.getText().equals ("Copy"))
    {
      // Copy the current entry 
      editPanel.saveEntry (true);
    }
    else if (source.getText().equals ("Undo"))
    {
      // Reset the edit panel 
      editPanel.undo ();
    }
    else if (source.getText().equals ("Save"))
    {
      // Save the current entry
      editPanel.saveEntry (false);
    }
    else if (source.getText().equals ("Delete"))
    {
      // Delete the current entry
      editPanel.deleteEntry ();
    }
  }
  
  public Boolean isButton (Component component, String label)
  {
    return component == buttons.get (label);
  } 
  
}
