package w3c.rtsp.client;

// mediascape.java
// $Id: mediascape.java,v 1 1997/07/10 15:00 osofia Exp $
// (c) COPYRIGHT MIT and INRIA, 1997.
// Please first read the full copyright statement in file COPYRIGHT.html

import java.io.*;
import java.util.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import w3c.rtsp.common.*;
import w3c.rtsp.client.*;


  //Listener is a listener.use differents booleans variables
  // which are explain at the top of this program.

public  class MyActionListener implements ActionListener {

  protected mediascape app=null;

    public void actionPerformed(ActionEvent e) {
      boolean portHere=true;
      Object target = e.getSource();
      if (target == app.myC.myW.Texte ){
	target=app.myC.myW.MyButton;
      }
      if (target == app.dialogB4.getSample() ){
	target=app.dialogB4.OK;
      }
      if (target == app.dialogB.OK){
	app.stop();
	app.dialogB.hide();
      }
      if (target == app.dialogB1.OK){
	app.dialogB1.hide();
	app.stop();
      }
      if (target == app.dialogB2.OK){
	app.dialogB2.hide();
	app.stop();
      }
      if (target == app.dialogB4.OK){
	app.dialogB4.hide();
	app.myC.myW.Texte.setText(app.dialogB4.getAddress());
	app.SessionGo();
      }
      if ((app.dialogB7!=null)&&(target == app.dialogB7.CANCEL)){
	app.dialogB7.hide();
	//	app.dialogB7=null;
      }
      if (target == app.dialogB5.CANCEL){
	app.dialogB5.hide();
	try{
	  app.audiolastprog=app.audio_prog_used;
	  Runtime run=Runtime.getRuntime();
	  if(app.audiolastprog==app.audio_prog_used)
	    app.audio_p=run.exec(app.audio_prog_used+" "+app.serverurl.getHost()+"/"+app.port_nb);
	  else{
	    app.audio_p.destroy();
	    app.audio_p=run.exec(app.audio_prog_used+" "+app.serverurl.getHost()+"/"+app.port_nb);	  }
	  app.audio_in_use=true;
	}catch(IOException E){
	  System.out.println("\nERREUR I/O :");
	  E.printStackTrace();
	  app.stop();
	}
      }
      if (target == app.dialogB5.DEFAULT){
	app.tp.setText(Integer.toString(app.default_port_nb));
	app.atpg.setText(app.default_prog_audio_used);
	app.vtpg.setText(app.default_prog_video_used);
	app.tpt.setText(app.default_stream_protocol);
      }
      
      //we want to listen  to a audio sample specified in a dialogbox.

      if (target == app.myC.myW.MyButton ){ 
	app.SessionGo();
      }

      //we want to stop the last audio sample.

      if (target == app.myC.myW.MyButton2){
	app.stop();
	app.addItem("STILL STOPPING \n");
      }

      //we want to configure the Appli.
      //we have to save the configuration in a ascii file.

      if (target == app.dialogB5.OK){
	app.dialogB5.hide();
	app.port_nb=Integer.parseInt(app.tp.getText());
	app.audio_prog_used=app.atpg.getText();
	String audioprogram=new String();
	String videoprogram=new String();
	audioprogram=app.atpg.getText();
	videoprogram=app.vtpg.getText();
	app.stream_protocol=app.tpt.getText();
	
	app.myProp.put("audioprogram",audioprogram);
	app.myProp.put("videoprogram",videoprogram);
	app.myProp.put("protocol",app.stream_protocol);
	app.myProp.put("port",Integer.toString(app.port_nb));
	try {
	  BufferedOutputStream buffer =
	    new BufferedOutputStream ( new FileOutputStream(app.file_conf));
	  app.myProp.save( buffer, "Property resource");
	  buffer.close();
	} catch (IOException ex) {
	  throw new RuntimeException("Unable to save Properties file : "+
				     app.file_conf.getAbsolutePath());
	}
	if((app.audio_p==null)&&(app.serverurl.getHost()!=null))
	  app.launchAudio(app.audio_prog_used,app.serverurl.getHost(),app.port_nb);
      }

      //we want to enter the url location.

      if (target == app.menuitem1 ){ 
	app.dialogB4.display("");
      }

      //we want to exit the appli.

      if (target == app.menuitem2 ){ 
	app.destroy();
      }

      //we want to configure the general preferences.

      if (target == app.menuitem3 ){ 
	app.dialogB5.refresh();
	app.dialogB5.display("");
	if(app.audio_p!=null){
	  app.audio_p.destroy();
	  app.audio_p=null;
	}
	if(app.video_p!=null){
	  app.video_p.destroy();
	  app.video_p=null;
	}
        app.audio_in_use=false;
	app.video_in_use=false;
	app.stop();
      }

      //we want to set the default paramaters.

      if (target == app.menuitem4 ){ 
	app.dialogB6.display("");
	if(app.audio_p!=null)
	  app.audio_p.destroy();
	if(app.video_p!=null)
	  app.video_p.destroy();
	app.audio_in_use=false;
	app.video_in_use=false;
      }

      //we want to set few use example's of a Rtsp server.

      if (target == app.menuitem5 ){
	app.myC.myW.Texte.setText("rtsp://faucheux.inria.fr:8006/alanis.au");
	app.SessionGo();
      }
      if (target == app.menuitem6 ){
	app.myC.myW.Texte.setText("rtsp://faucheux.inria.fr:8006/nirvana.au");

	app.SessionGo();
      }
      if (target == app.menuitem7 ){
	app.myC.myW.Texte.setText("rtsp://faucheux.inria.fr:8006/cannes-retro.vcr");
	app.SessionGo();
      }
      //add to the bookmarks.

      if (target == app.menuitem8 ){
	if(!app.myC.myW.Texte.getText().equals(""))
	  app.addBookmarks();
      }

      //display the bookmarks remove box. 

      if (target == app.menuitem9 ){
	Choice ch=null;
	System.out.println("\nremoving :");
	
	//create the DialogBox with the tabItem.

	app.dialogB7=new DialogBox((ch=app.createChoice(app.tabMenuItem)),app);

	//create the mouseListener.

	app.itemListener= new MyItemListener(app.bitem,app.dialogB7);
	
	//set the mouseListener.

	app.dialogB7.getChoice().addItemListener(app.itemListener);

	//display the dialog box.

	app.dialogB7.display("");
      }

      //Parse the vector to known if my item is one of them.

      MenuItem tmp=null;
      
      //we want to play the bookmarks's sample.
      
      for(Enumeration en=app.tabMenuItem.elements();en.hasMoreElements();)
	if(target == (tmp=(MenuItem)en.nextElement())){
	  app.myC.myW.Texte.setText(tmp.getLabel());
	  app.SessionGo();
	}

      //we want to remove a element of the boomarks.
      if((app.dialogB7!=null)&&(target == app.dialogB7.DELETE)){
	TextField it=null;
	it=app.dialogB7.getItem();
	System.out.println("\nFIELD SELECTED :"+it.getText());
	if(app.getItem(it.getText())!=null){
	  app.dialogB8=new DialogBox(app,it.getText());
	  app.dialogB8.display("Want to remove ?");
	}
      }
      if((app.dialogB8!=null)&&(target==app.dialogB8.OK)){
	TextField it=null;
	it=app.dialogB7.getItem();
	app.removeBookmarks(app.getItem(it.getText()));
	app.dialogB8.hide();
	app.dialogB7.hide();
      }
      if((app.dialogB8!=null)&&(target == app.dialogB8.CANCEL))
	app.dialogB8.hide();
    }

  public MyActionListener(mediascape app){
    this.app=app;
  }

  }
