package music.gui; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * MyFrame.java * * Created on Nov 13, 2010, 2:53:27 PM */ import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import javax.sql.rowset.spi.SyncFactoryException; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import util.Constants; import music.info.MusicInfo; import music.search.MusicSearcher; import music.threads.DownloadThread; import music.threads.ImageThread; import music.threads.SyncThread; /** * * @author liutao */ public class MyFrame extends javax.swing.JFrame implements ActionListener { private ArrayList<MusicInfo> musicList; private MusicSearcher musicSearcher; private final static int TABLE_MAXROWS = 7; private Object[][] rawdata; private String[] columnNames; public void initVariable() { columnNames = new String[] { "Title", "Artist", "Album", "Status" }; rawdata = new String[TABLE_MAXROWS][4]; for (int i = 0; i < TABLE_MAXROWS; i++) rawdata[i] = new String[] { "", "", "", "" }; musicList = new ArrayList<MusicInfo>(); } public void setMusicSearcher(MusicSearcher searcher) { musicSearcher = searcher; } /** Creates new form MyFrame */ public MyFrame() { setTitle("Hello"); initVariable(); 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"> private void initComponents() { loadButton = new javax.swing.JButton(); keywordList = new javax.swing.JComboBox(); searchButton = new javax.swing.JButton(); makeButton = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); musicTable = new javax.swing.JTable(); jScrollPane2 = new javax.swing.JScrollPane(); logTextArea = new javax.swing.JTextArea(); clearButton = new javax.swing.JButton(); syncButton = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); loadButton.setText("Load"); loadButton.addActionListener(this); keywordList.setModel(new javax.swing.DefaultComboBoxModel()); searchButton.setText("Search"); searchButton.addActionListener(this); makeButton.setText("Make"); makeButton.addActionListener(this); musicTable.setModel(new javax.swing.table.DefaultTableModel(rawdata , columnNames)); musicTable.getTableHeader().setPreferredSize(new Dimension(200, 29)); musicTable.setRowHeight(26); jScrollPane1.setViewportView(musicTable); logTextArea.setColumns(20); logTextArea.setLineWrap(true); logTextArea.setRows(5); logTextArea.setAutoscrolls(true); jScrollPane2.setViewportView(logTextArea); clearButton.setText("Clear"); clearButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { logTextArea.setText(""); } }); syncButton.setText("Sync Mabilo"); syncButton.addActionListener(this); 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) .addGroup( layout.createSequentialGroup() .addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup( layout.createSequentialGroup() .addComponent( jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent( syncButton, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent( clearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup( layout.createSequentialGroup() .addComponent( loadButton, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent( keywordList, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent( searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent( makeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent( jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE) .addContainerGap())))); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] { clearButton, syncButton }); layout.setVerticalGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( layout.createSequentialGroup() .addContainerGap() .addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(loadButton) .addComponent( makeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent( keywordList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(searchButton)) .addGap(24, 24, 24) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup( layout.createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addGroup( layout.createSequentialGroup() .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent( jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE) .addContainerGap()) .addGroup( layout.createSequentialGroup() .addGap(20, 20, 20) .addComponent( clearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE) .addComponent( syncButton, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(21, 21, 21))))); layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] { loadButton, searchButton, makeButton, keywordList }); pack(); }// </editor-fold> /** * @param args * the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MyFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton loadButton; private javax.swing.JButton searchButton; private javax.swing.JButton makeButton; private javax.swing.JButton clearButton; private javax.swing.JButton syncButton; private javax.swing.JComboBox keywordList; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTable musicTable; private javax.swing.JTextArea logTextArea; // End of variables declaration @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == loadButton) { keywordList.removeAllItems(); try { JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(this); File file = chooser.getSelectedFile(); //File file = new File("info"); if (file != null) { BufferedReader reader = new BufferedReader(new FileReader( file)); String temp; while ((temp = reader.readLine()) != null) { keywordList.addItem(temp); } reader.close(); } } catch (IOException e1) { JOptionPane.showMessageDialog(this, "Infomation Load Error!"); } } else if (e.getSource() == searchButton) { String keyword = (String)keywordList.getSelectedItem(); if(keyword != null && !keyword.equals("")) { try { // do not change status of music of last page if(musicList != null) for(MusicInfo info: musicList) info.inValide(); musicList = musicSearcher.search(keyword); clearMusicTable(); int i = 0; for (MusicInfo music : musicList) { music.setIndex(i); musicTable.setValueAt(music.getTitle(), i, 0); musicTable.setValueAt(music.getArtist(), i, 1); musicTable.setValueAt(music.getAlbum(), i, 2); musicTable.setValueAt(Constants.music_status[Constants.BLANK], i, 3); i++; if (i >= TABLE_MAXROWS) break; } } catch (Exception e1) { JOptionPane.showMessageDialog(this, "Internet Connection Error!"); e1.printStackTrace(); } } } else if(e.getSource() == makeButton) { if(musicList == null) return ; int []array = musicTable.getSelectedRows(); for(int i=0; i<array.length; i++) { MusicInfo temp = musicList.get(array[i]); if(temp != null) try { // download, step1:get correspond url step2:download music and image musicSearcher.fetchDownloadUrl(temp); new Thread(new DownloadThread(this, temp)).start(); new Thread(new ImageThread(this, temp)).start(); } catch (Exception e1) { JOptionPane.showMessageDialog(this,temp.getTitle()+" Download Error!"); e1.printStackTrace(); } } } else if(e.getSource() == syncButton) { new Thread(new SyncThread(this)).start(); } } public void clearMusicTable() { for(int i=0; i<TABLE_MAXROWS; i++) for(int j=0; j<4; j++) musicTable.setValueAt(rawdata[i][j], i, j); } // show message in log window public void showMessage(String msg) { logTextArea.append(msg+"\n"); } public void changeStatus(MusicInfo music, int statusID) { if(music.isValid()) { musicTable.setValueAt(Constants.music_status[statusID], music.getIndex(), 3); } } }