00001 package i2bpro.layer.Panels;
00002
00003 import i2bpro.Exceptions.ErrorMsg;
00004 import i2bpro.Exceptions.LayerException;
00005 import i2bpro.layer.Layer;
00006 import i2bpro.playlist.PlayList;
00007 import i2bpro.player.Player;
00008 import i2bpro.layer.Menus.PlayListPopupMenu;
00009 import i2bpro.playlist.PlayListCellRenderer;
00010 import java.awt.BorderLayout;
00011 import java.awt.Color;
00012 import java.awt.GridLayout;
00013 import java.awt.datatransfer.DataFlavor;
00014 import java.awt.datatransfer.Transferable;
00015 import java.awt.datatransfer.UnsupportedFlavorException;
00016 import java.awt.dnd.DnDConstants;
00017 import java.awt.dnd.DropTarget;
00018 import java.awt.dnd.DropTargetDragEvent;
00019 import java.awt.dnd.DropTargetDropEvent;
00020 import java.awt.dnd.DropTargetEvent;
00021 import java.awt.dnd.DropTargetListener;
00022 import java.awt.event.MouseEvent;
00023 import java.awt.event.MouseListener;
00024 import java.awt.event.MouseMotionListener;
00025 import java.io.File;
00026 import java.io.IOException;
00027 import java.net.MalformedURLException;
00028 import java.util.ArrayList;
00029 import java.util.HashMap;
00030 import java.util.Iterator;
00031 import java.util.List;
00032 import javax.media.NoPlayerException;
00033 import javax.swing.JPanel;
00034 import javax.swing.JScrollPane;
00035 import javax.swing.JTable;
00036 import javax.swing.ListSelectionModel;
00037 import javax.swing.border.EmptyBorder;
00038 import org.jaudiotagger.audio.exceptions.CannotReadException;
00039 import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
00040 import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
00041 import org.jaudiotagger.tag.TagException;
00042
00043
00044
00059 public final class CenterPanel extends JPanel implements DropTargetListener
00060
00061 {
00062 private JTable table = new JTable(PlayList.getInstance());
00063 private JScrollPane sp =
00064 new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
00065 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
00066 private int rolloverID = -1;
00067 private int pressed_id = -1;
00068 private int old_change_id = -1;
00069 private boolean pressed = false;
00070 private HashMap<Integer, HashMap<String, String>> tmpPlayList;
00071 private boolean old_save;
00072 private int count = 0;
00073
00074
00075
00084 public CenterPanel()
00085 {
00086 initTable();
00087 setLayout(new GridLayout(1,1));
00088 setBorder(new EmptyBorder(5,5,5,5));
00089 JPanel p = new JPanel();
00090 p.setLayout(new BorderLayout());
00091 p.add(sp, BorderLayout.CENTER);
00092 add(p);
00093 addListeners();
00094 }
00095
00096
00097
00113 private void initTable()
00114 {
00115 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
00116 table.setCellSelectionEnabled(false);
00117 table.setColumnSelectionAllowed(false);
00118 table.setRowSelectionAllowed(true);
00119 table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
00120 table.getColumnModel().getColumn(0).setMinWidth(20);
00121 table.getColumnModel().getColumn(0).setMaxWidth(20);
00122 table.getColumnModel().getColumn(1).setMinWidth(40);
00123 table.getColumnModel().getColumn(1).setMaxWidth(50);
00124 table.getColumnModel().getColumn(2).setMinWidth(200);
00125 table.getColumnModel().getColumn(2).setPreferredWidth(400);
00126 table.getColumnModel().getColumn(3).setMinWidth(200);
00127 table.getColumnModel().getColumn(3).setPreferredWidth(400);
00128 table.getColumnModel().getColumn(4).setMinWidth(200);
00129 table.getColumnModel().getColumn(4).setPreferredWidth(400);
00130 table.getColumnModel().getColumn(5).setMinWidth(50);
00131 table.getColumnModel().getColumn(5).setPreferredWidth(50);
00132 table.getColumnModel().getColumn(6).setMinWidth(50);
00133 table.getColumnModel().getColumn(6).setPreferredWidth(50);
00134 table.setBackground(Color.cyan);
00135 table.setForeground(Color.black);
00136 table.setGridColor(Color.black);
00137 table.setDefaultRenderer(Object.class, new PlayListCellRenderer());
00138 table.setDragEnabled(false);
00139 DropTarget dropTarget = new DropTarget (this, this);
00140 }
00141
00142
00143
00153 private void addListeners()
00154 {
00155 table.addMouseListener(new MouseListener()
00156 {
00157 public void mouseClicked(MouseEvent e) { TableMouseClicked(e); }
00158
00159 public void mousePressed(MouseEvent e) { TableMousePressed(e); }
00160
00161 public void mouseReleased(MouseEvent e)
00162 {
00163
00164
00165 if (e.getButton() == MouseEvent.BUTTON1) pressed = false;
00166 }
00167
00168 public void mouseEntered(MouseEvent e) {}
00169
00170 public void mouseExited(MouseEvent e) {}
00171 });
00172
00173 table.addMouseMotionListener(new MouseMotionListener()
00174 {
00175 public void mouseDragged(MouseEvent e) { TableMouseDragged(e); }
00176
00177 public void mouseMoved(MouseEvent e) { TableMouseMoved(e); }
00178 });
00179 }
00180
00181
00182
00202 private void TableMouseClicked(MouseEvent e)
00203 {
00204 int click_id = (int)(e.getY() / table.getRowHeight());
00205 table.setRowSelectionInterval(click_id, click_id);
00206 if(e.getButton() == MouseEvent.BUTTON3)
00207 showPlayListPopupMenu(e, click_id);
00208 else if (e.getButton() == MouseEvent.BUTTON2)
00209 {
00210 try
00211 {
00212 if (Player.getInstance().getState() > 0)
00213 Player.getInstance().stop();
00214 Layer.getInstance().play(click_id);
00215 }
00216 catch (LayerException ex) { ErrorMsg.show(ex.getMessage()); }
00217 catch (MalformedURLException ex) { ErrorMsg.show(ex.getMessage()); }
00218 catch (IOException ex) { ErrorMsg.show(ex.getMessage()); }
00219 catch (NoPlayerException ex) { ErrorMsg.show(ex.getMessage()); }
00220 }
00221 }
00222
00223
00224
00238 private void TableMousePressed(MouseEvent e)
00239 {
00240 if (e.getButton() == MouseEvent.BUTTON1)
00241 {
00242 if (pressed == false)
00243 {
00244 pressed_id = (int)(e.getY() / table.getRowHeight());
00245 old_change_id = pressed_id;
00246 tmpPlayList = PlayList.getInstance().getPlayList();
00247 pressed = true;
00248 old_save = Layer.getInstance().isSave();
00249 }
00250 }
00251 }
00252
00253
00254
00268 private void TableMouseDragged(MouseEvent e)
00269 {
00270 if (pressed)
00271 {
00272 int change_id = (int)(e.getY() / table.getRowHeight());
00273 if (change_id != old_change_id)
00274 {
00275 if ((change_id >= 0)
00276 && (change_id < PlayList.getInstance().getPlayList().size())
00277 && (e.getX() > table.getX())
00278 && (e.getX() < (table.getX() + table.getSize().getWidth())))
00279 {
00280 {
00281 PlayList.getInstance().changeSequence(pressed_id, change_id);
00282 table.updateUI();
00283 pressed_id = change_id;
00284 Layer.getInstance().setSave(true);
00285 }
00286 } else {
00287 PlayList.getInstance().changePlayList(tmpPlayList);
00288 Layer.getInstance().setSave(old_save);
00289 }
00290 }
00291 old_change_id = change_id;
00292 }
00293 }
00294
00295
00296
00306 private void TableMouseMoved(MouseEvent e)
00307 {
00308 int new_rolloverID = (int)(e.getY() / table.getRowHeight());
00309 if (new_rolloverID != rolloverID)
00310 {
00311 rolloverID = new_rolloverID;
00312 table.setToolTipText(PlayList.getInstance().getMeta(
00313 rolloverID, "media"));
00314 }
00315 }
00316
00317
00318
00336 private void showPlayListPopupMenu(MouseEvent event, int click_id)
00337 {
00338 PlayListPopupMenu tpm = new PlayListPopupMenu(click_id);
00339 tpm.show(table, event.getX(), event.getY());
00340 }
00341
00342
00343
00352 public void dragEnter(DropTargetDragEvent dtde) {}
00353
00354
00355
00364 public void dragOver(DropTargetDragEvent dtde) {}
00365
00366
00367
00376 public void dropActionChanged(DropTargetDragEvent dtde) {}
00377
00378
00379
00388 public void dragExit(DropTargetEvent dte) {}
00389
00390
00391
00406 public void drop(DropTargetDropEvent dtde)
00407 {
00408 Transferable tr = dtde.getTransferable();
00409 if (tr.isDataFlavorSupported (DataFlavor.javaFileListFlavor))
00410 {
00411 try
00412 {
00413 dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
00414 java.util.List files = (java.util.List)
00415 tr.getTransferData(DataFlavor.javaFileListFlavor);
00416 InsertMedia(files);
00417 dtde.getDropTargetContext().dropComplete(true);
00418 }
00419 catch (UnsupportedFlavorException ex) { ErrorMsg.show(ex.getMessage()); }
00420 catch (IOException ex) { ErrorMsg.show(ex.getMessage()); }
00421 catch (CannotReadException ex) { ErrorMsg.show(ex.getMessage()); }
00422 catch (TagException ex) { ErrorMsg.show(ex.getMessage()); }
00423 catch (ReadOnlyFileException ex) { ErrorMsg.show(ex.getMessage()); }
00424 catch (InvalidAudioFrameException ex) { ErrorMsg.show(ex.getMessage()); }
00425 } else {
00426 dtde.rejectDrop();
00427 }
00428 }
00429
00430
00431
00440 public void PlayListChange()
00441 {
00442 table.updateUI();
00443 }
00444
00445
00446
00447
00455 public int getSelectedRow() { return table.getSelectedRow(); }
00456
00457
00458
00478 private void InsertMedia(List files) throws CannotReadException,
00479 IOException, TagException, ReadOnlyFileException,
00480 InvalidAudioFrameException
00481 {
00482 ArrayList<File> list = new ArrayList<File>();
00483 Iterator it = files.iterator();
00484 while(it.hasNext())
00485 {
00486 String filename = it.next().toString();
00487 File file = new File(filename);
00488 if (file.isFile())
00489 {
00490 String extension =
00491 filename.substring(filename.length()-4).toLowerCase();
00492 if (extension.equals(".wav") || extension.equals(".mp3"))
00493 list.add(file);
00494 }
00495 }
00496 File[] liste = new File[list.size()];
00497 it = list.iterator();
00498 int i = 0;
00499 while(it.hasNext())
00500 {
00501 liste[i] = (File) it.next();
00502 i++;
00503 }
00504 PlayList.getInstance().InsertMedia(liste);
00505 }
00506
00507
00508
00517 public void setRowSelection(int selected_id)
00518 {
00519 table.setRowSelectionInterval(selected_id, selected_id);
00520 }
00521 }
00522