Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.webcrawler; import com.webcrawler.manager.ProcessManager; import com.webcrawler.manager.UIManager; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JTextField; import org.apache.commons.lang3.StringUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * @author jose */ public class WebCrawlerMain extends javax.swing.JFrame implements UIManager { private ApplicationContext context; private ProcessManager processManager; private JFileChooser fileChooser; public static final String DOWNLOAD_TEXT = "Downloading file "; /** * Creates new form WebCrawlerMain */ public WebCrawlerMain() { initComponents(); initSpringComponents(); fixComponentVisibility(); initInternalComponents(); } @Override public void setProgressBarValue(int val) { progBarDownload.setValue(val); } @Override public void showMessageDialog(String message, String dialogHeader) { JOptionPane.showMessageDialog(this, message, dialogHeader, JOptionPane.INFORMATION_MESSAGE); } @Override public void showErrorMessageDialog(String message, String dialogHeader) { JOptionPane.showMessageDialog(this, message, dialogHeader, JOptionPane.ERROR_MESSAGE); } @Override public void setUIFileInfo(String fileName, int length) { pnlDownload.setVisible(true); lblDownload.setText(DOWNLOAD_TEXT + fileName); } public void initInternalComponents() { progBarDownload.setStringPainted(true); fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setVisible(false); } private void fixComponentVisibility() { pnlDownload.setVisible(false); } private void initSpringComponents() { context = new ClassPathXmlApplicationContext("applicationContext.xml"); processManager = (ProcessManager) context.getBean("processManager"); processManager.setParentComponent(this); } /** * 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() { jPanel1 = new javax.swing.JPanel(); lblEnterURL = new javax.swing.JLabel(); txtURL = new javax.swing.JTextField(); btnDownload = new javax.swing.JButton(); jPanel2 = new javax.swing.JPanel(); pnlDownload = new javax.swing.JPanel(); progBarDownload = new javax.swing.JProgressBar(); lblDownload = new javax.swing.JLabel(); pnlChooseDirectory = new javax.swing.JPanel(); lblSaveInDir = new javax.swing.JLabel(); txtFldChooseDir = new javax.swing.JTextField(); btnBrowseDir = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Image Downloader"); jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); lblEnterURL.setText("Enter URL"); txtURL.setToolTipText("Type URL here"); txtURL.setName("txtURL"); // NOI18N txtURL.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { txtURLActionPerformed(evt); } }); btnDownload.setText("Download Images!"); btnDownload.setName("btnDownload"); // NOI18N btnDownload.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDownloadActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout .setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(lblEnterURL) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtURL, javax.swing.GroupLayout.PREFERRED_SIZE, 564, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnDownload).addContainerGap(13, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGap(0, 10, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblEnterURL) .addComponent(txtURL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnDownload)))); txtURL.getAccessibleContext().setAccessibleName("txtURL"); pnlDownload.setBorder(javax.swing.BorderFactory.createEtchedBorder()); lblDownload.setText("jLabel2"); javax.swing.GroupLayout pnlDownloadLayout = new javax.swing.GroupLayout(pnlDownload); pnlDownload.setLayout(pnlDownloadLayout); pnlDownloadLayout.setHorizontalGroup(pnlDownloadLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlDownloadLayout.createSequentialGroup().addGap(25, 25, 25) .addGroup(pnlDownloadLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlDownloadLayout.createSequentialGroup().addComponent(lblDownload) .addContainerGap()) .addComponent(progBarDownload, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))); pnlDownloadLayout.setVerticalGroup(pnlDownloadLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlDownloadLayout.createSequentialGroup().addGap(15, 15, 15).addComponent(lblDownload) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(progBarDownload, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(26, Short.MAX_VALUE))); pnlChooseDirectory.setBorder(javax.swing.BorderFactory.createEtchedBorder()); lblSaveInDir.setText("Save In Directory:"); txtFldChooseDir.setEnabled(false); btnBrowseDir.setText("Browse"); btnBrowseDir.setName("btnBrowse"); // NOI18N btnBrowseDir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnBrowseDirActionPerformed(evt); } }); javax.swing.GroupLayout pnlChooseDirectoryLayout = new javax.swing.GroupLayout(pnlChooseDirectory); pnlChooseDirectory.setLayout(pnlChooseDirectoryLayout); pnlChooseDirectoryLayout.setHorizontalGroup( pnlChooseDirectoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlChooseDirectoryLayout.createSequentialGroup().addComponent(lblSaveInDir) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtFldChooseDir, javax.swing.GroupLayout.PREFERRED_SIZE, 534, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnBrowseDir).addGap(0, 0, Short.MAX_VALUE))); pnlChooseDirectoryLayout.setVerticalGroup( pnlChooseDirectoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlChooseDirectoryLayout.createSequentialGroup().addContainerGap() .addGroup(pnlChooseDirectoryLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblSaveInDir) .addComponent(txtFldChooseDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btnBrowseDir)) .addContainerGap(14, Short.MAX_VALUE))); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout .setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(pnlDownload, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(pnlChooseDirectory, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap() .addComponent(pnlDownload, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(pnlChooseDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); 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() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(41, 41, 41) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(25, 25, 25))); pack(); }// </editor-fold>//GEN-END:initComponents private void btnDownloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDownloadActionPerformed if (fileChooser.getSelectedFile() == null) { showFileSelectionErrorDialog(); } else if (StringUtils.isEmpty(fileChooser.getSelectedFile().getAbsolutePath())) { showFileSelectionErrorDialog(); } else { disableSearching(); processManager.processDownload(txtURL.getText(), fileChooser.getSelectedFile().getAbsolutePath()); } }//GEN-LAST:event_btnDownloadActionPerformed private void showFileSelectionErrorDialog() { JOptionPane.showMessageDialog(this, "Select a folder first", "Error", JOptionPane.ERROR_MESSAGE); } public void disableSearching() { btnDownload.setEnabled(false); txtURL.setEditable(false); txtURL.setEnabled(false); } public void enableSearching() { btnDownload.setEnabled(true); txtURL.setText(""); txtURL.setEditable(true); txtURL.setEnabled(true); progBarDownload.setValue(0); pnlDownload.setVisible(false); } public void resetProgressBar() { progBarDownload.setValue(0); } private void txtURLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtURLActionPerformed // TODO add your handling code here: }//GEN-LAST:event_txtURLActionPerformed private void btnBrowseDirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseDirActionPerformed fileChooser.setVisible(true); int returnVal = fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { txtFldChooseDir.setText(fileChooser.getSelectedFile().getAbsolutePath()); } }//GEN-LAST:event_btnBrowseDirActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(WebCrawlerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(WebCrawlerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(WebCrawlerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(WebCrawlerMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new WebCrawlerMain().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnBrowseDir; private javax.swing.JButton btnDownload; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JLabel lblDownload; private javax.swing.JLabel lblEnterURL; private javax.swing.JLabel lblSaveInDir; private javax.swing.JPanel pnlChooseDirectory; private javax.swing.JPanel pnlDownload; private javax.swing.JProgressBar progBarDownload; private javax.swing.JTextField txtFldChooseDir; private javax.swing.JTextField txtURL; // End of variables declaration//GEN-END:variables public ApplicationContext getContext() { return context; } public void setContext(ApplicationContext context) { this.context = context; } public ProcessManager getProcessManager() { return processManager; } public void setProcessManager(ProcessManager processManager) { this.processManager = processManager; } public JButton getBtnDownload() { return btnDownload; } public void setBtnDownload(JButton btnDownload) { this.btnDownload = btnDownload; } public JLabel getjLabel1() { return lblEnterURL; } public void setjLabel1(JLabel jLabel1) { this.lblEnterURL = jLabel1; } public JTextField getTxtURL() { return txtURL; } public void setTxtURL(JTextField txtURL) { this.txtURL = txtURL; } public JLabel getLblDownload() { return lblDownload; } public void setLblDownload(JLabel lblDownload) { this.lblDownload = lblDownload; } public JLabel getLblEnterURL() { return lblEnterURL; } public void setLblEnterURL(JLabel lblEnterURL) { this.lblEnterURL = lblEnterURL; } public JPanel getPnlDownload() { return pnlDownload; } public void setPnlDownload(JPanel pnlDownload) { this.pnlDownload = pnlDownload; } public JProgressBar getProgBarDownload() { return progBarDownload; } public void setProgBarDownload(JProgressBar progBarDownload) { this.progBarDownload = progBarDownload; } }