/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  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 3 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, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.application;

import javax.swing.JInternalFrame;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.JMenu;
import javax.swing.ImageIcon;

import com.approximatrix.draco.application.icons.VectorIcon;
import com.approximatrix.draco.main.ui.MainWindow;

/** Class that defines all internal Draco frames.  Each draco frame must
 * store a list of toolbar buttons and menu items.  The main window handles
 * changing menus and toolbars based on the get*() functions.
 * 
 * @author armstrongj
 *
 */
public abstract class ChildFrame extends JInternalFrame {

	/** Flag indicating custom window icons should be used */
	private static final boolean USE_PER_WINDOW_ICON = false;
	
	/** Default icon key */
	private static final String DEFAULT_WIN_ICON = "DEFAULTWIN";
	
	/** The icon to be displayed by default for all child windows */
	private static final String PATH_TO_ICON = "/com/approximatrix/draco/main/ui/images/icons/draco.png";
	
	/** The buffered VectorIcon for all windows by default */
	private static VectorIcon default_icon = null;
	
	/** Toolbar buttons specific to this object to be shown on the
	 * main window toolbar
	 */
	protected Component[] toolbarButtons = null;
	
	/** Menus and items specific to this object to be shown on the
	 * main window toolbar
	 */
	protected JMenu[] menuItems = null;
	
	/**
	 * 
	 */
	public ChildFrame() {
		super();
		this.setIcon(PATH_TO_ICON);
	}

	/**
	 * @param title
	 * @param resizable
	 * @param closable
	 * @param maximizable
	 * @param iconifiable
	 */
	public ChildFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable) {
		super(title, resizable, closable, maximizable, iconifiable);
		this.setIcon(PATH_TO_ICON);
	}

	/**
	 * @param title
	 * @param resizable
	 * @param closable
	 * @param maximizable
	 */
	public ChildFrame(String title, boolean resizable, boolean closable, boolean maximizable) {
		super(title, resizable, closable, maximizable);
		this.setIcon(PATH_TO_ICON);
	}

	/**
	 * @param title
	 * @param resizable
	 * @param closable
	 */
	public ChildFrame(String title, boolean resizable, boolean closable) {
		super(title, resizable, closable);
		this.setIcon(PATH_TO_ICON);
	}

	/**
	 * @param title
	 * @param resizable
	 */
	public ChildFrame(String title, boolean resizable) {
		super(title, resizable);
		this.setIcon(PATH_TO_ICON);
	}

	/**
	 * @param title
	 */
	public ChildFrame(String title) {
		super(title);
		this.setIcon(PATH_TO_ICON);
	}
	
	/** Returns an array of toolbar buttons to be added to the main window
	 * toolbar, or null if there are no applicable buttons
	 * 
	 * @return an array of JButtons, or null if none are present
	 */
	public abstract Component[] getToolbarButtons();
	
	/** Returns an array of menus with corresponding menu items to be
	 * shown in the main window menu.  The main window should handle
	 * all integration of these menu items with constant menu items.
	 *  
	 * @return an array of JMenu[], or null if none are present
	 */
	public abstract JMenu[] getMenuItems();
	
	/** Returns true if this child can be renamed.
	 * 
	 * @return true if renaming is allowed, false otherwise
	 */
	public abstract boolean isRenameable();
	
	/** Returns the MainWindow object that is controlling and containing this
	 * JInternalFrame object
	 * 
	 * @return the MainWindow that encloses this internal frame
	 */
	protected MainWindow getMainWindow() {
		return (MainWindow)this.getDesktopPane().getTopLevelAncestor();
	}
	
	/** Returns the string used to request topical help for this window
	 * 
	 * @return a String to be passed to the help system
	 */
	public abstract String getHelpKey();
	
	/** Sets the icon for this child window based on the passed string
	 * 
	 * @param resource the path to the resource containing the icon image
	 */
	protected void setIcon(String resource) {
		if(! USE_PER_WINDOW_ICON) {
			if(default_icon == null) {
				int w = this.getFrameIcon().getIconWidth();
				int h = this.getFrameIcon().getIconHeight();
				try {
					default_icon = ApplicationManager.GetIcon(DEFAULT_WIN_ICON);
					default_icon.setDimension(new Dimension(w,h));
					default_icon.setFixedSize(true);
				} catch(NullPointerException npe) {
					return;
				}
			}
			this.setFrameIcon(default_icon);
			return;
		}
		
		ImageIcon imageIcon;
		JInternalFrame.JDesktopIcon jDeskTopIcon;
		
		imageIcon=new ImageIcon(this.getClass().getResource(resource));
		if(imageIcon == null) return;
		
		super.setFrameIcon(imageIcon);
		jDeskTopIcon=new JInternalFrame.JDesktopIcon(this);
		this.setDesktopIcon(jDeskTopIcon); 
	}
}

