/*
 Tiamat - a role-playing game framework
 EditorInternalFrame.java - internal frame that contains an editor
 
 Copyright  (C) 2006 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.adventureStudio;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import com.huguesjohnson.tiamat.tools.BaseEditorFrame;

/**  EditorInternalFrame - internal frame that contains an editor
 * @author Hugues Johnson
 */
public class EditorInternalFrame extends JInternalFrame{
	/* 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=6746919456470297804L;

	private BaseEditorFrame editor;
	
	public EditorInternalFrame(BaseEditorFrame editor){
		super();
		this.editor=editor;
		JScrollPane scrollPane=new JScrollPane(editor.getContentPanel());
		this.setContentPane(scrollPane);
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    	this.addInternalFrameListener(new InternalFrameAdapter(){
    		public void internalFrameClosing(InternalFrameEvent e){
            	onInternalFrameClosing(e);
            }
        });	
    }
	
	public void onInternalFrameClosing(InternalFrameEvent e){
		//prompt
		String message="Save changes to "+this.getTitle()+"?";
		String title="Confirm Save";
		int result=JOptionPane.showConfirmDialog(null,message,title,JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
		if(result==JOptionPane.YES_OPTION){
			this.save();
			this.dispose();
		} else if(result==JOptionPane.NO_OPTION){
			this.dispose();
		} 
	}
	
	public void save(){
		this.editor.onSave();
	}
	
	public void viewXml(){
		this.editor.onViewXml();
	}
}