package com.pok.music.client;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import com.pok.music.util.ClientTools;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1697027872020652092L;
// tab
private JTabbedPane tab = new JTabbedPane(JTabbedPane.TOP);
// 服务器音乐列表
private JList jList_serverMusicList = null;
// 我的音乐列表list
private JList myMusicList = null;
// 文件选择
private JFileChooser jFileChooser = null;
// 所选文件名称
private String selectFileName = "";
DownloadPanel dp = new DownloadPanel();
ClientTools ct = new ClientTools();
public MainFrame(String albumInfo, String albumMusicList) {
init();
}
public void init() {
// 设置标题
this.setTitle("在线音乐平台");
// 设置布局管理器为null,不推荐此做法
getContentPane().setLayout(null);
// 创建图片
ImageIcon ii = new ImageIcon("images/main.jpg");
// 设置窗体大小,与背景图一致
this.setSize(ii.getIconWidth(), ii.getIconHeight() + 25);
// 获取显示屏有效宽度
double width = Toolkit.getDefaultToolkit().getScreenSize().getWidth();
// 获取显示屏有效高度
double height = Toolkit.getDefaultToolkit().getScreenSize().getHeight();
// 设置居中
this.setLocation((int) (width - this.getWidth()) / 2,
(int) (height - this.getHeight()) / 2);
// 设置背景图片
JLabel jL_bg = new JLabel(ii);
jL_bg.setBounds(0, 0, ii.getIconWidth(), ii.getIconHeight());
// 设置tab页
tab.setBounds(3, 43, ii.getIconWidth() - 10, ii.getIconHeight() - 50);
tab.add("首页", mainPage());
tab.add("歌曲列表", serverMusicList());
tab.add("下载列表", dp);
tab.add("我的音乐", myMusicList());
// 设置不可编辑大小
setResizable(false);
// 添加控件及背景
this.add(tab);
this.add(jL_bg);
}
/**
* 首页JPanel
*
* @return
*/
public JPanel mainPage() {
// 添加专辑图片
JButton jButton_AlbumPic = null;
// 专辑介绍
final String albumInfo = ct.getAlbumInfo();
// 专辑音乐列表
String albumMusicList = ct.getAlbumMusicList();
ImageIcon ii = new ImageIcon(ClientTools.getMyMusicPath()+"album.jpg");
jButton_AlbumPic = new JButton(ii);
jButton_AlbumPic.setBounds(5, 5, 450, 345);
jButton_AlbumPic.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
StringBuffer musicInfoBuffer = new StringBuffer(albumInfo);
for (int i = 60; i < musicInfoBuffer.length(); i += 60) {
musicInfoBuffer.insert(i, "\n");
}
JOptionPane.showMessageDialog(null, musicInfoBuffer);
}
});
// 专辑信息
JLabel jLabel_albumInfo = null;
if (albumInfo.length() > 150)
jLabel_albumInfo = new JLabel("<html>"
+ albumInfo.substring(0, 150) + "..." + "</html>");
else
jLabel_albumInfo = new JLabel("<html>" + albumInfo + "</html>");
jLabel_albumInfo.setBorder(BorderFactory.createTitledBorder("专辑介绍"));
jLabel_albumInfo.setBounds(510, 5, 250, 180);
// 专辑音乐列表
DefaultListModel model = new DefaultListModel();
JList jList_albumMusicList = new JList(model);
jList_albumMusicList
.setBorder(BorderFactory.createTitledBorder("专辑曲目"));
StringTokenizer st = new StringTokenizer(albumMusicList, "^");
while (st.hasMoreTokens()) {
model.addElement(st.nextToken());
}
jList_albumMusicList.setBounds(510, 200, 250, 150);
/*
* 绘制首页专辑面板
*/
JPanel jP = new JPanel();
jP.setLayout(null);
jP.add(jButton_AlbumPic);
jP.add(jLabel_albumInfo);
jP.add(jList_albumMusicList);
return jP;
}
/**
* 音乐列表
*
* @return
*/
public JScrollPane serverMusicList() {
jList_serverMusicList = new JList();
jList_serverMusicList.setBorder(BorderFactory
.createTitledBorder("音乐列表-双击下载"));
DefaultListModel model = new DefaultListModel();
StringTokenizer st = new StringTokenizer(ct.getServerMusicList(), "\\^");
while (st.hasMoreTokens()) {
model.addElement(st.nextToken());
}
jList_serverMusicList.setModel(model);
// 重写DefaultListCellRenderer,设置行背景色
jList_serverMusicList.setCellRenderer(new DefaultListCellRenderer() {
private static final long serialVersionUID = 5972168643843596748L;
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
if (!isSelected) {
if (index % 2 == 0) {
this.setBackground(Color.gray);
this.setForeground(Color.black);
} else {
this.setBackground(Color.white);
this.setForeground(Color.black);
}
}
return this;
}
});
jList_serverMusicList.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
// 鼠标点击事件
@Override
public void mouseClicked(MouseEvent me) {
// 所选文件保存路径,
String distPath = null;
String path = "";
if(me.getSource() == jList_serverMusicList){
// 鼠标双击
if(me.getClickCount() == 2){
selectFileName = (String)jList_serverMusicList.getSelectedValue();
//选择保存路径
jFileChooser = new JFileChooser(".");
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jFileChooser.setDialogTitle("请选择音乐保存路径");
// 设置为配置路径
jFileChooser.setCurrentDirectory(new File(ClientTools.getMyMusicPath()));
int ret = jFileChooser.showOpenDialog(null);
if (ret == JFileChooser.APPROVE_OPTION) {
distPath = jFileChooser.getSelectedFile().getAbsolutePath()+"\\";
}
// 判断是否有相同文件名的文件
if(distPath != null) {
path = distPath + selectFileName;
} else {
return;
}
File copy_file = new File(path);
int isDownload = 0;
if(copy_file.exists())
isDownload = JOptionPane.showConfirmDialog(null, "文件已存在,继续下载将会删除原有文件,继续下载吗?");
if(isDownload != 0) {
return;
} else {
copy_file.delete();
}
tab.setSelectedIndex(2);
dp.download(copy_file, ct.getDownloadMusicSize(selectFileName));
ct.getMusic(selectFileName, distPath);
// 更新我的音乐列表
tab.setComponentAt(3, myMusicList());
}
}
}
});
/*
* 绘制服务器音乐列表面板
*/
JScrollPane jsp = new JScrollPane(jList_serverMusicList);
return jsp;
}
/**
* 我的音乐列表
*
* @return
*/
public JPanel myMusicList() {
// 我的音