FileDocCategorySizeDatePackage
FlattenableQTEditor.javaAPI DocExample13967Wed Nov 10 12:59:54 GMT 2004com.oreilly.qtjnotebook.ch03

FlattenableQTEditor

public class FlattenableQTEditor extends Frame implements ActionListener

Fields Summary
Component
comp
QTFile
file
Movie
movie
MovieController
controller
Menu
fileMenu
Menu
editMenu
MenuItem
openItem
MenuItem
closeItem
MenuItem
newItem
MenuItem
quitItem
MenuItem
copyItem
MenuItem
cutItem
MenuItem
pasteItem
MenuItem
undoItem
MenuItem
redoItem
MenuItem
saveItem
MenuItem
flattenItem
static int
newFrameX
static int
newFrameY
static int
windowCount
UndoManager
undoManager
Constructors Summary
public FlattenableQTEditor()
no-arg constructor for "new" movie

    
              
         
        super ("FlattenableQTEditor");
        setLayout (new BorderLayout());
        QTSessionCheck.check();
        movie = new Movie(StdQTConstants.newMovieActive);
        controller = new MovieController (movie);
        controller.enableEditing(true);
        doMyLayout();
    
public FlattenableQTEditor(QTFile file)
file-based constructor for opening movies

        super ("FlattenableQTEditor");
        this.file = file;
        setLayout (new BorderLayout());
        QTSessionCheck.check();
        OpenMovieFile omf = OpenMovieFile.asRead (file);
        movie = Movie.fromFile (omf);
        controller = new MovieController (movie);
        controller.enableEditing(true);
        doMyLayout();
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)
handles menu actions

        Object source = e.getSource();
        try {
            if (source == quitItem) doQuit();
            else if (source == openItem) doOpen();
            else if (source == closeItem) doClose();
            else if (source == saveItem) doSave();
            else if (source == flattenItem) doFlatten();
            else if (source == newItem) doNew();
            else if (source == copyItem) doCopy();
            else if (source == cutItem) doCut();
            else if (source == pasteItem) doPaste();
            else if (source == undoItem) doUndo();
            else if (source == redoItem) doRedo();
        } catch (QTException qte) {
            qte.printStackTrace();
        }
    
public voiddoClose()

        setVisible(false);
        dispose();
        // quit if no windows now showing
        if (--windowCount == 0)
            doQuit();
    
public voiddoCopy()

        Movie copied = movie.copySelection();
        copied.putOnScrap(0);
        controller.movieChanged();
    
public voiddoCut()

        MovieEditState oldState = movie.newEditState();
        Movie cut = movie.cutSelection();
        MovieEditState newState = movie.newEditState();
        QTEdit edit = new QTEdit (oldState, newState, "Cut");
        undoManager.addEdit (edit);
        cut.putOnScrap(0);
        controller.movieChanged();
    
public voiddoFlatten()

        // always attempts to save to a new location,
        // so prompt for filename
        FileDialog fd = new FileDialog (this,
                                        "Flatten...",
                                        FileDialog.SAVE);
        fd.setVisible(true); // blocks
        if ((fd.getDirectory() == null) ||
            (fd.getFile() == null))
            return;
        QTFile flatFile =
            new QTFile (new File (fd.getDirectory(),
                                  fd.getFile()));
        if (flatFile.exists()) {
            // JOptionPane is a bit of cheat-for-clarity here,
            // building a working AWT dialog would be punitive
            int choice = 
                JOptionPane.showConfirmDialog (this,
                              "Overwrite " + flatFile.getName() + "?",
                              "Flatten",
                              JOptionPane.OK_CANCEL_OPTION);
            if (choice != JOptionPane.OK_OPTION)
                return;
        }
        // flatten call has lots of flag options, see
        // http://developer.apple.com/documentation/QuickTime/APIREF/SOURCESI/flattenmovie.htm
        System.out.println ("Calling flatten()");
        movie.flatten(StdQTConstants.flattenAddMovieToDataFork |
                      StdQTConstants.flattenForceMovieResourceBeforeMovieData, // movieFlattenFlags
                      flatFile, // fileOut
                      StdQTConstants.kMoviePlayer, // creator
                      IOConstants.smSystemScript, // scriptTag
                      StdQTConstants.createMovieFileDeleteCurFile, // createQTFileFlags
                      StdQTConstants.movieInDataForkResID, // resID
                      null); // resName
        System.out.println ("Called flatten()");

    
private voiddoMyLayout()
gets component from controller, makes menus

        // add movie component
        QTComponent qtc =
            QTFactory.makeQTComponent (controller);
        comp = qtc.asComponent();
        add (comp, BorderLayout.CENTER);
        // file menu
        fileMenu = new Menu ("File");
        newItem = new MenuItem ("New Movie");
        newItem.addActionListener (this);
        fileMenu.add (newItem);
        openItem = new MenuItem ("Open Movie...");
        openItem.addActionListener (this);
        fileMenu.add (openItem);
        closeItem = new MenuItem ("Close");
        closeItem.addActionListener (this);
        fileMenu.add (closeItem);
        saveItem = new MenuItem ("Save");
        saveItem.addActionListener (this);
        fileMenu.add (saveItem);
        flattenItem = new MenuItem ("Flatten");
        flattenItem.addActionListener (this);
        fileMenu.add (flattenItem);
        fileMenu.addSeparator();
        quitItem = new MenuItem ("Quit");
        quitItem.addActionListener (this);
        fileMenu.add(quitItem);
        // edit menu
        editMenu = new Menu ("Edit");
        undoItem = new MenuItem ("Undo");
        undoItem.addActionListener(this);
        editMenu.add(undoItem);
        redoItem = new MenuItem ("Redo");
        redoItem.addActionListener(this);
        editMenu.add(redoItem);
        editMenu.addSeparator();
        copyItem = new MenuItem ("Copy");
        copyItem.addActionListener(this);
        editMenu.add(copyItem);
        cutItem = new MenuItem ("Cut");
        cutItem.addActionListener(this);
        editMenu.add(cutItem);
        pasteItem = new MenuItem ("Paste");
        pasteItem.addActionListener(this);
        editMenu.add(pasteItem);
        // make menu bar
        MenuBar bar = new MenuBar();
        bar.add (fileMenu);
        bar.add (editMenu);
        setMenuBar (bar);
        // add close-button handling
        addWindowListener (new WindowAdapter() {
                public void windowClosing (WindowEvent e) {
                    doClose();
                }
            });
    
public voiddoNew()

        makeNewAndShow();
    
public voiddoOpen()

        QTFile file =
            QTFile.standardGetFilePreview (QTFile.kStandardQTFileTypes);
        Frame f = new FlattenableQTEditor (file);
        f.pack();
        if (newFrameX >= 0)
            f.setLocation (newFrameX+=16, newFrameY+=16);
        f.setVisible(true);
        windowCount++;
    
public voiddoPaste()

        MovieEditState oldState = movie.newEditState();
        Movie pasted = Movie.fromScrap(0);
        movie.pasteSelection (pasted);
        MovieEditState newState = movie.newEditState();
        QTEdit edit = new QTEdit (oldState, newState, "Paste");
        undoManager.addEdit (edit);
        controller.movieChanged();
        pack();
    
public voiddoQuit()

        System.exit(0);
    
public voiddoRedo()

        if (! undoManager.canRedo()) {
            System.out.println ("can't redo");
            return;
        }
        System.out.println ("doRedo() - " +
                            undoManager.getRedoPresentationName());
        undoManager.redo();
    
public voiddoSave()

        // if no home file, then prompt for one
        if (file == null) {
            FileDialog fd = new FileDialog (this,
                                            "Save...",
                                            FileDialog.SAVE);
            fd.setVisible(true); // blocks
            if ((fd.getDirectory() == null) ||
                (fd.getFile() == null))
                return;
            file = new QTFile (new File (fd.getDirectory(),
                                         fd.getFile()));
        }
        int flags = StdQTConstants.createMovieFileDeleteCurFile |
                    StdQTConstants.createMovieFileDontCreateResFile |
                    StdQTConstants.showUserSettingsDialog;
        movie.convertToFile (file, // file
                             StdQTConstants.kQTFileTypeMovie, // filetype,
                             StdQTConstants.kMoviePlayer, // creator
                             IOConstants.smSystemScript, // scriptTag
                             flags);
    
public voiddoUndo()

        if (! undoManager.canUndo()) {
            System.out.println ("can't undo");
            return;
        }
        System.out.println ("doUndo() - " +
                            undoManager.getUndoPresentationName());
        undoManager.undo();
    
public java.awt.DimensiongetPreferredSize()
Force frame's size to respect movie size

        if (controller == null)
            return new Dimension (0,0);
        try {
            QDRect contRect = controller.getBounds();
            Dimension compDim = comp.getPreferredSize();
            if (contRect.getHeight() > compDim.height) {
                return new Dimension (contRect.getWidth() +
                                      getInsets().left +
                                      getInsets().right,
                                      contRect.getHeight() +
                                      getInsets().top +
                                      getInsets().bottom);

            } else {
                return new Dimension (compDim.width +
                                      getInsets().left +
                                      getInsets().right,
                                      compDim.height +
                                      getInsets().top +
                                      getInsets().bottom);

            }
        } catch (QTException qte) {
            return new Dimension (0,0);
        }
    
public static voidmain(java.lang.String[] args)
opens a single new movie window

        try {
            Frame f = makeNewAndShow();
            // note its x, y for future calls
            newFrameX = f.getLocation().x;
            newFrameY = f.getLocation().y;
        } catch (Exception e) {
            e.printStackTrace();
        }
    
private static java.awt.FramemakeNewAndShow()
creates "new" movie frame, packs and shows. used by main() and "new"

        Frame f = new FlattenableQTEditor();
        f.pack();
        if (newFrameX >= 0)
            f.setLocation (newFrameX+=16, newFrameY+=16);
        f.setVisible(true);
        windowCount++;
        return f;