package SwingMan;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

/**
 * <p>Title: CSC221 GUI building Package</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: UNCW</p>
 * @author Sridhar Narayan
 * @version 2.0
 */

public abstract class BasicGUITool
	extends JFrame
	implements ActionListener, MouseListener, MouseMotionListener, Serializable {
	
	protected Container contentPane;
		
	public BasicGUITool() {
		contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		setTitle("GUccI 1.0");
		setBounds(10, 10, 600, 600);

		toolPanel = new JPanel();
		toolPanel.setPreferredSize(new Dimension(100,600));
		toolPanel.setBackground(Color.green);
		toolPanel.add(new JLabel("Tool Panel",JLabel.CENTER));

		drawPanel = new JPanel();
		drawPanel.setLayout(null);
		drawPanel.setPreferredSize(new Dimension(500,600));
		drawPanel.setBackground(Color.white);

		// any GUI can hold a maximum of 100 SwingThings
		figures = new SwingThing[100];

		// number of SwingThings currently in the drawing
		numberOfShapes = 0;

		contentPane.add(drawPanel, BorderLayout.CENTER);

		contentPane.add(toolPanel, BorderLayout.EAST);
		
		statusBar = createStatusBar();
		
		contentPane.add(statusBar, BorderLayout.SOUTH);
		setVisible(true);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	protected int numberOfShapes; // in this GUI
	protected SwingThing[] figures;
	// array to hold all SwingThings in this GUI
	
	protected JPanel toolPanel, drawPanel;
	private JPanel statusBar;
	protected JLabel statusLabel;


	// read figures from specified ObjectInputStream into figures array
	protected void readFiguresFromFile(ObjectInputStream inStream) {

		try {
			// how many SwingThings were in the saved GUI ?
			numberOfShapes = inStream.readInt();

			for (int i = 0; i < numberOfShapes; i++) {
				Object o = inStream.readObject();
				if (o instanceof SwingThing)
					figures[i] = (SwingThing) o;
				else
					throw new ClassNotFoundException();
			}

			inStream.close();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.exit(0);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	// save objects in figures array to a file
	protected abstract void saveFiguresToFile(ObjectOutputStream outStream);

    // export Java source for building the GUI to a file
    protected abstract void exportJavaCode(File javaSourceFile);
    
	// refresh the displayed figures
	protected abstract void refresh();

	// erase all the displayed figures
	protected void erase() {
		for (int i = figures.length-1; i > 0; i--)
			figures[i].erase();
		refresh();
	}

	// Null implementations for event handlers; override as needed
	// ActionListener
	public void actionPerformed(ActionEvent e) {
	}

	//MouseListener
	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {

	}

	public void mouseReleased(MouseEvent e) {

	}

	//MouseMotionListener
	public void mouseDragged(MouseEvent e) {

	}
	public void mouseMoved(MouseEvent e) {

	}
	private JPanel createStatusBar() {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		statusLabel = new JLabel("None");
		panel.add(statusLabel, BorderLayout.CENTER);

		panel.setBorder(new BevelBorder(BevelBorder.LOWERED));
				
		return panel;
	}
	
}