jcaster_vaadin.Gui2.java Source code

Java tutorial

Introduction

Here is the source code for jcaster_vaadin.Gui2.java

Source

//    Copyright (C) 2011  Petri Tuononen
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package jcaster_vaadin;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import recording.Record;

import com.cattura.packet_multibroadcaster.constants.AudioVideoTypes;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

import configurations.CaptureSettings;

/**
 * 
 * Vaadin GUI for web.
 * 
 * @author Petri Tuononen
 *
 */
public class Gui2 extends CustomComponent {

    private static Record record;
    private CaptureSettings settings;
    @AutoGenerated
    private VerticalLayout mainLayout;
    @AutoGenerated
    private TabSheet tabSheet_1;
    @AutoGenerated
    private Panel audioPanel;
    @AutoGenerated
    private VerticalLayout verticalLayout_4;
    @AutoGenerated
    private GridLayout gridLayout_5;
    @AutoGenerated
    private NativeSelect slctSmplRt;
    @AutoGenerated
    private Label splRtLbl;
    @AutoGenerated
    private NativeSelect slctSmplSz;
    @AutoGenerated
    private Label smplSzLbl;
    @AutoGenerated
    private NativeSelect slctChnls;
    @AutoGenerated
    private Label chnlsLbl;
    @AutoGenerated
    private Panel videoPanel;
    @AutoGenerated
    private VerticalLayout verticalLayout_3;
    @AutoGenerated
    private GridLayout gridLayout_4;
    @AutoGenerated
    private NativeSelect slctCdc;
    @AutoGenerated
    private Label cdcLbl;
    @AutoGenerated
    private NativeSelect slctFrmt;
    @AutoGenerated
    private Label frmtLbl;
    @AutoGenerated
    private Panel generalPanel;
    @AutoGenerated
    private VerticalLayout verticalLayout_2;
    @AutoGenerated
    private GridLayout gridLayout_3;
    @AutoGenerated
    private TextField txtDrRcrd;
    @AutoGenerated
    private Label DrRcrdLbl;
    @AutoGenerated
    private TextField txtRrcdCtd;
    @AutoGenerated
    private Label RcrdCtdLbl;
    @AutoGenerated
    private NativeSelect slctRrcdTp;
    @AutoGenerated
    private Label rcrdLbl;
    @AutoGenerated
    private NativeButton brwBtn;
    @AutoGenerated
    private TextField txtSvLc;
    @AutoGenerated
    private Label svlcLbl;
    @AutoGenerated
    private TextField txtFlnm;
    @AutoGenerated
    private Label flnmLbl;
    @AutoGenerated
    private HorizontalLayout horizontalLayout_1;
    @AutoGenerated
    private NativeButton stpBtn;
    @AutoGenerated
    private NativeButton psBtn;
    @AutoGenerated
    private NativeButton rcrdBtn;
    @AutoGenerated
    private MenuBar menuBar_1;
    private static final long serialVersionUID = -8946951435673616428L;

    /**
     * The constructor should first build the main layout, set the
     * composition root and then do any custom initialization.
     *
     * The constructor will not be automatically regenerated by the
     * visual editor.
     */
    public Gui2() {
        buildMainLayout();
        setCompositionRoot(mainLayout);
    }

    @AutoGenerated
    private VerticalLayout buildMainLayout() {
        // common part: create layout
        mainLayout = new VerticalLayout();

        // top-level component properties
        setWidth("400px");
        setHeight("340px");

        // menuBar_1
        menuBar_1 = new MenuBar();
        menuBar_1.setWidth("100.0%");
        menuBar_1.setHeight("-1px");
        menuBar_1.setImmediate(false);
        mainLayout.addComponent(menuBar_1);

        // horizontalLayout_1
        horizontalLayout_1 = buildHorizontalLayout_1();
        mainLayout.addComponent(horizontalLayout_1);

        // tabSheet_1
        tabSheet_1 = buildTabSheet_1();
        mainLayout.addComponent(tabSheet_1);
        mainLayout.setExpandRatio(tabSheet_1, 1.0f);
        mainLayout.setComponentAlignment(tabSheet_1, new Alignment(48));

        return mainLayout;
    }

    @AutoGenerated
    private HorizontalLayout buildHorizontalLayout_1() {
        // common part: create layout
        horizontalLayout_1 = new HorizontalLayout();
        horizontalLayout_1.setWidth("100.0%");
        horizontalLayout_1.setHeight("60px");
        horizontalLayout_1.setImmediate(false);
        horizontalLayout_1.setMargin(false);
        horizontalLayout_1.setSpacing(true);

        // rcrdBtn
        rcrdBtn = new NativeButton();
        rcrdBtn.setWidth("100.0%");
        rcrdBtn.setHeight("40px");
        rcrdBtn.setCaption("Record");
        rcrdBtn.setImmediate(true);
        horizontalLayout_1.addComponent(rcrdBtn);
        horizontalLayout_1.setExpandRatio(rcrdBtn, 1.0f);
        horizontalLayout_1.setComponentAlignment(rcrdBtn, new Alignment(33));

        rcrdBtn.addListener(new Button.ClickListener() {
            private static final long serialVersionUID = 8186248537694916910L;

            public void buttonClick(Button.ClickEvent event) {
                record();
            }
        });

        // psBtn
        psBtn = new NativeButton();
        psBtn.setWidth("100.0%");
        psBtn.setHeight("40px");
        psBtn.setCaption("Pause");
        psBtn.setEnabled(false); //set disabled at start
        psBtn.setImmediate(true);
        horizontalLayout_1.addComponent(psBtn);
        horizontalLayout_1.setExpandRatio(psBtn, 1.0f);
        horizontalLayout_1.setComponentAlignment(psBtn, new Alignment(48));

        psBtn.addListener(new Button.ClickListener() {

            private static final long serialVersionUID = 675799252337301498L;

            public void buttonClick(Button.ClickEvent event) {
                pause();
            }
        });

        // stpBtn
        stpBtn = new NativeButton();
        stpBtn.setWidth("100.0%");
        stpBtn.setHeight("40px");
        stpBtn.setCaption("Stop");
        stpBtn.setEnabled(false); //set disabled at start
        stpBtn.setImmediate(true);
        horizontalLayout_1.addComponent(stpBtn);
        horizontalLayout_1.setExpandRatio(stpBtn, 1.0f);
        horizontalLayout_1.setComponentAlignment(stpBtn, new Alignment(34));

        stpBtn.addListener(new Button.ClickListener() {

            private static final long serialVersionUID = -3317641878189301674L;

            public void buttonClick(Button.ClickEvent event) {
                stop();
            }
        });

        return horizontalLayout_1;
    }

    @AutoGenerated
    private TabSheet buildTabSheet_1() {
        // common part: create layout
        tabSheet_1 = new TabSheet();
        tabSheet_1.setWidth("100.0%");
        tabSheet_1.setHeight("100.0%");
        tabSheet_1.setImmediate(true);

        // generalPanel
        generalPanel = buildGeneralPanel();
        tabSheet_1.addTab(generalPanel, "General settings", null);

        // videoPanel
        videoPanel = buildVideoPanel();
        tabSheet_1.addTab(videoPanel, "Video settings", null);

        // audioPanel
        audioPanel = buildAudioPanel();
        tabSheet_1.addTab(audioPanel, "Audio settings", null);

        return tabSheet_1;
    }

    @AutoGenerated
    private Panel buildGeneralPanel() {
        // common part: create layout
        generalPanel = new Panel();
        generalPanel.setWidth("100.0%");
        generalPanel.setHeight("100.0%");
        generalPanel.setImmediate(false);

        // verticalLayout_2
        verticalLayout_2 = buildVerticalLayout_2();
        generalPanel.setContent(verticalLayout_2);

        return generalPanel;
    }

    @AutoGenerated
    private VerticalLayout buildVerticalLayout_2() {
        // common part: create layout
        verticalLayout_2 = new VerticalLayout();
        verticalLayout_2.setWidth("100.0%");
        verticalLayout_2.setHeight("100.0%");
        verticalLayout_2.setImmediate(false);
        verticalLayout_2.setMargin(false);

        // gridLayout_3
        gridLayout_3 = buildGridLayout_3();
        verticalLayout_2.addComponent(gridLayout_3);
        verticalLayout_2.setComponentAlignment(gridLayout_3, new Alignment(48));

        return verticalLayout_2;
    }

    @AutoGenerated
    private GridLayout buildGridLayout_3() {
        // common part: create layout
        gridLayout_3 = new GridLayout();
        gridLayout_3.setWidth("100.0%");
        gridLayout_3.setHeight("100.0%");
        gridLayout_3.setImmediate(false);
        gridLayout_3.setMargin(false);
        gridLayout_3.setSpacing(true);
        gridLayout_3.setColumns(4);
        gridLayout_3.setRows(7);

        // flnmLbl
        flnmLbl = new Label();
        flnmLbl.setWidth("-1px");
        flnmLbl.setHeight("-1px");
        flnmLbl.setValue("Filename");
        flnmLbl.setImmediate(false);
        gridLayout_3.addComponent(flnmLbl, 0, 1);

        // txtFlnm
        txtFlnm = new TextField();
        txtFlnm.setWidth("-1px");
        txtFlnm.setHeight("-1px");
        txtFlnm.setImmediate(false);
        txtFlnm.setValue("test"); //set default filename
        gridLayout_3.addComponent(txtFlnm, 1, 1);

        // svlcLbl
        svlcLbl = new Label();
        svlcLbl.setWidth("-1px");
        svlcLbl.setHeight("-1px");
        svlcLbl.setValue("Save location");
        svlcLbl.setImmediate(false);
        gridLayout_3.addComponent(svlcLbl, 0, 2);

        // txtSvLc
        txtSvLc = new TextField();
        txtSvLc.setWidth("-1px");
        txtSvLc.setHeight("-1px");
        txtSvLc.setImmediate(false);
        txtSvLc.setValue(System.getProperty("user.home") + File.separator);
        gridLayout_3.addComponent(txtSvLc, 1, 2);

        // brwBtn
        brwBtn = new NativeButton();
        brwBtn.setWidth("-1px");
        brwBtn.setHeight("-1px");
        brwBtn.setCaption("Browse...");
        brwBtn.setImmediate(true);
        gridLayout_3.addComponent(brwBtn, 2, 2);

        brwBtn.addListener(new Button.ClickListener() {

            private static final long serialVersionUID = -8389582613309805795L;

            public void buttonClick(Button.ClickEvent event) {
                //            browse();
            }
        });

        // rcrdLbl
        rcrdLbl = new Label();
        rcrdLbl.setWidth("-1px");
        rcrdLbl.setHeight("-1px");
        rcrdLbl.setValue("Record type");
        rcrdLbl.setImmediate(false);
        gridLayout_3.addComponent(rcrdLbl, 0, 3);

        // slctRrcdTp
        slctRrcdTp = new NativeSelect();
        slctRrcdTp.setWidth("-1px");
        slctRrcdTp.setHeight("-1px");
        slctRrcdTp.setImmediate(false);
        slctRrcdTp.addItem("audio+video");
        slctRrcdTp.addItem("video only");
        slctRrcdTp.addItem("audio only");
        slctRrcdTp.setValue("audio+video"); //set default record type
        gridLayout_3.addComponent(slctRrcdTp, 1, 3);

        // RcrdCtdLbl
        RcrdCtdLbl = new Label();
        RcrdCtdLbl.setWidth("-1px");
        RcrdCtdLbl.setHeight("-1px");
        RcrdCtdLbl.setValue("Recording countdown");
        RcrdCtdLbl.setImmediate(false);
        gridLayout_3.addComponent(RcrdCtdLbl, 0, 4);

        // txtRrcdCtd
        txtRrcdCtd = new TextField();
        txtRrcdCtd.setWidth("-1px");
        txtRrcdCtd.setHeight("-1px");
        txtRrcdCtd.setValue(0);
        txtRrcdCtd.setImmediate(false);
        gridLayout_3.addComponent(txtRrcdCtd, 1, 4);

        // DrRcrdLbl
        DrRcrdLbl = new Label();
        DrRcrdLbl.setWidth("-1px");
        DrRcrdLbl.setHeight("-1px");
        DrRcrdLbl.setValue("Duration of record");
        DrRcrdLbl.setImmediate(false);
        gridLayout_3.addComponent(DrRcrdLbl, 0, 5);

        // txtDrRcrd
        txtDrRcrd = new TextField();
        txtDrRcrd.setWidth("-1px");
        txtDrRcrd.setHeight("-1px");
        txtDrRcrd.setValue(0);
        txtDrRcrd.setImmediate(false);
        gridLayout_3.addComponent(txtDrRcrd, 1, 5);

        return gridLayout_3;
    }

    @AutoGenerated
    private Panel buildVideoPanel() {
        // common part: create layout
        videoPanel = new Panel();
        videoPanel.setWidth("100.0%");
        videoPanel.setHeight("100.0%");
        videoPanel.setImmediate(false);

        // verticalLayout_3
        verticalLayout_3 = buildVerticalLayout_3();
        videoPanel.setContent(verticalLayout_3);

        return videoPanel;
    }

    @AutoGenerated
    private VerticalLayout buildVerticalLayout_3() {
        // common part: create layout
        verticalLayout_3 = new VerticalLayout();
        verticalLayout_3.setWidth("100.0%");
        verticalLayout_3.setHeight("100.0%");
        verticalLayout_3.setImmediate(false);
        verticalLayout_3.setMargin(false);

        // gridLayout_4
        gridLayout_4 = buildGridLayout_4();
        verticalLayout_3.addComponent(gridLayout_4);

        return verticalLayout_3;
    }

    @AutoGenerated
    private GridLayout buildGridLayout_4() {
        // common part: create layout
        gridLayout_4 = new GridLayout();
        gridLayout_4.setWidth("100.0%");
        gridLayout_4.setHeight("100.0%");
        gridLayout_4.setImmediate(false);
        gridLayout_4.setMargin(false);
        gridLayout_4.setSpacing(true);
        gridLayout_4.setColumns(5);
        gridLayout_4.setRows(7);

        // frmtLbl
        frmtLbl = new Label();
        frmtLbl.setWidth("-1px");
        frmtLbl.setHeight("-1px");
        frmtLbl.setValue("Format");
        frmtLbl.setImmediate(false);
        gridLayout_4.addComponent(frmtLbl, 0, 1);

        // slctFrmt
        slctFrmt = new NativeSelect();
        slctFrmt.setWidth("-1px");
        slctFrmt.setHeight("-1px");
        slctFrmt.setImmediate(false);
        slctFrmt.addItem("mp4");
        slctFrmt.addItem("avi");
        slctFrmt.addItem("mov");
        slctFrmt.addItem("flv");
        slctFrmt.setValue("mp4"); //set default video format
        gridLayout_4.addComponent(slctFrmt, 1, 1);

        // cdcLbl
        cdcLbl = new Label();
        cdcLbl.setWidth("-1px");
        cdcLbl.setHeight("-1px");
        cdcLbl.setValue("Codec");
        cdcLbl.setImmediate(false);
        gridLayout_4.addComponent(cdcLbl, 0, 2);

        // slctCdc
        slctCdc = new NativeSelect();
        slctCdc.setWidth("-1px");
        slctCdc.setHeight("-1px");
        slctCdc.setImmediate(false);
        gridLayout_4.addComponent(slctCdc, 1, 2);

        return gridLayout_4;
    }

    @AutoGenerated
    private Panel buildAudioPanel() {
        // common part: create layout
        audioPanel = new Panel();
        audioPanel.setWidth("100.0%");
        audioPanel.setHeight("100.0%");
        audioPanel.setImmediate(false);

        // verticalLayout_4
        verticalLayout_4 = buildVerticalLayout_4();
        audioPanel.setContent(verticalLayout_4);

        return audioPanel;
    }

    @AutoGenerated
    private VerticalLayout buildVerticalLayout_4() {
        // common part: create layout
        verticalLayout_4 = new VerticalLayout();
        verticalLayout_4.setWidth("100.0%");
        verticalLayout_4.setHeight("100.0%");
        verticalLayout_4.setImmediate(false);
        verticalLayout_4.setMargin(false);

        // gridLayout_5
        gridLayout_5 = buildGridLayout_5();
        verticalLayout_4.addComponent(gridLayout_5);

        return verticalLayout_4;
    }

    @AutoGenerated
    private GridLayout buildGridLayout_5() {
        // common part: create layout
        gridLayout_5 = new GridLayout();
        gridLayout_5.setWidth("100.0%");
        gridLayout_5.setHeight("100.0%");
        gridLayout_5.setImmediate(false);
        gridLayout_5.setMargin(false);
        gridLayout_5.setSpacing(true);
        gridLayout_5.setColumns(4);
        gridLayout_5.setRows(7);

        // chnlsLbl
        chnlsLbl = new Label();
        chnlsLbl.setWidth("-1px");
        chnlsLbl.setHeight("-1px");
        chnlsLbl.setValue("Channels");
        chnlsLbl.setImmediate(false);
        gridLayout_5.addComponent(chnlsLbl, 0, 1);

        // slctChnls
        slctChnls = new NativeSelect();
        slctChnls.setWidth("-1px");
        slctChnls.setHeight("-1px");
        slctChnls.setImmediate(false);
        slctChnls.addItem(1);
        slctChnls.addItem(2);
        slctChnls.setValue(2); //set default number of channels
        gridLayout_5.addComponent(slctChnls, 1, 1);

        // smplSzLbl
        smplSzLbl = new Label();
        smplSzLbl.setWidth("-1px");
        smplSzLbl.setHeight("-1px");
        smplSzLbl.setValue("Sample size");
        smplSzLbl.setImmediate(false);
        gridLayout_5.addComponent(smplSzLbl, 0, 2);

        // slctSmplSz
        slctSmplSz = new NativeSelect();
        slctSmplSz.setWidth("-1px");
        slctSmplSz.setHeight("-1px");
        slctSmplSz.setImmediate(false);
        slctSmplSz.addItem("8-bit");
        slctSmplSz.addItem("16-bit");
        slctSmplSz.setValue("16-bit"); //set default sample size
        gridLayout_5.addComponent(slctSmplSz, 1, 2);

        // splRtLbl
        splRtLbl = new Label();
        splRtLbl.setWidth("-1px");
        splRtLbl.setHeight("-1px");
        splRtLbl.setValue("Sample rate");
        splRtLbl.setImmediate(false);
        gridLayout_5.addComponent(splRtLbl, 0, 3);

        // slctSmplRt
        slctSmplRt = new NativeSelect();
        slctSmplRt.setWidth("-1px");
        slctSmplRt.setHeight("-1px");
        slctSmplRt.setImmediate(false);
        slctSmplRt.addItem(8000);
        slctSmplRt.addItem(11025);
        slctSmplRt.addItem(16000);
        slctSmplRt.addItem(22050);
        slctSmplRt.addItem(44100);
        slctSmplRt.setValue(44100); //set default sample rate
        gridLayout_5.addComponent(slctSmplRt, 1, 3);

        return gridLayout_5;
    }

    /**
     * Setup recording process.
     */
    protected void setupRecording() {
        int captureDuration = 0;
        int time = getRecordingDuration(); //time in seconds
        //check if timed recording is set
        if (time > 0) {
            captureDuration = time * 1000; //time in milliseconds
        }
        try {
            String selectedAudioVideoType = getAudioVideoType();
            if (selectedAudioVideoType.equalsIgnoreCase(AudioVideoTypes.AUDIO_AND_VIDEO)) {
                settings = new CaptureSettings(AudioVideoTypes.AUDIO_AND_VIDEO, getSaveLocation(), getFilename(),
                        getVideoFormat(), captureDuration);
            } else if (selectedAudioVideoType.equalsIgnoreCase(AudioVideoTypes.VIDEO)) {
                settings = new CaptureSettings(AudioVideoTypes.VIDEO, getSaveLocation(), getFilename(),
                        getVideoFormat(), captureDuration);
            } else if (selectedAudioVideoType.equalsIgnoreCase(AudioVideoTypes.AUDIO)) {
                settings = new CaptureSettings(AudioVideoTypes.AUDIO, getSaveLocation(), getFilename(),
                        getVideoFormat(), captureDuration);
            }
            record = new Record(settings);
        } catch (Exception e) {
            System.out.println("Error when trying to configure recording settings.");
        }
    }

    /**
     * Record.
     * 
     */
    private void record() {
        //setup recording settings
        setupRecording();
        final int duration = settings.getCaptureDuration();
        //if countdown enabled
        int countdown = getRecordingCountdown(); //time seconds
        if (countdown > 0) {
            rcrdBtn.setEnabled(false);
            countdown *= 1000; //time in milliseconds
            //          showCountdownTimer(countdown); //TODO: implement later for Vaadin
            (new Timer()).schedule(new TimerTask() {
                public void run() {
                    //if timer is enabled
                    if (duration > 0) {
                        record.startRecording();
                        stpBtn.setEnabled(true);
                        //psBtn.setEnabled(true); //TODO: pause button action
                        (new Timer()).schedule(new TimerTask() {
                            public void run() {
                                //stop recording
                                record.stopRecording();
                                rcrdBtn.setEnabled(true);
                                stpBtn.setEnabled(false);
                                psBtn.setEnabled(false);
                            }
                        }, duration);
                    } else { //countdown, no timer
                        //start recording
                        record.startRecording();
                        //stop and pause buttons come visible after countdown has run
                        rcrdBtn.setEnabled(false);
                        stpBtn.setEnabled(true);
                        //psBtn.setEnabled(true); TODO: implement pause action
                        //TODO: Mimimize frame to taskbar
                    }
                }
            }, countdown + 300); //add 0.3s offset for countdown frame to exit
        } else { // no countdown
            //if timer enabled
            if (duration > 0) {
                record.startRecording();
                rcrdBtn.setEnabled(false);
                stpBtn.setEnabled(true);
                //psBtn.setEnabled(true); TODO: implement pause action
                (new Timer()).schedule(new TimerTask() {
                    public void run() {
                        //stop recording
                        record.stopRecording();
                        rcrdBtn.setEnabled(true);
                        stpBtn.setEnabled(false);
                        psBtn.setEnabled(false);
                    }
                }, duration);
            } else { //no countdown, no timer
                record.startRecording();
                rcrdBtn.setEnabled(false);
                stpBtn.setEnabled(true);
                //psBtn.setEnabled(true); TODO: implement pause action
            }
        }
    }

    /**
     * Stop recording.
     * 
     */
    private void stop() {
        //stop recording
        record.stopRecording();
        stpBtn.setEnabled(false);
        psBtn.setEnabled(false);
        rcrdBtn.setEnabled(true);
    }

    private void pause() {

    }

    //Get selected values from input fields

    private String getFilename() {
        return (String) txtFlnm.getValue();
    }

    private String getSaveLocation() {
        return (String) txtSvLc.getValue();
    }

    private String getAudioVideoType() {
        return (String) slctRrcdTp.getValue();
    }

    private int getRecordingCountdown() {
        return (Integer) txtRrcdCtd.getValue();
    }

    private int getRecordingDuration() {
        return (Integer) txtDrRcrd.getValue();
    }

    private String getVideoFormat() {
        return (String) slctFrmt.getValue();
    }

    private String getVideoCoded() {
        return (String) slctCdc.getValue();
    }

    private int getAudioChannels() {
        return (Integer) slctChnls.getValue();
    }

    private int getSampleSize() {
        return (Integer) smplSzLbl.getValue();
    }

    private int getSampleRate() {
        return (Integer) slctSmplRt.getValue();
    }

}