1 package org.apache.ojb.tools.mapping.reversedb2.dnd2; 2 3 17 18 import java.awt.dnd.DropTarget ; 19 47 public class DropTargetHelper 48 { 49 private java.awt.dnd.DropTargetListener defaultDTListener 50 = new org.apache.ojb.tools.mapping.reversedb2.dnd2.DropTargetHelper.DTListener(); 51 52 private DropTarget defaultDropTarget = new DropTarget (); 53 54 private java.util.Set dropPasteWorkerSet = new java.util.HashSet (); 55 56 57 public DropTargetHelper () 58 { 59 super(); 60 try 61 { 62 defaultDropTarget.addDropTargetListener(defaultDTListener); 63 defaultDropTarget.setActive(true); 64 } 65 catch (java.util.TooManyListenersException tmle) 66 { 67 throw new RuntimeException ("Internal Error: Drop Target already has a listener registered but this mustn't be at this stage"); 68 } 69 } 70 71 74 public void removeDefaultDropTargetListener() 75 { 76 defaultDropTarget.removeDropTargetListener(defaultDTListener); 77 } 78 79 82 public void registerDefaultDropTargetListener() 83 throws java.util.TooManyListenersException 84 { 85 defaultDropTarget.addDropTargetListener(defaultDTListener); 86 } 87 88 91 public void setDefaultDropTargetListener(java.awt.dnd.DropTargetListener dtl) 92 { 93 this.defaultDTListener = dtl; 94 } 95 96 99 public void registerDropPasteWorker(DropPasteWorkerInterface worker) 100 { 101 this.dropPasteWorkerSet.add(worker); 102 defaultDropTarget.setDefaultActions( 103 defaultDropTarget.getDefaultActions() 104 | worker.getAcceptableActions(defaultDropTarget.getComponent()) 105 ); 106 } 107 108 111 public void removeDropPasteWorker(DropPasteWorkerInterface worker) 112 { 113 this.dropPasteWorkerSet.remove(worker); 114 java.util.Iterator it = this.dropPasteWorkerSet.iterator(); 115 int newDefaultActions = 0; 116 while (it.hasNext()) 117 newDefaultActions |= ((DropPasteWorkerInterface)it.next()).getAcceptableActions(defaultDropTarget.getComponent()); 118 defaultDropTarget.setDefaultActions(newDefaultActions); 119 } 120 121 124 public DropTarget getDropTarget() 125 { 126 return this.defaultDropTarget; 127 } 128 129 132 public class DTListener implements java.awt.dnd.DropTargetListener 133 { 134 137 public void dragEnter (java.awt.dnd.DropTargetDragEvent dropTargetDragEvent) 138 { 139 System.err.println("DTListener.dragEnter()" + dropTargetDragEvent); 142 java.util.Iterator it = dropPasteWorkerSet.iterator(); 143 int dropTargetSupportedActions = DnDWorkerConstants.NONE; 144 while (it.hasNext()) 145 dropTargetSupportedActions |= 146 ((DropPasteWorkerInterface)it.next()).getAcceptableActions( 147 dropTargetDragEvent.getDropTargetContext().getComponent(), 148 dropTargetDragEvent.getCurrentDataFlavors()); 149 int dragSourceSupportedActions = dropTargetDragEvent.getSourceActions(); 151 if ((dropTargetSupportedActions & dragSourceSupportedActions) != DnDWorkerConstants.NONE) 153 { 154 System.err.println(" accepting " + (dropTargetSupportedActions & dragSourceSupportedActions)); 155 dropTargetDragEvent.acceptDrag(dropTargetSupportedActions & dragSourceSupportedActions); 156 } 157 else if (dropTargetSupportedActions != DnDWorkerConstants.NONE) 159 { 160 System.err.println(" accepting " + dropTargetSupportedActions); 161 dropTargetDragEvent.acceptDrag(dropTargetSupportedActions); 162 } 163 else 165 { 166 System.err.println(" rejecting"); 167 dropTargetDragEvent.rejectDrag(); 168 } 169 } 170 171 174 public void dragExit (java.awt.dnd.DropTargetEvent dropTargetEvent) 175 { 176 System.err.println("DTListener.dragExit()" + dropTargetEvent); 177 } 178 179 182 public void dragOver (java.awt.dnd.DropTargetDragEvent dropTargetDragEvent) 183 { 184 185 } 186 187 190 public void drop (java.awt.dnd.DropTargetDropEvent dropTargetDropEvent) 191 { 192 System.err.println("DTListener.drop()" + dropTargetDropEvent); 193 194 java.util.Iterator it = dropPasteWorkerSet.iterator(); 195 boolean result = false; 196 197 while (!result & it.hasNext()) 198 { 199 DropPasteWorkerInterface worker = (DropPasteWorkerInterface)it.next(); 200 int acceptableActions = worker.getAcceptableActions(dropTargetDropEvent.getDropTargetContext ().getComponent(), 201 dropTargetDropEvent.getTransferable().getTransferDataFlavors()); 202 if ((acceptableActions & dropTargetDropEvent.getDropAction()) 203 != DnDWorkerConstants.NONE) 204 { 205 dropTargetDropEvent.acceptDrop(acceptableActions & dropTargetDropEvent.getDropAction()); 206 result = worker.importData(dropTargetDropEvent.getDropTargetContext ().getComponent(), 207 dropTargetDropEvent.getTransferable(), 208 dropTargetDropEvent.getDropAction()); 209 } 210 } 211 dropTargetDropEvent.dropComplete(result); 212 } 213 214 218 public void dropActionChanged (java.awt.dnd.DropTargetDragEvent dropTargetDragEvent) 219 { 220 System.err.println("DTListener.dragEnter()" + dropTargetDragEvent); 221 dragEnter(dropTargetDragEvent); 222 } 223 224 } 225 226 } 227 | Popular Tags |