Java tutorial
/* * Copyright (C) 2015 Shubham Rao * * 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 com.github.cshubhamrao.MediaConverter; import com.github.cshubhamrao.MediaConverter.Library.FFMpegLoader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.SwingWorker; import org.apache.commons.exec.*; /** * This class is the main UI for the app. * * @author Shubham Rao * @version 0.1.0 */ public class MainUI extends javax.swing.JFrame { /* * Represents Files used by all parts of program */ File inputFileLocation; File outputFileLocation; /** Constant <code>logFile</code> */ public static File logFile; /** * Creates new form MainUI */ public MainUI() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { inputFileChooser = new javax.swing.JFileChooser(); outputFileChooser = new javax.swing.JFileChooser(); filePanel = new javax.swing.JPanel(); inputFileLabel = new javax.swing.JLabel(); inputFile = new javax.swing.JTextField(); inputFileBrowse = new javax.swing.JButton(); outputFileLabel = new javax.swing.JLabel(); outputFile = new javax.swing.JTextField(); outputFileBrowse = new javax.swing.JButton(); progressBar = new javax.swing.JProgressBar(); exitButton = new javax.swing.JButton(); startButton = new javax.swing.JButton(); versionInfo = new javax.swing.JButton(); outputLogLabel = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); outputArea = new javax.swing.JTextArea(); inputFileChooser.setCurrentDirectory(new java.io.File("C:\\Users\\Shubham\\Videos")); inputFileChooser.setDialogTitle("Open file for conversion"); outputFileChooser.setDialogType(javax.swing.JFileChooser.SAVE_DIALOG); outputFileChooser.setCurrentDirectory(new java.io.File("C:\\Users\\Shubham\\Videos")); outputFileChooser.setDialogTitle("Save the output file as"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Media Converter"); setLocationByPlatform(true); setResizable(false); filePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "File Selection", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N inputFileLabel.setText("Input File"); inputFileBrowse.setText("Browse"); inputFileBrowse.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { inputFileBrowseActionPerformed(evt); } }); outputFileLabel.setText("Output File"); outputFileBrowse.setText("Browse"); outputFileBrowse.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { outputFileBrowseActionPerformed(evt); } }); javax.swing.GroupLayout filePanelLayout = new javax.swing.GroupLayout(filePanel); filePanel.setLayout(filePanelLayout); filePanelLayout.setHorizontalGroup(filePanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(filePanelLayout.createSequentialGroup().addContainerGap() .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(outputFileLabel).addComponent(inputFileLabel)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(outputFile).addComponent(inputFile)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(inputFileBrowse, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(outputFileBrowse, javax.swing.GroupLayout.Alignment.TRAILING)) .addContainerGap())); filePanelLayout.setVerticalGroup(filePanelLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(filePanelLayout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(inputFileLabel) .addComponent(inputFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(inputFileBrowse)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(outputFileLabel) .addComponent(outputFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(outputFileBrowse)))); progressBar.setName(""); // NOI18N progressBar.setStringPainted(true); exitButton.setText("Exit"); exitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { exitButtonActionPerformed(evt); } }); startButton.setText("Start"); versionInfo.setText("Display Version"); versionInfo.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { versionInfoActionPerformed(evt); } }); outputLogLabel.setText("Output Log"); outputArea.setColumns(20); outputArea.setRows(5); outputArea.setText("Version: 0.1.0 Pre Alpha"); jScrollPane2.setViewportView(outputArea); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(exitButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 172, Short.MAX_VALUE) .addComponent(startButton).addContainerGap()) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10) .addComponent(filePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(10, 10, 10)) .addGroup(layout.createSequentialGroup().addGap(10, 10, 10) .addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(10, 10, 10)) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane2) .addContainerGap()) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(outputLogLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(versionInfo).addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(filePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(outputLogLabel).addComponent(versionInfo)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER) .addComponent(exitButton).addComponent(startButton)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed System.exit(0); }//GEN-LAST:event_exitButtonActionPerformed private void outputFileBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outputFileBrowseActionPerformed outputFileChooser.showDialog(this, "Open this File"); outputFile.setText(outputFileChooser.getSelectedFile().getPath()); // Finally set outputFileLocation as text of the text bar outputFileLocation = new File(outputFile.getText()); }//GEN-LAST:event_outputFileBrowseActionPerformed private void inputFileBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputFileBrowseActionPerformed inputFileChooser.showDialog(this, "Open this File"); inputFile.setText(inputFileChooser.getSelectedFile().getPath()); // Finally set inputFileLocation as text of the text bar inputFileLocation = new File(inputFile.getText()); // If the file doesn't exist, show an error message if (!inputFileLocation.exists()) { // Creates a scary error message dialog box. JOptionPane.showMessageDialog(this, "The input file doesn't exsist." + "\nPlease choose another file", "Inavlid file selected", javax.swing.JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_inputFileBrowseActionPerformed private void versionInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_versionInfoActionPerformed new DisplayVersion(outputArea).execute(); }//GEN-LAST:event_versionInfoActionPerformed /** * This is the main method for {@code MainUI} * * @param args the command line arguments */ public static void main(String args[]) { try { logFile = File.createTempFile("MediaConverter", ".txt"); } catch (IOException ex) { Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex); } // Create the FFMpeg executable in %temp% on startup. new Thread(new FFMpegLoader()).start(); try { /* * Set the Nimbus look and feel */ for (javax.swing.UIManager.LookAndFeelInfo lafInfo : javax.swing.UIManager.getInstalledLookAndFeels()) { if (lafInfo.getName().equals("Nimbus")) { System.out.println(lafInfo.getName()); javax.swing.UIManager.setLookAndFeel(lafInfo.getClassName()); } else { // javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } /* * Create and display the form */ // FOR JDK 8 // java.awt.EventQueue.invokeLater(() -> {new MainUI().setVisible(true);}); //For JDK 7 java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new MainUI().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton exitButton; private javax.swing.JPanel filePanel; private javax.swing.JTextField inputFile; private javax.swing.JButton inputFileBrowse; private javax.swing.JFileChooser inputFileChooser; private javax.swing.JLabel inputFileLabel; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTextArea outputArea; private javax.swing.JTextField outputFile; private javax.swing.JButton outputFileBrowse; private javax.swing.JFileChooser outputFileChooser; private javax.swing.JLabel outputFileLabel; private javax.swing.JLabel outputLogLabel; private javax.swing.JProgressBar progressBar; private javax.swing.JButton startButton; private javax.swing.JButton versionInfo; // End of variables declaration//GEN-END:variables } class DisplayVersion extends SwingWorker<Void, String> { File ffmpeg; CommandLine cmd; JTextArea outputArea; DisplayVersion(JTextArea where) { this.outputArea = where; } /** Runs ffmpeg -version */ @Override protected Void doInBackground() { ffmpeg = FFMpegLoader.getFFMpegExecutable(); if (ffmpeg != null) { try { cmd = new CommandLine(ffmpeg); cmd.addArgument("-version"); OutputStream outputStream = new ByteArrayOutputStream(); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(10000); exec.setWatchdog(watchdog); exec.execute(cmd); publish(outputStream.toString()); } catch (ExecuteException ex) { Logger.getLogger(DisplayVersion.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(DisplayVersion.class.getName()).log(Level.SEVERE, null, ex); } } else { try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(DisplayVersion.class.getName()).log(Level.SEVERE, null, ex); } } return null; } /** Shows output on textArea */ @Override protected void process(List<String> chunks) { for (String versionInfo : chunks) { outputArea.setText(versionInfo); } } }