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

// $Id: GroupListDraw.java,v 1.2 2004/02/25 17:24:17 cadlerun Exp $
// GroupListDraw.java -- 
// Author: Aaron Adler <cadlerun@fracas.ai.mit.edu> 
// Copyright: Copyright (C) 2003 by MIT.  All rights reserved.
// Created: <Tue Apr  8 10:28:18 2003> 

package rec.multi;

import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.tree.TreePath;
import javax.swing.JButton;
import javax.swing.AbstractButton;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import java.util.Iterator;
import java.util.Set;

import edu.mit.tools.watch.display.GroupList;
import edu.mit.tools.watch.timeline.TimelineDataElement;
import edu.mit.tools.watch.Logging;

import org.apache.log4j.Logger;

public class GroupListDraw extends JPanel implements ActionListener {
  private static Logger LOG = Logging.GROUP;

  private DefaultMutableTreeNode m_top;
  private JButton m_identicalButton;
  private JButton m_eqSpaceButton;
  private JButton m_touchButton;
  private JButton m_speechButton1;
  private JButton m_speechButton2;
  private JButton m_speechButton3;
  private JButton m_speechButton4;
  private JButton m_speechButton5;
  private JButton m_speechButton6;
  private JButton m_speechButton7;
  
  private JTree m_tree;
  private SpeechAnalyzer m_sa;

  /***
   * This class shows a list of the events in the timeline by the group
   * that they are in. A group is a set of events between two breaks
   * There are also buttons to introduce speech events without needing
   * to actually have to talk to allow for testing.
   * This will draw the group list info in a JPanel
   ***/
  
  public GroupListDraw(Iterator eventGroupIterator, SpeechAnalyzer sa) {
    super();
    m_sa = sa;
    m_top = new DefaultMutableTreeNode("Groups of Events");

    // add nodes
    int g = 0;
    while (eventGroupIterator.hasNext()) {
      g++;
      Iterator i = ((Set)eventGroupIterator.next()).iterator();
      GroupList gl = new GroupList("Group " +g);
      DefaultMutableTreeNode group = new DefaultMutableTreeNode(gl);
      while (i.hasNext()) {
	TimelineDataElement tde = (TimelineDataElement)i.next();
	DefaultMutableTreeNode event = new DefaultMutableTreeNode(tde);
	gl.add(tde);
	LOG.debug("adding "+tde.toString()+" to group "+g);
	group.add(event);
      }
      m_top.add(group);
    }

    m_tree = new JTree(m_top);
    //m_tree.setRootVisible(false);
    m_tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

    JScrollPane scroll = new JScrollPane(m_tree);
    scroll.setPreferredSize(new Dimension(350, 350));
    scroll.setBackground(Color.white);

    m_identicalButton = new JButton("Identical");
    m_identicalButton.setVerticalTextPosition(AbstractButton.CENTER);
    m_identicalButton.setHorizontalTextPosition(AbstractButton.LEADING);
    m_identicalButton.setActionCommand("identical");
    m_identicalButton.addActionListener(this);
    
    m_eqSpaceButton = new JButton("Equally Space");
    m_eqSpaceButton.setVerticalTextPosition(AbstractButton.CENTER);
    m_eqSpaceButton.setHorizontalTextPosition(AbstractButton.LEADING);
    m_eqSpaceButton.setActionCommand("equally spaced");
    m_eqSpaceButton.addActionListener(this);
    
    m_touchButton = new JButton("Touch");
    m_touchButton.setVerticalTextPosition(AbstractButton.CENTER);
    m_touchButton.setHorizontalTextPosition(AbstractButton.LEADING);
    m_touchButton.setActionCommand("touch");
    m_touchButton.addActionListener(this);
    
    m_speechButton1 = new JButton("speech -- identical");
    m_speechButton1.setVerticalTextPosition(AbstractButton.CENTER);
    m_speechButton1.setHorizontalTextPosition(AbstractButton.LEADING);
    m_speechButton1.setActionCommand("speech-identical");
    m_speechButton1.addActionListener(this);
    
    m_speechButton2 = new JButton("speech -- identical equal");
    m_speechButton2.setVerticalTextPosition(AbstractButton.CENTER);
    m_speechButton2.setHorizontalTextPosition(AbstractButton.LEADING);
    m_speechButton2.setActionCommand("speech-identical equal");
    m_speechButton2.addActionListener(this);
    
    m_speechButton3 = new JButton("speech -- identical touch");
    m_speechButton3.setVerticalTextPosition(AbstractButton.CENTER);
    m_speechButton3.setHorizontalTextPosition(AbstractButton.LEADING);
    m_speechButton3.setActionCommand("speech-identical touch");
    m_speechButton3.addActionListener(this);
    
    m_speechButton4 = new JButton("speech -- equal");
    m_speechButton4.setVerticalTextPosition(AbstractButton.CENTER);
    m_speechButton4.setHorizontalTextPosition(AbstractButton.LEADING);
    m_speechButton4.setActionCommand("speech-equal");
    m_speechButton4.addActionListener(this);
    
    m_speechButton5 = new JButton("speech -- touch");
    m_speechButton5.setVerticalTextPosition(AbstractButton.CENTER);
    m_speechButton5.setHorizontalTextPosition(AbstractButton.LEADING);
    m_speechButton5.setActionCommand("speech-touch");
    m_speechButton5.addActionListener(this);
    
    m_speechButton6 = new JButton("2 wheel identical");
    m_speechButton6.setVerticalTextPosition(AbstractButton.CENTER);
    m_speechButton6.setHorizontalTextPosition(AbstractButton.LEADING);
    m_speechButton6.setActionCommand("2-wheel-id");
    m_speechButton6.addActionListener(this);
    
    m_speechButton7 = new JButton("3 wheel identical");
    m_speechButton7.setVerticalTextPosition(AbstractButton.CENTER);
    m_speechButton7.setHorizontalTextPosition(AbstractButton.LEADING);
    m_speechButton7.setActionCommand("3-wheel-id");
    m_speechButton7.addActionListener(this);
    
    JPanel buttons = new JPanel();
    buttons.add(m_identicalButton);
    buttons.add(m_eqSpaceButton);
    buttons.add(m_touchButton);
    buttons.add(m_speechButton1);
    buttons.add(m_speechButton2);
    buttons.add(m_speechButton3);
    buttons.add(m_speechButton4);
    buttons.add(m_speechButton5);
    buttons.add(m_speechButton6);
    buttons.add(m_speechButton7);
    
    setLayout(new BorderLayout());
    add(scroll, BorderLayout.CENTER);
    add(buttons, BorderLayout.SOUTH);

  }

  public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    LOG.debug("Command received: "+command);

    // speech testing
    if ("2-wheel-id".equals(e.getActionCommand())) {
      m_sa.insertWheelSpeechPlus("two identical wheels");
    }
    if ("3-wheel-id".equals(e.getActionCommand())) {
      m_sa.insertWheelSpeechPlus("three identical wheels");
    }    
    if ("speech-identical".equals(e.getActionCommand())) {
      m_sa.insertSpeechPlus("identical");
    }
    if ("speech-identical equal".equals(e.getActionCommand())) {
      m_sa.insertSpeechPlus("identical equally spaced");
    }
    if ("speech-identical touch".equals(e.getActionCommand())) {
      m_sa.insertSpeechPlus("identical touching");
    }
    if ("speech-equal".equals(e.getActionCommand())) {
      m_sa.insertSpeechPlus("equally spaced");
    }
    if ("speech-touch".equals(e.getActionCommand())) {
      m_sa.insertSpeechPlus("touching");
    }
    
    TreePath treePath = m_tree.getSelectionPath();
    if ((treePath != null) && (treePath.getPathCount() > 1)) {
      // a group is selected -- get the group
      DefaultMutableTreeNode node = (DefaultMutableTreeNode)treePath.getPath()[1];
      LOG.debug("Node "+node.toString());
      GroupList gl = (GroupList) node.getUserObject();

      if ("identical".equals(e.getActionCommand())) {
	m_sa.makeWidgetsIdentical(gl);
      }
      if ("equally spaced".equals(e.getActionCommand())) {
	m_sa.makeWidgetsEqSpaced(gl);
      }
      if ("touch".equals(e.getActionCommand())) {
	m_sa.makeWidgetsTouch(gl);
      }
    }
  }
  
}
