// (c) MIT 2003.  All rights reserved.

// $Id: DynamicDrawTimeline.java,v 1.1 2003/06/03 15:37:13 cadlerun Exp $
// DrawTimeline.java -- 
// Author: Aaron Adler <cadlerun@fracas.ai.mit.edu> 
// Copyright: Copyright (C) 2003 by MIT.  All rights reserved.
// Created: <Thu Sep 19 12:14:55 2002> 


// This class will deal with the actual drawing of the timeline
// including the Axis, the Key and the text box and the scroll panel

package rec.multi;

import edu.mit.tools.watch.display.ScrollableTimeline;
import edu.mit.tools.watch.display.Key;
import edu.mit.tools.watch.display.Axis;
import edu.mit.tools.watch.display.Corner;
import edu.mit.tools.watch.display.TimelineDisplayElement;
import edu.mit.tools.watch.display.TimelineDisplay;

import javax.swing.JPanel;
import javax.swing.JTextArea; 
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.BorderFactory;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.BorderLayout; 
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputAdapter;
import java.util.Iterator;

public class DynamicDrawTimeline extends JPanel {
  private JTextArea m_elementDescription;
  private boolean m_enabled = true;
  private JScrollPane m_scroller;
  private TimelineDisplay m_timelineDisplay;
  
  public DynamicDrawTimeline(TimelineDisplay td) {
    
    setOpaque(true);    

    m_timelineDisplay = td;
    
    //Set up the drawing area.
    ScrollableTimeline st = new ScrollableTimeline(5, td);
    st.setBackground(Color.white);
    st.addMouseListener(new MouseClickListener());

    // Set up the Key
    Key key = td.getKey();
    key.setBackground(Color.white);

    // Set up the Axis
    Axis axis = td.getAxis();
    axis.setPreferredWidth((int)td.getTimelineSize().getWidth());
    st.setMaxUnitIncrement(axis.getIncrement());

    //Set up the text box for the Information about a element
    m_elementDescription = new JTextArea(10, 15);
    m_elementDescription.setEditable(false);
    m_elementDescription.setLineWrap(true);
    m_elementDescription.setWrapStyleWord(true);

    //Put the drawing area in a scroll pane.
    m_scroller = new JScrollPane(st);
    m_scroller.setPreferredSize(new Dimension(350, 350));
    m_scroller.setViewportBorder(BorderFactory.createLineBorder(Color.black));
    m_scroller.setColumnHeaderView(axis);
    m_scroller.setRowHeaderView(key);
    m_scroller.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new Corner());
    m_scroller.setBackground(Color.white);

    //Layout this demo.
    setLayout(new BorderLayout());
    add(m_scroller, BorderLayout.CENTER);
    add(m_elementDescription, BorderLayout.SOUTH);
  }

  /**
   * returns the corner (as a point) of the scollable part of the timeline
   * used to keep the timeline view the same
   **/
  public Point getViewCorner() {
    return m_scroller.getViewport().getViewPosition();
  }

  /**
   * used to set the view of the scrollable part of the timeline
   **/
  public void setViewCorner(Point oldView) {
    m_scroller.getViewport().setViewPosition(oldView);
  }
  
  /**
   * returns the size (as Dimension) of the scollable part of the timeline
   * used to keep the timeline view the same
   **/
  public Dimension getSize() {
    return m_scroller.getSize(); 
  }

  /**
   * used to set the size of the scrollable part of the timeline
   **/
  public void setSize (Dimension oldSize) {
    // need to set preffered size or else it gets ignored. 
    m_scroller.setPreferredSize(oldSize);
  }
    
  class MouseClickListener extends MouseInputAdapter {

    public void mouseReleased(MouseEvent e) {

      if (SwingUtilities.isLeftMouseButton(e)) {
	//On a left click, we want to update the text
	//displayed based on where the use clicked.
	int x = e.getX();
	int y = e.getY();

	Iterator  i = m_timelineDisplay.getDisplayElements();
	boolean found = false;
	TimelineDisplayElement tde = null;
	
	while (i.hasNext() && (!found)) {
	  // look to see if the user clicked in a rectangle and
	  // if so, which one
	  tde = (TimelineDisplayElement)i.next();
	  found = tde.getDisplayObject().contains(x, y);
	}

	if (!found) {
	  int x_diff = 1;

	  while  ((x_diff < 5) && (!found)) {
	    i = m_timelineDisplay.getDisplayElements();

	    while (i.hasNext() && (!found)) {
	      // look to see if the user clicked in a rectangle and
	      // if so, which one - move x to see if they click close by
	      tde = (TimelineDisplayElement)i.next();
	      found = tde.getDisplayObject().contains((x+x_diff), y);
	      if (!found)
		found = tde.getDisplayObject().contains((x-x_diff), y);
	    }

	    x_diff++;
	  }
	}

	if (found) {
	  // found it
	  // update the text field
	  m_elementDescription.setText(tde.getText());

	}
	else {
	  // we didn't find it even with the fudge factor
	  // clear the text window
	  m_elementDescription.setText("");
	}
      }
    }
  }
  
}
