// Created by Michael D. Fischer on 27/07/2006.
// Copyright (c) 2006, Centre for Social Anthropology and Computing,
// University of Kent. All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// Neither the name of the Centre for Social Anthropology and Computing,
// University of Kent nor the names of its contributors may be used
// to endorse or promote products derived from this software without
// specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE
// COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
import quicktime.qd.*;
import quicktime.*;
import quicktime.io.*;
import quicktime.std.*;
import quicktime.std.movies.*;
import quicktime.std.image.*;
import quicktime.app.view.QTFactory;
import quicktime.app.view.QTComponent;
// import quicktime.app.display.*;
// import quicktime.app.players.*;
// import quicktime.app.image.*;
// import quicktime.app.actions.*;
// import quicktime.app.anim.*;
import quicktime.std.clocks.TimeRecord;
public class MediaPresenter extends Panel implements StdQTConstants {
MovieController mc=null;
private String currentURL=null;
Movie currentMovie = null;
QTComponent currentComponent=null;
int kWidth = 420, kHeight = 320;
MediaPresenter () throws Exception {
setLayout(new BorderLayout());
setBackground (Color.white);
setSize(480,320);
}
void stopPlayer () throws QTException {
if (mc != null)
mc.play((float) 0.0);
}
void setRate (float rate) throws QTException {
if (mc != null) {
if (rate > 3.0) rate = 3.0f;
if (rate < -3.0) rate = -3.0f;
mc.play(rate);
// ctrl.showRate();
}
}
void setRate (double rate) throws QTException {
if (mc != null)
setRate((float)rate);
}
float getRate () throws QTException {
if (mc != null)
return mc.getPlayRate();
else return (float) 0.0;
}
int getTime () throws QTException {
if (mc != null) {
return mc.getMovie().getTime();
} else return 0;
}
void setTime (long t) throws QTException {
if (mc != null)
mc.goToTime(new TimeRecord (mc.getTimeScale(),t));
}
int getScale () throws QTException {
if (mc != null)
return mc.getTimeScale();
else
return 1;
}
public String getCurrentURL() {
return currentURL;
}
public MovieController getMovieController() {
return mc;
}
private Hashtable drawers = new Hashtable();
class saveMovie {
MovieController mc;
Movie movie;
saveMovie(MovieController m, Movie mv) {
mc = m;
movie = mv;
}
}
void openFromURL (String url) throws QTException {
openFromURL(url,0);
}
void openFromURL (String url, long startTime) throws QTException {
int ndx = url.indexOf("//");
if (ndx != -1)
url = url.substring(ndx+2);
openFromPath(url,0);
}
void openFromPath (String url) throws QTException {
openFromPath(url,0);
}
// need code to close and open another movie;
void openFromPath (String url, long startTime) throws QTException {
if (currentURL != null) {
if (currentURL.equals(url)) {
mc.goToTime(new TimeRecord (mc.getTimeScale(), startTime)); // set start-time
return;
}
System.out.println("Falling through url check");
}
saveMovie sc = (saveMovie) drawers.get(url);
if (sc != null) {
if (currentComponent != null) {
remove (currentComponent.asComponent());
}
mc = sc.mc;
QTComponent qtc = QTFactory.makeQTComponent(mc);
currentComponent = qtc;
mc.goToTime(new TimeRecord (mc.getTimeScale(), startTime));
add (currentComponent.asComponent(),
BorderLayout.CENTER);
//mc.validate();
//validate();
currentURL = url;
currentMovie = sc.movie;
//invalidate();
//repaint();
System.out.println("fetching from cache");
}
else
try {
QTFile qtf = new QTFile (url);
OpenMovieFile omf = OpenMovieFile.asRead (qtf);
currentMovie = Movie.fromFile (omf);
mc = new MovieController (currentMovie);
mc.enableEditing(false);
mc.goToTime(new TimeRecord (mc.getTimeScale(), startTime));
if (currentComponent != null)
remove (currentComponent.asComponent());
currentComponent = QTFactory.makeQTComponent(mc);
add (currentComponent.asComponent(),
BorderLayout.CENTER);
// mc.validate();
//validate();
saveMovie nc = new saveMovie(mc, currentMovie);
drawers.put(url,nc);
// invalidate();
//repaint();
} catch (QTException qte) {
System.out.println ("Error in MediaPresenter.openFromPath "+qte.toString());
}
// validate();
repaint();
currentURL = url;
}
public void repaint() {
Dimension s = getParent().getSize();
try {
Movie m = mc.getMovie();
QDRect qdx = m.getBounds();
System.out.println("Movie "+qdx.getWidth()+" "+qdx.getHeight());
System.out.println("Window "+s);
double w_h = (double) qdx.getWidth() / (double) qdx.getHeight();
double h_w = 1.0 / w_h;
if (qdx.getWidth() +20 != s.width) {
qdx.setWidth(s.width-20);
qdx.setHeight((int) (qdx.getWidth() * h_w));
m.setBounds(qdx);
}
if (qdx.getHeight() +300 != s.height) {
qdx.setHeight(s.height-300);
qdx.setWidth((int) (qdx.getHeight()*w_h));
m.setBounds(qdx);
}
resize(qdx.getWidth(), qdx.getHeight());
validate();
} catch (QTException qte) {
System.out.println ("Error in MediaPresenter.setSize() "+qte.toString());
}
}
Movie getMovieFromURL (String url) throws QTException {
int ndx = url.indexOf("//");
if (ndx != -1)
url = url.substring(ndx+2);
return getMovieFromPath(url);
}
Movie getMovieFromPath (String url) throws QTException {
saveMovie sc = (saveMovie) drawers.get(url);
if (sc != null) {
return sc.movie;
}
else try {
QTFile qtf = new QTFile (url);
OpenMovieFile omf = OpenMovieFile.asRead (qtf);
return Movie.fromFile (omf);
} catch (QTException qte) {
System.out.println ("Error in MediaPresenter.getMovieFromPath "+qte.toString());
return null;
}
}
private void redrawDrawer () throws QTException {
/* groupDrawer.memberChanged (myDrawer);
if (myDrawer instanceof QTPlayer) {
((QTPlayer)myDrawer).getMovieController().movieChanged();
}
groupDrawer.redraw (null);
*/
}
boolean hasDrawer () { return mc != null; }
void reset () throws QTException {
// myDrawer.setMatrix (new Matrix());
redrawDrawer ();
}
void setScaling (float scale) throws QTException {
//Matrix mat = myDrawer.getMatrix();
//mat.scale (scale, scale, 0, 0);
//myDrawer.setMatrix (mat);
redrawDrawer ();
}
void rotateDrawer () throws QTException {
//Matrix mat = myDrawer.getMatrix();
// rotate 90 degress and keep origin at 0,0 of group
// this rotation bounds stuff ONLY works currently
// with 90 degree rotations - there are some bounds stuff
// on the matrix class which WILL be added to this code
// so that any rotation can be done and the result positioned at top / left
//float rx = 0;
//float ry = 0;
//mat.rotate (90, rx, ry);
/*
if (myDrawer instanceof QTPlayer) {
myDrawer.setMatrix (mat);
myDrawer.setLocation (0, 0);
} else {
QDDimension d = myDrawer.getOriginalSize();
if (mat.getSx() == 0) {
if (mat.getB() > 0) {
float xLoc = mat.getC() * -d.getHeight();
mat.setTx (xLoc);
mat.setTy (0);
} else {
float yLoc = mat.getB() * -d.getWidth();
mat.setTy (yLoc);
mat.setTx (0);
}
} else {
if (mat.getSx () > 0)
mat.setTx (0);
else
mat.setTx (d.getWidth() * Math.abs (mat.getSx()));
if (mat.getSy () > 0)
mat.setTy (0);
else
mat.setTy (d.getHeight()* Math.abs (mat.getSy()));
}
myDrawer.setMatrix (mat);
}
*/
redrawDrawer ();
}
public static void main (String args[]) {
try {
QTSession.open();
MediaPresenter gd = new MediaPresenter();
Frame x = new Frame("x");
x.setLayout(new BorderLayout());
x.add(gd,"Center");
gd.show();
} catch (Exception e) {
e.printStackTrace();
QTSession.close();
}
}
}