/*
 * QuickTime for Java SDK Sample Code

   Usage subject to restrictions in SDK License Agreement
 * Copyright: © 1996-1999 Apple Computer, Inc.

 */
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

import quicktime.*;
import quicktime.io.QTFile;

import quicktime.app.QTFactory;
import quicktime.app.display.*;
import quicktime.app.image.ImageDrawer;

public class QTStreamingApplet extends Applet {

	private Drawable myQTContent;
	private QTCanvas myQTCanvas;
    private Vector urlTable;
    private TextField urlTextField;
    private PopupMenu pm;
    
	public void init () {
		try {
			QTSession.open();
				// set up a QTCanvas which will disply its content 
				// at its original size of smaller and centered in the space given
				// to the QTCanvas when the applet is layed out
			setLayout (new BorderLayout());
			urlTable = new Vector();
			myQTCanvas = new QTCanvas (QTCanvas.kInitialSize, 0.5F, 0.5F);
			add (myQTCanvas, "Center");		
			
			myQTContent = ImageDrawer.getQTLogo();
		
			add (bottomPanel(), "South");
			readURL();
						
		} catch (QTException qtE) {
				//in this case the only QTException is in QTSession.open()
			throw new RuntimeException (qtE.getMessage());		
		}
	}	
	
	public void start () { 
		try { // if QT was not successfully initialized the QTCanvas will be null
			if (myQTCanvas != null)
				myQTCanvas.setClient (myQTContent, true);			
		} catch (QTException e) {
			e.printStackTrace();
		}
	}
	
	public void stop () { 
		if (myQTCanvas != null)
			myQTCanvas.removeClient();
	}
	
	public void destroy () {
		QTSession.close();
	}
		
		
	/**
	 * creates drawable object from the URL the user has entered
	 * or from the menu item chosen
	 */
	private void createNewMovieFromURL(String selURL) throws QTException {
		String url = urlTextField.getText();	
		myQTContent = QTFactory.makeDrawable (url);
		myQTCanvas.setClient (myQTContent, true);
	}

	/**
	 * the parameter list in the html file should have a name = total having the value of the total
	 * number of url's to be appended to the popup menu. Each url should start with "url" + i , where i
	 * is 1, 2.... in increasing order till the i = total.
	 */
	private void readURL() throws QTException{
		for ( int i = 0; i < Integer.parseInt(getParameter("total")); i++) {
			String url = getParameter("url" + (i + 1));		
			if ( url != null) 
				appendItem(url);
		}
	}
	
	public Component bottomPanel () {
		Panel row2 =  new Panel();
		row2.setLayout(new FlowLayout(FlowLayout.CENTER));
		row2.setBackground(Color.white);	
			
		urlTextField = new TextField ("file:///... Enter an URL to a movie", 40);//Enter URL to movie here", 30);
		urlTextField.setFont (new Font ("Dialog", Font.PLAIN, 10));
		urlTextField.setEditable (true);
		urlTextField.addActionListener (new ActionListener () {
			//TextField tf = urlTextField;
			
			public void actionPerformed (ActionEvent ae) {
				if (myQTCanvas != null) {
					try {
						createNewMovieFromURL(urlTextField.getText());
						appendItem(urlTextField.getText());
					} catch (QTException e) {
							//probably a non-fatal error that the Applet 
							//should deal with more informatively
						e.printStackTrace();
					}
				}
			}
		});

		row2.add(urlTextField);
		
		pm = new PopupMenu();
 		
		Image img1 = Toolkit.getDefaultToolkit().getImage(getCodeBase().getFile() + "images/p1.gif");
		Image img2 = Toolkit.getDefaultToolkit().getImage(getCodeBase().getFile() + "images/r1.gif");
		IconButton menuButton = new PopupMenuButton (img1, img2, pm);
					
		row2.add (menuButton);
		
		return row2;
	}
	
	/** 
	 * This appends the input movie url entered in the textfield or
	 * selected from the Choose movie menu to the PopupMenu. It checks
	 * for its duplicates and then appends it to the list in the popupmenu
     * @param     urlItem  Item to be appended to the PopupMenu
	 */
    private void appendItem(String urlItem) throws QTException {
    	if (urlTable.contains(urlItem) == false) {
    		MenuItem item = new MenuItem(urlItem);
        	pm.add (item);
        	item.addActionListener (new ActionListener () {
        		public void actionPerformed(ActionEvent event) {
        			try {
					MenuItem jm = (MenuItem)event.getSource();
					String selURL = jm.getLabel();

					urlTextField.setText(selURL);
					createNewMovieFromURL(selURL);
					}catch (QTException ex) {
						ex.printStackTrace();
					}
				}	
			});
        	urlTable.addElement(urlItem);
        }	
    }
}
