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

// -*-Mode: JDE-*- 
// $Id: TreeCausalPane.java,v 1.3 2003/03/06 00:40:49 moltmans Exp $
// TreeCausalPane.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Created: <Mon Jun 12 15:34:30 2000> 
// Time-stamp: <00/07/17 23:56:39 moltmans> 
package rec.annotate.gui;

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

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

public class TreeCausalPane extends JPanel
{
  private Notebook m_notebook;
  private DefaultMutableTreeNode m_top;
  private DefaultTreeModel m_treeModel;
  /**
   * Top level nodes are disconected causal chains.
   * Next level are canonical and sub evnts.
   * What do we do if chain forks? i.e. operating modes...
   **/
  public TreeCausalPane( Notebook book )
  {
    m_notebook = book;

    //Create the nodes.
    m_top = new DefaultMutableTreeNode( "Causal Structure" );
    createNodes( );
    m_treeModel = new DefaultTreeModel( m_top );

    //Create a tree that allows one selection at a time.
    JTree tree = new JTree( m_treeModel );

    tree.getSelectionModel().setSelectionMode
      ( TreeSelectionModel.SINGLE_TREE_SELECTION );
    tree.putClientProperty("JTree.lineStyle", "Angled");
    
    //Create the scroll pane and add the tree to it. 
    JScrollPane treeView = new JScrollPane( tree );

    setLayout( new BorderLayout() );
    add( treeView, BorderLayout.CENTER );
  }

  public void updateNotes() 
  {
    m_top.removeAllChildren();
    m_treeModel.reload( m_top );
    createNodes();
  }


  public DefaultMutableTreeNode addObject( DefaultMutableTreeNode parent,
					   Object child )
  {
    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode( child );

    if (parent == null) {
      parent = m_top;
    }

    m_treeModel.insertNodeInto( childNode, parent, 
				parent.getChildCount() );

    return childNode;
  }

  private void createNodes()
  {
    DefaultMutableTreeNode chainNode = null;
    CausalStructure causes = m_notebook.getCausalStructure();

    Iterator chains = causes.getChains().iterator();

    int count = 1;
    while( chains.hasNext() ) {
      CanonicalEvent event = (CanonicalEvent)chains.next();
      chainNode = addObject( m_top, "Chain " + count++ );
      addChainSubNodes( chainNode, event );
    }
  }

  private void addChainSubNodes( DefaultMutableTreeNode parent,
				 CanonicalEvent cause )
  {
    DefaultMutableTreeNode eventNode = null;
    DefaultMutableTreeNode canonicalNode = null;

    Iterator it = cause.getEffects().iterator();
    while( it.hasNext() ) {
      CanonicalEvent event = (CanonicalEvent)it.next();
      eventNode = addObject( parent, event.toEnglish() );
      //      canonicalNode = addObject( eventNode, "Canonical events" );
      //      addCanonicalSubNodes( canonicalNode, event );
      addCanonicalSubNodes( eventNode, event );
    }
  }

  private void addCanonicalSubNodes( DefaultMutableTreeNode parent,
				     CanonicalEvent event )
  {
    Iterator it = event.getEvents().iterator();
    while( it.hasNext() ) {
      Event evt = (Event)it.next();
      addObject( parent, evt.toEnglish() );
    }
  }
}


