package charly.demo;

import java.awt.*;
import java.awt.event.*;

import charly.client.*;
import charly.server.devices.ChGpibTimeOutException;

/** An AWT-Frame for sending a string to a GPIB-device. You can find the
  * <a href="doc-files/ChAWTGpib.java.html">source here</a>.
  * @see #send()
  */
public class ChAWTGpib extends Frame 
{
  ChConnect server;
  TextField dvcNrField;
  TextField inputField;
  Label messageLabel;
  
  public static void main(String args[])
    throws Exception
  {
    System.out.println("usage: ChAWTGpib [TCP/IP-address]");
    
    // get server - address from command line
    ChConnect server=new ChConnect(args.length==0?"localhost":args[0]);
      
    ChAWTGpib awtGpib=new ChAWTGpib(server);
    awtGpib.start();
  }
  
  ChAWTGpib(ChConnect server)
  {
    super("Charly AWT Gpib");
    this.server=server;
    
    setSize(600,150);
    addWindowListener(new WindowAdapter()
      {
        public void windowClosing(WindowEvent ev)
        {
          System.exit(0);
        }
      });
    setLayout(new GridLayout(4,1,4,4));  // zwei Panels, ein Button und ein Label übereinander
    
    Panel p=new Panel();  
    p.setLayout(new GridLayout(1,2,4,4)); // Beschriftung und Eingabefeld
    p.add(new Label("Device number:"));
    dvcNrField = new TextField("7"); 
    p.add(dvcNrField);
    add(p);
    
    p=new Panel();  
    p.setLayout(new GridLayout(1,2,4,4)); // Beschriftung und Eingabefeld
    p.add(new Label("Text to send:"));
    inputField = new TextField(":AUT"); 
    p.add(inputField);
    add(p);
    
    Button b=new Button("Send");
    b.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent ev)
        {
          send();
        }
      });
    add(b);
    
    messageLabel = new Label("Initialized!");
    add(messageLabel);
  }
    
  void start()
  {
    setVisible(true);
  }
  

  void message(String msg)
  {
    System.out.println(msg);
    messageLabel.setText(msg);
  }
  
  /** In this method you can find the core of this mini-application.
    */
  void send()
  {  
    ChTask task=new ChTask("AWTGpib");  // create task;
    message("Sending...");
    
    try
    {
      int dvcNr=Integer.parseInt(dvcNrField.getText());
          // a NumberFormatException can be thrown 
      ChGpibClient gpib=new ChGpibClient(server); // create gpib-Client
    
    
      task.addResource(server, gpib.getResourceName(dvcNr));
        // task needs resource dvcNr of server 
        
      task.start(true);  // start task and wait until resources are here
    
      gpib.setTask(task);
    
      gpib.send(dvcNr,inputField.getText()); // sending ":AUT" to the device nr 7
      message("Successfully sent!");
      
    } catch(NumberFormatException e)
    {
      message("Device number needed!");
    } catch(ChGpibTimeOutException e)
    {
      message("Device timed out!");
    }catch(Exception e)
    { 
      message(e.getMessage()); // print error message
      e.printStackTrace();
    }
    
    task.stop();  // I M P O R T A N T: frees resources on the server
    
  }
}