// Copyright 1996, Marimba Inc. All Rights Reserved.


// @(#)PlayerDialog.java, 1.15, 12/19/96





package marimba.gui;





import java.io.*;


import java.net.*;


import java.awt.*;


import java.util.*;





import marimba.text.TextView;





/**


 * A dialog for displaying presentations. This class also


 * provides some shortcuts for acccessing widgets.


 *


 * @author	Arthur van Hoff


 * @version 	1.15, 12/19/96


 */


public class PlayerDialog extends Dialog implements WidgetConstants {


    /**


     * The PlayerPanel for this dialog.


     * @see #getPlayerPanel


     */


    public PlayerPanel  player;





    /**


     * The PlayerUtil for manipulating widgets in this dialog.


     * @see #getPlayerUtil


     */


    public PlayerUtil  util;








    /**


     * Construct a new player dialog.


     */


    public PlayerDialog(Frame parent) {


	this(parent, null, (URL)null);


    }





    /**


     * Construct a new player dialog and show a presentation in it.


     */


    public PlayerDialog(Frame parent, URL url) {


	this(parent, null, url);


    }





    /**


     * Construct a new player fialog, exit on destroy.


     */


    public PlayerDialog(Frame parent, Object context, URL url) {


	super(parent, true);


	add("Center", player = createPlayerPanel(context));


	util = new PlayerUtil(player);


	if (url != null) {


	    util.setPresentation(url);


	}


    }





    /**


     * Construct a new player fialog, exit on destroy.


     * This constructor takes the presentation as argument.


     */


    public PlayerDialog(Frame parent, Object context, Presentation p) {


	super(parent, true);


	add("Center", player = createPlayerPanel(context));


	player.setPresentation(p);


	util = new PlayerUtil(player);


    }





    /**


     * Get the playerUtil


     * @see #util


     */


    public PlayerUtil getPlayerUtil() {


	return util;


    }





    /**


     * Get the playerPanel


     * @see #player


     */


    public PlayerPanel getPlayerPanel() {


	return player;


    }





    /**


     * Create the PlayerPanel.


     */


    protected PlayerPanel createPlayerPanel(Object context) {


	return new PlayerPanel(context);


    }





    /**


     * Show the frame, making sure it is visible on the screen.


     */


    public void show() {


	if (!isVisible()) {


	    if (PC) {


		Dimension d = size();


		Dimension sd = getToolkit().getScreenSize();


		move((sd.width - d.width)/2, (sd.height - d.height)/2);


	    }


	}


	super.show();


	// This makes the PC hang


	//player.requestFocus();





	// Make sure a focus event is send...


	player.postEvent(new Event(player, Event.GOT_FOCUS, null));





	player.start();


	player.firstFocus();


    }





    /**


     * Hide the frame.


     */


    public void hide() {


	player.stop();


	super.hide();





	// Make sure that the parent gets the focus


	if (PC) {


	    Container parent = getParent();


	    if (parent != null) {


		parent.requestFocus();


	    }


	}


    }





    /**


     * Dispose the frame


     */


    public void dispose() {


	player.destroy();


	// This does not work in the JDK 1.0.2


	//super.dispose();


	hide();


    }





    /**


     * Hide and dispose the frame.


     */


    public void close() {


	hide();


	dispose();





	// This is needed on Solaris in 1.0.2 to avoid a coredump


	if (!PC) {


	    try {


		System.gc();


		Thread.currentThread().sleep(200);


	    } catch (InterruptedException e) {


	    }


	}


    }





    /**


     * Handle an event.


     */


    public boolean handleEvent(Event evt) {


	if (evt.target instanceof Widget) {


	    return getParent().handleEvent(evt);


	}


	switch (evt.id) {


	  case Event.WINDOW_ICONIFY:


	    player.stop();


	    break;





	  case Event.WINDOW_DEICONIFY:


	    player.start();


	    break;





	  case Event.WINDOW_DESTROY: {


	    Widget w = util.getWidget("dialog-cancel", false);


	    if (w != null) {


		evt.id = Event.ACTION_EVENT;


		evt.target = w;


		return getParent().handleEvent(evt);


	    }


	    break;


	  }


	}


	return super.handleEvent(evt);


    }


}


