You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReorderableJList.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.components.reorderablelist;
  23. import com.dmdirc.addons.ui_swing.components.GenericListModel;
  24. import java.awt.Cursor;
  25. import java.awt.Point;
  26. import java.awt.Rectangle;
  27. import java.awt.datatransfer.DataFlavor;
  28. import java.awt.datatransfer.UnsupportedFlavorException;
  29. import java.awt.dnd.DnDConstants;
  30. import java.awt.dnd.DragGestureEvent;
  31. import java.awt.dnd.DragGestureListener;
  32. import java.awt.dnd.DragSource;
  33. import java.awt.dnd.DragSourceDragEvent;
  34. import java.awt.dnd.DragSourceDropEvent;
  35. import java.awt.dnd.DragSourceEvent;
  36. import java.awt.dnd.DragSourceListener;
  37. import java.awt.dnd.DropTarget;
  38. import java.awt.dnd.DropTargetDragEvent;
  39. import java.awt.dnd.DropTargetDropEvent;
  40. import java.awt.dnd.DropTargetEvent;
  41. import java.awt.dnd.DropTargetListener;
  42. import java.io.IOException;
  43. import java.util.ArrayList;
  44. import javax.swing.DefaultListModel;
  45. import javax.swing.JList;
  46. import javax.swing.ListModel;
  47. import javax.swing.ListSelectionModel;
  48. /**
  49. * Reorderable JList.
  50. *
  51. * @param <T> Type contained in the list
  52. */
  53. public class ReorderableJList<T> extends JList<T> implements DragSourceListener,
  54. DropTargetListener, DragGestureListener {
  55. /**
  56. * A version number for this class.
  57. */
  58. private static final long serialVersionUID = 1;
  59. /** Drag source. */
  60. private final DragSource dragSource;
  61. /** Drag target. */
  62. private final DropTarget dropTarget;
  63. /** Drop target. */
  64. private Object dropTargetCell;
  65. /** Dragged index. */
  66. private int draggedIndex = -1;
  67. /** Data flavor. */
  68. private DataFlavor dataFlavor;
  69. /** Below drop target. */
  70. private boolean belowTarget;
  71. /** Instantiate new ReorderableJList. */
  72. public ReorderableJList() {
  73. this(new GenericListModel<>());
  74. }
  75. /**
  76. * Instantiate new ReorderableJList.
  77. *
  78. * @param model Model
  79. */
  80. public ReorderableJList(final GenericListModel<T> model) {
  81. super(model);
  82. setCellRenderer(new ReorderableJListCellRenderer<>(this));
  83. setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  84. setTransferHandler(new ArrayListTransferHandler<T>());
  85. dragSource = DragSource.getDefaultDragSource();
  86. dropTarget = new DropTarget(this, this);
  87. dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_MOVE, this);
  88. try {
  89. dataFlavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType
  90. + ";class=java.util.List");
  91. } catch (ClassNotFoundException e) {
  92. //This class will always exist
  93. }
  94. }
  95. @Override
  96. public GenericListModel<T> getModel() {
  97. return (GenericListModel<T>) super.getModel();
  98. }
  99. @Override
  100. public void setModel(final ListModel<T> model) {
  101. if (model instanceof DefaultListModel) {
  102. super.setModel(model);
  103. } else {
  104. throw new IllegalArgumentException("model needs to be an instance of GenericListModel");
  105. }
  106. }
  107. /**
  108. * Returns the target drop item.
  109. *
  110. * @return Drop target cell
  111. */
  112. public Object getTargetCell() {
  113. return dropTargetCell;
  114. }
  115. /**
  116. * Returns whether the target is below the drop cell.
  117. *
  118. * @return if the target is above or below the point
  119. */
  120. public boolean getBelowTarget() {
  121. return belowTarget;
  122. }
  123. @Override
  124. public void dragEnter(final DragSourceDragEvent dsde) {
  125. //Ignore
  126. }
  127. @Override
  128. public void dragOver(final DragSourceDragEvent dsde) {
  129. //Ignore
  130. }
  131. @Override
  132. public void dropActionChanged(final DragSourceDragEvent dsde) {
  133. //Ignore
  134. }
  135. @Override
  136. public void dragExit(final DragSourceEvent dse) {
  137. //Ignore
  138. }
  139. @Override
  140. public void dragDropEnd(final DragSourceDropEvent dsde) {
  141. //clear drop variables and repaint
  142. dropTargetCell = null;
  143. draggedIndex = -1;
  144. repaint();
  145. }
  146. @Override
  147. public void dragEnter(final DropTargetDragEvent dtde) {
  148. //check whether to accept drag
  149. if (dtde.getSource() == dropTarget) {
  150. dtde.acceptDrag(DnDConstants.ACTION_MOVE);
  151. } else {
  152. dtde.rejectDrag();
  153. }
  154. }
  155. @Override
  156. public void dragOver(final DropTargetDragEvent dtde) {
  157. //Reject drops on self
  158. if (dtde.getSource() != dropTarget) {
  159. dtde.rejectDrag();
  160. }
  161. //get location and index
  162. final Point dragPoint = dtde.getLocation();
  163. final int index = locationToIndex(dragPoint);
  164. //set drag variables and repaint
  165. if (index == -1) {
  166. dropTargetCell = null;
  167. } else {
  168. dropTargetCell = getModel().getElementAt(index);
  169. //check whether the drop point is after the last index
  170. final Rectangle bounds = getCellBounds(index, index);
  171. belowTarget = index == getModel().getSize() - 1
  172. && dragPoint.y > bounds.y + bounds.height;
  173. }
  174. repaint();
  175. }
  176. @Override
  177. public void dropActionChanged(final DropTargetDragEvent dtde) {
  178. //Ignore
  179. }
  180. @Override
  181. public void dragExit(final DropTargetEvent dte) {
  182. //Ignore
  183. }
  184. @Override
  185. public void drop(final DropTargetDropEvent dtde) {
  186. //check source and reject
  187. if (dtde.getSource() != dropTarget) {
  188. dtde.rejectDrop();
  189. return;
  190. }
  191. //get object location and index
  192. final Point dropPoint = dtde.getLocation();
  193. int index = locationToIndex(dropPoint);
  194. if (belowTarget) {
  195. index++;
  196. }
  197. //reject invalid drops
  198. if (index == -1 || index == draggedIndex) {
  199. dtde.rejectDrop();
  200. return;
  201. }
  202. //accept drop as a move
  203. dtde.acceptDrop(DnDConstants.ACTION_MOVE);
  204. //get dropped item
  205. final Object dragged;
  206. try {
  207. dragged = dtde.getTransferable().getTransferData(dataFlavor);
  208. } catch (UnsupportedFlavorException | IOException e) {
  209. //Don't transfer if this fails
  210. return;
  211. }
  212. //move items
  213. final boolean sourceBeforeTarget = draggedIndex < index;
  214. final GenericListModel<T> mod = getModel();
  215. final int newIndex = sourceBeforeTarget ? index - 1 : index;
  216. mod.remove(draggedIndex);
  217. for (Object item : (Iterable<?>) dragged) {
  218. @SuppressWarnings("unchecked")
  219. final T genericItem = (T) item;
  220. mod.add(newIndex, genericItem);
  221. }
  222. getSelectionModel().setSelectionInterval(newIndex, newIndex);
  223. //drop complete
  224. dtde.dropComplete(true);
  225. }
  226. @Override
  227. public void dragGestureRecognized(final DragGestureEvent dge) {
  228. //find the objects location and index
  229. final Point clickPoint = dge.getDragOrigin();
  230. final int index = locationToIndex(clickPoint);
  231. if (index == -1) {
  232. return;
  233. }
  234. //get the list object
  235. final T target = getModel().getElementAt(index);
  236. //create the transferable object
  237. final ArrayList<T> transferObject = new ArrayList<>();
  238. transferObject.add(target);
  239. final ListTransferable<T> trans = new ListTransferable<>(transferObject);
  240. //start drag
  241. draggedIndex = index;
  242. dragSource.startDrag(dge, Cursor.getDefaultCursor(), trans, this);
  243. }
  244. }