/*
Tiamat - a role-playing game framework
BaseEditorFrame.java - base frame for editors
 
 Copyright  (C) 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 com.huguesjohnson.LaunchBrowser;
import com.huguesjohnson.ui.RecentFileMenu;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JToolBar;

public abstract class BaseEditorFrame extends JFrame{
    private String editorName;
	
	/* begin protected UI components */
    protected JButton buttonNew;
    protected JButton buttonOpen;
    protected JButton buttonSave;
    protected JButton buttonSaveAs;
    protected JMenuBar menu;
    protected JMenu menuFile;
    protected JMenu menuHelp;
    protected JMenuItem menuHelpHomepage;
    protected JMenuItem menuHelpEditors;
    protected JMenuItem menuFileExit;
    protected JMenuItem menuFileNew;
    protected JMenuItem menuFileOpen;
    protected JMenuItem menuFileSave;
    protected JMenuItem menuFileSaveAs;
    protected RecentFileMenu menuRecent;
    protected JMenu menuView;
    protected JMenuItem menuViewXml;
    protected JPanel panelContent;
    protected JToolBar toolBar;    
    /* end protected UI components */

	public BaseEditorFrame(String editorName){
		super();
		this.editorName=new String(editorName);
		this.baseInitComponents();
		this.initComponents();
		this.centerWindow();
	}
	
	@SuppressWarnings("serial")
	public void baseInitComponents(){
    	//set frame properties
		this.setIconImage((new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/icon.gif"))).getImage());
    	this.setResizable(false);
    	this.setTitle("Tiamat "+this.editorName+" Editor");
    	
    	//add listener for the window closing event
    	this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent windowEvent){
                onWindowClose();
            }
        });	
    	
    	//add the content panel
    	this.panelContent=new JPanel();
        this.getContentPane().add(this.panelContent,BorderLayout.CENTER);
        
        //add the toolbar
        this.toolBar=new JToolBar();
        this.toolBar.setFloatable(false);
        this.toolBar.setRollover(true);
        this.buttonNew=new JButton();
        this.buttonNew.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/new.gif")));
        this.buttonNew.setToolTipText("Create a new "+this.editorName);
        this.buttonNew.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onNew();
            }
        });
        this.toolBar.add(this.buttonNew);
        this.buttonOpen=new JButton();
        this.buttonOpen.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/open.gif")));
        this.buttonOpen.setToolTipText("Open a saved "+this.editorName);
        this.buttonOpen.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onOpen();
            }
        });
        this.toolBar.add(this.buttonOpen);
        this.buttonSave=new JButton();
        this.buttonSave.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/save.gif")));
        this.buttonSave.setToolTipText("Save this "+this.editorName);
        this.buttonSave.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onSave();
            }
        });
        this.toolBar.add(this.buttonSave);
        this.buttonSaveAs=new JButton();
        this.buttonSaveAs.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/save_as.gif")));
        this.buttonSaveAs.setToolTipText("Save this "+this.editorName+" as");
        this.buttonSaveAs.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onSaveAs();
            }
        });
        this.toolBar.add(this.buttonSaveAs);
        this.getContentPane().add(this.toolBar,BorderLayout.NORTH);
        
        //add the menu
        this.menu=new JMenuBar();
        this.menuFile=new JMenu();
        this.menuFile.setMnemonic('F');
        this.menuFile.setText("File");
        this.menuFileNew=new JMenuItem();
        this.menuFileNew.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/new.gif")));
        this.menuFileNew.setMnemonic('N');
        this.menuFileNew.setText("New");
        this.menuFileNew.setToolTipText("Create a new "+this.editorName);
        this.menuFileNew.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onNew();
            }
        });
        this.menuFile.add(this.menuFileNew);
        this.menuFileOpen=new JMenuItem();
        this.menuFileOpen.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/open.gif")));
        this.menuFileOpen.setMnemonic('O');
        this.menuFileOpen.setText("Open..");
        this.menuFileOpen.setToolTipText("Open a saved "+this.editorName);
        this.menuFileOpen.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onOpen();
            }
        });
        this.menuFile.add(this.menuFileOpen);
        this.menuRecent=new RecentFileMenu(this.editorName,4){
        	public void onSelectFile(String filePath){
        		onRecentFile(filePath);
        	}
        };
        this.menuFile.add(this.menuRecent);
        this.menuFile.add(new JSeparator());
        this.menuFileSave=new JMenuItem();
        this.menuFileSave.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/save.gif")));
        this.menuFileSave.setMnemonic('S');
        this.menuFileSave.setText("Save");
        this.menuFileSave.setToolTipText("Save this "+this.editorName);
        this.menuFileSave.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onSave();
            }
        });
        this.menuFile.add(this.menuFileSave);
        this.menuFileSaveAs=new JMenuItem();
        this.menuFileSaveAs.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/save_as.gif")));
        this.menuFileSaveAs.setMnemonic('A');
        this.menuFileSaveAs.setText("Save As..");
        this.menuFileSaveAs.setToolTipText("Save this "+this.editorName+" as");
        this.menuFileSaveAs.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onSaveAs();
            }
        });
        this.menuFile.add(this.menuFileSaveAs);
        this.menuFile.add(new JSeparator());
        this.menuFileExit=new JMenuItem();
        this.menuFileExit.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/exit.gif")));
        this.menuFileExit.setMnemonic('X');
        this.menuFileExit.setText("Exit");
        this.menuFileExit.setToolTipText("Exit "+this.editorName+" editor");
        this.menuFileExit.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onWindowClose();
            }
        });
        this.menuFile.add(this.menuFileExit);
        this.menu.add(this.menuFile);
        this.menuView=new JMenu();
        this.menuView.setMnemonic('V');
        this.menuView.setText("View");
        this.menuViewXml=new JMenuItem();
        this.menuViewXml.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/file.gif")));
        this.menuViewXml.setMnemonic('X');
        this.menuViewXml.setText("XML..");
        this.menuViewXml.setToolTipText("View XML file (read-only)");
        this.menuViewXml.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
                onViewXml();
            }
        });
        this.menuView.add(this.menuViewXml);
        this.menu.add(this.menuView);
        this.menuHelp=new JMenu();
        this.menuHelp.setMnemonic('H');
        this.menuHelp.setText("Help");
        this.menuHelpHomepage=new JMenuItem();
        this.menuHelpHomepage.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/icon.gif")));
        this.menuHelpHomepage.setMnemonic('T');
        this.menuHelpHomepage.setText("Tiamat Homepage..");
        this.menuHelpHomepage.setToolTipText("Go to the Tiamat Homepage");
        this.menuHelpHomepage.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
            	onHelpHomepage();
            }
        });
        this.menuHelp.add(this.menuHelpHomepage);
        this.menuHelpEditors=new JMenuItem();
        this.menuHelpEditors.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/icon.gif")));
        this.menuHelpEditors.setMnemonic('E');
        this.menuHelpEditors.setText(this.editorName+" editor..");
        this.menuHelpEditors.setToolTipText("Go to the help page for the Tiamat editors..");
        this.menuHelpEditors.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent actionEvent){
            	onHelpEditors();
            }
        });
        this.menuHelp.add(this.menuHelpEditors);
        this.menu.add(this.menuHelp);
        this.setJMenuBar(this.menu);
	}
	
	/* begin concrete methods common to all editors */

    public void onWindowClose(){
        this.setVisible(false);
        this.exitEvent();
    }
    
    // override this if creating an instance of this class from another class 
    public void exitEvent(){
    	System.exit(0);
    }
    
    //disable save, view xml
    protected void disableSave(){
        this.menuFileSave.setEnabled(false);
        this.menuViewXml.setEnabled(false);
        this.buttonSave.setEnabled(false);
    }
    
    // enable save, view xml
    protected void enableSave(){
        this.menuFileSave.setEnabled(true);
        this.menuViewXml.setEnabled(true);
        this.buttonSave.setEnabled(true);
    }
    
    //centers the window in the middle of the screen
     protected void centerWindow(){
          Dimension screenDimension=Toolkit.getDefaultToolkit().getScreenSize();
          Rectangle windowDimension=this.getBounds();
          this.setLocation((screenDimension.width-windowDimension.width)/2,(screenDimension.height-windowDimension.height)/2);
     }
     
     //opens an xml file in the default web browser
     protected void viewXml(String path){
         String filePath="file:///"+path;
         String result=(new LaunchBrowser()).launch(filePath);
         if(result.length()>0){
             JOptionPane.showMessageDialog(this,result,"Error",JOptionPane.ERROR_MESSAGE);
         }
     }
     
     //open the Tiamat home page in the default browser
     protected void onHelpHomepage(){
         String result=(new LaunchBrowser()).launch("http://www.huguesjohnson.com/tiamat.html");
         if(result.length()>0){
             JOptionPane.showMessageDialog(this,result,"Error",JOptionPane.ERROR_MESSAGE);
         }
     }
     
     //open the Tiamat editor help in the default browser
     protected void onHelpEditors(){
         String result=(new LaunchBrowser()).launch("http://huguesjohnson.com/tiamat_docs/help/java-editors.html");
         if(result.length()>0){
             JOptionPane.showMessageDialog(this,result,"Error",JOptionPane.ERROR_MESSAGE);
         }
     }
     
     //add an entry to the recent file list
     protected void addRecentFile(String filePath){
    	 this.menuRecent.addEntry(filePath);
     }
     
     //return the content pane
     public JPanel getContentPanel(){
    	 return(this.panelContent);
     }
     
	/* end concrete methods common to all editors */

	/* begin abstract methods common to all editors */
    public abstract void onSave(); //changed to public for adventureStudio
    protected abstract void onSaveAs();
    protected abstract void onNew();
    protected abstract void onOpen();
    protected abstract void onRecentFile(String filePath);
    public abstract void onViewXml(); //changed to public for adventureStudio
    protected abstract void initComponents();
	/* end abstract methods common to all editors */
}