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 */
030package org.openimaj.demos.campusview;
031
032import java.awt.GridBagConstraints;
033import java.awt.GridBagLayout;
034import java.awt.Insets;
035import java.awt.event.ActionEvent;
036import java.awt.event.ActionListener;
037import java.io.File;
038
039import javax.swing.BorderFactory;
040import javax.swing.JButton;
041import javax.swing.JFileChooser;
042import javax.swing.JLabel;
043import javax.swing.JPanel;
044import javax.swing.JTextField;
045import javax.swing.SwingConstants;
046
047/**
048 * JPanel with controls for capturing
049 * 
050 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
051 *
052 */
053public class CaptureControls extends JPanel {
054        private static final long serialVersionUID = 1L;
055        
056        private JTextField capHeightField;
057        private JTextField capWidthField;
058        private JTextField capRateTextField;
059        private JTextField batchID;
060        private JTextField operator;
061        
062        private CaptureControlsDelegate delegate;
063
064        private JLabel metaFile;
065        private JLabel imageDir;
066
067        private JButton stopBatch;
068
069        private JButton startBatch;
070        
071        /**
072         * Create the panel.
073         */
074        public CaptureControls() {
075                
076                setLayout( new GridBagLayout() );
077                
078                final JPanel batchControls = new JPanel(new GridBagLayout());
079                batchControls.setBorder( BorderFactory.createTitledBorder( "Batch Controls" ) );
080                final JPanel captureControls = new JPanel(new GridBagLayout());
081                captureControls.setBorder( BorderFactory.createTitledBorder( "Capture Controls" ) );
082                
083                // setBackground( new Color(255,255,255,210) );
084                
085                GridBagLayout gridBagLayout = new GridBagLayout();
086//              gridBagLayout.columnWidths = new int[]{123, 97, 217, 77, 0};
087//              gridBagLayout.rowHeights = new int[]{16, 16, 16, 21, 1, 16, 28, 0};
088//              gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
089//              gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
090                setLayout(gridBagLayout);
091                setBorder( BorderFactory.createEmptyBorder( 10,10,10,10 ) );
092
093                GridBagConstraints gbc = new GridBagConstraints();
094                JLabel batchIdL = new JLabel( "Batch Identifier: ");
095                gbc.insets = new Insets(0, 0, 5, 5);
096                gbc.anchor = GridBagConstraints.EAST;
097                gbc.gridx = 0;
098                gbc.gridy = 0;
099                batchControls.add( batchIdL, gbc );
100                
101                batchID = new JTextField("1");
102                gbc.gridx++;
103                gbc.fill = GridBagConstraints.HORIZONTAL;
104                gbc.anchor = GridBagConstraints.NORTH;
105                gbc.weightx = 1;
106                batchControls.add( batchID, gbc );
107                
108                JLabel operatorLabel = new JLabel( "Operator: ");
109                gbc.gridx = 0; gbc.weightx = 0;
110                gbc.anchor = GridBagConstraints.EAST;
111                gbc.fill = GridBagConstraints.NONE;
112                gbc.gridy++;
113                batchControls.add( operatorLabel, gbc );
114                
115                operator = new JTextField("Jon");
116                gbc.fill = GridBagConstraints.HORIZONTAL;
117                gbc.anchor = GridBagConstraints.NORTH;
118                gbc.gridx++;
119                gbc.weightx = 1;
120                batchControls.add( operator, gbc );
121                
122                JLabel lblCaptureWidth = new JLabel("Capture Width:");
123                lblCaptureWidth.setHorizontalAlignment(SwingConstants.TRAILING);
124                GridBagConstraints gbc_lblCaptureWidth = new GridBagConstraints();
125                gbc_lblCaptureWidth.anchor = GridBagConstraints.EAST;
126                gbc_lblCaptureWidth.insets = new Insets(0, 0, 5, 5);
127                gbc_lblCaptureWidth.gridx = 0;
128                gbc_lblCaptureWidth.gridy = 10;
129                batchControls.add(lblCaptureWidth, gbc_lblCaptureWidth);
130                
131                capWidthField = new JTextField();
132                capWidthField.setColumns(10);
133                GridBagConstraints gbc_capWidthField = new GridBagConstraints();
134                gbc_capWidthField.fill = GridBagConstraints.HORIZONTAL;
135                gbc_capWidthField.anchor = GridBagConstraints.NORTH;
136                gbc_capWidthField.insets = new Insets(0, 0, 5, 5);
137                gbc_capWidthField.gridx = 1;
138                gbc_capWidthField.gridy = 10;
139                gbc_capWidthField.weightx = 1;
140                batchControls.add(capWidthField, gbc_capWidthField);
141                
142                JLabel lblNewLabel = new JLabel("Capture Height:");
143                lblNewLabel.setHorizontalAlignment(SwingConstants.TRAILING);
144                GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
145                gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
146                gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
147                gbc_lblNewLabel.gridx = 0;
148                gbc_lblNewLabel.gridy = 20;
149                batchControls.add(lblNewLabel, gbc_lblNewLabel);
150                
151                capHeightField = new JTextField();
152                GridBagConstraints gbc_capHeightField = new GridBagConstraints();
153                gbc_capHeightField.anchor = GridBagConstraints.NORTH;
154                gbc_capHeightField.fill = GridBagConstraints.HORIZONTAL;
155                gbc_capHeightField.insets = new Insets(0, 0, 5, 5);
156                gbc_capHeightField.gridx = 1;
157                gbc_capHeightField.gridy = 20;
158                gbc_capHeightField.weightx = 1;
159                batchControls.add(capHeightField, gbc_capHeightField);
160                capHeightField.setColumns(10);
161                
162                JLabel lblCaptureRate = new JLabel("Capture Rate (secs):");
163                lblCaptureRate.setHorizontalAlignment(SwingConstants.TRAILING);
164                GridBagConstraints gbc_lblCaptureRate = new GridBagConstraints();
165                gbc_lblCaptureRate.anchor = GridBagConstraints.EAST;
166                gbc_lblCaptureRate.insets = new Insets(0, 0, 5, 5);
167                gbc_lblCaptureRate.gridx = 0;
168                gbc_lblCaptureRate.gridy = 30;
169                gbc_lblCaptureRate.weightx = 1;
170                batchControls.add(lblCaptureRate, gbc_lblCaptureRate);
171                
172                capRateTextField = new JTextField("5");
173                capRateTextField.setColumns(10);
174                GridBagConstraints gbc_textField = new GridBagConstraints();
175                gbc_textField.anchor = GridBagConstraints.NORTH;
176                gbc_textField.fill = GridBagConstraints.HORIZONTAL;
177                gbc_textField.insets = new Insets(0, 0, 5, 5);
178                gbc_textField.gridx = 1;
179                gbc_textField.gridy = 30;
180                batchControls.add(capRateTextField, gbc_textField);
181                
182                JLabel lblImageDirectory = new JLabel("Image Directory:");
183                lblImageDirectory.setHorizontalAlignment(SwingConstants.TRAILING);
184                GridBagConstraints gbc_lblImageDirectory = new GridBagConstraints();
185                gbc_lblImageDirectory.fill = GridBagConstraints.HORIZONTAL;
186                gbc_lblImageDirectory.insets = new Insets(0, 0, 5, 5);
187                gbc_lblImageDirectory.gridx = 0;
188                gbc_lblImageDirectory.gridy = 40;
189                batchControls.add(lblImageDirectory, gbc_lblImageDirectory);
190                
191                imageDir = new JLabel( System.getProperty("user.home")+"/campusview/images" );
192                GridBagConstraints gbc_imageDir = new GridBagConstraints();
193                gbc_imageDir.gridwidth = 2;
194                gbc_imageDir.fill = GridBagConstraints.HORIZONTAL;
195                gbc_imageDir.insets = new Insets(0, 0, 5, 5);
196                gbc_imageDir.gridx = 1;
197                gbc_imageDir.gridy = 40;
198                batchControls.add(imageDir, gbc_imageDir);
199                
200                JButton btnSetDir = new JButton("Set");
201                GridBagConstraints gbc_btnSetDir = new GridBagConstraints();
202                gbc_btnSetDir.anchor = GridBagConstraints.WEST;
203                gbc_btnSetDir.insets = new Insets(0, 0, 5, 0);
204                gbc_btnSetDir.gridx = 3;
205                gbc_btnSetDir.gridy = 40;
206                batchControls.add(btnSetDir, gbc_btnSetDir);
207                btnSetDir.addActionListener( new ActionListener()
208                {                       
209                        @Override
210                        public void actionPerformed( ActionEvent e )
211                        {
212                            JFileChooser chooser = new JFileChooser(); 
213                            chooser.setCurrentDirectory( new File(imageDir.getText()) );
214                            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
215                            chooser.setAcceptAllFileFilterUsed( false );
216                            if( chooser.showSaveDialog( CaptureControls.this ) == JFileChooser.APPROVE_OPTION )
217                                imageDir.setText( chooser.getSelectedFile().getAbsolutePath() );
218                        }
219                } );
220                
221//              JLabel lblMetadataFile = new JLabel("Metadata File:");
222//              lblMetadataFile.setHorizontalAlignment(SwingConstants.TRAILING);
223//              GridBagConstraints gbc_lblMetadataFile = new GridBagConstraints();
224//              gbc_lblMetadataFile.fill = GridBagConstraints.HORIZONTAL;
225//              gbc_lblMetadataFile.insets = new Insets(0, 0, 5, 5);
226//              gbc_lblMetadataFile.gridx = 0;
227//              gbc_lblMetadataFile.gridy = 50;
228//              batchControls.add(lblMetadataFile, gbc_lblMetadataFile);
229//              
230//              metaFile = new JLabel(System.getProperty("user.home")+"/campusview/metadata.csv");
231//              GridBagConstraints gbc_metaFile = new GridBagConstraints();
232//              gbc_metaFile.gridwidth = 2;
233//              gbc_metaFile.anchor = GridBagConstraints.WEST;
234//              gbc_metaFile.insets = new Insets(0, 0, 5, 5);
235//              gbc_metaFile.gridx = 1;
236//              gbc_metaFile.gridy = 50;
237//              batchControls.add(metaFile, gbc_metaFile);
238//              
239//              JButton btnSetFile = new JButton("Set");
240//              GridBagConstraints gbc_btnSetFile = new GridBagConstraints();
241//              gbc_btnSetFile.insets = new Insets(0, 0, 5, 0);
242//              gbc_btnSetFile.anchor = GridBagConstraints.WEST;
243//              gbc_btnSetFile.gridx = 3;
244//              gbc_btnSetFile.gridy = 50;
245//              batchControls.add(btnSetFile, gbc_btnSetFile);
246//              btnSetFile.addActionListener( new ActionListener()
247//              {                       
248//                      @Override
249//                      public void actionPerformed( ActionEvent e )
250//                      {
251//                          JFileChooser chooser = new JFileChooser(); 
252//                          chooser.setCurrentDirectory( new File(metaFile.getText()) );
253//                          chooser.setAcceptAllFileFilterUsed( false );
254//                          if( chooser.showSaveDialog( CaptureControls.this ) == JFileChooser.APPROVE_OPTION )
255//                              metaFile.setText( chooser.getSelectedFile().getAbsolutePath() );
256//                      }
257//              } );
258                
259                JButton btnSnapshot = new JButton("Snapshot");
260                GridBagConstraints gbc_btnSnapshot = new GridBagConstraints();
261                gbc_btnSnapshot.anchor = GridBagConstraints.SOUTH;
262                gbc_btnSnapshot.fill = GridBagConstraints.HORIZONTAL;
263                gbc_btnSnapshot.insets = new Insets(0, 0, 0, 5);
264                gbc_btnSnapshot.gridx = 0;
265                gbc_btnSnapshot.gridy = 6;
266                btnSnapshot.addActionListener(new ActionListener() 
267                {
268                        @Override
269                        public void actionPerformed(ActionEvent e) {
270                                delegate.snapshot();
271                        }
272                        
273                });
274                captureControls.add(btnSnapshot, gbc_btnSnapshot);
275                btnSnapshot.setEnabled( false );
276                
277                final JButton btnStartRecording = new JButton("Start Recording");
278                final JButton btnStopRecording = new JButton("Stop Recording");
279                
280                GridBagConstraints gbc_btnStartRecording = new GridBagConstraints();
281                gbc_btnStartRecording.gridwidth = 1;
282                gbc_btnStartRecording.anchor = GridBagConstraints.SOUTHWEST;
283                gbc_btnStartRecording.insets = new Insets(0, 0, 0, 5);
284                gbc_btnStartRecording.gridx = 1;
285                gbc_btnStartRecording.gridy = 6;
286                captureControls.add(btnStartRecording, gbc_btnStartRecording);
287                btnStartRecording.setEnabled( false );
288                btnStartRecording.addActionListener( new ActionListener()
289                {
290                        @Override
291                        public void actionPerformed( ActionEvent e )
292                        {
293                                delegate.startRecording( getRate() );
294                                btnStopRecording.setEnabled( true );
295                                btnStartRecording.setEnabled( false );
296                                stopBatch.setEnabled( false );
297                        }
298                });
299
300                GridBagConstraints gbc_btnStopRecording = new GridBagConstraints();
301                gbc_btnStopRecording.gridwidth = 1;
302                gbc_btnStopRecording.anchor = GridBagConstraints.SOUTHWEST;
303                gbc_btnStopRecording.insets = new Insets(0, 0, 0, 5);
304                gbc_btnStopRecording.gridx = 2;
305                gbc_btnStopRecording.gridy = 6;
306                captureControls.add(btnStopRecording, gbc_btnStopRecording);
307                btnStopRecording.setEnabled( false );
308                btnStopRecording.addActionListener( new ActionListener()
309                {
310                        @Override
311                        public void actionPerformed( ActionEvent e )
312                        {
313                                delegate.stopRecording();
314                                btnStartRecording.setEnabled( true );
315                                stopBatch.setEnabled( true );
316                                btnStopRecording.setEnabled( false );
317                        }
318                });
319
320                gbc.gridx = 0;
321                gbc.gridy = 100;
322                gbc.weightx = 1; gbc.weighty = 0;
323                gbc.fill = GridBagConstraints.HORIZONTAL;
324                JPanel buttonPanel = new JPanel( new GridBagLayout() );
325                
326                startBatch = new JButton("Start Batch");
327                stopBatch = new JButton("Stop Batch");
328                startBatch.addActionListener( new ActionListener()
329                {
330                        @Override
331                        public void actionPerformed( ActionEvent e )
332                        {
333                                for( int i = 0; i < captureControls.getComponentCount(); i++ )
334                                        captureControls.getComponent( i ).setEnabled( true );
335                                for( int i = 0; i < batchControls.getComponentCount(); i++ )
336                                        batchControls.getComponent( i ).setEnabled( false );
337                                stopBatch.setEnabled( true );
338                                startBatch.setEnabled( false );
339                                btnStopRecording.setEnabled( false );
340                                
341                                // TODO: hard coded values here
342                                delegate.startBatch( getImageDir(), null, getOperator(), "Private" );
343                        }
344                });
345                buttonPanel.add( startBatch, gbc );
346                
347                stopBatch.setEnabled( false );
348                stopBatch.addActionListener( new ActionListener()
349                {
350                        @Override
351                        public void actionPerformed( ActionEvent e )
352                        {
353                                for( int i = 0; i < captureControls.getComponentCount(); i++ )
354                                        captureControls.getComponent( i ).setEnabled( false );
355                                for( int i = 0; i < batchControls.getComponentCount(); i++ )
356                                        batchControls.getComponent( i ).setEnabled( true );
357                                stopBatch.setEnabled( false );
358                                startBatch.setEnabled( true );
359                                batchID.setText( ""+(getBatchId()+1) );
360                                
361                                delegate.stopBatch();
362                        }
363                });
364                gbc.gridx++;
365                buttonPanel.add( stopBatch, gbc );
366                gbc.gridx = 0;
367                gbc.gridwidth = 3;
368                batchControls.add( buttonPanel, gbc );
369                
370                gbc.gridx = gbc.gridy = 0;
371                gbc.weightx = gbc.weighty = 1;
372                gbc.fill = GridBagConstraints.BOTH;
373                add( batchControls, gbc );
374                gbc.gridy++;
375                add( captureControls, gbc );
376        }
377
378        protected File getImageDir() {
379                return new File(imageDir.getText());
380        }
381
382        protected File getMetadataFile() {
383                return new File(metaFile.getText());
384        }
385
386        /**
387         * Set the delegate
388         * @param delegate
389         */
390        public void setDelegate(CaptureControlsDelegate delegate) {
391                this.delegate = delegate;
392        }
393        
394        /**
395         * @return the id of the batch
396         */
397        public int getBatchId()
398        {
399                return Integer.parseInt( batchID.getText() );
400        }
401        
402        /**
403         * @return the operators name
404         */
405        public String getOperator()
406        {
407                return operator.getText();
408        }
409        
410        /**
411         * @return the capture rate
412         */
413        public int getRate()
414        {
415                return Integer.parseInt( this.capRateTextField.getText() );
416        }
417}