1 11 package org.eclipse.jface.util; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.swt.dnd.DragSource; 18 import org.eclipse.swt.dnd.DragSourceEvent; 19 import org.eclipse.swt.dnd.DragSourceListener; 20 import org.eclipse.swt.dnd.Transfer; 21 import org.eclipse.swt.dnd.TransferData; 22 23 90 public class DelegatingDragAdapter implements DragSourceListener { 91 private List listeners = new ArrayList (); 92 93 private List activeListeners = new ArrayList (); 94 95 private TransferDragSourceListener currentListener; 96 97 102 public void addDragSourceListener(TransferDragSourceListener listener) { 103 listeners.add(listener); 104 } 105 106 115 public void dragFinished(final DragSourceEvent event) { 116 SafeRunnable.run(new SafeRunnable() { 119 public void run() throws Exception { 120 if (currentListener != null) { 121 currentListener.dragFinished(event); 123 } else { 124 Iterator iterator = activeListeners.iterator(); 127 while (iterator.hasNext()) { 128 ((TransferDragSourceListener) iterator.next()) 129 .dragFinished(event); 130 } 131 } 132 } 133 }); 134 currentListener = null; 135 activeListeners.clear(); 136 } 137 138 145 public void dragSetData(final DragSourceEvent event) { 146 149 updateCurrentListener(event); if (currentListener != null) { 151 SafeRunnable.run(new SafeRunnable() { 152 public void run() throws Exception { 153 currentListener.dragSetData(event); 154 } 155 }); 156 } 157 } 158 159 169 public void dragStart(final DragSourceEvent event) { 170 boolean doit = false; List transfers = new ArrayList (listeners.size()); 174 175 activeListeners.clear(); 176 for (int i = 0; i < listeners.size(); i++) { 177 final TransferDragSourceListener listener = (TransferDragSourceListener) listeners 178 .get(i); 179 event.doit = true; SafeRunnable.run(new SafeRunnable() { 181 public void run() throws Exception { 182 listener.dragStart(event); 183 } 184 }); 185 if (event.doit) { transfers.add(listener.getTransfer()); 187 activeListeners.add(listener); 188 } 189 doit |= event.doit; 190 } 191 192 if (doit) { 193 ((DragSource) event.widget).setTransfer((Transfer[]) transfers 194 .toArray(new Transfer[transfers.size()])); 195 } 196 197 event.doit = doit; 198 } 199 200 205 public Transfer[] getTransfers() { 206 Transfer[] types = new Transfer[listeners.size()]; 207 for (int i = 0; i < listeners.size(); i++) { 208 TransferDragSourceListener listener = (TransferDragSourceListener) listeners 209 .get(i); 210 types[i] = listener.getTransfer(); 211 } 212 return types; 213 } 214 215 221 public boolean isEmpty() { 222 return listeners.isEmpty(); 223 } 224 225 231 public void removeDragSourceListener(TransferDragSourceListener listener) { 232 listeners.remove(listener); 233 if (currentListener == listener) { 234 currentListener = null; 235 } 236 if (activeListeners.contains(listener)) { 237 activeListeners.remove(listener); 238 } 239 } 240 241 249 private void updateCurrentListener(DragSourceEvent event) { 250 currentListener = null; 251 if (event.dataType == null) { 252 return; 253 } 254 Iterator iterator = activeListeners.iterator(); 255 while (iterator.hasNext()) { 256 TransferDragSourceListener listener = (TransferDragSourceListener) iterator 257 .next(); 258 259 if (listener.getTransfer().isSupportedType(event.dataType)) { 260 currentListener = listener; 263 return; 264 } 265 } 266 } 267 268 } 269 | Popular Tags |