/*=========================================================================

  Program:   Orbit Visualization Tool
  Source:    $Source: /stor/devel/ovt2g/ovt/gui/XYZMenuBar.java,v $
  Date:      $Date: 2001/04/19 00:25:41 $
  Version:   $Revision: 1.26 $


Copyright (c) 2000 OVT Team (Kristof Stasiewicz, Mykola Khotyaintsev, 
Yuri Khotyaintsev)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification is permitted provided that the following conditions are met:

 * No part of the software can be included in any commercial package without
written consent from the OVT team.

 * Redistributions of the source or binary code must retain the above
copyright notice, this list of conditions and the following disclaimer.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT OR
INDIRECT DAMAGES  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE.

OVT Team (http://ovt.irfu.se)   K. Stasiewicz, M. Khotyaintsev, Y.
Khotyaintsev

=========================================================================*/

package ovt.gui;

import ovt.*;
import ovt.mag.*;
import ovt.util.*;
import ovt.object.*;
import ovt.datatype.*;
import ovt.interfaces.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.io.*;


public class XYZMenuBar extends JMenuBar {
    public OVTCore core;
    public XYZWindow xyzWin;

public XYZMenuBar(OVTCore acore, XYZWindow xyzwin) {
  super();
  this.core = acore;
  xyzWin = xyzwin;
  JMenuItem menuItem;
  JRadioButtonMenuItem rbMenuItem;
  JCheckBoxMenuItem cbMenuItem;
  String ItemName;

  Font font = Style.getMenuFont();


//--------------Build the File menu.-----------------

	JMenu menu = new JMenu("File");
	menu.setFont(font);
        
	menuItem = new JMenuItem("Export Image...");
        menuItem.setFont(font);
        menuItem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            ImageOperations.exportImageDialog(getCore());
          }
        });
	
        menu.add(menuItem);
        
        menu.addSeparator();
        
        menuItem = new JMenuItem("Load Settings...");
          menuItem.setFont(font);
          menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                String defaultFile = OVTCore.getGlobalSetting(OVTCore.DEFAULT_SETTINGS_FILE, core.getConfDir());
                String file = Settings.showOpenDialog(xyzWin, new File(defaultFile));
                if (file != null) {
                    try {
                        // hide all objects
                        //Settings.load(core.getConfDir() + "hideall.xml", core);
                        getCore().hideAllVisibleObjects();
                        // load new settings
                        Settings.load(file, core);
                        OVTCore.setGlobalSetting(OVTCore.DEFAULT_SETTINGS_FILE, file);
                        core.Render();
                    } catch (IOException e2) {
                        core.sendErrorMessage("Error Loading Settings", e2.getMessage());
                    }
                }
            }
          });

        menu.add(menuItem);
          
        menuItem = new JMenuItem("Save Settings...");
        menuItem.setFont(font);
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                String defaultFile = OVTCore.getGlobalSetting(OVTCore.DEFAULT_SETTINGS_FILE, core.getConfDir());
                String file = Settings.showSaveDialog(xyzWin, new File(defaultFile));
                if (file != null) {
                    try {
                        Settings.save(core, file);
                        OVTCore.setGlobalSetting(OVTCore.DEFAULT_SETTINGS_FILE, file);
                    } catch (IOException e2) {
                        core.sendErrorMessage("Error Saving Settings", e2.getMessage());
                    }
                }
            }
        });
        
        menu.add(menuItem);
        
        menu.addSeparator();
          
        if ( xyzWin.windowResizable ){
          menuItem = new JMenuItem("Print...");
          menuItem.setFont(font);
          menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
              ImageOperations.print(getCore());
            }
          });
          menu.add(menuItem);
        }

        menu.addSeparator();

        menuItem = new JMenuItem("Exit");
        menuItem.setFont(font);
        menuItem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            getVW().quit();
          }
        });
        menu.add(menuItem);
        
        add(menu);
        
        // Options
        menu = new JMenu("Options");
        menu.setFont(font);
        
        // View Control
        
        menuItem = new JMenuItem("View Control...");
        menuItem.setFont(font);
        menuItem.setMnemonic(KeyEvent.VK_V);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                 KeyEvent.VK_V, ActionEvent.CTRL_MASK));
        menuItem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            getCore().getCamera().setCustomizerVisible(true);
          }
        });
        menu.add(menuItem);
        
        menu.addSeparator();
        
        // Magnetic Field 
        
        menuItem = new JMenuItem("Magnetic Field...");
        menuItem.setFont(font);
        menuItem.setMnemonic(KeyEvent.VK_V);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                 KeyEvent.VK_M, ActionEvent.CTRL_MASK));
        menuItem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            getCore().getMagProps().setEditorVisible(true);
          }
        });
        menu.add(menuItem);
        
        menu.add(getActivityDataMenu());
        
        menu.addSeparator();
        
        // Space Colour
        
        menuItem = ((MenuItemsSource)getCore().getDescriptors().getDescriptor("backgroundColor").getPropertyEditor()).getMenuItems()[0];
        menu.add(menuItem);
        
        add(menu);
        
        // Help
        
        menu = new JMenu("Help");
        menu.setFont(font);
        
        menuItem = new JMenuItem("About");
        menuItem.setFont(font);
        menuItem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            HTMLBrowser hw = getVW().getHTMLBrowser();
            String url = "file:"+new File(getCore().getOVTHomeDir()).getAbsolutePath()+"docs/about.html";
            //String url = "http://www.yahoo.com";
            try {
              hw.setPage(url);
            } catch (IOException e) {e.printStackTrace();}
            hw.setVisible(true);
          }
        });
        menu.add(menuItem);
        
        /*menuItem = new JMenuItem("User Guide");
        menuItem.setFont(font);
        menuItem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            HTMLBrowser hw = getVW().getHTMLBrowser();
            String url = "file:"+getCore().getOVTHomeDir()+"docs/userguide.html";
            //String url = "http://www.yahoo.com";
            try {
              hw.setPage(url);
            } catch (IOException e) {e.printStackTrace();}
            hw.setVisible(true);
          }
        });
        menu.add(menuItem);*/
        
        add(menu);

}

private JMenu getActivityDataMenu() {
    JMenu menu = new JMenu("Activity Data");
    menu.setFont(Style.getMenuFont());
    
    MagProps magProps = getCore().getMagProps();
    for (int i=1; i<MagProps.MAX_ACTIVITY_INDEX; i++) {
        JMenuItem menuItem = new ActivityDataMenuItem(magProps, i);
        menu.add(menuItem);
    }
    return menu;
}


protected XYZWindow getVW()
	{ return xyzWin; }

protected OVTCore getCore()
	{ return xyzWin.getCore(); }
        
        
        
}

class ActivityDataMenuItem extends JMenuItem implements ActionListener {

    private int activityIndex;
    private MagProps magProps;
    ActivityDataMenuItem(MagProps mp, int activityIndex) {
        super();
        setText(mp.getActivityName(activityIndex) + "...");
        setFont(Style.getMenuFont());
        this.activityIndex = activityIndex;
        this.magProps = mp;
        addActionListener(this);
    }
    
    public void actionPerformed(ActionEvent evt) {
            magProps.activityEditors[activityIndex].setVisible(true);
    }
    
}