001    package org.rakeshv.applications;
002    
003    import java.awt.BorderLayout;
004    import java.awt.Dimension;
005    import java.awt.Toolkit;
006    import java.awt.event.ActionEvent;
007    import java.awt.event.ActionListener;
008    import java.awt.event.KeyEvent;
009    import java.io.File;
010    import java.util.ArrayList;
011    import java.util.List;
012    import java.util.ListIterator;
013    
014    import javax.swing.KeyStroke;
015    import javax.swing.JFileChooser;
016    import javax.swing.JInternalFrame;
017    import javax.swing.JMenu;
018    import javax.swing.JMenuBar;
019    import javax.swing.JMenuItem;
020    import javax.swing.JPanel;
021    
022    import org.rakeshv.gui.CloseableFrame;
023    import org.rakeshv.gui.SlideShow;
024    
025    /**
026            *       This class creates a <code>JFrame</code> which in turn will
027            *       contain as many internal <code>JInternalFrame(s)</code> as
028            *       required.  Each <code>JInternalFrame</code> will play all the
029            *       images that are found within the specified <code>directory
030            *       tree</code>.
031            *
032            *       @see org.rakeshv.gui.CloseableFrame
033            *
034            *       @author Rakesh Vidyadharan 18<sup><small>th</small></sup> July, 2002
035            *
036            *       <p>Copyright 2002, Rakesh Vidyadharan</p>
037            *
038            *       $Id: ImagePlayer.java,v 1.2 2004/05/26 11:42:36 rakesh Exp $
039            */
040    public class ImagePlayer extends CloseableFrame
041            implements ActionListener
042    {
043            /**
044                    *       A <code>List</code> that holds references to all the
045                    *       {@link org.rakeshv.gui.SlideShow SlideShow}
046                    *       <code>JInternalFrames</code> spawned by the application.
047                    */
048            List framesList = new ArrayList();
049    
050            /**
051                    *       The <code>JMenu</code> that holds a list of all the 
052                    *       {@link org.rakeshv.gui.SlideShow SlideShow}
053                    *       <code>JInternalFrames</code> spawned by the application.
054                    *       This menu can be used to switch between the individual 
055                    *       <code>frames</code>.
056                    */
057            JMenu slideShowsMenu = new JMenu( "SlideShows" );
058    
059            /**
060                    *       Default constructor.  Just calls {@link #ImagePlayer( String )
061                    *       ImagePlayer( String )} <code>constructor</code>.
062                    */
063            public ImagePlayer()
064            {
065                    this( "Image Player" );
066            }
067    
068            /**
069                    *       Creates a new <code>Frame</code> with the specified title.
070                    *
071                    *       <p>Creates a <code>Play menu item</code> that displays a 
072                    *       <code>JFileChooser<code> which prompts the user to select the
073                    *       desired <code>directory tree</code> from which to start playing
074                    *       images.  Each call to <code>Play</code> will create a new
075                    *       {@link org.rakeshv.gui.SlideShow SlideShow} which will display 
076                    *       the <code>image files</code> sequentially, in an <code>endless 
077                    *       loop</code>.</p>
078                    *
079                    *       <p>Each time a new {@link org.rakeshv.gui.SlideShow SlideShow}
080                    *       instance is created, a <code>JMenuItem</code> is created and
081                    *       added to the {@link #slideShowsMenu} <code>menu</code>.  The 
082        *   <code>menu</code> can be used to switch between the 
083        *   <code>JInternalFrames</code>.</p>
084                    *
085                    *       @param title - The title to set for the main frame.
086                    */
087            public ImagePlayer( String title )
088            {
089                    super( title );
090    
091                    // Set the content panel
092                    JPanel panel = new JPanel( new BorderLayout() );
093                    this.setContentPane( panel );
094    
095                    // Create the menu bar
096                    JMenuBar menuBar = new JMenuBar();
097    
098                    // Set up the programme menu
099                    JMenu programmeMenu = new JMenu( "Programme" );
100                    programmeMenu.setMnemonic( KeyEvent.VK_P );
101                    programmeMenu.setAccelerator( 
102                            KeyStroke.getKeyStroke( KeyEvent.VK_P, ActionEvent.CTRL_MASK )
103                    );
104    
105                    // Create the menu items for the programme menu
106                    JMenuItem playItem = new JMenuItem( "Play files" );
107                    playItem.setActionCommand( "play" );
108                    playItem.addActionListener( this );
109    
110                    JMenuItem exitItem = new JMenuItem( "Exit" );
111                    exitItem.setActionCommand( "exit" );
112                    exitItem.addActionListener( this );
113    
114                    // Add the menu items to the programme menu
115                    programmeMenu.add( playItem );
116                    programmeMenu.addSeparator();
117                    programmeMenu.add( exitItem );
118    
119                    // Set up the slide shows menu
120                    slideShowsMenu.setMnemonic( KeyEvent.VK_S );
121                    slideShowsMenu.setAccelerator( 
122                            KeyStroke.getKeyStroke( KeyEvent.VK_S, ActionEvent.CTRL_MASK )
123                    );
124    
125                    menuBar.add( programmeMenu );
126                    menuBar.add( slideShowsMenu );
127            }
128    
129            /**
130                    *       Creates an instance of <code>ImagePlayer</code> and set its
131                    *       initial size and layout.  No <code>command-line</code>
132                    *       arguments are processed.
133                    */
134            public static void main( String[] args )
135            {
136                    ImagePlayer ip = new ImagePlayer( "Image Player" );
137    
138                    // Set the frame size
139                    Toolkit toolkit = Toolkit.getDefaultToolkit();
140                    Dimension screenSize = toolkit.getScreenSize();
141                    ip.setBounds( 
142                            (int) ( screenSize.width * 0.1 ),
143                            (int) ( screenSize.height * 0.1 ),
144                            (int) ( screenSize.width * 0.8 ),
145                            (int) ( screenSize.height * 0.8 )
146                    );
147    
148                    ip.setVisible( true );
149            }
150    
151            /**
152                    *       The method from <code>java.awt.event.ActionListener</code>
153                    *       that handles <code>events</code> that are fired by the
154                    *       components in the <code>menu bar</code>.
155                    *
156                    *       <p>The <code>Play</code> event fires an action that displays
157                    *       a <code>JFileChooser</code> that displays directories.  A
158                    *       new <code>JInternalFrame</code> will be created that starts
159                    *       showing images in the selected directory.</p>
160                    *
161                    *       <p>The <code>Exit</code> event fires an action that closes all
162                    *       the <code>JInternalFrames</code> that are displaying images,
163                    *       and then closes the application.</p>
164                    */
165            public void actionPerformed( ActionEvent e )
166            {
167                    String command = e.getActionCommand();
168    
169                    if ( command.equals( "play" ) )
170                    {
171                            // Popup a file dialogue prompting user for the directory
172                            // tree from which to start displaying images.
173                            JFileChooser chooser = new JFileChooser();
174                            chooser.setDialogTitle( "Choose image directory" );
175                            chooser.setDialogType( JFileChooser.DIRECTORIES_ONLY );
176                            int returnValue = chooser.showOpenDialog( this );
177                            if ( returnValue == JFileChooser.APPROVE_OPTION )
178                            {
179                                    File selectedFile = chooser.getSelectedFile();
180                                    String fileName = selectedFile.getName();
181                                    JInternalFrame internalFrame = 
182                                            new JInternalFrame( fileName, true, true, true, true );
183                                    SlideShow slideShow = new SlideShow( selectedFile );
184                                    framesList.add( internalFrame );
185                                    slideShowsMenu.add( fileName );
186                            }
187                    }
188                    else if ( command.equals( "exit" ) )
189                    {
190                            // Loop through and close each internal frame
191                            for ( ListIterator iterator = framesList.listIterator(); iterator.hasNext(); )
192                            {
193                                    ( ( JInternalFrame ) iterator.next() ).doDefaultCloseAction();
194                            }
195                            System.exit( 0 );
196                    }
197            }
198    }