/**
 *#########################################################################
 *
 * A component of the Gatherer application, part of the Greenstone digital
 * library suite from the New Zealand Digital Library Project at the
 * University of Waikato, New Zealand.
 *
 * <BR><BR>
 *
 * Author: John Thompson, Greenstone Digital Library, University of Waikato
 *
 * <BR><BR>
 *
 * Copyright (C) 1999 New Zealand Digital Library Project
 *
 * <BR><BR>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * <BR><BR>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * <BR><BR>
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *########################################################################
 */
package org.greenstone.gatherer.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import org.greenstone.gatherer.Configuration;
import org.greenstone.gatherer.Dictionary;
import org.greenstone.gatherer.Gatherer;
import org.greenstone.gatherer.util.JarTools;

/** The menu bar for the Gatherer main GUI. 
 * @author John Thompson, Greenstone Digital Library, University of Waikato
 * @version 2.2
 */
public class MenuBar
    extends JMenuBar
{
    static public ImageIcon BLANK_ICON = JarTools.getImage("blank.gif");
    static public ImageIcon HELP_ICON = JarTools.getImage("help.gif");

    /** The icon to be displayed alongside the context choosen help file. */
    private int current_tab          = -1;

    private JMenu file               = null;
    private JMenu edit               = null;
    public JMenu help                = null;
    public JMenuItem file_associations;
    public JMenuItem file_cdimage    = null;
    public JMenuItem file_close      = null;
    public JMenuItem file_delete     = null;
    public JMenuItem file_exit       = null;
    public JMenuItem file_exportas   = null;
    public JMenuItem file_new        = null;
    public JMenuItem file_open       = null;
    public JMenuItem file_options    = null;
    public JMenuItem file_save       = null;
    public JMenuItem edit_copy;
    public JMenuItem edit_cut;
    public JMenuItem edit_paste;
    public JMenuItem help_general;
    public JMenuItem help_download;
    public JMenuItem help_gather;
    public JMenuItem help_enrich;
    public JMenuItem help_design;
    public JMenuItem help_create;
    public JMenuItem help_format;
    public JMenuItem help_about;


    public MenuBar(MenuListener menu_listener)
    {
	file = new JMenu();
	file.setText(Dictionary.get("Menu.File"));
	
	file_associations = new JMenuItem(Dictionary.get("Menu.File_Associations"));
	file_associations.addActionListener(Gatherer.g_man);	  
	
	file_cdimage = new JMenuItem(Dictionary.get("Menu.File_CDimage"));
	file_cdimage.addActionListener(Gatherer.g_man);
	file_cdimage.setEnabled(!Gatherer.isGsdlRemote);
	
	file_close = new JMenuItem(Dictionary.get("Menu.File_Close"));
	file_close.addActionListener(Gatherer.g_man);
	file_close.setEnabled(false);
	
	file_delete = new JMenuItem(Dictionary.get("Menu.File_Delete"));
	file_delete.addActionListener(Gatherer.g_man);
	
	file_exit = new JMenuItem(Dictionary.get("Menu.File_Exit"));
	file_exit.addActionListener(Gatherer.g_man);
	
	file_exportas = new JMenuItem(Dictionary.get("Menu.File_ExportAs"));
	file_exportas.addActionListener(Gatherer.g_man);
	file_exportas.setEnabled(!Gatherer.isGsdlRemote);
	
	file_new = new JMenuItem(Dictionary.get("Menu.File_New"));
	file_new.addActionListener(Gatherer.g_man);
	
	file_open = new JMenuItem(Dictionary.get("Menu.File_Open"));
	file_open.addActionListener(Gatherer.g_man);
	
	file_options = new JMenuItem(Dictionary.get("Menu.File_Options"));
	file_options.addActionListener(Gatherer.g_man);
	
	file_save = new JMenuItem(Dictionary.get("Menu.File_Save"));
	file_save.addActionListener(Gatherer.g_man);
	file_save.setEnabled(false);
	
	// Layout (file menu)
	file.add(file_new);
	file.add(file_open);
	file.add(file_save);
	file.add(file_close);
	file.add(new JSeparator());
	file.add(file_delete);
	file.add(file_exportas);
	file.add(file_cdimage);
	file.add(new JSeparator());
	file.add(file_associations);
	file.add(file_options);
	file.add(new JSeparator());
	file.add(file_exit);

	// Edit menu
	edit = new JMenu();
	edit.setText(Dictionary.get("Menu.Edit"));
			  
	edit_cut = new JMenuItem(Dictionary.get("Menu.Edit_Cut"));
	edit_cut.addActionListener(Gatherer.g_man);
	
	edit_copy = new JMenuItem(Dictionary.get("Menu.Edit_Copy"));
	edit_copy.addActionListener(Gatherer.g_man);
	
	edit_paste = new JMenuItem(Dictionary.get("Menu.Edit_Paste"));
	edit_paste.addActionListener(Gatherer.g_man);
	
	// Layout (edit menu)
	edit.add(edit_cut);
	edit.add(edit_copy);
	edit.add(edit_paste);

	// Help menu
	help = new JMenu();
	help.setIcon(HELP_ICON);
	help.setText(Dictionary.get("Menu.Help"));
	
	help_general = new JMenuItem(Dictionary.get("Source.General"));
	help_general.addActionListener(Gatherer.g_man);
	
	help_download = new JMenuItem(Dictionary.get("GUI.Download"), BLANK_ICON);
	help_download.addActionListener(Gatherer.g_man);
	
	help_gather = new JMenuItem(Dictionary.get("GUI.Gather"), BLANK_ICON);
	help_gather.addActionListener(Gatherer.g_man);
	
	help_enrich = new JMenuItem(Dictionary.get("GUI.Enrich"), BLANK_ICON);
	help_enrich.addActionListener(Gatherer.g_man);
	
	help_design = new JMenuItem(Dictionary.get("GUI.Design"), BLANK_ICON);
	help_design.addActionListener(Gatherer.g_man);
	
	help_create = new JMenuItem(Dictionary.get("GUI.Create"), BLANK_ICON);
	help_create.addActionListener(Gatherer.g_man);
	
	help_format = new JMenuItem(Dictionary.get("GUI.Format"), BLANK_ICON);
	help_format.addActionListener(Gatherer.g_man);
	
	help_about = new JMenuItem(Dictionary.get("Menu.Help_About"));
	help_about.addActionListener(Gatherer.g_man);
	
	// Layout (help menu)
	help.add(help_general);
	help.add(new JSeparator());
	if (Configuration.get("workflow.download", true)) {
	    help.add(help_download);
	}
	if (Configuration.get("workflow.gather", true)) {
	    help.add(help_gather);
	}
	if (Configuration.get("workflow.enrich", true)) {
	    help.add(help_enrich);
	}
	if (Configuration.get("workflow.design", true)) {
	    help.add(help_design);
	}
	if (Configuration.get("workflow.create", true)) {
	    help.add(help_create);
	}
	if (Configuration.get("workflow.format", true)) {
	    help.add(help_format);
	}
	help.add(new JSeparator());
	help.add(help_about);

	// Layout (menu bar)
	this.add(file);
	this.add(Box.createHorizontalStrut(15));
	this.add(edit);
	this.add(Box.createHorizontalGlue());
	this.add(help);
    }

    /** Once a quit has been requested by the user, prevent any further menu selections. */
    public void exit() {
	file.setEnabled(false);
	edit.setEnabled(false);
	help.setEnabled(false);
    }

    public void refresh(int refresh_reason, boolean ready)
    {
	file_close.setEnabled(ready);
	file_save.setEnabled(ready);
    }


    /** In order to provide context aware help advice we keep track of which
     *  tab the user has open, and then highlight that help menu item with
     *  separators.
     *  @param tab_index The index of the selected tab (0-7).
     */
    public void tabSelected(int tab_index) {
	JMenuItem selected;
	if(current_tab != -1) {
	    // Remove the image
	    selected = help.getItem(current_tab);
	    if(selected != null) {
		selected.setIcon(BLANK_ICON);
	    }
	}
	current_tab = tab_index + 2;
	selected = help.getItem(current_tab);
	if(selected != null) {
	    selected.setIcon(HELP_ICON);
	}
	selected = null;
    }
}
