1 19 package org.openide.explorer.view; 20 21 import org.openide.nodes.Node; 22 23 import java.awt.Dialog ; 24 import java.awt.datatransfer.Transferable ; 25 import java.awt.dnd.*; 26 27 import java.io.IOException ; 28 29 import java.util.TooManyListenersException ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 33 import javax.swing.JComponent ; 34 import javax.swing.SwingUtilities ; 35 import org.openide.util.Exceptions; 36 37 38 42 abstract class ExplorerDragSupport implements DragSourceListener, DragGestureListener { 43 45 46 boolean active = false; 47 48 49 DragGestureRecognizer defaultGesture; 50 51 52 protected JComponent comp; 53 ExplorerDnDManager exDnD = ExplorerDnDManager.getDefault(); 54 55 abstract int getAllowedDropActions(); 56 57 58 public void dragGestureRecognized(DragGestureEvent dge) { 59 Node[] nodes = obtainNodes(dge); 61 62 if ((nodes == null) || (nodes.length == 0)) { 64 return; 65 } 66 67 int possibleNodeAction = getAllowedDragActions(); 69 70 for (int i = 0; i < nodes.length; i++) { 71 if ((possibleNodeAction & DnDConstants.ACTION_MOVE) != 0) { 72 if (!nodes[i].canCut()) { 73 possibleNodeAction = DnDConstants.ACTION_COPY | DnDConstants.ACTION_REFERENCE; 74 } 75 } 76 77 if ((possibleNodeAction & DnDConstants.ACTION_COPY) != 0) { 78 if (!nodes[i].canCopy()) { 79 possibleNodeAction = DnDConstants.ACTION_NONE; 80 } 81 } 82 } 83 84 exDnD = ExplorerDnDManager.getDefault(); 85 exDnD.setNodeAllowedActions(possibleNodeAction); 86 87 int dragAction = dge.getDragAction(); 88 89 boolean dragStatus = canDrag(dragAction, possibleNodeAction); 90 91 try { 93 Transferable transferable; 95 96 if ((possibleNodeAction & DnDConstants.ACTION_MOVE) != 0) { 97 transferable = DragDropUtilities.getNodeTransferable(nodes, DnDConstants.ACTION_MOVE); 99 exDnD.setDraggedTransferable(transferable, true); 100 101 transferable = DragDropUtilities.getNodeTransferable(nodes, DnDConstants.ACTION_COPY); 103 exDnD.setDraggedTransferable(transferable, false); 104 } else if ((possibleNodeAction & DnDConstants.ACTION_COPY) != 0) { 105 transferable = DragDropUtilities.getNodeTransferable(nodes, DnDConstants.ACTION_COPY); 107 exDnD.setDraggedTransferable(transferable, false); 108 } else { 109 transferable = Node.EMPTY.drag(); 111 exDnD.setDraggedTransferable(transferable, false); 112 } 113 114 exDnD.setDraggedNodes(nodes); 115 116 Dialog d = (Dialog ) SwingUtilities.getAncestorOfClass(Dialog .class, comp); 117 118 if ((d != null) && d.isModal()) { 119 exDnD.setDnDActive(false); 120 121 return; 122 } else { 123 exDnD.setDnDActive(true); 124 dge.startDrag(null, transferable, this); 125 } 126 } catch (InvalidDnDOperationException exc) { 127 Logger.getLogger(ExplorerDragSupport.class.getName()).log(Level.WARNING, null, exc); 129 exDnD.setDnDActive(false); 130 } catch (IOException exc) { 131 Exceptions.printStackTrace(exc); 133 exDnD.setDnDActive(false); 134 } 135 } 136 137 protected int getAllowedDragActions() { 138 return DnDConstants.ACTION_NONE; 139 } 140 141 private boolean canDrag(int targetAction, int possibleAction) { 142 return (possibleAction & targetAction) != 0; 143 } 144 145 public void dragEnter(DragSourceDragEvent dsde) { 146 doDragOver(dsde); 147 } 148 149 public void dragOver(DragSourceDragEvent dsde) { 150 doDragOver(dsde); 151 } 152 153 public void dropActionChanged(DragSourceDragEvent dsde) { 154 } 155 156 public void dragExit(DragSourceEvent dse) { 157 } 158 159 private void doDragOver(DragSourceDragEvent dsde) { 160 } 161 162 public void dragDropEnd(DragSourceDropEvent dsde) { 163 exDnD.setDraggedTransferable(null, true); 165 exDnD.setDraggedTransferable(null, false); 166 167 exDnD.setDraggedNodes(null); 169 170 NodeRenderer.dragExit(); 172 173 exDnD.setDnDActive(false); 175 } 176 177 182 public void activate(boolean active) { 183 if (this.active == active) { 184 return; 185 } 186 187 this.active = active; 188 189 DragGestureRecognizer dgr = getDefaultGestureRecognizer(); 190 191 if (active) { 192 dgr.setSourceActions(getAllowedDragActions()); 193 194 try { 195 dgr.removeDragGestureListener(this); 196 dgr.addDragGestureListener(this); 197 } catch (TooManyListenersException exc) { 198 throw new IllegalStateException ("Too many listeners for drag gesture."); } 200 } else { 201 dgr.removeDragGestureListener(this); 202 } 203 } 204 205 208 DragGestureRecognizer getDefaultGestureRecognizer() { 209 if (defaultGesture == null) { 210 DragSource ds = DragSource.getDefaultDragSource(); 211 defaultGesture = ds.createDefaultDragGestureRecognizer(comp, getAllowedDragActions(), this); 212 } 213 214 return defaultGesture; 215 } 216 217 223 abstract Node[] obtainNodes(DragGestureEvent dge); 224 } 225 226 | Popular Tags |