/*
 * The org.opensourcephysics.controls package defines the framework for building
 * user interface controls for the book Simulations in Physics.
 * Copyright (c) 2003  H. Gould, J. Tobochnik, and W. Christian.
 */

/* This class was modified by Max Perkins and Dr. J. Hasbun in February 2004.
 * Only the class name was modified: ControlFrame to EControlFrame.
 * It had to be modified to accompany the changes in OSPControl (now EOSPControl)
 *
 * Max Perkins was sponsored by the NASA Space Grant Consortium through Dr. Ben de Mayo 
 * of the Physics Department at the State University of West Georgia.
 */

package org.opensourcephysics.jahasbun.QE.controls;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Clipboard;
import java.io.*;
import org.opensourcephysics.controls.*;
import org.opensourcephysics.display.OSPFrame;

/**
 *  A frame with a menu bar for saving and loading control parameters
 *
 * @author       Wolfgang Christian
 * @version 1.0
 */
public abstract class EControlFrame extends JFrame implements Control {

  final static int       MENU_SHORTCUT_KEY_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();

  protected JMenu        fileMenu;
  protected JMenu        editMenu;
  protected JMenuBar     menuBar;
  protected JMenuItem    readItem;
  protected JMenuItem    saveAsItem;
  protected JMenuItem    copyItem;

  public EControlFrame(String title){
    super(title);
      createMenuBar();
  }

  private void createMenuBar() {
    menuBar = new JMenuBar();
    if(!OSPFrame.appletMode) {
      setJMenuBar(menuBar);
    }
    fileMenu = new JMenu("File");
    editMenu = new JMenu("Edit");
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    readItem   = new JMenuItem("Read");
    saveAsItem   = new JMenuItem("Save As...");
    copyItem     = new JMenuItem("Copy");
    fileMenu.add(readItem);
    fileMenu.add(saveAsItem);
    editMenu.add(copyItem);
    copyItem.setAccelerator(KeyStroke.getKeyStroke('C', MENU_SHORTCUT_KEY_MASK));
    copyItem.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        copy();
      }
    });
    saveAsItem.setAccelerator(KeyStroke.getKeyStroke('S', MENU_SHORTCUT_KEY_MASK));
    saveAsItem.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        save();  // cannot use a static method here because of run-time binding
      }
    });
    readItem.setAccelerator(KeyStroke.getKeyStroke('R', MENU_SHORTCUT_KEY_MASK));
    readItem.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        readParameters();  // cannot use a static method here because of run-time binding
      }
    });
    validate();
  }

  /** Saves a file containing the control parameters to the disk.*/
  public void save() {
    ControlUtils.saveToFile( this, EControlFrame.this);
  }

  /** Loads a file containing the control parameters from the disk.*/
  public void readParameters() {
    ControlUtils.loadParameters((Control)this,EControlFrame.this);
  }

  /** Copies the data in the table to the system clipboard */
  public void copy() {
    Clipboard       clipboard       = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection stringSelection = new StringSelection(this.toString());
    clipboard.setContents(stringSelection, stringSelection);
  }

}

