home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / qtstreamingapplet.win / src / qtstreamingapplet.java < prev   
Encoding:
Java Source  |  2000-09-28  |  4.6 KB  |  157 lines

  1. /*
  2.  * QuickTime for Java SDK Sample Code
  3.  
  4.    Usage subject to restrictions in SDK License Agreement
  5.  * Copyright: © 1996-1999 Apple Computer, Inc.
  6.  
  7.  */
  8. import java.applet.Applet;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import java.util.*;
  12.  
  13. import quicktime.*;
  14. import quicktime.io.QTFile;
  15.  
  16. import quicktime.app.QTFactory;
  17. import quicktime.app.display.*;
  18. import quicktime.app.image.ImageDrawer;
  19.  
  20. public class QTStreamingApplet extends Applet {
  21.  
  22.     private Drawable myQTContent;
  23.     private QTCanvas myQTCanvas;
  24.     private Vector urlTable;
  25.     private TextField urlTextField;
  26.     private PopupMenu pm;
  27.     
  28.     public void init () {
  29.         try {
  30.             QTSession.open();
  31.                 // set up a QTCanvas which will disply its content 
  32.                 // at its original size of smaller and centered in the space given
  33.                 // to the QTCanvas when the applet is layed out
  34.             setLayout (new BorderLayout());
  35.             urlTable = new Vector();
  36.             myQTCanvas = new QTCanvas (QTCanvas.kInitialSize, 0.5F, 0.5F);
  37.             add (myQTCanvas, "Center");        
  38.             
  39.             myQTContent = ImageDrawer.getQTLogo();
  40.         
  41.             add (bottomPanel(), "South");
  42.             readURL();
  43.                         
  44.         } catch (QTException qtE) {
  45.                 //in this case the only QTException is in QTSession.open()
  46.             throw new RuntimeException (qtE.getMessage());        
  47.         }
  48.     }    
  49.     
  50.     public void start () { 
  51.         try { // if QT was not successfully initialized the QTCanvas will be null
  52.             if (myQTCanvas != null)
  53.                 myQTCanvas.setClient (myQTContent, true);            
  54.         } catch (QTException e) {
  55.             e.printStackTrace();
  56.         }
  57.     }
  58.     
  59.     public void stop () { 
  60.         if (myQTCanvas != null)
  61.             myQTCanvas.removeClient();
  62.     }
  63.     
  64.     public void destroy () {
  65.         QTSession.close();
  66.     }
  67.         
  68.         
  69.     /**
  70.      * creates drawable object from the URL the user has entered
  71.      * or from the menu item chosen
  72.      */
  73.     private void createNewMovieFromURL(String selURL) throws QTException {
  74.         String url = urlTextField.getText();    
  75.         myQTContent = QTFactory.makeDrawable (url);
  76.         myQTCanvas.setClient (myQTContent, true);
  77.     }
  78.  
  79.     /**
  80.      * the parameter list in the html file should have a name = total having the value of the total
  81.      * number of url's to be appended to the popup menu. Each url should start with "url" + i , where i
  82.      * is 1, 2.... in increasing order till the i = total.
  83.      */
  84.     private void readURL() throws QTException{
  85.         for ( int i = 0; i < Integer.parseInt(getParameter("total")); i++) {
  86.             String url = getParameter("url" + (i + 1));        
  87.             if ( url != null) 
  88.                 appendItem(url);
  89.         }
  90.     }
  91.     
  92.     public Component bottomPanel () {
  93.         Panel row2 =  new Panel();
  94.         row2.setLayout(new FlowLayout(FlowLayout.CENTER));
  95.         row2.setBackground(Color.white);    
  96.             
  97.         urlTextField = new TextField ("file:///... Enter an URL to a movie", 40);//Enter URL to movie here", 30);
  98.         urlTextField.setFont (new Font ("Dialog", Font.PLAIN, 10));
  99.         urlTextField.setEditable (true);
  100.         urlTextField.addActionListener (new ActionListener () {
  101.             //TextField tf = urlTextField;
  102.             
  103.             public void actionPerformed (ActionEvent ae) {
  104.                 if (myQTCanvas != null) {
  105.                     try {
  106.                         createNewMovieFromURL(urlTextField.getText());
  107.                         appendItem(urlTextField.getText());
  108.                     } catch (QTException e) {
  109.                             //probably a non-fatal error that the Applet 
  110.                             //should deal with more informatively
  111.                         e.printStackTrace();
  112.                     }
  113.                 }
  114.             }
  115.         });
  116.  
  117.         row2.add(urlTextField);
  118.         
  119.         pm = new PopupMenu();
  120.          
  121.         Image img1 = Toolkit.getDefaultToolkit().getImage(getCodeBase().getFile() + "images/p1.gif");
  122.         Image img2 = Toolkit.getDefaultToolkit().getImage(getCodeBase().getFile() + "images/r1.gif");
  123.         IconButton menuButton = new PopupMenuButton (img1, img2, pm);
  124.                     
  125.         row2.add (menuButton);
  126.         
  127.         return row2;
  128.     }
  129.     
  130.     /** 
  131.      * This appends the input movie url entered in the textfield or
  132.      * selected from the Choose movie menu to the PopupMenu. It checks
  133.      * for its duplicates and then appends it to the list in the popupmenu
  134.      * @param     urlItem  Item to be appended to the PopupMenu
  135.      */
  136.     private void appendItem(String urlItem) throws QTException {
  137.         if (urlTable.contains(urlItem) == false) {
  138.             MenuItem item = new MenuItem(urlItem);
  139.             pm.add (item);
  140.             item.addActionListener (new ActionListener () {
  141.                 public void actionPerformed(ActionEvent event) {
  142.                     try {
  143.                     MenuItem jm = (MenuItem)event.getSource();
  144.                     String selURL = jm.getLabel();
  145.  
  146.                     urlTextField.setText(selURL);
  147.                     createNewMovieFromURL(selURL);
  148.                     }catch (QTException ex) {
  149.                         ex.printStackTrace();
  150.                     }
  151.                 }    
  152.             });
  153.             urlTable.addElement(urlItem);
  154.         }    
  155.     }
  156. }
  157.