001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030/**
031 * 
032 */
033package org.openimaj.demos.video.utils;
034
035import java.awt.GridBagConstraints;
036import java.awt.GridBagLayout;
037import java.awt.event.ActionEvent;
038import java.awt.event.ActionListener;
039import java.io.IOException;
040
041import javax.swing.BorderFactory;
042import javax.swing.ButtonGroup;
043import javax.swing.JFileChooser;
044import javax.swing.JPanel;
045import javax.swing.JRadioButton;
046
047import org.openimaj.demos.video.VideoProcessingDemo;
048
049/**
050 *  @author David Dupplaw (dpd@ecs.soton.ac.uk)
051 *      
052 *      @created 28 Sep 2011
053 */
054public class SourcePanel extends JPanel
055{
056        /** */
057    private static final long serialVersionUID = 1L;
058        private JRadioButton webcamButton;
059        private JRadioButton fileButton;
060        private VideoProcessingDemo vpd;
061
062        /**
063         *      Default constructor that takes the video processing demo for
064         *      which this is the selection panel.
065         *  @param vpd The demo
066         */
067    public SourcePanel( VideoProcessingDemo vpd )
068    {
069        this.vpd = vpd;
070        init();
071    }
072    
073    private void init()
074    {
075        this.setLayout( new GridBagLayout() );
076        this.setBorder( BorderFactory.createTitledBorder( "Source" ) );
077        
078        GridBagConstraints gbc = new GridBagConstraints();
079        gbc.fill = GridBagConstraints.HORIZONTAL;
080        gbc.gridx = gbc.gridy = 0;
081        gbc.weightx = 1; gbc.weighty = 0;
082        
083        webcamButton = new JRadioButton("Webcam");
084        fileButton   = new JRadioButton("File");
085        
086        ButtonGroup bg = new ButtonGroup();
087        bg.add( webcamButton );
088        bg.add( fileButton );
089        
090        webcamButton.addActionListener( new ActionListener()
091                {
092                        @Override
093                        public void actionPerformed( ActionEvent e )
094                        {
095                                try
096                {
097                        vpd.useWebcam();
098                }
099                catch( IOException e1 )
100                {
101                        e1.printStackTrace();
102                }
103                        }
104                });
105
106        fileButton.addActionListener( new ActionListener()
107                {
108                        @Override
109                        public void actionPerformed( ActionEvent e )
110                        {
111                                JFileChooser jfc = new JFileChooser();
112        
113                            int returnVal = jfc.showOpenDialog( vpd );              
114                            if( returnVal == JFileChooser.APPROVE_OPTION ) 
115                            {                           
116                                        vpd.useFile( jfc.getSelectedFile() );
117                            }
118                        }
119                });
120        
121        this.add( webcamButton, gbc );
122        gbc.gridy++;
123        this.add( fileButton, gbc );
124    }
125}