1 19 package org.openide.explorer.view; 20 21 import org.openide.nodes.Node; 22 import org.openide.util.RequestProcessor; 23 import org.openide.util.datatransfer.PasteType; 24 25 import java.awt.datatransfer.*; 26 import java.awt.dnd.*; 27 28 import javax.swing.JList ; 29 import javax.swing.SwingUtilities ; 30 31 32 36 final class ListViewDropSupport implements DropTargetListener, Runnable { 37 39 40 boolean active = false; 41 boolean dropTargetPopupAllowed; 42 43 44 DropTarget dropTarget; 45 46 47 int lastIndex = -1; 48 49 51 52 protected ListView view; 53 54 55 protected JList list; 56 57 public ListViewDropSupport(ListView view, JList list) { 59 this(view, list, true); 60 } 61 62 63 public ListViewDropSupport(ListView view, JList list, boolean dropTargetPopupAllowed) { 64 this.view = view; 65 this.list = list; 66 this.dropTargetPopupAllowed = dropTargetPopupAllowed; 67 } 68 69 public void setDropTargetPopupAllowed(boolean value) { 70 dropTargetPopupAllowed = value; 71 } 72 73 public boolean isDropTargetPopupAllowed() { 74 return dropTargetPopupAllowed; 75 } 76 77 78 public void dragEnter(DropTargetDragEvent dtde) { 79 ExplorerDnDManager.getDefault().setMaybeExternalDragAndDrop( true ); 80 int dropAction = ExplorerDnDManager.getDefault().getAdjustedDropAction( 81 dtde.getDropAction(), view.getAllowedDropActions() 82 ); 83 84 lastIndex = indexWithCheck(dtde); 85 86 if (lastIndex < 0) { 87 dtde.rejectDrag(); 88 } else { 89 dtde.acceptDrag(dropAction); 90 NodeRenderer.dragEnter(list.getModel().getElementAt(lastIndex)); 91 list.repaint(list.getCellBounds(lastIndex, lastIndex)); 92 } 93 } 94 95 96 public void dragOver(DropTargetDragEvent dtde) { 97 ExplorerDnDManager.getDefault().setMaybeExternalDragAndDrop( true ); 98 int dropAction = ExplorerDnDManager.getDefault().getAdjustedDropAction( 99 dtde.getDropAction(), view.getAllowedDropActions() 100 ); 101 102 int index = indexWithCheck(dtde); 103 104 if (index < 0) { 105 dtde.rejectDrag(); 106 107 if (lastIndex >= 0) { 108 NodeRenderer.dragExit(); 109 list.repaint(list.getCellBounds(lastIndex, lastIndex)); 110 lastIndex = -1; 111 } 112 } else { 113 dtde.acceptDrag(dropAction); 114 115 if (lastIndex != index) { 116 if (lastIndex < 0) { 117 lastIndex = index; 118 } 119 120 NodeRenderer.dragExit(); 121 NodeRenderer.dragEnter(list.getModel().getElementAt(index)); 122 list.repaint(list.getCellBounds(lastIndex, index)); 123 lastIndex = index; 124 } 125 } 126 } 127 128 public void dropActionChanged(DropTargetDragEvent dtde) { 129 } 131 132 133 public void dragExit(DropTargetEvent dte) { 134 ExplorerDnDManager.getDefault().setMaybeExternalDragAndDrop( false ); 135 if (lastIndex >= 0) { 136 NodeRenderer.dragExit(); 137 list.repaint(list.getCellBounds(lastIndex, lastIndex)); 138 } 139 } 140 141 144 public void drop(DropTargetDropEvent dtde) { 145 int index = list.locationToIndex(dtde.getLocation()); 147 Object obj = list.getModel().getElementAt(index); 148 Node dropNode = null; 149 150 if (obj instanceof VisualizerNode) { 151 dropNode = ((VisualizerNode) obj).node; 152 } 153 154 int dropAction = ExplorerDnDManager.getDefault().getAdjustedDropAction( 155 dtde.getDropAction(), view.getAllowedDropActions() 156 ); 157 158 ExplorerDnDManager.getDefault().setMaybeExternalDragAndDrop( false ); 159 160 if ((index < 0) || !canDrop(dropNode, dropAction, dtde.getTransferable(), index)) { 162 dtde.rejectDrop(); 163 164 return; 165 } 166 167 Transferable t = ExplorerDnDManager.getDefault().getDraggedTransferable((DnDConstants.ACTION_MOVE & dropAction) != 0); 169 if( null == t ) 170 t = dtde.getTransferable(); 171 PasteType pt = DragDropUtilities.getDropType( dropNode, t, dropAction, index ); 172 173 if (pt == null) { 174 dtde.dropComplete(false); 175 176 RequestProcessor.getDefault().post(this, 500); 180 181 return; 182 } 183 184 dtde.acceptDrop(dropAction); 186 187 if (dropAction == DnDConstants.ACTION_LINK) { 188 } else { 191 DragDropUtilities.performPaste(pt, null); 192 } 193 } 194 195 196 197 private boolean canDrop(Node n, int dropAction, Transferable dndEventTransferable, int dropIndex) { 199 if (n == null) { 200 return false; 201 } 202 203 if (ExplorerDnDManager.getDefault().getNodeAllowedActions() == DnDConstants.ACTION_NONE) { 204 return false; 205 } 206 207 if ((DnDConstants.ACTION_MOVE & dropAction) != 0) { 210 Node[] nodes = ExplorerDnDManager.getDefault().getDraggedNodes(); 211 212 if( null != nodes ) { 213 for (int i = 0; i < nodes.length; i++) { 214 if (n.equals(nodes[i].getParentNode())) { 215 return false; 216 } 217 } 218 } 219 } 220 221 Transferable trans = ExplorerDnDManager.getDefault().getDraggedTransferable( 222 (DnDConstants.ACTION_MOVE & dropAction) != 0 223 ); 224 225 if (trans == null) { 226 trans = dndEventTransferable; 227 if( trans == null ) { 228 return false; 229 } 230 } 231 232 PasteType pt = DragDropUtilities.getDropType(n, trans, dropAction, dropIndex); 234 235 return (pt != null); 236 } 237 238 243 public void activate(boolean active) { 244 if (this.active == active) { 245 return; 246 } 247 248 this.active = active; 249 getDropTarget().setActive(active); 250 } 251 252 254 public void run() { 255 if (!SwingUtilities.isEventDispatchThread()) { 256 SwingUtilities.invokeLater(this); 257 258 return; 259 } 260 261 DragDropUtilities.dropNotSuccesfull(); 262 } 263 264 268 int indexWithCheck(DropTargetDragEvent dtde) { 269 int dropAction = ExplorerDnDManager.getDefault().getAdjustedDropAction( 270 dtde.getDropAction(), view.getAllowedDropActions() 271 ); 272 273 if ((dropAction & view.getAllowedDropActions()) == 0) { 275 return -1; 276 } 277 278 int index = list.locationToIndex(dtde.getLocation()); 280 if (index == -1) return -1; 281 Object obj = list.getModel().getElementAt(index); 282 283 if (obj instanceof VisualizerNode) { 284 obj = ((VisualizerNode) obj).node; 285 } 286 287 if (index < 0) { 288 return -1; 289 } 290 291 if (!(obj instanceof Node)) { 292 return -1; 293 } 294 295 304 305 return index; 307 } 308 309 311 DropTarget getDropTarget() { 312 if (dropTarget == null) { 313 dropTarget = new DropTarget(list, view.getAllowedDropActions(), this, false); 314 } 315 316 return dropTarget; 317 } 318 } 319 | Popular Tags |