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

/***
 * This class is used to create a menu and manage the menu
 * options. Specifically the QuickTime menu can not be
 * accessed unless QuickTime is supported on the user's
 * platform
 */
 
package rec.multi;

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JFileChooser;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.KeyStroke;

import java.io.File;

/*
 * This class is the menu bar for the timeline
 */

public class DynamicTimelineMenu extends JMenuBar implements ActionListener {

  private JMenuBar m_menuBar;
  private JMenuItem m_loadMenuItem, m_quitMenuItem, m_aboutMenuItem; // m_movieMenuItem, 
  //private JCheckBoxMenuItem m_movieCheckBoxMenuItem, m_autoCheckBoxMenuItem, m_unrestrictedCheckBoxMenuItem;
  private JMenu m_fileMenu; //, m_quickTimeMenu, m_recentFiles;

  //private RecentFilesMenu m_rfm;
  private DynamicWatchController m_watchController;

  // to keep track of the last files opened for future file chooser windows
  private File m_loadFileRoot = null;
  private File m_loadMovieRoot = null;

  public DynamicTimelineMenu (DynamicWatchController dwc) {

    int key_modifier = ActionEvent.SHIFT_MASK; // The key to use for menu shortcuts
    // alt seems to have problems in unix and ctrl seems to have issues in windoz so shift seems safe
    
    // set the watch controller varaible... it is used for menu return calls
    m_watchController = dwc;
    
    //Create the menu bar.
    m_menuBar = new JMenuBar();

    //Create the recent files menu
    //m_rfm = new RecentFilesMenu(wc);
    //m_recentFiles = m_rfm.getMenu();
    
    //Build the file menu.
    m_fileMenu = new JMenu("File");
    m_fileMenu.setMnemonic(KeyEvent.VK_F);
    m_fileMenu.getAccessibleContext().setAccessibleDescription("Menu for file commands.");
    this.add(m_fileMenu);
    
    //a group of JMenuItems
    m_aboutMenuItem = new JMenuItem("About...", KeyEvent.VK_B);
    m_aboutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, key_modifier));
    m_aboutMenuItem.getAccessibleContext().setAccessibleDescription("About WATCH.");
    m_aboutMenuItem.addActionListener(this);
    m_fileMenu.add(m_aboutMenuItem);
    
    //m_loadMenuItem = new JMenuItem("Load data...", KeyEvent.VK_L);
    //m_loadMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, key_modifier));
    //m_loadMenuItem.getAccessibleContext().setAccessibleDescription("Load a file.");
    //m_loadMenuItem.addActionListener(this);
    //m_fileMenu.add(m_loadMenuItem);

    //m_fileMenu.add(m_recentFiles); 
      
    m_quitMenuItem = new JMenuItem("Quit", KeyEvent.VK_Q);
    m_quitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, key_modifier));
    m_quitMenuItem.getAccessibleContext().setAccessibleDescription("Quit.");
    m_quitMenuItem.addActionListener(this);
    m_fileMenu.add(m_quitMenuItem);

    
    //Build QuickTime menu in the menu bar.
    //m_quickTimeMenu = new JMenu("QuickTime");
    //m_quickTimeMenu.setMnemonic(KeyEvent.VK_K);
    //m_quickTimeMenu.getAccessibleContext().setAccessibleDescription("QuickTime options.");
    //this.add(m_quickTimeMenu);

    // add QuickTime menu items
    //m_movieMenuItem = new JMenuItem("Associate with a movie...", KeyEvent.VK_A);
    //m_movieMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, key_modifier));
    //m_movieMenuItem.getAccessibleContext().setAccessibleDescription("Associate a file with a QuickTime movie.");
    //m_movieMenuItem.addActionListener(this);
    //m_quickTimeMenu.add(m_movieMenuItem);

    // Run movies check box
    //m_movieCheckBoxMenuItem = new JCheckBoxMenuItem("Run QuickTime movies");
    //m_movieCheckBoxMenuItem.setMnemonic(KeyEvent.VK_R);
    //m_movieCheckBoxMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, key_modifier));
    //m_movieCheckBoxMenuItem.addItemListener(this);
    //m_quickTimeMenu.add(m_movieCheckBoxMenuItem);

    // Auto play check box
    //m_autoCheckBoxMenuItem = new JCheckBoxMenuItem("Automatically play QuickTime movies");
    //m_autoCheckBoxMenuItem.setMnemonic(KeyEvent.VK_P);
    //m_autoCheckBoxMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, key_modifier));
    //m_autoCheckBoxMenuItem.addItemListener(this);
    //m_quickTimeMenu.add(m_autoCheckBoxMenuItem);

    // Unrestricted play check box
    //m_unrestrictedCheckBoxMenuItem = new JCheckBoxMenuItem("Unrestricted play of QuickTime movies");
    //m_unrestrictedCheckBoxMenuItem.setMnemonic(KeyEvent.VK_U);
    //m_unrestrictedCheckBoxMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, key_modifier));
    //m_unrestrictedCheckBoxMenuItem.addItemListener(this);
    //m_quickTimeMenu.add(m_unrestrictedCheckBoxMenuItem);

  }

  public void actionPerformed(ActionEvent e) {
    JMenuItem l_source = (JMenuItem)(e.getSource());
    if (l_source == m_aboutMenuItem) {
      m_watchController.aboutTimeline();
    } else if (l_source == m_quitMenuItem) {
      m_watchController.quitTimeline();
    } //else if (l_source == m_loadMenuItem) {
    //  File file = getLoadFile();
    //  if (file != null) m_watchController.loadTimeline(file);
    //} else if (l_source == m_movieMenuItem) {
    //  File file = getMovieFile();
    //  if (file != null) m_watchController.loadMovie(file);
    //}
  }
  
  /*
  public void itemStateChanged(ItemEvent e) {
    JMenuItem l_source = (JMenuItem)(e.getSource());
    if (l_source == m_movieCheckBoxMenuItem) {
      m_watchController.playMovies((e.getStateChange() == ItemEvent.SELECTED));
    } else if (l_source == m_autoCheckBoxMenuItem) {
      m_watchController.autoPlayMovies((e.getStateChange() == ItemEvent.SELECTED));
    } if (l_source == m_unrestrictedCheckBoxMenuItem) {
      m_watchController.unrestrictedPlayMovies((e.getStateChange() == ItemEvent.SELECTED));
    }
  }
  */
  
  // Returns just the class name -- no package info.
  protected String getClassName(Object o) {
    String classString = o.getClass().getName();
    int dotIndex = classString.lastIndexOf(".");
    return classString.substring(dotIndex+1);
  }

  /*
  public void setQuickTimeEnabled(boolean enable) {
    // enable or disable the QuickTime menu
    m_quickTimeMenu.setEnabled(enable);   
  }
  
  public void setQuickTimeExtrasEnabled(boolean enable) {
    // enable or disable the Run QuickTime Movies option in the QuickTIme menu
    m_movieCheckBoxMenuItem.setEnabled(enable);
    m_autoCheckBoxMenuItem.setEnabled(enable);
    m_unrestrictedCheckBoxMenuItem.setEnabled(enable);
  }

  public void setRunQuickTimeState(boolean state) {
    // set the check box on or off
    m_movieCheckBoxMenuItem.setState(state);
  }

  private File getLoadFile() {
    JFileChooser fc = new JFileChooser(m_loadFileRoot);
    File file = null;
    
    fc.addChoosableFileFilter(new CSVFilter());
    fc.repaint();
    int returnVal = fc.showDialog(TimelineMenu.this, "Load file...");

    if (returnVal == JFileChooser.APPROVE_OPTION) {
      file = fc.getSelectedFile();
    }

    if (file != null) {
      m_loadFileRoot = file;
      m_rfm.fileOpened(file);
    }
    return file;
  }
  
  private File getMovieFile() {
    JFileChooser fc = new JFileChooser(m_loadMovieRoot);
    File file = null;
    
    fc.addChoosableFileFilter(new MOVFilter());
    fc.repaint();
    int returnVal = fc.showDialog(TimelineMenu.this, "Associate with a movie...");

    if (returnVal == JFileChooser.APPROVE_OPTION) {
      file = fc.getSelectedFile();
    }

    if (file != null) m_loadMovieRoot = file;
    return file;
  }
  */

}
