package com.MUSIC.ui;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import com.MUSIC.common.Common;
import com.MUSIC.list.PlayListVO;
import com.MUSIC.util.MusicPlayListForXMLUtil;
import com.MUSIC.util.MusicUtil;
import com.MUSIC.util.TrayUtil;
public class MainUI extends Mp3UI
{
private static final long serialVersionUID = 1L;
private JPanel songInfoPanel;
private JMenuBar menuBar;
private JButton nextButton;
private JPanel playControlPanel;
private JButton playModeButton;
private JButton playOrSuspendButton;
private JButton previousButton;
private JButton singerImageButton;
public JLabel songName;
private JMenu file;
private JMenu scan;
private JMenu tools;
private JMenuItem playListItem;
private JMenuItem cleanPlayListItem;
public static boolean isStop = true;
public boolean isSuspend = false;
public static int play_mode_value = 2;
private JMenuItem scanInGivenDerectoryItem;
private JMenuItem changeSkinItem;
public static int pointX = 0;
public static int pointY = 0;
public static List<PlayListVO> playList = new ArrayList<PlayListVO>();
private TrayUtil tray;
private ScanInGivenDirectoryUI scanInGivenDirectoryUI;
private SkinManagerUI skinManagerUI;
private PlayListUI playListUI;
private MusicUtil musicUtil;
private Thread barsThread;
private MusicPlayListForXMLUtil xmlUtil;
public MainUI(String title)
{
super(title);
this.setTitle(title);
}
public void init()//主窗口
{
initComponents();
this.setVisible(true);
this.setResizable(false);
this.setSize(430, 550);
this.setLocationRelativeTo(null);
setJMenuBar(menuBar);
addComponentListener(new ComponentListener()
{
@Override
public void componentShown(ComponentEvent e)
{
}
@Override
public void componentResized(ComponentEvent e)
{
}
@Override
public void componentMoved(ComponentEvent e)
{
}
public void componentHidden(ComponentEvent e)
{
}
});
addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent e)
{
System.out.println("最小化到托盘");
}
});
if (null == tray)
{
tray = new TrayUtil(MainUI.this);
} else
{
tray.initTray();
}
loadPlayList();
}
private void initComponents()
{
initMenu();
initMainControlPanel();
mainLayout();
}
private void initMenu()
{
menuBar();
menuFile();
menuScan();
menuTools();
setJMenuBar(menuBar);
}
private void menuBar()
{
menuBar = new JMenuBar();
}
private void menuFile()//目录菜单
{
file = new JMenu(Common.FILE);
playListItem = new JMenuItem(Common.PLAY_LIST);
playListItem.addActionListener(this);
file.add(playListItem);
cleanPlayListItem = new JMenuItem(Common.CLEAN_PLAY_LIST);
cleanPlayListItem.addActionListener(this);
file.add(cleanPlayListItem);
menuBar.add(file);
}
private void menuScan()//导入菜单
{
scan = new JMenu(Common.SCAN);
scanInGivenDerectoryItem = new JMenuItem(Common.SCAN_IN_GIVEN_DIRECTORY);
scanInGivenDerectoryItem.addActionListener(this);
scan.add(scanInGivenDerectoryItem);
menuBar.add(scan);
}
private void menuTools()//设置菜单
{
tools = new JMenu(Common.SETTING);
changeSkinItem = new JMenuItem(Common.CHANGE_SKIN);
changeSkinItem.addActionListener(this);
tools.add(changeSkinItem);
menuBar.add(tools);
}
private void initMainControlPanel()
{
initPlayControlPanel();
initSongInfoPanel();
}
private void initSongInfoPanel()//歌曲信息面板
{
songInfoPanel = new JPanel();
singerImageButton = new JButton();
songName = new JLabel();
singerImageButton.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource(Common.JPG_17)));
songName.setFont(new Font(Common.FONT_SONG_TI, 1, 20));
songName.setHorizontalAlignment(SwingConstants.CENTER);
songInfoPanel();
}
private void initPlayControlPanel()//播放控制面板
{
playControlPanel = new JPanel();
nextButton = new JButton();
playOrSuspendButton = new JButton();
previousButton = new JButton();
playModeButton = new JButton();
nextButton.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource(Common.PNG_1)));
nextButton.setMnemonic(KeyEvent.VK_RIGHT);
nextButton.addActionListener(this);
nextButton.setToolTipText(Common.NEXT);
playOrSuspendButton.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource(Common.PNG_7)));
playOrSuspendButton.addActionListener(this);
playOrSuspendButton.setToolTipText(Common.PLAY);
previousButton.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource(Common.PNG_9)));
previousButton.setMnemonic(KeyEvent.VK_LEFT);
previousButton.addActionListener(this);
previousButton.setToolTipText(Common.PREVIOUS);
playModeButton.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource(Common.PNG_13)));
playModeButton.addActionListener(this);
playModeButton.setToolTipText(Common.ORDER_MODE);
playControlPanelLayout();
}
@SuppressWarnings("static-access")
private void threadYield()
{
if (null != barsThread)
{
barsThread.yield();
}
}
@Override
public void actionPerformed(ActionEvent e)
{
threadYield();
actionForFileMenuItem(e);
actionForScanMenuItem(e);
actionForToolsMenuItem(e);
actionForPlayControlPanel(e);
}
private void actionForFileMenuItem(ActionEvent e)
{
if (e.getSource() == playListItem)
{
playListUIOperation();
} else if (e.getSource() == cleanPlayListItem)
{
cleanPlayListOperation();
}
}
public void openOperation(MainUI mainUI)
{
int beforeSize = 0;
int afterSize = 0;
if (!isPlayListEmpty())
{
beforeSize = playList.size();
}
if (!isPlayListEmpty())
{
afterSize = playList.size();
if (afterSize != beforeSize)
{
refreshPlaylistUIWhenUIVisible();
generatePlayListXML();
}
}
}
public void generatePlayListXML()
{
if (null == xmlUtil)
{
xmlUtil = new MusicPlayListForXMLUtil();
xmlUtil.save(playList);
} else
{
xmlUtil.save(playList);
}
}
public void refreshPlaylistUIWhenUIVisible()//更新播放列表
{
if (null != playList && null != playListUI)
{
distoryPlayListUI();
showPlayListUI();
}
}
public void playListUIOperation()//播放列表界面操作
{
setMainUIXY();
if (null == playListUI)
{
showPlayListUI();
} else
{
if (playListUI.isVisible())
{
distoryPlayListUI();
showPlayListUI();
} else
{
showPlayListUI();
}
}
}
public void cleanPlayListOperation()//清空播放列表
{
int option = JOptionPane.showConfirmDialog(MainUI.this, Common.CONFIM_CLEAN_PLAY_LIST, Common.CONFIM_CLEAN,
JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION)
{
File file = new File(Common.HEAD_PATH);
if (file.exists())
{
File[] files = file.listFiles();
for (File f : files)
{
if (f.exists() && f.isFile())
{
f.delete();
}
}
}
file = new File(Common.PLAY_LIST_SAVE_PATH);
if (file.exists() && file.isFile())
{
file.delete();
}
playList = new ArrayList<PlayListVO>();
refreshPlaylistUIWhenUIVisible();
generatePlayListXML();
stopOperation();
}
}
private void distoryPlayListUI()//销毁
{
playListUI.setVisible(false);
playListUI = null;
}
private void showPlayListUI()//显示
{
playListUI = new PlayListUI(Common.PLAY_LIST);
playListUI.setMainUI(Mai