/*
 * MainWindow.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 main window for the program
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class MainWindow extends JFrame
{
  static final Border PADDING = BorderFactory.createEmptyBorder (5, 5, 5, 5);
  private static final String BOOK_ICON = "images/LDAP AddressBook.png";
  private Container content = getContentPane ();
  private GroupLayout.ParallelGroup horizontalGroup;
  private GroupLayout.SequentialGroup verticalGroup;

  int numRows = 0;
  private AddressBookConfiguration configuration;
  
  public MainWindow (String title)
  {
    super (title);
    
    // Create group layout
    GroupLayout layout = new GroupLayout(content);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);    
    content.setLayout(layout);
    
    horizontalGroup = layout.createParallelGroup(GroupLayout.Alignment.TRAILING);
    layout.setHorizontalGroup (horizontalGroup);
    
    verticalGroup = layout.createSequentialGroup();
    layout.setVerticalGroup (verticalGroup);
    
    // Set default close action
    setDefaultCloseOperation (WindowConstants.DISPOSE_ON_CLOSE);
    
    addWindowListener (new WindowAdapter()
    {
      public void windowClosing (WindowEvent e)
      {
        // Save the configuration
        configuration.save ();
                
        dispose();
        System.exit(0);
      }
    });

    // Set icon
    setIconImage ((new ImageIcon (getClass().getResource (BOOK_ICON))).getImage());  
  }
  
  public void add (JComponent component, String labelText)
  {
    // Create panel for row
    JPanel panel = new JPanel ();
    panel.setLayout (new BoxLayout (panel, BoxLayout.LINE_AXIS));
    panel.setBorder (PADDING);
    
    // Add some 'glue' to push everything to the right
    panel.add(Box.createHorizontalGlue());
    
    // Add the label
    panel.add (new JLabel (labelText));
    
    // Add a spacer
    panel.add (Box.createRigidArea (new Dimension(10, 0)));
    
    // Ensure component does not get stretched
    component.setMaximumSize (component.getPreferredSize ());
    
    // Add the component                                          
    panel.add (component);
    
    // Add the panel to the content pane
    horizontalGroup.addComponent (panel);
    verticalGroup.addComponent (panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE);
  }
  
  public void add (JComponent component)
  { 
  
    // Add the component                                 
    horizontalGroup.addComponent (component);
    verticalGroup.addComponent (component, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
                                GroupLayout.PREFERRED_SIZE);
  }  
  
  public void initialise (AddressBookConfiguration configuration)
  {
    this.configuration = configuration;
  }
   
}
  
       
