日期:2014-05-20 浏览次数:20811 次
package service;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import service.Dto.MyType;
public class MyTree{
private JPanel panel;
private Dto dto;
private Play play;
private FileInput fileinput = null;
private JScrollPane jscrollTree;
// 设置树
private JTree jtree;
// 设置节点(此节点为跟节点)
private DefaultMutableTreeNode dmtnRoot = new DefaultMutableTreeNode();
private DefaultMutableTreeNode dmtnLeaf;
private LinkedList<DefaultMutableTreeNode> treeList;
private TreePath path;
private static int num = 0;
JPopupMenu popMenu;
JMenuItem addItem;
JMenuItem delItem;
public MyTree(JPanel panel, Dto dto, Play play) {
this.panel = panel;
this.dto = dto;
this.play = play;
treeList = new LinkedList<DefaultMutableTreeNode>();
treeList.add(new DefaultMutableTreeNode("默认列表"));
treeList.add(new DefaultMutableTreeNode("最近播放"));
initView();
setOnlineList();
popMenu = new JPopupMenu();
addItem = new JMenuItem("添加");
delItem = new JMenuItem("移除");
popMenu.add(addItem);
popMenu.add(delItem);
actionListener();
addItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (fileinput == null)
fileinput = new FileInput(MusicPlayerFrame.musicplayer);
fileinput.open();
File[] files = fileinput.getFiles();
saveOpenFiles(files);
setOnlineList();
}
});
delItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtree.setSelectionPath(path);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) jtree
.getLastSelectedPathComponent();
if (node != null) {
String name = node.toString();
if (name.equals("默认列表")) {
removeAll();
} else {
removeFile(name);
}
}
}
});
}
private void removeAll() {
Vector<MyType> playlist = dto.getPlayList();
playlist.clear();
dto.setCurrentIndex(-1);
dto.setStop();
treeList.getLast().removeAllChildren();
setOnlineList();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jtree.updateUI();
}
});
}
private boolean checkSame(String name, Vector<MyType> list) {
for (Iterator<MyType> iterator = list.iterator(); iterator.hasNext();) {
MyType myType = (MyType) iterator.next();
if (myType.getFilename().equals(name)) {
return true;
}
}
return false;
}
public void removeFile(String name) {
Vector<MyType> list = dto.getPlayList();
int k = findFileLoation(name);
if (k < 0 || k >= list.size()) {
return;
}
int cur = dto.getCurrentIndex();
if (cur > k) {
cur--;
} else if (cur == k) {
play.Next();
dto.setCurrentIndex(cur);
} else {}
list.remove(k);
setOnlineList();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jtree.updateUI();
}
});
}
private void saveOpenFiles(File[] files) {
Vector<MyType> playlist = dto.getPlayList();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
getDirectoryFiles(file.getAbsolutePath(), playlist);
}
else if (!checkSame(file.getName().toLowerCase(), playlist)) {
MyType temp = dto.newMyType(file.getName().toLowerCase(),
file.getAbsolutePath().toLowerCase());
playlist.add(temp);
} else {}
}