/*
 * FileMenu.java
 *
 * Copyright 2011 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This class implements the file menu 
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FileMenu extends JMenu implements ActionListener
{
  AddressBookConfiguration configuration;
  JMenuItem saveItem = new JMenuItem ("Save Settings");
  JMenuItem exitItem = new JMenuItem ("Exit");
  JMenuItem quitItem = new JMenuItem ("Quit");
  
  public FileMenu ()
  {
    super ("File");
  }
  
  public void initialise (AddressBookConfiguration configuration)
  {
  
    this.configuration = configuration;

    // Add save command    
    saveItem.addActionListener (this);
    add (saveItem);
    
    // Add exit command    
    exitItem.addActionListener (this);
    add (exitItem);
    
    // Add quit command    
    quitItem.addActionListener (this);
    add (quitItem);
    
  }
  
  public void actionPerformed (ActionEvent e)
  {
    if (e.getSource() == quitItem)
    {
      // Confirm quit without saving
      if (JOptionPane.showConfirmDialog (this, 
                                         "Do you really want to quit without saving configuration",
                                         "Confirm quit",
                                         JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
      {
        System.exit (0);
      }
    }
    else
    {
      // Save setting for both exit & save commands
      configuration.save ();
      
      // Exit if exit command
      if (e.getSource() == exitItem)
      {
        System.exit (0);
      }
    }
  }
}	