FileDocCategorySizeDatePackage
DefaultControlPanel.javaAPI DocJMF 2.1.1e38656Mon May 12 12:20:50 BST 2003com.sun.media.ui

DefaultControlPanel

public class DefaultControlPanel extends BufferedPanelLight implements ComponentListener, ActionListener, ControllerListener, ItemListener, GainChangeListener

Fields Summary
static final Color
colorBackground
Variables
private static final String
MENU_PROPERTIES
private static final String
MENU_RATE_1_4
private static final String
MENU_RATE_1_2
private static final String
MENU_RATE_1_1
private static final String
MENU_RATE_2_1
private static final String
MENU_RATE_4_1
private static final String
MENU_RATE_8_1
private static final String
MENU_MEDIA
private static final String
MENU_AUDIO
private static final String
MENU_VIDEO
Player
player
Frame
parentFrame
Container
container
TransparentPanel
panelLeft
TransparentPanel
panelRight
TransparentPanel
panelProgress
boolean
boolAdded
ButtonComp
buttonPlay
ButtonComp
buttonStepBack
ButtonComp
buttonStepFwd
AudioButton
buttonAudio
ButtonComp
buttonMedia
ProgressSlider
progressSlider
CheckboxMenuItem
menuItemCheck
PopupMenu
menuPopup
WindowListener
wl
private boolean
firstTime
private boolean
started
private Integer
localLock
GainControlComponent
audioControls
PropertySheet
propsSheet
FramePositioningControl
controlFrame
ProgressControl
progressControl
GainControl
gainControl
SliderRegionControl
regionControl
String
urlName
long
lFrameStep
private CheckboxMenuItem
menuRate_1_4
private CheckboxMenuItem
menuRate_1_2
private CheckboxMenuItem
menuRate_1_1
private CheckboxMenuItem
menuRate_2_1
private CheckboxMenuItem
menuRate_4_1
private CheckboxMenuItem
menuRate_8_1
private Vector
vectorTracksAudio
private Vector
vectorTracksVideo
private int
pausecnt
private boolean
resetMediaTimeinPause
Constructors Summary
public DefaultControlPanel(Player player)
Methods


          

       
        this.player = player;
        try {
            init ();
        }
        catch ( Exception exception ) {
            exception.printStackTrace ();
        }
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent ae)

        String    command;

        command = ae.getActionCommand();
        if ( command.equalsIgnoreCase(buttonPlay.getLabel()) ) {
            playStop();
        }
        if ( buttonAudio != null  &&  command.equalsIgnoreCase(buttonAudio.getLabel()) ) {
            audioMute ();
        }
        else if ( command.equalsIgnoreCase(buttonMedia.getLabel())
                  ||  command.equalsIgnoreCase(MENU_PROPERTIES) ) {
            showPropsSheet();
        }
        else if ( buttonStepBack != null  &&  command.equalsIgnoreCase(buttonStepBack.getLabel()) ) {
            playStep ( false );
        }
        else if ( buttonStepFwd != null  &&  command.equalsIgnoreCase(buttonStepFwd.getLabel()) ) {
            playStep ( true );
        }
    
public voidaddNotify()

        boolean                        boolLightweight = true;
        Container                      containerParent;
        java.awt.peer.ComponentPeer    compPeer;


        if ( boolAdded == false ) {

            containerParent = this.getParent ();
            while ( containerParent != null  &&  boolLightweight == true ) {
                compPeer = containerParent.getPeer ();
                containerParent = containerParent.getParent ();
                if ( containerParent == null )
                    break;
                if ( compPeer != null  &&  !(compPeer instanceof java.awt.peer.LightweightPeer) )
                    boolLightweight = false;
            }

            if ( container != null ) {
                container.remove ( panelLeft );
                container.remove ( panelRight );
                container.remove ( panelProgress );
                if ( container != this )
                    this.remove ( container );
            }

            if ( boolLightweight == true ) {
                container = this;
            }
            else {
                container = new BufferedPanel ( new BorderLayout() );
                container.setBackground(colorBackground);
                ((BufferedPanel)container).setBackgroundTile ( BasicComp.fetchImage("texture3.gif") );
                this.add ( container, BorderLayout.CENTER );
            }
            container.add ( panelLeft, BorderLayout.WEST );
            container.add ( panelRight, BorderLayout.EAST );
            container.add ( panelProgress, BorderLayout.CENTER );

            boolAdded = true;
        }

        setVisible ( true );
        super.addNotify ();
        this.validate ();
    
private voidaudioMute()

        boolean boolState;

        if ( gainControl == null )
            return;

        boolState = buttonAudio.getValue();
        gainControl.setMute ( boolState );
    
public voidcomponentHidden(java.awt.event.ComponentEvent e)

    
public voidcomponentMoved(java.awt.event.ComponentEvent e)

    
public voidcomponentResized(java.awt.event.ComponentEvent e)

        this.validate ();
    
public voidcomponentShown(java.awt.event.ComponentEvent e)

    
public voidcontrollerUpdate(javax.media.ControllerEvent ce)

        long    lDuration;
        long    lMedia;

        synchronized (localLock) {
            if (player == null)
                return;
            if (ce instanceof StartEvent) {
                buttonPlay.setValue(true);
                buttonPlay.setEnabled(true);
                if ( buttonStepFwd != null )
                    buttonStepFwd.setEnabled ( true );
                if ( buttonStepBack != null )
                    buttonStepBack.setEnabled ( true );
            }
            else if (ce instanceof StopEvent ||
		     ce instanceof ResourceUnavailableEvent) {
                buttonPlay.setValue(false);
                buttonPlay.setEnabled(true);

                Thread.yield ();
                lDuration = player.getDuration().getNanoseconds();
                lMedia = player.getMediaNanoseconds();
                if ( buttonStepFwd != null ) {
                    if ( lMedia < lDuration - 1 )
                        buttonStepFwd.setEnabled ( true );
                    else
                        buttonStepFwd.setEnabled ( false );
                }
                if ( buttonStepBack != null ) {
                    if ( lMedia > 0 )
                        buttonStepBack.setEnabled ( true );
                    else
                        buttonStepBack.setEnabled ( false );
                }
            }
            else if (ce instanceof DurationUpdateEvent) {
                Time duration = player.getDuration();
                if ( duration == Duration.DURATION_UNKNOWN  ||  duration == Duration.DURATION_UNBOUNDED )
                    progressSlider.setEnabled(false);
                else
                    progressSlider.setEnabled(true);

                if (propsSheet != null)
                    propsSheet.updateDuration();
            }
            else if (ce instanceof MediaTimeSetEvent) {
                Thread.yield ();
                lDuration = player.getDuration().getNanoseconds();
                lMedia = player.getMediaNanoseconds();
                if ( buttonStepFwd != null ) {
                    if ( lMedia < lDuration - 1 )
                        buttonStepFwd.setEnabled ( true );
                    else
                        buttonStepFwd.setEnabled ( false );
                }
                if ( buttonStepBack != null ) {
                    if ( lMedia > 0 )
                        buttonStepBack.setEnabled ( true );
                    else
                        buttonStepBack.setEnabled ( false );
                }

                resetMediaTimeinPause = true;
            }
            else if (ce instanceof RateChangeEvent) {
                float    fRate;

                menuRate_1_4.setState ( false );
                menuRate_1_2.setState ( false );
                menuRate_1_1.setState ( false );
                menuRate_2_1.setState ( false );
                menuRate_4_1.setState ( false );
                menuRate_8_1.setState ( false );

                fRate = player.getRate ();
                if ( fRate < 0.5 ) {
                    menuRate_1_4.removeItemListener ( this );
                    menuRate_1_4.setState ( true );
                    menuRate_1_4.addItemListener ( this );
                }
                else if ( fRate < 1.0 ) {
                    menuRate_1_2.removeItemListener ( this );
                    menuRate_1_2.setState ( true );
                    menuRate_1_2.addItemListener ( this );
                }
                else if ( fRate > 4.0 ) {
                    menuRate_8_1.removeItemListener ( this );
                    menuRate_8_1.setState ( true );
                    menuRate_8_1.addItemListener ( this );
                }
                else if ( fRate > 2.0 ) {
                    menuRate_4_1.removeItemListener ( this );
                    menuRate_4_1.setState ( true );
                    menuRate_4_1.addItemListener ( this );
                }
                else if ( fRate > 1.0 ) {
                    menuRate_2_1.removeItemListener ( this );
                    menuRate_2_1.setState ( true );
                    menuRate_2_1.addItemListener ( this );
                }
                else {
                    menuRate_1_1.removeItemListener ( this );
                    menuRate_1_1.setState ( true );
                    menuRate_1_1.addItemListener ( this );
                }
            }
        }
    
public voiddispose()

        synchronized (localLock) {

	    if (player == null)
		return;

            if (propsSheet != null) {
                propsSheet.dispose();
                propsSheet = null;
            }
            if (progressSlider != null) {
                progressSlider.dispose();
                progressSlider = null;
            }
            if (audioControls != null) {
                remove(audioControls);
                audioControls = null;
            }
	    if (buttonAudio != null) {
		buttonAudio.dispose();
		buttonAudio = null;
	    }

            player = null;
            gainControl = null;
	    controlFrame = null;

            if (parentFrame != null && wl != null) {
                parentFrame.removeWindowListener(wl);
                parentFrame = null;
                wl = null;
            }

    	    vectorTracksAudio.removeAllElements();
    	    vectorTracksVideo.removeAllElements();

	    /**
	     * Don't ask why, the following few lines of code
	     * will allow the Solaris production VM to garbage-collect
	     * the DefaultControlPanel.
	     */
	    if (menuItemCheck != null)
		menuItemCheck.removeItemListener(this);
            menuRate_1_4.removeItemListener(this);
            menuRate_1_2.removeItemListener(this);
            menuRate_8_1.removeItemListener(this);
            menuRate_4_1.removeItemListener(this);
            menuRate_2_1.removeItemListener(this);
            menuRate_1_1.removeItemListener(this);
	    buttonMedia.setPopupMenu(null);
        }
    
protected voidfindFrame()

        synchronized (localLock) {
            if (firstTime) {
                firstTime = false;
                Component c = getParent();
                while (!(c instanceof Frame) && c != null) {
                    c = c.getParent();
                }
                if (c instanceof Frame) {
                    parentFrame = (Frame)c;
                    ((Frame)c).addWindowListener(wl = new WindowAdapter() {
                        public void windowClosing(WindowEvent we) {
                            minicleanUp();
                        }
                    } );

                }
            }
        }
    
public voidgainChange(javax.media.GainChangeEvent event)

        boolean    boolMute;

        boolMute = gainControl.getMute ();
        buttonAudio.setValue ( boolMute );
    
public java.awt.InsetsgetInsets()

        Insets    insets;

        insets = new Insets (1,0,0,0);
        return ( insets );
    
private voidgetPlayerControls()

        Control    control;

        if (player == null)
            return;
        gainControl = player.getGainControl();

        control = player.getControl ( "javax.media.control.FramePositioningControl" );
        if ( control != null  &&  control instanceof javax.media.control.FramePositioningControl )
            controlFrame = (javax.media.control.FramePositioningControl) control;
/*
        Control [] controls = player.getControls();
        for (int i = 0; i < controls.length; i++) {
            Control ctl = controls[i];
            if (ctl instanceof GainControl) {
                gainControl = (GainControl)ctl;
            }
        }
*/
    
private voidinit()

        Time                duration;
        Dimension           dim;
        int                 i;
        int                 nCount;
        Control             arrControls [];
        TrackControl        trackControl;
        Format              format;
        VideoFormat         formatVideo;
        float               frameRate;
        MenuItem            menuItem;
        boolean             boolEnable;


        getPlayerControls();
        if (gainControl != null)
            gainControl.addGainChangeListener ( this );

        setBackground(colorBackground);
        setLayout ( new BorderLayout() );
        this.addComponentListener ( this );

        container = this;
        panelLeft = new TransparentPanel ( new GridLayout(1,0) );
        container.add ( panelLeft, BorderLayout.WEST );
        panelRight = new TransparentPanel ( new GridLayout(1,0) );
        container.add ( panelRight, BorderLayout.EAST );
        panelProgress = new TransparentPanel ( new BorderLayout() );
        container.add ( panelProgress, BorderLayout.CENTER );

        buttonPlay = new ButtonComp ( "Play",
                                      "play.gif", "play-active.gif", "play-pressed.gif", "play-disabled.gif",
                                      "pause.gif", "pause-active.gif", "pause-pressed.gif", "pause-disabled.gif" );
        buttonPlay.setActionListener ( this );
        panelLeft.add ( buttonPlay );

//        if ( player instanceof com.sun.media.BasicPlayer
//                &&  ((com.sun.media.BasicPlayer)player).isFramePositionable() ) {
        if ( controlFrame != null ) {
            buttonStepBack = new ButtonComp ( "StepBack",
                                          "step-back.gif", "step-back-active.gif", "step-back-pressed.gif", "step-back-disabled.gif",
                                          "step-back.gif", "step-back-active.gif", "step-back-pressed.gif", "step-back-disabled.gif" );
            buttonStepBack.setActionListener ( this );
            buttonStepBack.setContMousePress ( true );
            panelLeft.add ( buttonStepBack );

            buttonStepFwd = new ButtonComp ( "StepForward",
                                          "step-fwd.gif", "step-fwd-active.gif", "step-fwd-pressed.gif", "step-fwd-disabled.gif",
                                          "step-fwd.gif", "step-fwd-active.gif", "step-fwd-pressed.gif", "step-fwd-disabled.gif" );
            buttonStepFwd.setActionListener ( this );
            buttonStepFwd.setContMousePress ( true );
            panelLeft.add ( buttonStepFwd );
        }

        if (gainControl != null) {
            buttonAudio = new AudioButton ( gainControl );
            buttonAudio.setActionListener ( this );
            panelRight.add ( buttonAudio );
        }
	
        buttonMedia = new ButtonComp ( "Media",
                                      "media.gif", "media-active.gif", "media-pressed.gif", "media-disabled.gif",
                                      "media.gif", "media-active.gif", "media-pressed.gif", "media-disabled.gif" );
        buttonMedia.setActionListener ( this );
        panelRight.add ( buttonMedia );

        progressSlider = new ProgressSlider( "mediatime", this, player );
        progressSlider.setActionListener ( this );
        panelProgress.add ( progressSlider, BorderLayout.CENTER );
        duration = player.getDuration ();
        if (duration == Duration.DURATION_UNBOUNDED  ||  duration == Duration.DURATION_UNKNOWN )
            progressSlider.setEnabled(false);

        updateButtonState ();

	// Add GUI controls for audio
/*
	if (gainControl != null) {
	    audioControls = new GainControlComponent(gainControl);
	    add(audioControls);
	    minimumWidth += audioControls.getPreferredSize().width;
	}
*/

        validate();
        dim = this.getPreferredSize ();
        this.setSize ( dim );
        setVisible ( true );
        setBackgroundTile ( BasicComp.fetchImage("texture3.gif") );
        player.addControllerListener ( this );

        arrControls = player.getControls ();
        nCount = arrControls.length;
        for ( i = 0;  i < nCount;  i++ ) {
            if ( !(arrControls[i] instanceof TrackControl) )
                continue;
            trackControl = (TrackControl) arrControls[i];
            format = trackControl.getFormat ();

            if ( format instanceof AudioFormat ) {
                vectorTracksAudio.addElement ( trackControl );
            }
            else if ( format instanceof VideoFormat ) {
                vectorTracksVideo.addElement ( trackControl );
                formatVideo = (VideoFormat) format;
                frameRate = formatVideo.getFrameRate ();
                lFrameStep = (long)(Time.ONE_SECOND / frameRate);
            }
        }

        menuPopup = new PopupMenu ( MENU_MEDIA );
        buttonMedia.setPopupMenu ( menuPopup );

        nCount = vectorTracksAudio.size ();
	// The audio icon for the first enabled track is
	// enabled. For all the remaining tracks, the
	// audio icon is set to muted state.
	boolean aTrackAudioIconEnabled = false;
        if ( nCount > 1 ) {
            for ( i = 0;  i < nCount;  i++ ) {
                trackControl = (TrackControl)vectorTracksAudio.elementAt ( i );
		boolEnable = false;
		if (!aTrackAudioIconEnabled && trackControl.isEnabled()) {
		    aTrackAudioIconEnabled = true;
		    boolEnable = true;
		}
                menuItemCheck = new CheckboxMenuItem ( MENU_AUDIO + " " + i, boolEnable );
                muteAudioTrack ( trackControl, !boolEnable );
                menuItemCheck.addItemListener ( this );
                menuPopup.add ( menuItemCheck );
            }
            menuPopup.addSeparator ();
        }
/*
        nCount = vectorTracksVideo.size ();
        if ( nCount > 1 ) {
            for ( i = 0;  i < nCount;  i++ ) {
                menuItemCheck = new CheckboxMenuItem ( "Video " + i, true );
                menuItemCheck.addItemListener ( this );
                menuPopup.add ( menuItemCheck );
            }
            menuPopup.addSeparator ();
        }
*/
        menuRate_1_4 = new CheckboxMenuItem ( MENU_RATE_1_4, false );
        menuRate_1_4.addItemListener ( this );
        menuPopup.add ( menuRate_1_4 );
        menuRate_1_2 = new CheckboxMenuItem ( MENU_RATE_1_2, false );
        menuRate_1_2.addItemListener ( this );
        menuPopup.add ( menuRate_1_2 );
        menuRate_1_1 = new CheckboxMenuItem ( MENU_RATE_1_1, true );
        menuRate_1_1.addItemListener ( this );
        menuPopup.add ( menuRate_1_1 );
        menuRate_2_1 = new CheckboxMenuItem ( MENU_RATE_2_1, false );
        menuRate_2_1.addItemListener ( this );
        menuPopup.add ( menuRate_2_1 );
        menuRate_4_1 = new CheckboxMenuItem ( MENU_RATE_4_1, false );
        menuRate_4_1.addItemListener ( this );
        menuPopup.add ( menuRate_4_1 );
        menuRate_8_1 = new CheckboxMenuItem ( MENU_RATE_8_1, false );
        menuRate_8_1.addItemListener ( this );
        menuPopup.add ( menuRate_8_1 );
//        menuPopup.addSeparator ();

//        menuItem = new MenuItem ( MENU_PROPERTIES );
//        menuItem.addActionListener ( this );
//        menuPopup.add ( menuItem );
    
public voiditemStateChanged(java.awt.event.ItemEvent event)

        ItemSelectable  item;
        Object          objectItem;
        String          strItem;
        int             nIndex;
        TrackControl    trackControl;
        boolean         boolEnabled;
        int             nState;
    

        item = event.getItemSelectable ();
        nState = event.getStateChange ();
        objectItem = event.getItem ();
        if ( item == menuRate_1_4  &&  nState == ItemEvent.SELECTED ) {
            menuRate_1_4.setState ( false ); // will be set only if successfull, i.e. on RateChangeEvent
            player.setRate ( (float)0.25 );
        }
        else if ( item == menuRate_1_2  &&  nState == ItemEvent.SELECTED ) {
            menuRate_1_2.setState ( false );
            player.setRate ( (float)0.5 );
        }
        else if ( item == menuRate_1_1  &&  nState == ItemEvent.SELECTED ) {
            menuRate_1_1.setState ( false );
            player.setRate ( (float)1.0 );
        }
        else if ( item == menuRate_2_1  &&  nState == ItemEvent.SELECTED ) {
            menuRate_2_1.setState ( false );
            player.setRate ( (float)2.0 );
        }
        else if ( item == menuRate_4_1  &&  nState == ItemEvent.SELECTED ) {
            menuRate_4_1.setState ( false );
            player.setRate ( (float)4.0 );
        }
        else if ( item == menuRate_8_1  &&  nState == ItemEvent.SELECTED ) {
            menuRate_8_1.setState ( false );
            player.setRate ( (float)8.0 );
        }
        else if ( objectItem instanceof String ) {
            strItem = (String) objectItem;
            if ( strItem.substring(0,5).equalsIgnoreCase(MENU_AUDIO) ) {
                nIndex = Integer.valueOf(strItem.substring(6)).intValue();
                trackControl = (TrackControl)vectorTracksAudio.elementAt ( nIndex );
                boolEnabled = event.getStateChange() == ItemEvent.SELECTED;
                muteAudioTrack ( trackControl, !boolEnabled );
            }
            else if ( strItem.substring(0,5).equalsIgnoreCase(MENU_VIDEO) ) {
            }
        }
    
public voidminicleanUp()

        synchronized (localLock) {
            firstTime = true;
        }
    
private voidmuteAudioTrack(javax.media.control.TrackControl trackControl, boolean boolMute)

        int               i;
        int               nCount;
        Object            arrControls [];

        arrControls = trackControl.getControls ();
        nCount = arrControls.length;
        for ( i = 0;  i < nCount;  i++ ) {
            if ( arrControls[i] instanceof GainControl )
                ((GainControl)arrControls[i]).setMute ( boolMute );
        }
    
public voidpaint(java.awt.Graphics g)
Overridden Component methods

        if (firstTime) {
            findFrame();
        }
        super.paint(g);
    
private voidplayStep(boolean boolFwd)

        long            lValue;

        if ( controlFrame == null )
            return;

        if (player.getTargetState() == Controller.Started) {
            buttonPlay.setEnabled(false);
            player.stop();
        }
        controlFrame.skip ( (boolFwd?1:(-1)) );
    
private voidplayStop()

        long    lDuration;
        long    lMedia;

        boolean state = buttonPlay.getValue();

        synchronized (localLock) {
            if (player == null || buttonPlay == null)
                return;
            if (state) {
                if (player.getTargetState() == Controller.Started) {
                    // Ignore
                }
                else {
                    buttonPlay.setEnabled(false);

                    lDuration = player.getDuration().getNanoseconds();
                    lMedia = player.getMediaNanoseconds();
                    if ( lMedia >= lDuration )
                        player.setMediaTime ( new Time(0) );
                    player.start();
                }
            }
            else {
                if (player.getTargetState() == Controller.Started) {
                    buttonPlay.setEnabled(false);
                    player.stop();
                }
                else {
                    // Ignore
                }
            }
        }
    
public voidremoveNotify()

//        cleanUp();
        super.removeNotify ();
        if ( boolAdded == true ) {
//            container.remove ( panelLeft );
//            container.remove ( panelRight );
//            container.remove ( panelProgress );
//            if ( container != this )
//                this.remove ( container );
            boolAdded = false;
        }
    
protected voidremovePlayButton()

        panelLeft.remove(buttonPlay);
    
voidresetPauseCount()

        pausecnt = -1;
    
private voidshowPropsSheet()

        Point       point;
        Dimension   dim;

        if (propsSheet == null) {
            try {
                propsSheet = new PropertySheet ( parentFrame, player );
                if ( this.isShowing() ) {
                    point = this.getLocationOnScreen ();
                    dim = this.getSize ();
                    point.y += dim.height;
                    propsSheet.setLocation ( point );
                }
            }
            catch (Exception e) {
                propsSheet = null;
            }
        }

        if (propsSheet != null) {
            propsSheet.setVisible(true);
        }

    
voidupdate()

        if ( propsSheet == null || player == null )
            return;
	
        if ( player.getState() == Controller.Started ) {
            pausecnt = -1;
            propsSheet.update();
        }
        else {
            if (pausecnt < 5 ) {
                pausecnt ++;
                propsSheet.update();
            }
            else if ( pausecnt == 5 ) {
                pausecnt++;
                propsSheet.clearBRFR();
            }
            else if ( resetMediaTimeinPause ) {
                resetMediaTimeinPause = false;
                propsSheet.updateMediaTime();
            }
        }
	
    
private voidupdateButtonState()

        if ( player == null ) {
            buttonPlay.setEnabled ( false );
        }
        else {
            buttonPlay.setEnabled ( true );
            if ( player.getState() == Controller.Started )
                buttonPlay.setValue ( true );
            else
                buttonPlay.setValue ( false );
        }