/*
 *  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.plot.ui;

import javax.swing.JButton;
import javax.swing.JMenu;


import java.awt.datatransfer.Clipboard;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Toolkit;

import java.util.ArrayList;


import com.approximatrix.charting.event.*;
import com.approximatrix.draco.application.ApplicationManager;
import com.approximatrix.draco.application.ChildFrame;
import com.approximatrix.draco.main.ui.MainWindow;
import com.approximatrix.draco.io.ImageSupport;
import com.approximatrix.draco.io.PrintSupport;
import com.approximatrix.draco.io.clipboard.ClipboardImage;


/*
 * Created on Jan 26, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author armstrongj
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PlotDialog extends ChildFrame implements  
      ActionListener, ChartDataModelListener {

	private static final String DIALOG_NAME = "Plot";
	
	/** The string for requesting topical help */
	private static final String HELP_KEY = "plotwindow";
	
	/** The icon to be displayed for this child window */
	private static final String PATH_TO_ICON = "/com/approximatrix/draco/main/ui/images/plotdata.png";
	
	private javax.swing.JPanel plotContentPane = null;

	private GenericPlotInterface plotter = null;
	
	private JButton plot_props_button = null;
	
	private JButton saveas_button = null;
	private JButton print_button = null;
	private JButton copy_button = null;
	
	/**
	 * This is the default constructor
	 */
	public PlotDialog() {
		super();
		initialize();
	}
	
	public PlotDialog(GenericPlotInterface plotter) {
		super(DIALOG_NAME, 
				true, //resizable
				true, //closable
				true, //maximizable
				true);
		
		this.plotter = plotter;
		
		initialize();
		this.setTitle(DIALOG_NAME);
	}
	
	public PlotDialog(String title, GenericPlotInterface plotter) {
		super(title, 
				true, //resizable
				true, //closable
				true, //maximizable
				true);
		
		this.plotter = plotter;
		
		initialize();
		this.setTitle(title);
	}
	
	public void chartDataChanged(ChartDataModelEvent evt) {
		// The DataModel changed -> update display
		plotter.getPlotPanel().revalidate();
		repaint();
	}    
	
	public void actionPerformed(ActionEvent evt) {
		// The Timer generated an event -> update DataModel with random data
		//data.insertValue(0, new Double(Math.random() * 10.0 + 20.0), new Double(time));
		//data.insertValue(1, new Double(Math.random() * 7.0 + 10.0), new Double(time));
		//time++;
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(500, 500);
		this.setContentPane(getPlotContentPane());
		//this.setVisible(true);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		//this.addWindowListener(new java.awt.event.WindowAdapter() { 
		//	public void windowClosing(java.awt.event.WindowEvent e) {    
		//		close_window();
		//	}
		//});
		this.getContentPane().add(plotter.getPlotPanel(), BorderLayout.CENTER);
		
		// Allow dynamic zooming
		//this.plotter.getPlotPanel().enableZoom(true);
		
		// Icon
		super.setIcon(PATH_TO_ICON);
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getPlotContentPane() {
		if(plotContentPane == null) {
			plotContentPane = new javax.swing.JPanel();
			plotContentPane.setLayout(new java.awt.BorderLayout());
		}
		return plotContentPane;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getPlot_props_button() {
		if (plot_props_button == null) {
			plot_props_button = new JButton();
			//plot_props_button.setText("Plot Properties...");
			plot_props_button.setToolTipText("Configure Plot Properties...");
			plot_props_button.setIcon(ApplicationManager.GetIcon("OPTIONS"));
			plot_props_button.addMouseListener(new java.awt.event.MouseAdapter() { 
				public void mouseClicked(java.awt.event.MouseEvent e) {    
					plotter.setPlotProperties(PlotDialog.this);
				}
			});
		}
		return plot_props_button;
	}
	
	/** Returns the plot from the dialog
	 * 
	 * @return the plotter associated with the window
	 */
	public GenericPlotInterface getPlotter() {
		return plotter;
	}
	
	private JButton getSaveas_button() {
		if (saveas_button == null) {
			saveas_button = new JButton();
			saveas_button.setToolTipText("Save an image of this plot...");
			saveas_button.setIcon(ApplicationManager.GetIcon("SAVEAS"));
			saveas_button.setBackground(java.awt.Color.YELLOW);
			saveas_button.addMouseListener(new java.awt.event.MouseAdapter() { 
				public void mouseClicked(java.awt.event.MouseEvent e) {    
					
					// Disable buffering for the save
					plotter.getPlotPanel().setBuffering(false);
					
					ImageSupport.RequestSave(plotter.getPlotPanel());
					
					// Reenable buffering
					plotter.getPlotPanel().setBuffering(true);
					
				}
			});
		}
		return saveas_button;
	}
	
	private JButton getPrint_button() {
		if (print_button == null) {
			print_button = new JButton();
			print_button.setToolTipText("Print this plot...");
			print_button.setIcon(ApplicationManager.GetIcon("PRINT"));
			print_button.addMouseListener(new java.awt.event.MouseAdapter() { 
				public void mouseClicked(java.awt.event.MouseEvent e) {    
					PrintSupport.RequestPrint(plotter.getPlotPanel());
				}
			});
		}
		return print_button;
	}
	
	/** Returns the Copy button on the toolbar
	 * 
	 * @return the toolbar button for copying images to the clipboard
	 */
	private JButton getCopy_button() {
		if (copy_button == null) {
			copy_button = new JButton();
			copy_button.setToolTipText("Copy to clipboard...");
			copy_button.setIcon(ApplicationManager.GetIcon("COPY"));
			copy_button.addMouseListener(new java.awt.event.MouseAdapter() { 
				public void mouseClicked(java.awt.event.MouseEvent e) {    
					copyPlot();
				}
			});
		}
		return copy_button;
	}
	
	/** Copies the current plot to the clipboard
	 * 
	 */
	private void copyPlot() {
		BufferedImage image = ImageSupport.generateImage(plotter.getPlotPanel());
		
		Clipboard myClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		ClipboardImage to_clip = new ClipboardImage(image);
		
		myClipboard.setContents(to_clip,null);
		
	}

	/** For now, just institute the toolbar buttons
	 * 
	 * @return the menu items associated with the plot dialog
	 */
	public JMenu[] getMenuItems() {
		return null;
	}

	/** Returns the toolbar buttons for the plot dialog
	 * 
	 * @return an array of toolbar items
	 */
	public Component[] getToolbarButtons() {
		if(toolbarButtons == null) {
			ArrayList<Component> buttons = new ArrayList<Component>();
			
			buttons.add(getSaveas_button());
			buttons.add(getCopy_button());
			buttons.add(getPrint_button());
			buttons.add(new javax.swing.JToolBar.Separator());
			buttons.add(getPlot_props_button());
			
			toolbarButtons = new Component[buttons.size()];
			buttons.toArray(toolbarButtons);
		}
		return toolbarButtons;
	}

	/** Returns true, meaning that all plot dialogs can be renamed
	 * 
	 * @return true
	 */
	public boolean isRenameable() {
		return true;
	}

	/** Plot dialogs must have the getMainWindow() method public so that
	 * any dialogs generated (specifically, the properties dialog) can
	 * be made modal with respect to the main window.
	 * 
	 * @return the main window that encloses this ChildFrame
	 */
	public MainWindow getMainWindow() {
		return (MainWindow)this.getDesktopPane().getTopLevelAncestor();
	}
	
	/** Returns the help key for the Plot window
	 * 
	 * @return the proper help key, defined by HELP_KEY
	 * @see #HELP_KEY
	 * @see com.approximatrix.draco.application.ChildFrame#getHelpKey()
	 */
	public String getHelpKey() {
		return HELP_KEY;
	}
	
}


