/*
Tiamat - a role-playing game framework
CharacterDialog.java - dialog for browsing for a character
 
 Copyright  (C) 2004 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 com.huguesjohnson.fileFilter.XmlFileFilter;
import com.huguesjohnson.ui.FileBrowsePanel;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;

/** CharacterDialog - dialog for browsing for a character
 * an awful lot of this code was generated through the NetBeans 3.6 form editor
 * @author Hugues Johnson
 */
public class CharacterDialog 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=-7787931913731512703L;
	
	/** Creates new form CharacterDialog
     * @param parent the parent form
     * @param modal whether the dialog is modal
     */
    public CharacterDialog(Frame parent,boolean modal){
        super(parent,modal);
        this.initComponents();
        this.addBrowsePanel();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }
    
    /** Creates new form CharacterDialog
     * modal display
     * @param parent the parent form
     */
    public CharacterDialog(Frame parent){
        super(parent,true);
        this.initComponents();
        this.addBrowsePanel();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }
    
    /** Creates new form CharacterDialog
     * @param parent the parent form
     * @param filePath the path to a character file
     * @param modal whether the dialog is modal
     */
    public CharacterDialog(Frame parent,String filePath,boolean modal){
        super(parent,modal);
        this.initComponents();
        this.addBrowsePanel();
        this.browsePanel.setFilePath(filePath);
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }
    
    /** Creates new form CharacterDialog
     * modal display
     * @param parent the parent form
     * @param filePath the path to a character file
     */
    public CharacterDialog(Frame parent,String filePath){
        super(parent,true);
        this.initComponents();
        this.addBrowsePanel();
        this.browsePanel.setFilePath(filePath);
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }
    
    /** centers the window in the middle of the screen
     */
    private void centerWindow(){
        Dimension screenDimension=Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle windowDimension=this.getBounds();
        this.setLocation((screenDimension.width-windowDimension.width)/2,(screenDimension.height-windowDimension.height)/2);
    }
    
    /** centers the window in the middle of the screen
     */
    private void centerToParent(Frame parent){
        Rectangle parentDimension=parent.getBounds();
        Rectangle windowDimension=this.getBounds();
        this.setLocation((parentDimension.width-windowDimension.width)/2,(parentDimension.height-windowDimension.height)/2);
    }
    
    /** add the browse panel
     */
    private void addBrowsePanel(){
        FileFilter[] filters=new FileFilter[1];
        filters[0]=new XmlFileFilter();
        this.browsePanel=new FileBrowsePanel("Character File Path",filters);
        this.panelPath.add(this.browsePanel);
        this.pack();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
    	this.panelPath = new javax.swing.JPanel();
    	this.panelButtons = new javax.swing.JPanel();
    	this.buttonEdit = new javax.swing.JButton();
    	this.buttonEdit.setVisible(false);
    	this.buttonOk = new javax.swing.JButton();
    	this.buttonCancel = new javax.swing.JButton();

    	this.getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

    	this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    	this.setTitle("Character");
    	this.setModal(true);
    	this.setResizable(false);
    	this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

    	this.getContentPane().add(this.panelPath);

    	this.panelButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

    	this.buttonEdit.setText("Edit..");
    	this.buttonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditActionPerformed(evt);
            }
        });

    	this.panelButtons.add(this.buttonEdit);

    	this.buttonOk.setText("OK");
    	this.buttonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

    	this.panelButtons.add(this.buttonOk);

    	this.buttonCancel.setText("Cancel");
    	this.buttonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

    	this.panelButtons.add(this.buttonCancel);

    	this.getContentPane().add(this.panelButtons);

    	this.pack();
    }//GEN-END:initComponents
    
    public void buttonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditActionPerformed
        this.runCharacterEditor();
    }//GEN-LAST:event_buttonEditActionPerformed
    
    public void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
        this.closeDialog(false);
    }//GEN-LAST:event_buttonOkActionPerformed
    
    public void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        this.closeDialog(true);
    }//GEN-LAST:event_buttonCancelActionPerformed
    
    public void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.closeDialog(true);
    }//GEN-LAST:event_formWindowClosing
    
    /** get the path to the character
     * @return path to the character file
     */
    public String getCharacterPath(){
        return(this.browsePanel.getFilePath());
    }
    
    /** launch the character editor
     */
    private void runCharacterEditor(){
        new CharacterEditor(){ public void exitHandler(){ dispose(); } };
    }
    
    /** create a new CharacterDialog
     * @param args the command line arguments
     */
    public static void main(String args[]){
        (new CharacterDialog(new JFrame(),true)).setVisible(true);
    }
    
    /** event that fires when dialog is closed
     * override this in the implementing class 
     */
    public void closeDialog(boolean cancel){
        this.dispose();
        System.exit(0);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonEdit;
    private javax.swing.JButton buttonOk;
    private javax.swing.JPanel panelButtons;
    private javax.swing.JPanel panelPath;
    // End of variables declaration//GEN-END:variables
    private FileBrowsePanel browsePanel;
}