menuBar.add(initMenuMedia());
menuBar.add(initMenuUserPlaylists());
menuBar.add(Box.createHorizontalGlue());
menuBar.add(mediaPrevious = new JMenu());
mediaPrevious.setIcon(getImage("previous.png"));
mediaPrevious.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent arg0) {
mediaPrevious.setSelected(false);
}
});
mediaPrevious.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent arg0) {
mediaPrevious.setSelected(false);
previous();
}
});
menuBar.add(mediaPlay = new JMenu());
mediaPlay.setIcon(getImage("play.png"));
mediaPlay.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent arg0) {
mediaPlay.setSelected(false);
}
});
mediaPlay.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent arg0) {
mediaPlay.setSelected(false);
if (!player.isPaused()){
player.pause();
}else{
player.resume();
}
}
});
menuBar.add(mediaNext = new JMenu());
mediaNext.setIcon(getImage("next.png"));
mediaNext.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent arg0) {
mediaNext.setSelected(false);
}
});
mediaNext.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent arg0) {
mediaNext.setSelected(false);
next();
}
});
dimension = menuBar.getPreferredSize();
dimension.height = 25;
menuBar.setPreferredSize(dimension);
final JPanel panelSlider = new JPanel();
panelSlider.addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent arg0) {
panelSlider.setFocusCycleRoot(false);
panelSlider.setFocusable(false);
}
});
dimension = panelSlider.getPreferredSize();
dimension.height = 25;
dimension.width = 298;
panelSlider.setPreferredSize(dimension);
panelSlider.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, new Color(204, 204, 204)));
panelSlider.setLayout(new BoxLayout(panelSlider, BoxLayout.LINE_AXIS));
panelSlider.setOpaque(false);
panelSlider.add(startDuration = new JLabel("00:00", JLabel.RIGHT));
// startDuration.setOpaque(true);
dimension = new Dimension(34, 25);
startDuration.setPreferredSize(dimension);
startDuration.setSize(dimension);
startDuration.setMaximumSize(dimension);
startDuration.setMinimumSize(dimension);
slider = new ColorSlider();
slider.addMouseListener(new MouseAdapter() {
public void mouseReleased(final MouseEvent e){
// slider.dragging = false;
if (!SwingUtilities.isLeftMouseButton(e)) return;
// setValue(slider.valueAtPoint(e));
player.seek(slider.getValue());
slider.dragging = false;
}
});
panelSlider.add(Box.createRigidArea(new Dimension(5, 25)));
panelSlider.add(slider);
panelSlider.add(Box.createRigidArea(new Dimension(5, 25)));
panelSlider.add(endDuration = new JLabel("00:00", JLabel.LEFT));
endDuration.setPreferredSize(dimension);
endDuration.setSize(dimension);
endDuration.setMaximumSize(dimension);
endDuration.setMinimumSize(dimension);
menuBar.add(panelSlider);
setJMenuBar(menuBar);
chooser = new JFileChooser();
dialogProxy = new ProxySelector();
dialogProxy.setMain(MusicOnline.this);
dialogProxy.setConfigure(configure);
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(initPanelSearch(), BorderLayout.NORTH);
// container.add(initMediaButton(), BorderLayout.SOUTH);
songs = new JList(modelSongs = new DefaultListModel()){
private JToolTip toolTip;
private static final long serialVersionUID = -5872981870254289711L;
@Override
public Point getToolTipLocation(MouseEvent event) {
if (toolTip == null) createToolTip();
return new Point(event.getPoint().x - (toolTip.getWidth() / 2), event.getPoint().y + 20);
}
@Override
public JToolTip createToolTip() {
toolTip = super.createToolTip();
return toolTip;
}
};
songs.setCellRenderer(new SongRenderer());
songs.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e){
int index = songs.locationToIndex(e.getPoint());
if (index < 0 || configure.songs.isEmpty() || index >= configure.songs.size()) return;
songs.setToolTipText(configure.songs.get(index).toString());
}
});
// links.setFixedCellHeight(50);
popupMenu = new JPopupMenu();
JMenuItem itemCopy = new JMenuItem("Copy direct links");
itemCopy.setIcon(getImage("copy.png"));
JMenuItem itemSaveLinks = new JMenuItem("Save direct links (*.m3u8)...");
itemSaveLinks.setIcon(getImage("save.png"));
JMenuItem itemSaveAsPlaylist = new JMenuItem("Save direct links (*.m3u)...");
itemSaveAsPlaylist.setIcon(getImage("m3u.png"));
itemSaveAsPlaylist.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
saveAsPlaylist();
}
});
itemSaveLinks.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
saveLinks();
}
});
itemCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Thread(){
@Override
public void run(){
int[] save = songs.getSelectedIndices();
StringBuilder saveString = new StringBuilder();
setStatus("COPYING");
for (int i = 0; i < save.length; i++){
try {
saveString.append(configure.songs.get(save[i]).getDirectLink());
if (i == save.length - 1) break;
saveString.append("\r\n");
} catch (IOException e) {
out.println(String.format("Can not copy link of %s", configure.songs.get(save[i])));
}
setStatus("COPYING: " + (i + 1) + "/" + save.length);
}
clipboard.setContents(new StringSelection(saveString.toString()), null);
setStatus("COPIED: " + save.length + "/" + save.length);
}
}.start();
}
});
JMenuItem itemRename = new JMenuItem("Rename title");
itemRename.setIcon(getImage("rename.png"));
JMenuItem itemCopyLink = new JMenuItem("Copy links");
itemCopyLink.setIcon(getImage("copy.png"));
itemCopyLink.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Thread(){
public void run(){
int[] save = songs.getSelectedIndices();
StringBuilder saveString = new StringBuilder();
setStatus("COPYING");
for (int i = 0; i < save.length; i++){
saveString.append(configure.songs.get(save[i]).getLink());
if (i == save.length - 1) break;
saveString.append("\r\n");
setStatus("COPYING: " + (i + 1) + "/" + save.length);
}
clipboard.setContents(new StringSelection(saveString.toString()), null);
setStatus("COPIED: " + save.length + "/" + save.length);
}
}.start();
}
});
itemShare = new JMenuItem("Share to facebook");
itemShare.setIcon(getImage("facebook.png"));
itemShare.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shareFacebook(configure.songs.get(songs.getSelectedIndex()).getLink());
}
});
JMenuItem itemDelete = new JMenuItem("Delete", getImage("delete.png"));
itemDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Thread(){
public void run(){
System.out.println(SwingUtilities.isEventDispatchThread());
deleteLinks();
}
}.start();
}
});
itemShowLyric = new JMenuItem("Show lyric");
itemShowLyric.setIcon(getImage("lyrics16.png"));
itemShowLyric.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (frameLyric == null) frameLyric = new FrameLyric();
new Thread(){
public void run(){
ISong song = configure.songs.get(songs.getSelectedIndex());
frameLyric.setTitle("Lyric - " + song.getFullTitle());
frameLyric.setLyric(song);
}
}.start();
frameLyric.setVisible(true);
}
});
itemOpenLink = new JMenuItem("Open in browser");
itemOpenLink.setIcon(getImage("chrome.png"));
itemOpenLink.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openLink(configure.songs.get(songs.getSelectedIndex()).getLink());
}
});
popupMenu.add(itemCopy);
popupMenu.add(itemCopyLink);
popupMenu.add(itemShowLyric);
popupMenu.add(itemShare);
popupMenu.add(itemOpenLink);
popupMenu.add(itemSaveLinks);
popupMenu.add(itemSaveAsPlaylist);
popupMenu.add(itemRename);
popupMenu.add(itemDelete);
popupMenu.addSeparator();
popupMenu.add(menuAddToPlaylist = initMenuAddToPlaylist());
popupMenu.addSeparator();
JMenu menuView = new JMenu("View mode");
menuView.setIcon(getImage("viewmode.png"));
JMenuItem modeDetail = new JMenuItem("Detail");
modeDetail.setIcon(getImage("viewmode_detail.png"));
menuView.add(modeDetail);
JMenuItem modeClassic = new JMenuItem("Classic");
modeClassic.setIcon(getImage("viewmode_classic.png"));
menuView.add(modeClassic);
modeDetail.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Thread(){
public void run(){
configure.viewModeSong = SongRenderer.VIEW_MODE_DETAIL;
// modelSongs.clear();
// setSongs(configure.songs, true);
songs.invalidate();
songs.validate();
songs.repaint();
songs.updateUI();
}
}.start();
}
});
modeClassic.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Thread(){
public void run(){
configure.viewModeSong = SongRenderer.VIEW_MODE_CLASSIC;
// modelSongs.clear();
// setSongs(configure.songs, true);
songs.invalidate();
songs.validate();
songs.repaint();
songs.updateUI();
}
}.start();
}
});
popupMenu.add(menuView);
songs.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
play();
}
if (arg0.getKeyCode() == KeyEvent.VK_DELETE) {
new Thread(){
public void run(){
deleteLinks();
}
}.start();
}
}
});
songs.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
if (songs.getSelectedIndices().length <= 1){
if (configure.songs.isEmpty()){
songs.clearSelection();
}else{
songs.setSelectedIndex(songs.locationToIndex(e.getPoint()));
}
}
popupMenu.show(songs, e.getX(), e.getY());
}
if (e.getClickCount() == 2) {
play();
}
}
});
songs.setDragEnabled(true);
songs.setDropTarget(new DropTarget(){
private static final long serialVersionUID = 5159432887394239344L;
public synchronized void drop(DropTargetDropEvent evt) {
try {
evt.acceptDrop(DnDConstants.ACTION_COPY);
// if (evt.isDataFlavorSupported(DRAG_DROP_URI)){
// final URL url = (URL) evt.getTransferable().getTransferData(DRAG_DROP_URI);
// new Thread(){
// public void run(){
// final Song song = new Song();
// song.setTitle(url.toString());
//// song.setSongInfo(song.getTitle());
// song.setDirectLink(Format.MP3_128_KBPS, song.getTitle());
// song.setSite(Site.INTERNET_URL);
// configure.songs.add(song);
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// modelSongs.addElement(song);
// }
// });
// }
// }.start();
// }
if (evt.isDataFlavorSupported(DataFlavor.javaFileListFlavor)){
final List<?> files = (List<?>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
new Thread(){
public void run(){
final List<ISong> songs = new ArrayList<ISong>();
for (Object object : files) {
if (object instanceof File){
File file = (File) object;
// if (!file.getName().toLowerCase().endsWith(".mp3") && !file.getName().toLowerCase().endsWith(".wav") && !file.getName().toLowerCase().endsWith(".flac")) continue;
// Song song = new Song();
// song.setTitle(file.getName());
//// song.setSongInfo(file.getAbsolutePath());
//// song.songInfo = file.getAbsolutePath();
// song.setDirectLink(Format.MP3_128_KBPS, "file:" + file.getAbsolutePath());
// song.setSite(Site.MY_COMPUTER);
songs.add(new LocalSong(file));
}
}
configure.songs.addAll(songs);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
for (ISong song : songs){
modelSongs.addElement(song);
}
}
});
}
}.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
JPanel panelList = new JPanel(new GridLayout(1, 2, 5, 0));
JScrollPane scrollPaneSong, scrollPaneAlbum;
panelList.add(scrollPaneSong = new JScrollPane(songs,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
scrollPaneSong.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent arg0) {
if (configure.update && !configure.lastValueSong.equals("") && updateSong == null && configure.songs.size() - 1 == songs.getLastVisibleIndex() && arg0.getUnitsToScroll() > 0){
updateSong();
}
}
});
panelList.add(scrollPaneAlbum = new JScrollPane(albums = new JList(modelAlbums = new DefaultListModel()),
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
scrollPaneAlbum.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent arg0) {
if (configure.update && !configure.lastValueAlbum.equals("") && updateAlbum == null && configure.albums.size() - 1 == albums.getLastVisibleIndex() && arg0.getUnitsToScroll() > 0){
updateAlbum();
}
}
});
container.add(panelList, BorderLayout.CENTER);
albums.setCellRenderer(new AlbumRenderer());
albums.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e){
int index = albums.locationToIndex(e.getPoint());
if (index < 0 || configure.albums.isEmpty() || index >= configure.albums.size()) return;
albums.setToolTipText(configure.albums.get(index).toString());
}