/*
Tiamat - a role-playing game framework
LayerDialog.java - dialog for editing a layer
 
 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.serialization.XmlFormatter;
import com.huguesjohnson.tiamat.graphics.Layer;
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.filechooser.FileFilter;

/** LayerDialog - dialog for editing a layer
 * an awful lot of this code was generated through the NetBeans 3.6 form editor
 * @author Hugues Johnson
 */
public class LayerDialog 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=-7568288775013551017L;
	
	/** Creates new form LayerDialog */
    public LayerDialog(Frame parent, boolean modal){
        super(parent,modal);
        this.initComponents();
        this.addComboBoxItems();
        this.addBrowsePanel();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }        
    }

    public LayerDialog(Frame parent){
        super(parent,true);
        initComponents();
        this.addComboBoxItems();
        this.addBrowsePanel();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }        
    }

    public LayerDialog(Frame parent, boolean modal, Layer layer){
        super(parent,modal);
        this.initComponents();
        this.addComboBoxItems();
        this.addBrowsePanel();
        int type=layer.getType();
        this.comboBoxType.setSelectedIndex(type);
        if(type==Layer.TYPE_MAP){
            this.browsePanel.setFilePath(layer.getMapPath());
        }
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }        
    }

    public LayerDialog(Frame parent, Layer layer){
        super(parent,true);
        this.initComponents();
        this.addComboBoxItems();
        this.addBrowsePanel();
        int type=layer.getType();
        this.comboBoxType.setSelectedIndex(type);
        if(type==Layer.TYPE_MAP){
            this.browsePanel.setFilePath(layer.getMapPath());
        }
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }        
    }    
    
     /** centers the window in the middle of the screen
     */     
     public 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
     */     
     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 addComboBoxItems(){
        this.comboBoxType.addItem(new String("0 - Map                    "));
        this.comboBoxType.addItem(new String("1 - Sprite                    "));
        this.comboBoxType.addItem(new String("2 - Dialog                    "));
    }
    
    private void addBrowsePanel(){
        FileFilter[] filters=new FileFilter[1];
        filters[0]=new XmlFileFilter();
        this.browsePanel=new FileBrowsePanel("Map File Path",filters);
        this.panelMapPath.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.jLabel1 = new javax.swing.JLabel();
    	this.panelType = new javax.swing.JPanel();
    	this.labelType = new javax.swing.JLabel();
    	this.comboBoxType = new javax.swing.JComboBox();
    	this.panelMapPath = new javax.swing.JPanel();
    	this.panelButtons = new javax.swing.JPanel();
    	this.buttonOk = new javax.swing.JButton();
    	this.buttonCancel = new javax.swing.JButton();

    	this.jLabel1.setText("jLabel1");

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

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

    	this.panelType.setLayout(new javax.swing.BoxLayout(this.panelType, javax.swing.BoxLayout.X_AXIS));

    	this.labelType.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    	this.labelType.setText("  Type: ");
    	this.panelType.add(this.labelType);

    	this.comboBoxType.setToolTipText("Select type of layer");
    	this.comboBoxType.setMaximumSize(new java.awt.Dimension(32767, 21));
    	this.panelType.add(this.comboBoxType);

    	this.getContentPane().add(this.panelType);

    	this.getContentPane().add(this.panelMapPath);

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

    	this.buttonOk.setText("OK");
    	this.buttonOk.setToolTipText("Save and close");
    	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.setToolTipText("Close without saving");
    	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 buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        this.closeDialog(true);
    }//GEN-LAST:event_buttonCancelActionPerformed

    public void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
        this.closeDialog(false);
    }//GEN-LAST:event_buttonOkActionPerformed

    public void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.closeDialog(true);
    }//GEN-LAST:event_formWindowClosing
   
    /** override this in the implementing class */
    public void closeDialog(boolean cancel){
        this.dispose();
        System.exit(0);
    }    
        
    /** get the layer represented by this dialog */
    public Layer getLayer(){
        int type=this.comboBoxType.getSelectedIndex();
        if(type==Layer.TYPE_MAP){
            return(new Layer(this.browsePanel.getFilePath(),new XmlFormatter()));
        }
		return(new Layer(type));
    }
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOk;
    private javax.swing.JComboBox comboBoxType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labelType;
    private javax.swing.JPanel panelButtons;
    private javax.swing.JPanel panelMapPath;
    private javax.swing.JPanel panelType;
    // End of variables declaration//GEN-END:variables
    private FileBrowsePanel browsePanel;
}
