/*
 * AddressBookWindow.java
 *
 * Copyright 2011 John W Dawson
 *
 * This code is distributed under the terms of the GNU General Public License, version 3
 *
 * This class is used as a base class for all configuration dialogues.
 * It ensure that the opening window is disabled and re-enabled
 */
 

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class AddressBookWindow extends JFrame implements WindowListener
{
  private JFrame opener;
  private static final String BOOK_ICON = "images/LDAP AddressBook.png";
   
  public AddressBookWindow (String title, JFrame opener)
  {
    // Set title
    super (title);
    
    // Save opening window
    this.opener = opener;
    
    // Set default close action
    setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE);
    
    // Set icon
    setIconImage ((new ImageIcon (getClass().getResource (BOOK_ICON))).getImage());  

    // Register ourselves to handle window events such as open and close
    addWindowListener (this);
  }    

  public void	windowActivated (WindowEvent e)
  {} 
  
  public void	windowClosed (WindowEvent e) 
  {
    if (closeAction ())
    {
      reEnablePreviousWindow ();
    }
    else
    {
      // Exit from program
      System.exit (0);
    }

  } 
  
  public void reEnablePreviousWindow ()
  {
    // Re-enable opening window
    opener.setEnabled (true);
    
    // Set focus
    opener.requestFocus ();
  }
  
  
  public void	windowClosing (WindowEvent e) 
  {} 
  
  public void	windowDeactivated (WindowEvent e) 
  {} 
  
  public void	windowDeiconified (WindowEvent e) 
  {} 
  
  public void	windowIconified (WindowEvent e) 
  {} 
  
  public void	windowOpened (WindowEvent e) 
  {
    // Disable opening window
    opener.setEnabled (false);
  } 
  
  protected Boolean closeAction ()
  {
    return true;
  }

}