Java tutorial
/** * eradioParser: This program guides the user through a graphical user interface * to create playlists from the stations registered on http://e-radio.gr * * Copyright (C) 2013 Lappas Dionysis * * This file is part of eradioParser. * * eradioParser 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. * * eradioParser 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/> * * You may contact the author at: dio@freelabs.net */ package gui; import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.logging.Filter; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.text.DefaultCaret; import javax.xml.parsers.ParserConfigurationException; import static noThreads.DefaultCaller.*; import noThreads.*; import org.apache.commons.lang3.StringEscapeUtils; import org.dom4j.DocumentException; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.xml.sax.SAXException; /** * * @author Dio */ public class TheGui extends javax.swing.JFrame { public static final String newline = "\n"; private enum PlaylistType { M3U, XSPF }; private PlaylistType playlistType; private String filePath; //private boolean playlistType =false; //default for xspff, true fro m3u /** * Creates new form TheGui */ public TheGui() { playlistType = PlaylistType.XSPF; initComponents(); this.setLocationRelativeTo(null); this.setTitle("Eradioparser v2.0 - create playlists"); JDialogClosed = false; jTextAreaTemp = jTextArea1; } private void reinitialize() { playlistType = PlaylistType.XSPF; theUrls.clear(); eradioLinks.clear(); jRadioButton1.setEnabled(true); jRadioButton2.setEnabled(true); jRadioButton3.setEnabled(true); jRadioButton4.setEnabled(true); jButton1.setEnabled(true); jRadioButton5.setEnabled(true); jRadioButton6.setEnabled(true); jTextArea1.setText(null); jTextArea1 = jTextAreaTemp; jProgressBar1.setValue(0); } /** * 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() { buttonGroup1 = new javax.swing.ButtonGroup(); buttonGroup2 = new javax.swing.ButtonGroup(); buttonGroup3 = new javax.swing.ButtonGroup(); buttonGroup4 = new javax.swing.ButtonGroup(); jPanel1 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel(); jRadioButton1 = new javax.swing.JRadioButton(); jRadioButton2 = new javax.swing.JRadioButton(); jRadioButton3 = new javax.swing.JRadioButton(); jRadioButton4 = new javax.swing.JRadioButton(); jPanel3 = new javax.swing.JPanel(); jPanel4 = new javax.swing.JPanel(); jRadioButton5 = new javax.swing.JRadioButton(); jRadioButton6 = new javax.swing.JRadioButton(); jPanel6 = new javax.swing.JPanel(); jRadioButton7 = new javax.swing.JRadioButton(); jRadioButton8 = new javax.swing.JRadioButton(); jPanel5 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jProgressBar1 = new javax.swing.JProgressBar(); jLabel1 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jMenuBar1 = new javax.swing.JMenuBar(); jMenu1 = new javax.swing.JMenu(); jMenuItem1 = new javax.swing.JMenuItem(); jMenu3 = new javax.swing.JMenu(); jMenuItem2 = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setResizable(false); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "EradioParser v2.0", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP)); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Main Options", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP)); buttonGroup1.add(jRadioButton1); jRadioButton1.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N jRadioButton1.setText("<html><b>Get</b> a playlist for all the stations at <b>e-radio.gr</b>.</html>"); jRadioButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton1ActionPerformed(evt); } }); buttonGroup1.add(jRadioButton2); jRadioButton2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N jRadioButton2 .setText("<html><b>View</b> the available <b>station Categories</b> and get a playlist.</html>"); buttonGroup1.add(jRadioButton3); jRadioButton3.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N jRadioButton3 .setText("<html><b>View</b> the available <b>station Locations</b> and get a playlist.</html>"); buttonGroup1.add(jRadioButton4); jRadioButton4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N jRadioButton4.setText( "<html><b>View</b> the station<b> Locations & Categories</b> and create a custom playlist.</html>"); jRadioButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton4ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(15, 15, 15).addGroup(jPanel2Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jRadioButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jRadioButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jRadioButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jRadioButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(56, Short.MAX_VALUE))); jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup().addGap(18, 18, 18) .addComponent(jRadioButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(15, Short.MAX_VALUE))); jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Run control", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.ABOVE_TOP)); jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Playlist type", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.BELOW_TOP)); buttonGroup3.add(jRadioButton5); jRadioButton5.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N jRadioButton5.setText(".m3u playlist"); jRadioButton5.setToolTipText("Select to create .m3u Playlist"); jRadioButton5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton5ActionPerformed(evt); } }); buttonGroup3.add(jRadioButton6); jRadioButton6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N jRadioButton6.setSelected(true); jRadioButton6.setText(".xspf playlist"); jRadioButton6.setToolTipText("Select to create .xspf Playlist"); jRadioButton6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton6ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup(jPanel4Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jRadioButton5).addComponent(jRadioButton6)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addGap(17, 17, 17).addComponent(jRadioButton5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jRadioButton6).addContainerGap(19, Short.MAX_VALUE))); jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Run Messges")); buttonGroup4.add(jRadioButton7); jRadioButton7.setText("Disable Run Messages"); jRadioButton7.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton7ActionPerformed(evt); } }); buttonGroup4.add(jRadioButton8); jRadioButton8.setSelected(true); jRadioButton8.setText("Enable Run Messages"); jRadioButton8.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jRadioButton8ActionPerformed(evt); } }); javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6); jPanel6.setLayout(jPanel6Layout); jPanel6Layout .setHorizontalGroup( jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addContainerGap(17, Short.MAX_VALUE) .addGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jRadioButton7).addComponent(jRadioButton8)))); jPanel6Layout.setVerticalGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addContainerGap().addComponent(jRadioButton7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jRadioButton8))); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 3, Short.MAX_VALUE))); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap() .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Progress Monitor", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP)); jScrollPane1.setAutoscrolls(true); jScrollPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); jScrollPane1.setVerifyInputWhenFocusTarget(false); jTextArea1.setColumns(20); jTextArea1.setEditable(false); jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 11)); // NOI18N jTextArea1.setRows(5); jTextArea1.setBorder(javax.swing.BorderFactory.createTitledBorder("")); jTextArea1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); DefaultCaret caret = (DefaultCaret) jTextArea1.getCaret(); caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); jScrollPane1.setViewportView(jTextArea1); jProgressBar1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Current Progress", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.TOP)); jLabel1.setText("Program Run Messages"); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout.setHorizontalGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1) .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel5Layout.createSequentialGroup().addGap(204, 204, 204).addComponent(jLabel1) .addContainerGap())); jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup() .addContainerGap() .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE) .addComponent(jLabel1).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))); jButton1.setFont(new java.awt.Font("Times New Roman", 1, 21)); // NOI18N jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/runIcon2.png"))); // NOI18N jButton1.setText("Run"); jButton1.setToolTipText("Click to run"); jButton1.setIconTextGap(10); jButton1.setMaximumSize(new java.awt.Dimension(65, 30)); jButton1.setMinimumSize(new java.awt.Dimension(65, 30)); jButton1.setPreferredSize(new java.awt.Dimension(65, 30)); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(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().addGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(10, 10, 10)) .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup().addGap(39, 39, 39).addComponent( jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(42, 42, 42) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap())))); jMenuBar1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)); jMenu1.setText("File"); jMenuItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/exitIcon.png"))); // NOI18N jMenuItem1.setText("Exit"); jMenuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem1ActionPerformed(evt); } }); jMenu1.add(jMenuItem1); jMenuBar1.add(jMenu1); jMenu3.setText("About"); jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/InfoIcon.png"))); // NOI18N jMenuItem2.setText("Info"); jMenuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jMenuItem2ActionPerformed(evt); } }); jMenu3.add(jMenuItem2); jMenuBar1.add(jMenu3); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)); pack(); }// </editor-fold>//GEN-END:initComponents private class MenuOption1 implements Runnable { @Override public void run() { DefaultCaller caller = new DefaultCaller(); final String URL = "http://e-radio.gr"; Document doc = parseUrl(URL, 0); if (doc == null) { JOptionPane.showMessageDialog(TheGui.this, "No connection to the server! Try again later! Exiting...", "No connection", JOptionPane.ERROR_MESSAGE); System.exit(1); } filePath = saveAs(); if (filePath != null) { Elements links = doc.select("div[id=paneContainer]").select("a[href*=/locations/]"); for (Element link : links) { theUrls.add(link.attr("abs:href")); } jTextArea1.append("...Processing All <e-radio> station links" + newline); Thread thr = new Thread(new CoreCode()); thr.start(); } else { jRadioButton1.setEnabled(true); jRadioButton2.setEnabled(true); jRadioButton3.setEnabled(true); jRadioButton4.setEnabled(true); jButton1.setEnabled(true); jRadioButton5.setEnabled(true); jRadioButton6.setEnabled(true); } } } private class MenuOption2 implements Runnable { @Override public void run() { DefaultCaller caller = new DefaultCaller(); final String URL = "http://e-radio.gr"; Document doc = parseUrl(URL, 0); //Get CATEGORIES if (doc == null) { JOptionPane.showMessageDialog(TheGui.this, "No connection to the server! Try again later! Exiting...", "No connection", JOptionPane.ERROR_MESSAGE); System.exit(1); } Elements categoryLinks = doc.select("div[class=menuOptions]").select("a[href*=/categories/]"); ArrayList<String> categories = new ArrayList<>(); for (int i = 0; i < categoryLinks.size(); i++) { categories.add(StringEscapeUtils.unescapeHtml4(categoryLinks.get(i).html())); } String[] theCategories = new String[categories.size()]; theCategories = categories.toArray(theCategories); String input = (String) JOptionPane.showInputDialog(TheGui.this, "Please select a station category", "Station categories", JOptionPane.QUESTION_MESSAGE, null, theCategories, theCategories[0]); if (input == null) { jRadioButton1.setEnabled(true); jRadioButton2.setEnabled(true); jRadioButton3.setEnabled(true); jRadioButton4.setEnabled(true); jButton1.setEnabled(true); jRadioButton5.setEnabled(true); jRadioButton6.setEnabled(true); } else { filePath = saveAs(); if (filePath != null) { int choice = categories.indexOf(input); print("category choice and input" + choice + " " + input); theUrls.add(categoryLinks.get(choice).attr("abs:href")); //JOptionPane.showMessageDialog(TheGui.this, "Processing category <" + StringEscapeUtils.unescapeHtml4(categoryLinks.get(choice).html()) + ">."); print("...Processing category <" + StringEscapeUtils.unescapeHtml4(categoryLinks.get(choice).html()) + ">."); Thread thr = new Thread(new CoreCode()); thr.start(); } else { jRadioButton1.setEnabled(true); jRadioButton2.setEnabled(true); jRadioButton3.setEnabled(true); jRadioButton4.setEnabled(true); jButton1.setEnabled(true); jRadioButton5.setEnabled(true); jRadioButton6.setEnabled(true); } } } } private class MenuOption3 implements Runnable { @Override public void run() { DefaultCaller caller = new DefaultCaller(); final String URL = "http://e-radio.gr"; Document doc = parseUrl(URL, 0); //GET locations if (doc == null) { JOptionPane.showMessageDialog(TheGui.this, "No connection to the server! Try again later! Exiting...", "No connection", JOptionPane.ERROR_MESSAGE); System.exit(1); } Elements locationLinks = doc.select("div[class=menuOptions]").select("a[href*=/locations/]"); ArrayList<String> locations = new ArrayList<>(); for (int i = 0; i < locationLinks.size(); i++) { locations.add(StringEscapeUtils.unescapeHtml4(locationLinks.get(i).html())); } String[] theLocations = new String[locations.size()]; theLocations = locations.toArray(theLocations); String input = (String) JOptionPane.showInputDialog(TheGui.this, "Please select a station location", "Station locations", JOptionPane.QUESTION_MESSAGE, null, theLocations, theLocations[0]); if (input == null) { jRadioButton1.setEnabled(true); jRadioButton2.setEnabled(true); jRadioButton3.setEnabled(true); jRadioButton4.setEnabled(true); jButton1.setEnabled(true); jRadioButton5.setEnabled(true); jRadioButton6.setEnabled(true); } else { filePath = saveAs(); if (filePath != null) { int choice = locations.indexOf(input); theUrls.add(locationLinks.get(choice).attr("abs:href")); //JOptionPane.showMessageDialog(TheGui.this, "Processing location <" + StringEscapeUtils.unescapeHtml4(locationLinks.get(choice).html()) + ">."); print("...Processing location <" + StringEscapeUtils.unescapeHtml4(locationLinks.get(choice).html()) + ">."); Thread thr = new Thread(new CoreCode()); thr.start(); } else { jRadioButton1.setEnabled(true); jRadioButton2.setEnabled(true); jRadioButton3.setEnabled(true); jRadioButton4.setEnabled(true); jButton1.setEnabled(true); jRadioButton5.setEnabled(true); jRadioButton6.setEnabled(true); } } } } private class MenuOption4 implements Runnable { @Override public void run() { DefaultCaller caller = new DefaultCaller(); final String URL = "http://e-radio.gr"; Document doc = parseUrl(URL, 0); if (doc == null) { JOptionPane.showMessageDialog(TheGui.this, "No connection to the server! Try again later! Exiting...", "No connection", JOptionPane.ERROR_MESSAGE); System.exit(1); } //get the locations Elements locationLinks = doc.select("div[class=menuOptions]").select("a[href*=/locations/]"); ArrayList<String> locations = new ArrayList<>(); for (int i = 0; i < locationLinks.size(); i++) { locations.add(StringEscapeUtils.unescapeHtml4(locationLinks.get(i).html())); } String[] theLocations = new String[locations.size()]; theLocations = locations.toArray(theLocations); //get the categories Elements categoryLinks = doc.select("div[class=menuOptions]").select("a[href*=/categories/]"); ArrayList<String> categories = new ArrayList<>(); for (int i = 0; i < categoryLinks.size(); i++) { categories.add(StringEscapeUtils.unescapeHtml4(categoryLinks.get(i).html())); } String[] theCategories = new String[categories.size()]; theCategories = categories.toArray(theCategories); CustomDialog custom = new CustomDialog(TheGui.this, true, theLocations, theCategories, locationLinks, categoryLinks); custom.setVisible(true); if (JDialogClosed == true) { jRadioButton1.setEnabled(true); jRadioButton2.setEnabled(true); jRadioButton3.setEnabled(true); jRadioButton4.setEnabled(true); jButton1.setEnabled(true); jRadioButton5.setEnabled(true); jRadioButton6.setEnabled(true); theUrls.clear(); } else { filePath = saveAs(); if (filePath != null) { Thread thr = new Thread(new CoreCode()); thr.start(); } else { jRadioButton1.setEnabled(true); jRadioButton2.setEnabled(true); jRadioButton3.setEnabled(true); jRadioButton4.setEnabled(true); jButton1.setEnabled(true); jRadioButton5.setEnabled(true); jRadioButton6.setEnabled(true); theUrls.clear(); } } } } public class OpenFileFilter extends FileFilter { String description = ""; String fileExt = ""; public OpenFileFilter(String extension) { fileExt = extension; } public OpenFileFilter(String extension, String typeDescription) { fileExt = extension; description = typeDescription; } @Override public boolean accept(File f) { if (f.isDirectory()) { return true; } return (f.getName().toLowerCase().endsWith(fileExt)); } @Override public String getDescription() { return description; } public String getFileExt() { return fileExt; } } private String saveAs() { final JFileChooser fc = new JFileChooser(); fc.setMultiSelectionEnabled(false); fc.setAcceptAllFileFilterUsed(false); OpenFileFilter m3u_filter = new OpenFileFilter(".m3u", "*.m3u, Playlist in m3u format"); OpenFileFilter xspf_filter = new OpenFileFilter(".xspf", "*.xspf, Playlist in xspf format"); if (playlistType == PlaylistType.M3U) { fc.addChoosableFileFilter(new OpenFileFilter(".m3u", "*.m3u, Playlist in m3u format")); } else { fc.addChoosableFileFilter(new OpenFileFilter(".xspf", "*.xspf, Playlist in xspf format")); } int returnVal = fc.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); filePath = file.getPath(); jTextArea1.append("Saving: " + file.getName() + "." + newline); if (playlistType == PlaylistType.M3U) { return file.getPath() + ".m3u"; } else { return file.getPath() + ".xspf"; } } else { jTextArea1.append("Save command cancelled by user." + newline); return null; } } public class CoreCode implements Runnable { @Override public void run() { try { coreActions(); } catch (IOException ex) { Logger.getLogger(TheGui.class.getName()).log(Level.SEVERE, null, ex); } } private void coreActions() throws IOException { ArrayList<String> diskFiles = new ArrayList<>(); //variables that hold time in msec, in order to calculate //how much time lasts a program execution long total_time = 0, startTime, endTime; startTime = System.currentTimeMillis(); /* * Create an object of Class ParseLevel0 in order to get the codes * of the radio stations */ ParseLevel0 pl0 = new ParseLevel0(); pl0.parseCodes(); /** * Initialize the Progress Bar and create the Observer * and Observable objects */ jProgressBar1.setMinimum(0); jProgressBar1.setMaximum(100); jProgressBar1.setValue(0); jProgressBar1.setIndeterminate(true); jProgressBar1.setStringPainted(true); ProgressBar barObserver = new ProgressBar(jProgressBar1); ProgressState stateObservable = new ProgressState(); stateObservable.addObserver(barObserver); /* * Create an object of Class ParseLevel1. Parse the codes to * GetFirstLinks method and get the first links of the radio * stations. From the extracted links get the tiles with the * getTitles method. */ ParseLevel1 pl1 = new ParseLevel1(stateObservable); pl1.getFirstLinks(pl0.getCodes()); pl1.getStationTitles(); ParseLevel2 pl2 = new ParseLevel2(stateObservable); try { pl2.getSecondLinks(pl1.getStationLinks1()); pl2.getFinalLinks(pl2.getStationLinks2(), pl1.getTitles()); } catch (IOException ex) { Logger.getLogger(TheGui.class.getName()).log(Level.SEVERE, null, ex); } jProgressBar1.setValue(100); Playlist p = new Playlist(); switch (playlistType) { case M3U: p.createM3uPlaylist(filePath); break; case XSPF: try { p.createPlaylist(filePath); } catch (IOException | DocumentException | SAXException | ParserConfigurationException ex) { Logger.getLogger(TheGui.class.getName()).log(Level.SEVERE, null, ex); } break; } endTime = System.currentTimeMillis(); total_time = total_time + (endTime - startTime); //CLEANUP CODE, delete unnecessary files diskFiles.add(pl1.getLinksFileName()); diskFiles.add(pl1.getTitlesFileNme()); diskFiles.add(pl2.getEradioLinksFileName()); diskFiles.add(pl2.getLinks2FileName()); for (String name : diskFiles) { File a = new File(name); a.delete(); } File tmp = new File(filePath); String thePath = tmp.getAbsolutePath(); jTextArea1.append("\n\nRUN SUMMARY:\n" + "Playlist successfully generated! \n" + "Elapsed time: " + total_time + " msec\n" + "Parsed: " + pl1.getStationLinks1().size() + " station links. \n" + "Valid links: " + DefaultCaller.eradioLinks.size() / 2 + "/" + pl1.getStationLinks1().size()); JOptionPane.showMessageDialog(TheGui.this, "Playlist created to: " + newline + thePath + newline + "Parsed: " + pl1.getStationLinks1().size() + " station links. \n" + "Valid links: " + DefaultCaller.eradioLinks.size() / 2 + "/" + pl1.getStationLinks1().size()); reinitialize(); } } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed jRadioButton5.setEnabled(false); jRadioButton6.setEnabled(false); if (jRadioButton1.isSelected()) { Thread t = new Thread(new MenuOption1()); t.start(); jRadioButton1.setEnabled(false); jRadioButton2.setEnabled(false); jRadioButton3.setEnabled(false); jRadioButton4.setEnabled(false); jButton1.setEnabled(false); } else if (jRadioButton2.isSelected()) { Thread t = new Thread(new MenuOption2()); t.start(); jRadioButton1.setEnabled(false); jRadioButton2.setEnabled(false); jRadioButton3.setEnabled(false); jRadioButton4.setEnabled(false); jButton1.setEnabled(false); } else if (jRadioButton3.isSelected()) { Thread t = new Thread(new MenuOption3()); t.start(); jRadioButton1.setEnabled(false); jRadioButton2.setEnabled(false); jRadioButton3.setEnabled(false); jRadioButton4.setEnabled(false); jButton1.setEnabled(false); } else if (jRadioButton4.isSelected()) { jRadioButton1.setEnabled(false); jRadioButton2.setEnabled(false); jRadioButton3.setEnabled(false); jRadioButton4.setEnabled(false); jButton1.setEnabled(false); Thread t = new Thread(new MenuOption4()); t.start(); } else { jRadioButton5.setEnabled(true); jRadioButton6.setEnabled(true); JOptionPane.showMessageDialog(this, "You need to make a choice!"); } }//GEN-LAST:event_jButton1ActionPerformed private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed System.exit(0); }//GEN-LAST:event_jMenuItem1ActionPerformed private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed JOptionPane.showMessageDialog(this, "EradioParser v2.0 " + newline + "Copyright (C) 2013 Lappas Dionysis " + newline + "Released under the GNU General Public License v3.0"); }//GEN-LAST:event_jMenuItem2ActionPerformed private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton4ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jRadioButton4ActionPerformed private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jRadioButton1ActionPerformed private void jRadioButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton8ActionPerformed jTextArea1 = jTextAreaTemp; jTextArea1.setEnabled(true); }//GEN-LAST:event_jRadioButton8ActionPerformed private void jRadioButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton7ActionPerformed jTextArea1.setEnabled(false); jTextArea1.setText(null); javax.swing.JTextArea jTextArea2 = new javax.swing.JTextArea(); jTextAreaTemp = jTextArea1; jTextArea1 = jTextArea2; }//GEN-LAST:event_jRadioButton7ActionPerformed private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton6ActionPerformed playlistType = PlaylistType.XSPF; }//GEN-LAST:event_jRadioButton6ActionPerformed private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton5ActionPerformed playlistType = PlaylistType.M3U; }//GEN-LAST:event_jRadioButton5ActionPerformed public javax.swing.JTextArea getTextArea1() { return jTextArea1; } /** * @param args the command line arguments */ public static void main(String args[]) { try { /* * 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 */ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { // if ("Nimbus".equals(info.getName())) { // javax.swing.UIManager.setLookAndFeel(info.getClassName()); // } // } } catch (ClassNotFoundException ex) { Logger.getLogger(TheGui.class.getName()).log(Level.SEVERE, null, ex); } catch (InstantiationException ex) { Logger.getLogger(TheGui.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(TheGui.class.getName()).log(Level.SEVERE, null, ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(TheGui.class.getName()).log(Level.SEVERE, null, ex); } //</editor-fold> /* * Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TheGui().setVisible(true); } }); } private static javax.swing.JTextArea jTextAreaTemp; public static boolean JDialogClosed; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.ButtonGroup buttonGroup1; private javax.swing.ButtonGroup buttonGroup2; private javax.swing.ButtonGroup buttonGroup3; private javax.swing.ButtonGroup buttonGroup4; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu3; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JMenuItem jMenuItem1; private javax.swing.JMenuItem jMenuItem2; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel6; private javax.swing.JProgressBar jProgressBar1; private javax.swing.JRadioButton jRadioButton1; private javax.swing.JRadioButton jRadioButton2; private javax.swing.JRadioButton jRadioButton3; private javax.swing.JRadioButton jRadioButton4; private javax.swing.JRadioButton jRadioButton5; private javax.swing.JRadioButton jRadioButton6; private javax.swing.JRadioButton jRadioButton7; private javax.swing.JRadioButton jRadioButton8; private javax.swing.JScrollPane jScrollPane1; public static javax.swing.JTextArea jTextArea1; // End of variables declaration//GEN-END:variables }