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


// @(#)PlayerFrame.java, 1.45, 11/09/96





package marimba.gui;





import java.io.*;


import java.net.*;


import java.awt.*;


import java.util.*;





import marimba.text.TextView;





/**


 * A frame for displaying presentations. This class also


 * provides some shortcuts for acccessing widgets.


 *


 * @author	Arthur van Hoff


 * @version 	1.45, 11/09/96


 */


public class PlayerFrame extends Frame implements WidgetConstants {


    public PlayerPanel player;


    public PlayerUtil util;


    boolean exit;





    /**


     * Construct a new player frame.


     */


    public PlayerFrame() {


	this(null, false);


    }





    /**


     * Construct a new player frame. Set a presentation


     * and show the frame.


     */


    public PlayerFrame(URL url) {


	this(null, false);


	util.setPresentation(url);


	show();


    }





    /**


     * Construct a new player frame. Set a presentation


     * and show the frame.


     */


    public PlayerFrame(Presentation presentation) {


	this(null, false);


	player.setPresentation(presentation);


	show();


    }





    /**


     * Construct a new player frame, exit on destroy.


     */


    public PlayerFrame(boolean exit) {


	this(null, exit);


    }





    /**


     * Construct a new player frame, exit on destroy.


     */


    public PlayerFrame(Object context, boolean exit) {


	this.exit = exit;


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


	util = new PlayerUtil(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()) {


	    Dimension d = (player.getPresentation() != null) ? size() : new Dimension(200, 100);


	    Dimension sd = getToolkit().getScreenSize();


	    Point p = location();


	    if (p.x + d.width > sd.width) {


		p.x = sd.width - d.width;


	    }


	    if (p.x < 0) {


		p.x = 0;


	    }


	    if (p.y + d.height > sd.height) {


		p.y = sd.height - d.height;


	    }


	    if (p.y < 0) {


		p.y = 0;


	    }


	    move(p.x, p.y);


	}


	super.show();


	player.start();


    }





    /**


     * Hide the frame.


     */


    public void hide() {


	player.stop();


	super.hide();


    }





    /**


     * Dispose the frame


     */


    public void dispose() {


	player.destroy();


	super.dispose();


    }





    /**


     * Event handler.


     */


    public boolean handleEvent(Event evt) {


	switch (evt.id) {


	  case Event.WINDOW_ICONIFY:


	    player.stop();


	    break;





	  case Event.WINDOW_DEICONIFY:


	    player.start();


	    break;





	  case Event.WINDOW_DESTROY:


	    if (exit) {


		player.stop();


		player.destroy();


		System.exit(0);


	    } else {


		player.stop();


		hide();


	    }


	    break;


	}


	return super.handleEvent(evt);


    }





    /**


     * Make an absolute file name.


     */


    static String absoluteFile(String file) {


	if (file.startsWith("~")) {


	    file = System.getProperty("user.home") + file.substring(1);


	}


	return new File(file).getAbsolutePath();


    }





    private int oldcursor = -1;





    /**


     * Work around for cursor flashing...


     */


    public synchronized void setCursor(int cursor) {


	if (cursor != oldcursor) {


	    super.setCursor(oldcursor = cursor);


	}


    }





    /**


     * Main program for showing presentations from a file.


     */


    public static void main(String argv[]) {


	try {


	    for (int i = 0 ; i < argv.length ; i++) {


		PlayerFrame frm = new PlayerFrame(true);


		String fnm = argv[i];


		if (fnm.indexOf(':') <= 2) {


		    fnm = "file:" + absoluteFile(fnm).replace(File.separatorChar, '/');


		}


		frm.player.setPresentation(Presentation.getPresentation(new URL(fnm)));


		frm.show();


	    }


	} catch (MalformedURLException e) {


	    System.err.println(e);


	} catch (InternalError e) {


	    System.err.println(e);


	}


    }


}


