/*
 Tiamat - a role-playing game framework
 EventListDialog.java - dialog for editing a list of events
 
 Copyright  (C) 2004-2005 Hugues Johnson
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 the GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.tiamat.tools;

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.huguesjohnson.tiamat.Event;

/**
 * EventListDialog - dialog for editing a list of events
 * @author Hugues Johnson
 */
public class EventListDialog extends JDialog{
	/* Determines if a de-serialized file is compatible with this class. See <a href="http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html">Java Object Serialization Specifications</a>. */
	private static final long serialVersionUID=-7115559039677144778L;
	
	private EventListPanel eventListPanel;
	
	public EventListDialog(JFrame owner){
		super(owner,"Events",true);
		this.initComponents();
		this.centerToParent(owner);
	}

	public EventListDialog(JFrame owner,Event[] events){
		super(owner,"Events",true);
		this.initComponents();
		this.setEvents(events);
		this.centerToParent(owner);
	}
	
    public void centerToParent(Frame parent){
        Rectangle parentDimension=parent.getBounds();
        Rectangle windowDimension=this.getBounds();
        this.setLocation((parentDimension.width-windowDimension.width)/2,(parentDimension.height-windowDimension.height)/2);
   }	
	
	private void initComponents(){
		this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
    	this.setResizable(false);
    	//add the event list
    	this.eventListPanel=new EventListPanel();
    	this.getContentPane().add(this.eventListPanel);
		//add a panel for ok and cancel buttons
    	JPanel buttonPanel=new JPanel();
    	buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    	JButton okButton=new JButton("OK");
    	okButton.setMnemonic(KeyEvent.VK_O);
    	okButton.addActionListener(new ActionListener(){
            @SuppressWarnings({"synthetic-access","unqualified-field-access"})
			public void actionPerformed(ActionEvent actionEvent){
            	eventListPanel.saveSelectedEvent();
            	onOk();
            }
        });
    	buttonPanel.add(okButton);
    	JButton cancelButton=new JButton("Cancel");
    	cancelButton.setMnemonic(KeyEvent.VK_C);
    	cancelButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onCancel();
            }
        });
    	buttonPanel.add(cancelButton);
    	this.getContentPane().add(buttonPanel);
    	
    	this.pack();
	}
	
	public void onOk(){ }
	
	public void onCancel(){ }
	
	public Event[] getEvents(){
		return(this.eventListPanel.getEventList());
	}
	
	public void setEvents(Event[] events){
		this.eventListPanel.setEventList(events);
	}
}