/*
     * The org.opensourcephysics.davidson.applets package contains the framework for
 * embedding Open Source Physics programs as applets in html pages.
 * Copyright (c) 2001 W. Christian.
 *
 */

/* This class was modified by Max Perkins and Dr. J. Hasbun in February 2004.
 * Only the class name was modified: AppletOSPControl to EAppletOSPControl.
 * 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.applets;

import org.opensourcephysics.controls.*;
import org.opensourcephysics.jahasbun.QE.controls.*;
import org.opensourcephysics.davidson.applets.*;
import org.opensourcephysics.display.OSPFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A GUI consisting of an input text area and a message area.
 *
 * Custom buttons can be added to invoke methods in the model.
 */
public class EAppletOSPControl extends EOSPControl {

  /**
   * AppletOSPControl constructor.
   * @param _animation
   */
    public EAppletOSPControl(Object model) {
    super(model);
    if (model instanceof Embeddable) {
      ( (Embeddable) model).getManager().addView("controlFrame", this);
    }
  }

  /**
   * Adds a button using a semicolon delimited parameter list.
   *
   * @param str
   */
  public void addButton(String str) {
    java.util.Map map = AppletUtils.getParameterMap(str);
    String method = (String) map.get("method");
    String text = (String) map.get("text");
    String tooltip = (String) map.get("tooltip");
    addButton(method, text, tooltip);
  }

  /**
 * Invokes a method of the named object.
 *
 * The object name can be an object that has been registered in the manager or the reserved
 * constants CONTROL or MODEL.
 *
 * @param objectName
 * @param methodName
 * @param value
 */
public void invokeMethod(String objectName, String methodName, String value) {
  Object target = null;
  if (objectName.equals("CONTROL")) {
    target = this;
  }
  else if (objectName.equals("MODEL")) {
    target = model;
  } else if (model instanceof Embeddable) {
    ObjectManager manager = ( (Embeddable) model).getManager();
    target = (Object) manager.getObject(objectName);
  }
  if (target == null) {
    System.err.println("Target of method not found in invokeMethod. Target=" +
                       objectName);
    if (model instanceof Embeddable)
      ( (Embeddable) model).getManager().printObjectsAndViews();
    return;
  }
  AppletUtils.invokeMethod(target, methodName, value);
}



  /**
   * Initializes this control after all objects have been created.
   */
  protected void init() {
    validate();
    pack();
    if (org.opensourcephysics.display.OSPFrame.appletMode) {
      setVisible(false);
      setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }
    else {
      Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
      setLocation( (d.width - getSize().width) / 2,
                  (d.height - getSize().height) / 2); // center the frame
      setVisible(true);      
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
  }

}
