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

// $Id: CausalPane.java,v 1.4 2003/03/06 00:40:49 moltmans Exp $
// CausalPane.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Wed Jul 12 19:32:01 2000> 
// Time-stamp: <2000-07-15 17:38:10 moltmans> 
package rec.annotate.gui;

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

import rec.util.*;
import rec.annotate.*;
import rec.annotate.rules.*;
import rec.annotate.facts.*;
import rec.tms.gui.*;

public class CausalPane extends JPanel
{
  private Notebook m_notebook;
  private JList m_causeList;
  private CanonicalEventPane m_eventPane;

  public CausalPane( Notebook book )
  {
    m_notebook = book;

    m_eventPane = new CanonicalEventPane();
    m_causeList = new JList();
    m_causeList.setVisibleRowCount( 3 );
    ListSelectionListener listener = new ListSelectionListener() {
	public void valueChanged( ListSelectionEvent e ) 
	{
	  updateSelection();
	}
      };
    m_causeList.addListSelectionListener( listener );
    
    updateCauseList();
    JScrollPane eventListPane = new JScrollPane( m_causeList );
    JSplitPane split = new JSplitPane( JSplitPane.VERTICAL_SPLIT,
				       eventListPane,
				       m_eventPane );
    setLayout( new BorderLayout() );
    add( split, BorderLayout.CENTER );
  }

  public void updateNotes()
  {
    updateCauseList();
  }

  private void updateSelection()
  {
    Object o = m_causeList.getSelectedValue();
    if( o != null ) {
      CanonicalEvent evt = (CanonicalEvent)o;
      m_eventPane.setEvent( evt );
    }
  }
  
  private void updateCauseList()
  {
    CausalStructure causes = m_notebook.getCausalStructure();
    m_causeList.setListData( new Vector( causes.getCanonicalEvents() ) );

    
    
// simple version for now add this latter
    
//      ArrayList list = new ArrayList();

//      LinkedList queue = new LinkedList( causes.getChains() );

//      while( !queue.isEmpty() ) {
//        CanonicalEvent event = (CanonicalEvent)queue.removeFirst();
//        if( !list.contains( event ) ) {
//  	list.add( event );
//  	queue.addAll( 0, event.getEffects() );
//        }
//      }
//      m_causeList.setListData( list.toArray() );
  }

}
