home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-01-30 | 15.6 KB | 544 lines |
- //******************************************************************************
- // Applet:
- //
- // fullJava.java (compiles to fullJava.class)
- //
- //******************************************************************************
- import java.applet.*;
- import java.awt.*;
- import netscape.javascript.JSObject; //provides access to objects
- import netscape.javascript.JSException; //JavaScript Exception
-
- import OviObserver; //OVI Plug-in listener
- import OviPlayer; //OviPlayer interface
-
- //==============================================================================
- // Main Class for applet fullJava
- //
- //==============================================================================
- public class fullJava extends Applet implements OviObserver
- {
- private JSObject window, document; //objects from Netscape env
- private OviPlayer ovi = null; //OviPlayer object
- int sess = 0;
-
- boolean started = false; //track state of applet
-
-
- // PARAMETER SUPPORT:
- // Parameters allow an HTML author to pass information to the applet;
- // the HTML author specifies them using the <PARAM> tag within the <APPLET>
- // tag. The following variables are used to store the values of the
- // parameters.
- //--------------------------------------------------------------------------
-
- // Members for applet parameters
- // <type> <MemberVar> = <Default Value>
- //--------------------------------------------------------------------------
- private String m_plgname = "";
- private String m_movie1Title = "";
- private String m_movie2Title = "";
- private String m_movie3Title = "";
- private String m_movie1 = "";
- private String m_movie2 = "";
- private String m_movie3 = "";
- private String[][] m_movieInfo;
-
- // Parameter names. To change a name of a parameter, you need only make
- // a single change. Simply modify the value of the parameter string below.
- //--------------------------------------------------------------------------
- private final String PARAM_plgname = "plgname";
- private final String PARAM_movie1Title = "movie1Title";
- private final String PARAM_movie1 = "movie1";
- private final String PARAM_movie2Title = "movie2Title";
- private final String PARAM_movie2 = "movie2";
- private final String PARAM_movie3Title = "movie3Title";
- private final String PARAM_movie3 = "movie3";
-
-
- //GUI COMPONENTS
- //---------------------------------------------------------------------------
- Panel ctrlPanel = null;
- Panel ckPanel = null;
- Panel statusPanel = null;
-
- Button btnPause = null;
- Button btnPlay = null;
- Button btnFwd = null;
- Button btnRwd = null;
- Button btnLoad = null;
- Button btnClose = null;
-
- Checkbox ckLoop = null;
- Checkbox ckStart = null;
- Label counter = null;
-
-
-
-
- // fullJava Class Constructor
- //--------------------------------------------------------------------------
- public fullJava()
- {
-
- }
-
- // APPLET INFO SUPPORT:
- // The getAppletInfo() method returns a string describing the applet's
- // author, copyright date, or miscellaneous information.
- //--------------------------------------------------------------------------
- public String getAppletInfo()
- {
- return "Name: fullJava.class\r\n" +
- "Author: Oracle Corp., All Rights Reserved\r\n" +
- "Created with Microsoft Visual J++ Version 1.1";
- }
-
- // PARAMETER SUPPORT
- // The getParameterInfo() method returns an array of strings describing
- // the parameters understood by this applet.
- //
- // fullJava Parameter Information:
- // { "Name", "Type", "Description" },
- //--------------------------------------------------------------------------
- public String[][] getParameterInfo()
- {
- String[][] info =
- {
- { PARAM_plgname, "String", "Name of Browser plug-in object" },
- { PARAM_movie1Title, "String", "Title of first movie clip" },
- { PARAM_movie1, "String", "Path to first movie clip" },
- { PARAM_movie2Title, "String", "Title of second movie clip" },
- { PARAM_movie2, "String", "Path to first movie clip" },
- { PARAM_movie3Title, "String", "Title of third movie clip" },
- { PARAM_movie3, "String", "Path to first movie clip" },
- };
- return info;
- }
-
- // The init() method is called by the AWT when an applet is first loaded or
- // reloaded. Override this method to perform whatever initialization your
- // applet needs, such as initializing data structures, loading images or
- // fonts, creating frame windows, setting the layout manager, or adding UI
- // components.
- //--------------------------------------------------------------------------
- public void init()
- {
- boolean cond = false;
- // PARAMETER SUPPORT
- // The following code retrieves the value of each parameter
- // specified with the <PARAM> tag and stores it in a member
- // variable.
- //----------------------------------------------------------------------
- String param;
-
- // param: Parameter description
- //----------------------------------------------------------------------
- param = getParameter(PARAM_plgname);
- if (param != null)
- m_plgname = param;
- param = getParameter(PARAM_movie1Title);
- if (param != null)
- m_movie1Title = param;
- param = getParameter(PARAM_movie1);
- if (param != null)
- m_movie1 = param;
- param = getParameter(PARAM_movie2Title);
- if (param != null)
- m_movie2Title = param;
- param = getParameter(PARAM_movie2);
- if (param != null)
- m_movie2 = param;
- param = getParameter(PARAM_movie1Title);
- if (param != null)
- m_movie3Title = param;
- param = getParameter(PARAM_movie3);
- if (param != null)
- m_movie3 = param;
-
- String [][] movieInfo =
- {
- { m_movie1Title, m_movie1},
- { m_movie2Title, m_movie2},
- { m_movie3Title, m_movie3}
- };
-
- m_movieInfo = movieInfo;
-
- // If you use a ResourceWizard-generated "control creator" class to
- // arrange controls in your applet, you may want to call its
- // CreateControls() method from within this method. Remove the following
- // call to resize() before adding the call to CreateControls();
- // CreateControls() does its own resizing.
- //----------------------------------------------------------------------
- resize(320, 45);
- setBackground(Color.white);
- setLayout( new GridLayout(2,1,2,2));
- resize(320, 45);
-
- ctrlPanel = new Panel ();
- ctrlPanel.setLayout(new GridLayout(1,4,2,2));
- ctrlPanel.resize (320, 24);
-
- ckPanel = new Panel ();
- ckPanel.setLayout (new FlowLayout(FlowLayout.LEFT,4,0));
-
- statusPanel = new Panel ();
- statusPanel.setLayout(new BorderLayout());
- statusPanel.setBackground(Color.pink);
- statusPanel.resize (318, 24);
-
- btnPause = new Button ("Pause");
- btnPlay = new Button ("Play");
- btnFwd = new Button ("Seek >>");
- btnRwd = new Button ("<< Seek");
- btnLoad = new Button ("Load Next");
- btnClose = new Button ("Close");
-
- counter = new Label (" 00:00:00");
- counter.setBackground(Color.black);
- counter.setForeground(Color.green);
- counter.resize (50, 20);
-
- ckLoop = new Checkbox ("Loop");
- ckStart = new Checkbox ("Auto Start");
-
- ctrlPanel.add (btnPlay);
- ctrlPanel.add (btnRwd);
- ctrlPanel.add (btnFwd);
- ctrlPanel.add (btnLoad);
- ctrlPanel.add (btnClose);
-
- ckPanel.add (ckStart);
- ckPanel.add (ckLoop);
-
- statusPanel.add ("Center", ckPanel);
- statusPanel.add ("East", counter);
-
- add (ctrlPanel);
- add (statusPanel);
-
- /* Give time for plug-in instance initialization */
- try{
- Thread.sleep(3000);
- } catch (InterruptedException e){
- System.out.println("Failed to sleep in start()...");
- }
- }
-
-
- // fullJava Paint Handler
- //--------------------------------------------------------------------------
- public void paint(Graphics g)
- {
- //Place applet paint code here
- }
-
- // The start() method is called when the page containing the applet
- // first appears on the screen. The AppletWizard's initial implementation
- // of this method starts execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void start()
- {
- // GRAB PLUG-IN OBJECT
- // Retrieve the plug-in object handle from the browser's document context
- //-------------------------------------------------------------------------
- ovi = getPlugin();
-
- // REGISTER CLASS AS PLUG-IN EVENT LISTENER
- // Set up plug-in listener relationship
- //-------------------------------------------------------------------------
- if (! started) {
- getObserver();
- }
- }
-
- // The stop() method is called when the page containing the applet is
- // no longer on the screen. The AppletWizard's initial implementation of
- // this method stops execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void stop()
- {
- started = false;
- }
-
- // Applet clean up code. destroy() is called when
- // when you applet is terminating and being unloaded.
- //-------------------------------------------------------------------------
- public void destroy()
- {
- ovi = null;
- document = null;
- window = null;
- System.gc();
- }
- //IMPLEMENT OVIOBSERVER METHODS
- //Because OviObserver class is only an interface, the methods of this class
- //must be defined by any Java class implementing this interface
- //-------------------------------------------------------------------------
-
-
- // IMPLEMENT ONSTOP() OVIOBSERVER METHOD
- // Catch and handle onStop() method of OviObserver interface
- // In this context, onStop is thrown when the end of the current
- // stream is reached, not when a pause is called.
- //------------------------------------------------------------------------
- public void onStop()
- {
-
- if(! ckLoop.getState()) {
-
- System.out.println("\nCurrent video has ended. Loading new video...");
- if(sess == 2)
- sess = 0;
- else
- sess++;
- counter.setText (" 00:00:00");
-
- if (ckStart.getState()) {
- ovi.setAutoStart (true);
- btnPlay.setLabel ("Pause");
- }
- else {
- ovi.setAutoStart (false);
- btnPlay.setLabel ("Play");
- }
- ovi.load (m_movieInfo[sess][1]);
- /*
- * wait for client initialization
- */
- while (ovi.getState() < ovi.ST_INIT);
-
- } //end if
- else
- System.out.println("\nLooping current video...");
- }
-
- // IMPLEMENT ONPOSITIONCHANGE() OVIOBSERVER METHOD
- // Catch and handle onPositionChange() method of OviObserver interface
- //--------------------------------------------------------------------------
- public void onPositionChange(int newPos)
- {
- started = true;
-
- long tmp;
- long tmp2;
- long hh;
- long mm;
- long ss;
- String s = "";
-
- tmp = newPos;
- tmp2 = tmp % 3600000;
- hh = (tmp - tmp2) / 3600000;
- if (tmp2 > 0)
- tmp = tmp2;
- tmp2 = tmp % 60000;
- mm = (tmp -tmp2) / 60000;
- if (tmp2 > 0)
- tmp = tmp2;
- tmp2 = tmp % 1000;
- ss = (tmp - tmp2) /1000;
- s = " " + pad (hh) + ":" + pad (mm) + ":" + pad (ss);
- counter.setText(s);
- }
-
- //HANDLE MOUSE EVENTS
- //Traps all action events to relevant components.
- //Event handling code for any additional components may be added here.
- //--------------------------------------------------------------------------
- public boolean mouseUp (Event evt, int x, int y)
- {
- boolean bCond = false;
-
- // GRAB PLUG-IN OBJECT
- // Refresh the plug-in object handle from the browser's document context.
- //-------------------------------------------------------------------------
- ovi = getPlugin();
-
- // REGISTER CLASS AS PLUG-IN EVENT LISTENER
- // Set up plug-in listener relationship
- //-------------------------------------------------------------------------
- if (! started) {
- getObserver();
- }
-
- // PROCESS MOUSE-UP EVENTS
- //-------------------------------------------------------------------------
- if (evt.target == ckLoop) {
- if(!ckLoop.getState()) {
- ovi.setLoop(true);
- }
- else {
- ovi.setLoop(false);
- }
- bCond = true;
- }
-
- if (evt.target == ckStart) {
-
- if (!ckStart.getState()) {
- ovi.setAutoStart(true);
- }
- else {
- ovi.setAutoStart(false);
- }
- bCond = true;
- }
-
- if (evt.target == btnPlay) {
- if (ovi.getState() == ovi.ST_PLAYING) {
- ovi.pause();
- btnPlay.setLabel("Resume");
- }
- else if (ovi.getState() == ovi.ST_PAUSED) {
- ovi.resume();
- btnPlay.setLabel("Pause");
- }
- else if (ovi.getState() == ovi.ST_REALIZED) {
- ovi.play();
- btnPlay.setLabel("Pause");
- }
- bCond = true;
- }
-
- if (evt.target == btnFwd) {
- ovi.pause();
-
- long cur = ovi.getPos();
- long len = ovi.getMaxPos();
- long dest = (long)(cur + (len * .10));
- if (dest > len)
- dest = len;
-
- ovi.setPos(dest);
- btnPlay.setLabel("Resume");
- bCond = true;
- }
-
- if (evt.target == btnRwd) {
- ovi.pause();
-
- long cur = ovi.getPos();
- long len = ovi.getMaxPos();
- long dest = (long)(cur - (len * .10));
- if (dest < 0)
- dest = 0;
-
- ovi.setPos(dest);
- btnPlay.setLabel("Resume");
- bCond = true;
- }
-
- if (evt.target == btnLoad) {
- if(sess == 2) {
- sess = 0;
- }
- else {
- sess++;
- }
- counter.setText (" 00:00:00");
- if (ckStart.getState()) {
- ovi.setAutoStart (true);
- btnPlay.setLabel("Pause");
- }
- else {
- ovi.setAutoStart (false);
- btnPlay.setLabel("Play");
- }
-
- ovi.load (m_movieInfo[sess][1]); /* load next video */
-
- /*
- * wait for client initialization
- */
- while (ovi.getState() < ovi.ST_INIT);
-
- bCond = true;
- }
-
- if (evt.target == btnClose) {
- ovi.unload ();
- counter.setText(" 00:00:00");
- btnPlay.setLabel("Play");
- }
-
- return bCond;
- } //end mouseUp()
-
- //UTILITY FUNCTION
- //-------------------------------------------------------------------------
- String pad (long l)
- {
- String s = Long.toString(l);
- if (s.length () == 1)
- s = "0" + s;
- return s;
- }
-
- // GET JS OBJECT HANDLES AND POINTER TO PLUGIN
- // Retrieves JavaScript Object contexts from Browswer.
- // This function depends upon the Netscape supplied Java package,
- // netscape.javascript. This function will NOT work under Internet
- // Explorer.
- //-------------------------------------------------------------------
- public OviPlayer getPlugin () {
-
- OviPlayer plugin = null;
- ovi = null;
- document = null;
- window = null;
- System.gc ();
-
- try {
- window = JSObject.getWindow(this); //grab window obj
- }
- catch(NullPointerException e) {
- System.out.println("getWindow() failed in getPlugin()...");
- }
-
- if (window != null)
- try {
- document = (JSObject) window.getMember("document"); //grab document obj
- }
- catch(NullPointerException e) {
- System.out.println("getMember() failed in getPlugin()...");
- }
- if (document != null) {
- try {
- plugin = (OviPlayer) document.getMember(m_plgname);
- }
- catch(NullPointerException e) {
- System.out.println("unable to grab plugin");
- }
- }
- else {
- System.out.println("Document null in getPlugin()...\n");
- }
-
- return plugin;
-
- } //end getPlugin
-
- // REGISTER CLASS AS PLUG-IN EVENT LISTENER
- // Retrieves JavaScript Object contexts from Browswer.
- // This function depends upon the Netscape supplied Java package,
- // netscape.javascript. This function will NOT work under Internet
- // Explorer.
- //-------------------------------------------------------------------
- private void getObserver ()
- {
- if (ovi != null) {
- try {
- if (ovi.advise(this)) {}
- }
- catch(NullPointerException e) {
- System.out.println("Caught NullPointerException in getObserver()...\n");
- }
- }
- else
- System.out.println("'ovi' null in getObserver()...\n");
- } // end getObserver
-
- } // end fullJava
-