package musicdownloader; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.io.*; public class MusicDownloader extends JFrame{ MenuPanel menu; DownloadPanel dPanel; String[] fileLines; Bookmark[] bookmarks; Queue downloadQueue; int windowLength; public MusicDownloader(){ setBackground(Color.BLUE); windowLength = 700; menu = new MenuPanel(windowLength,38); menu.setActionListeners(new menuActionListener()); setLayout(new BorderLayout()); add(menu,BorderLayout.NORTH); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); pack(); setVisible(true); menu.setActionListeners(new menuActionListener()); } private void ExtractBookmarksFromFile(){ try{ fileLines = readFile(); } catch (IOException e){ System.out.println(e.getMessage()); } int numberOfBookmarks = 0; for (int i = 0; i < fileLines.length; i++){ if ((fileLines[i].toLowerCase().contains("\"name\":")) && (fileLines[i+2].toLowerCase().contains("youtube"))){ numberOfBookmarks++; } } System.out.println("NumberOfBookmarks = "+numberOfBookmarks); bookmarks = new Bookmark[numberOfBookmarks]; downloadQueue = new Queue(numberOfBookmarks); int URLCount = 0; for (int i = 0; i < (fileLines.length - 18); i++){ if((fileLines[i].toLowerCase().contains("\"name\":")) && (fileLines[i+2].toLowerCase().contains("youtube"))){ //"\"name\":" bookmarks[URLCount] = new Bookmark(fileLines[i],fileLines[i+2]); URLCount++; } } for (int i = 0; i < numberOfBookmarks; i++){ String tempName = bookmarks[i].getName(); tempName = bookmarks[i].getName().toLowerCase().replace("\"name\": \"", ""); tempName = bookmarks[i].getName().replace("\",", ""); tempName = bookmarks[i].getName().toLowerCase().replace(" - youtube", ""); bookmarks[i].setName(tempName); String tempURL = bookmarks[i].getURL(); tempURL = bookmarks[i].getURL().toLowerCase().replace("\"url\": ", ""); tempURL = bookmarks[i].getURL().toLowerCase().replace("\"", ""); bookmarks[i].setURL(tempURL); //now display the names of the videos, and let user remove unwanted videos from the download list. } } private String[] readFile() throws IOException{ String[] arrayLines; ReadFile file = new ReadFile(dPanel.getFilePath()); arrayLines = file.openFile(); return (file.openFile()); } private class menuActionListener implements ActionListener{ public void actionPerformed(ActionEvent e){ String eventName = e.getActionCommand(); switch(eventName){ case "downloadMusic":{ dPanel = new DownloadPanel(windowLength,150,new downloadActionListener()); add(dPanel,BorderLayout.SOUTH); pack(); if (dPanel.getFileExists()){ System.out.println("File found"); }else{ dPanel.displaySelectFile(); pack(); } break; } case "renameMusic":{ System.out.println("rename"); break; } } } } private class downloadActionListener implements ActionListener{ public void actionPerformed(ActionEvent e){ String eventName = e.getActionCommand(); switch(eventName){ case "selectFile":{ dPanel.chooseFile(); pack(); break; } case "continue":{ dPanel.displaySongsList(bookmarks); } } } } private static void startProgram(){ MusicDownloader run = new MusicDownloader(); } public static void main(String[] args){ startProgram(); } }