// (c) MIT 2003.  All rights reserved.

// SimulateSurface.java -- 
// Author: Christine Alvarado <calvarad@ai.mit.edu> 
// Maintainer: Christine Alvarado <calvarad@ai.mit.edu> 
// Version: $id:$
// Created: <Mon Apr  5 19:29:30 1999> 
package rec.simulate;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import rec.core.widgets.*;
import rec.geo.*;
import rec.meche.WMForce;
import rec.meche.WMSpring;
import rec.simulate.widgets.*;
import rec.sys.*;
import rec.ui.RecFrame;
import rec.util.Assert;
import rec.util.Console;

public class SimulateSurfaceNetwork extends SimulateSurface
{
  private String m_server;
  private int m_port;
  
  public SimulateSurfaceNetwork()
  {
    super();
  }

  protected SimulatorRegistry getSimRegistry() 
  {
    // use a networked registry instead of simulating on the local
    // machine
    return new WMSimulatorRegistryNetwork();
  }

  public void addServer( String server, RecFrame frame ) 
  {
    // Use the first one as the default
    if( m_server == null ) {
      selectServer( server );
    }
    JMenuItem menuItem = new JMenuItem( server );
    menuItem.setActionCommand( server );
    menuItem.addActionListener( new ActionListener() {
	public void actionPerformed( ActionEvent e ) {
	  selectServer( e.getActionCommand() );
	}
      } );

    frame.addMenuItem( "Servers", menuItem );
  }


  public void selectServer( String serverAndPort )
  {
    StringTokenizer tokenizer = new StringTokenizer( serverAndPort, ":" );
    // get the server
    String server = tokenizer.nextToken();
    String portString = tokenizer.nextToken();

    int port = -1;
    try {
      port = Integer.parseInt( portString );       
    } catch( NumberFormatException e ) {
      Assert.error( "Bad port number: " + portString );
    }
    Console.writeln( "Selecting server: " + server + " on port: " + port );
    m_server = server;
    m_port = port;
  }

  public String getServer() 
  {
    return m_server;
  }

  public int getPort() 
  {
    return m_port;
  }
  
  
  
}
