1 /* 2 * aTunes 3 * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors 4 * 5 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors 6 * 7 * http://www.atunes.org 8 * http://sourceforge.net/projects/atunes 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 package net.sourceforge.atunes.gui.views.controls; 22 23 import java.awt.dnd.DnDConstants; 24 import java.awt.dnd.DragGestureEvent; 25 import java.awt.dnd.DragGestureListener; 26 import java.awt.dnd.DragSource; 27 import java.awt.dnd.DragSourceDragEvent; 28 import java.awt.dnd.DragSourceDropEvent; 29 import java.awt.dnd.DragSourceEvent; 30 import java.awt.dnd.DragSourceListener; 31 import java.util.ArrayList; 32 import java.util.List; 33 34 import javax.swing.JTable; 35 36 import net.sourceforge.atunes.gui.TransferableList; 37 import net.sourceforge.atunes.gui.views.CopyTransferHandler; 38 import net.sourceforge.atunes.model.ILookAndFeelManager; 39 import net.sourceforge.atunes.model.ITable; 40 41 /** 42 * Shows items of current selected elements in navigator tree 43 * 44 * @author alex 45 * 46 */ 47 public final class NavigationTable extends JTable implements 48 DragSourceListener, DragGestureListener, ITable { 49 50 private static final long serialVersionUID = -607346309523708685L; 51 private DragSource dragSource; 52 53 /** 54 * Instantiates a new drag source table. 55 * 56 * @param lookAndFeelManager 57 */ NavigationTable(final ILookAndFeelManager lookAndFeelManager)58 public NavigationTable(final ILookAndFeelManager lookAndFeelManager) { 59 super(); 60 lookAndFeelManager.getCurrentLookAndFeel().decorateTable(this); 61 setDragSource(); 62 setTransferHandler(new CopyTransferHandler()); 63 } 64 65 @Override dragDropEnd(final DragSourceDropEvent dsde)66 public void dragDropEnd(final DragSourceDropEvent dsde) { 67 // Nothing to do 68 } 69 70 @Override dragEnter(final DragSourceDragEvent dsde)71 public void dragEnter(final DragSourceDragEvent dsde) { 72 // Nothing to do 73 } 74 75 @Override dragExit(final DragSourceEvent dse)76 public void dragExit(final DragSourceEvent dse) { 77 // Nothing to do 78 } 79 80 @Override dragGestureRecognized(final DragGestureEvent dge)81 public void dragGestureRecognized(final DragGestureEvent dge) { 82 int[] rows = getSelectedRows(); 83 List<Integer> rowsToDrag = new ArrayList<Integer>(); 84 for (int element : rows) { 85 rowsToDrag.add(element); 86 } 87 TransferableList<Integer> items = new TransferableList<Integer>( 88 rowsToDrag); 89 this.dragSource.startDrag(dge, DragSource.DefaultCopyDrop, items, this); 90 } 91 92 @Override dragOver(final DragSourceDragEvent dsde)93 public void dragOver(final DragSourceDragEvent dsde) { 94 // Nothing to do 95 } 96 97 @Override dropActionChanged(final DragSourceDragEvent dsde)98 public void dropActionChanged(final DragSourceDragEvent dsde) { 99 // Nothing to do 100 } 101 102 /** 103 * Sets the drag source. 104 */ setDragSource()105 private void setDragSource() { 106 this.dragSource = new DragSource(); 107 this.dragSource.createDefaultDragGestureRecognizer(this, 108 DnDConstants.ACTION_COPY, this); 109 } 110 111 @Override getSwingComponent()112 public JTable getSwingComponent() { 113 return this; 114 } 115 } 116