/* --------------------------------------------------------------------

SearchTreePanel class

Copyright (c) 1996 by Christopher R. Waterson. All Rights Reserved.
Permission to use, copy, modify, and distribute this software
and its documentation for NON-COMMERCIAL purposes and without
fee is hereby granted provided that this copyright notice
appears in all copies.

File:		SearchTreePanel.java
Synopsis:	A tree "Canvas" that provides UI
Author:		Christopher R. Waterson <waterson@eecs.umich.edu>
Created:	18 Feb 1996
Updated:	Christopher R. Waterson <waterson@eecs.umich.edu>
Last Modified:	22 Oct 1996

-------------------------------------------------------------------- */

package eightpuzzle;
import java.awt.*;


/**
 * Implements a control panel for a displaying a dynamic search tree
 * and allows you to interact with it.
 */

class SearchTreePanel
extends java.awt.Panel {

	SearchTreeCanvas m_canvas;
	Scrollbar m_hScrollbar;
	Scrollbar m_vScrollbar;
	Button m_zoomInButton;
	Button m_zoomOutButton;


	/**
	 * Create a new search tree panel
	 */

	public SearchTreePanel() {
		setLayout(new BorderLayout());

		// Create the basic components
		m_zoomInButton = new Button("+");
		m_zoomOutButton = new Button("-");
		m_canvas = new SearchTreeCanvas();
		m_hScrollbar = new Scrollbar(Scrollbar.HORIZONTAL);
		m_vScrollbar = new Scrollbar(Scrollbar.VERTICAL);

		// Create a toolbar for the buttons
		Panel toolbarPanel = new Panel();
		toolbarPanel.setLayout(new GridLayout(1, 10));
		toolbarPanel.add(m_zoomInButton);
		toolbarPanel.add(m_zoomOutButton);

		// Lay everything out
		add("North", toolbarPanel);
		add("Center", m_canvas);

		// I'm not sure how to do scrollbars yet.
		// add("South", m_hScrollbar);
		// add("East", m_vScrollbar);
	}


	/**
	 * Return the preferred size for the canvas
	 */

	public Dimension
	preferredSize() {
		return minimumSize();
	}


	/**
	 * Return the minimum allowable size for the canvas
	 */

	public Dimension
	minimumSize() {
		Dimension d = m_canvas.preferredSize();
		return new Dimension(d.width + 10, d.height + 20);
	}


	/**
	 * Add a new node to the panel
	 */

	public void
	add(PuzzleNode node) {
		m_canvas.Add(node);
	}


	/**
	 * Set the root of the search tree
	 */

	public void
	setRoot(PuzzleNode node) {
		m_canvas.SetRoot(node);
	}


	/**
	 * Handle user input
	 */

	public boolean
	action(Event event, Object arg) {
		if (event.target == m_zoomInButton) {

			m_canvas.Zoom(2.0);

		} else if (event.target == m_zoomOutButton) {

			m_canvas.Zoom(0.5);

		} else if (event.target == m_hScrollbar
				|| event.target == m_vScrollbar) {

			m_canvas.SetOrigin(m_hScrollbar.getValue(), m_vScrollbar.getValue());
		}

		return true;
	}




	/**
	 * Deal with scrollbars
	 */

	public void
	updateScrollbars(
			int xOrigin, int xMin, int xMax, int width,
			int yOrigin, int yMin, int yMax, int height) {
		// m_hScrollbar.setValues(xOrigin, width, xMin, xMax);
		// m_vScrollbar.setValues(yOrigin, height, yMin, yMax);
	}
}




