package com.hux.frame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.Vector;
import com.hux.dao.*;
import com.hux.exception.*;
public class SongManagePane extends JPanel {
static JTable songTable;
static DefaultTableModel tableModel;
private JPanel queryPanel;
private JPanel buttonPanel;
private UpdateSongFrame updateSongFrame;
private static Song song;
private String songName;
private String songPinYin;
private String starName;
private String songURL;
private static Vector<Song> songs;
private static Vector<String> songNames;
private static Vector<String> songPinYins;
private static Vector<String> songURLs;
private static Vector<String> starNames;
public SongManagePane() {
init();
}
public void init() {
this.setLayout(new BorderLayout());
this.add(getQueryPanel(), "North");
this.add(getButtonPanel(), "Center");
this.add(new JScrollPane(getSongTable()), "East");
}
public JPanel getQueryPanel() {
queryPanel = new JPanel();
final JTextField queryText = new JTextField(20);
JButton queryButton = new JButton("查询");
queryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String songInfo = queryText.getText();
if (songInfo.length() != 0) {
try {
songs = new Vector<Song>();
songs = Song.findBySongName(songInfo);
if (!songs.isEmpty()) {
SongManagePane.refresh();
SongManagePane.addRecord(songs);
}
} catch (NotFoundException e1) {
try {
songs = new Vector<Song>();
songs = Song.findBySongPinYin(songInfo);
if (!songs.isEmpty()) {
SongManagePane.refresh();
SongManagePane.addRecord(songs);
}
} catch (NotFoundException e2) {
try {
songs = new Vector<Song>();
songs = Song.findByStarName(songInfo);
if (!songs.isEmpty()) {
SongManagePane.refresh();
SongManagePane.addRecord(songs);
}
} catch (NotFoundException e3) {
JOptionPane.showMessageDialog(null,
" 对不起,系统未检索到符合要求的歌曲.");
}
}
}
queryText.setText("");
}
}
});
queryButton.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String songInfo = queryText.getText();
if (songInfo.length() != 0) {
try {
songs = new Vector<Song>();
songs = Song.findBySongName(songInfo);
if (!songs.isEmpty()) {
SongManagePane.refresh();
SongManagePane.addRecord(songs);
}
} catch (NotFoundException e1) {
try {
songs = new Vector<Song>();
songs = Song.findBySongPinYin(songInfo);
if (!songs.isEmpty()) {
SongManagePane.refresh();
SongManagePane.addRecord(songs);
}
} catch (NotFoundException e2) {
try {
songs = new Vector<Song>();
songs = Song.findByStarName(songInfo);
if (!songs.isEmpty()) {
SongManagePane.refresh();
SongManagePane.addRecord(songs);
}
} catch (NotFoundException e3) {
JOptionPane.showMessageDialog(null,
" 对不起,系统未检索到符合要求的歌曲.");
}
}
}
queryText.setText("");
}
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
SongManagePane.WHEN_IN_FOCUSED_WINDOW);
queryPanel.add(queryText);
queryPanel.add(queryButton);
return queryPanel;
}
public JPanel getButtonPanel() {
buttonPanel = new JPanel();
buttonPanel.setLayout(null);
JButton refreshButton = new JButton("刷新 (R)");
refreshButton.setMnemonic(KeyEvent.VK_R);
refreshButton.setBounds(14, 35, 125, 50);
refreshButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
songs = new Vector<Song>();
try {
songs = Song.getAll();
SongManagePane.refresh();
SongManagePane.addRecord(songs);
} catch (NotFoundException e1) {
SongManagePane.refresh();
}
}
});
JButton addButton = new JButton("增加(A)");
addButton.setMnemonic(KeyEvent.VK_A);
addButton.setBounds(14, 90, 125, 50);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AddSongFrame();
}
});
JButton updateButton = new JButton("修改(U)");
updateButton.setMnemonic(KeyEvent.VK_U);
updateButton.setBounds(14, 145, 125, 50);
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selectedRow = SongManagePane.songTable.getSelectedRow();
if (selectedRow >= 0) {
songName = (String) SongManagePane.songTable.getValueAt(
selectedRow, 0);
songPinYin = (String) SongManagePane.songTable.getValueAt(
selectedRow, 1);
starName = (String) SongManagePane.songTable.getValueAt(
selectedRow, 2);
songURL = (String) SongManagePane.songTable.getValueAt(
selectedRow, 3);
updateSongFrame = new UpdateSongFrame();
updateSongFrame.getSongNameText().setText(songName);
updateSongFrame.getSongPinYinText().setText(songPinYin);
updateSongFrame.getStarNameText().setText(starName);
updateSongFrame.getSongUrlNameText().setText(songURL);
} else {
JOptionPane.showMessageDialog(null, "请先选中一条记录");
}
}
});
JButton deleteButton = new JButton("删除(D)");
deleteButton.setMnemonic(KeyEvent.VK_D);
deleteButton.setBounds(14, 200, 125, 50);
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] selectedRows = SongManagePane.songTable.getSelectedRows();
int columnCount = SongManagePane.songTable.getColumnCount();
Vector<Object> songInfo = new Vector<Object>();
if (selectedRows.length > 0) {
if (JOptionPane.showConfirmDialog(null, "确定要删除吗?", "确认删除",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
for (int i = 0; i < selectedRows.length; i++) {
for (int j = 0; j < columnCount; j++) {
songInfo.addElement(SongManagePane.songTable
.getValueAt(selectedRows[i], j));
}
SongManagePane.tableModel
.removeRow(selectedRows[i]);// 删除表格中对应的歌曲
song = new Song((String) songInfo.elementAt(0),
(String) songInfo.elementAt(1),
(String) songInfo.elementAt(2),
(String) songInfo.elementAt(3));
try {
Song.delete(song);
JOptionPane.showMessageDialog(null,
"您已成功将选中的歌曲从数据库中删除.");
} catch (NotFoundException e1) {
JOptionPane.showMessageDialog(null,
"系统中不存在该歌曲!");
}
}
}
} else {
JOptionPane.showMessageDialog(null, "请先选中一条或多条记录");
}
}
});
buttonPanel.add(refreshButton);
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
return buttonPanel;
}
public JTable getSongTable() {
songTable = new JTable() {
public boolean isCellEditable(int row, int column) {
return false;
}
};
tableModel = new DefaultTableModel();
songTable.setModel(tableModel);
tableModel.addColumn("歌名");
tableModel.addColumn("拼音缩写");
tableModel.addColumn("歌手");
tableModel.addColumn("歌曲路径");
songTable.getColumnModel().getColumn(0).setMaxWidth(100);
songTable.getColumnModel().getColumn(0).setMinWidth(80);
songTable.getColumnModel().getColumn(1).setMaxWidth(100);
songTable.getColumnModel().getColumn(1).setMinWidth(80);
songTable.getColumnModel().getColumn(2).setMaxWidth(100);
songTable.getColumnModel().getColumn(2).setMinWidth(80);
songTable.getColumnModel().getCol