/* -*- tab-width: 4 -*-
 *
 * Electric(tm) VLSI Design System
 *
 * File: HelpMenu.java
 *
 * Copyright (c) 2003, Static Free Software. All rights reserved.
 *
 * Electric(tm) 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 3 of the License, or
 * (at your option) any later version.
 *
 * Electric(tm) 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sun.electric.tool.user.menus;

import static com.sun.electric.tool.user.menus.EMenuItem.SEPARATOR;

import com.sun.electric.database.hierarchy.Cell;
import com.sun.electric.database.hierarchy.Library;
import com.sun.electric.tool.io.FileType;
import com.sun.electric.tool.user.ActivityLogger;
import com.sun.electric.tool.user.Resources;
import com.sun.electric.tool.user.dialogs.About;
import com.sun.electric.tool.user.help.ManualViewer;
import com.sun.electric.tool.user.ui.TopLevel;
import com.sun.electric.util.ClientOS;

import java.lang.reflect.Method;
import java.net.URL;

/**
 * Class to handle the commands in the "Help" pulldown menu.
 */
public class HelpMenu {

    static EMenu makeMenu() {
        /****************************** THE HELP MENU ******************************/

		// mnemonic keys available:  BCDEFGHIJ  MNOPQ ST VWXYZ
        return new EMenu("_Help",

            !ClientOS.isOSMac() ? new EMenuItem("_About Electric...") { public void run() {
                aboutCommand(); }} : null,
			!ClientOS.isOSMac() ? SEPARATOR : null,

		    new EMenuItem("_User's Manual...") { public void run() {
                ManualViewer.userManualCommand(); }},

            ManualViewer.hasRussianManual() ? new EMenuItem("User's Manual (_Russian)...") { public void run() {
                ManualViewer.userManualRussianCommand(); }} : null,

            new EMenuItem("Show _Key Bindings") { public void run() {
                MenuCommands.menuBar().keyBindingManager.printKeyBindings(); }},

        // mnemonic keys available:  BCDEFGHIJK MNOPQRSTUVWXYZ
            ManualViewer.isJava3DAvailable() ? new EMenu("_3D Showcase",
                new EMenuItem("_Load Library") { public void run() {
                    loadSamplesLibrary("floatingGates", "topCell"); }},
                new EMenuItem("_3D View of Cage Cell") { public void run() {
                    ManualViewer.open3DSample("floatingGates" ,"topCell", "3D ShowCase"); }}) : null,
//                new EMenuItem("_Animate Cage Cell") { public void run() {
//                    animate3DSample("demoCage.j3d"); }}

            new EMenuItem("_Load Sample Cells Library") { public void run() {
                loadSamplesLibrary("samples", "tech-MOSISCMOS"); }}
        );
    }

    // ---------------------- THE HELP MENU -----------------

	/**
	 * Method to invoke the "About" dialog.
	 */
	public static void aboutCommand()
    {
		About dialog = new About(TopLevel.getCurrentJFrame());
        dialog.setVisible(true);
    }

	/**
	 * Method to load a sample library from the lib area.
	 * @param fileName library name
	 */
	private static void loadSamplesLibrary(String fileName, String cellName)
	{
		Library lib = Library.findLibrary(fileName);
		if (lib != null)
		{
			System.out.println(lib + " already loaded");
			Cell cell = lib.findNodeProto(cellName);
			if (cell == null)
				System.out.println("Cell '" + cellName + "' does not exist in " + lib);
			return;
		}
		String libName = "helphtml/"+fileName+".jelib";
		URL url = ManualViewer.class.getResource(libName);
		if (url != null)
			new FileMenu.ReadLibrary(url, FileType.JELIB, cellName);
		else
			System.out.println("Library '" + libName + "' in Electric's jarfile");
	}

//	/**
//	 * Method to animate the 3D view of current layout cell
//	 * @param demoName name of j3d file containing the demo
//	 */
//	private static void animate3DSample(String demoName)
//	{
//		String fileName = "helphtml/" + demoName;
//		URL url = ManualViewer.class.getResource(fileName);
//		if (url == null)
//		{
//			System.out.println("Can't open 3D demo file '" + fileName + "'");
//			return;
//		}
//		Class<?> plugin3D = Resources.get3DClass("ui.J3DDemoDialog");
//		if (plugin3D != null)
//		{
//			// Adding 3D/Demo menu
//			try {
//				Method createMethod = plugin3D.getDeclaredMethod("create3DDemoDialog",
//					new Class[] {java.awt.Frame.class, URL.class});
//				createMethod.invoke(plugin3D, new Object[] {TopLevel.getCurrentJFrame(), url});
//			} catch (Exception e)
//			{
//				System.out.println("Can't open 3D demo dialog: " + e.getMessage());
//				ActivityLogger.logException(e);
//			}
//		}
//	}
    
}
