1 19 20 package org.openide.util.datatransfer; 21 22 import java.awt.datatransfer.DataFlavor ; 23 import java.awt.datatransfer.Transferable ; 24 import java.awt.datatransfer.UnsupportedFlavorException ; 25 import java.io.IOException ; 26 import java.util.HashSet ; 27 import java.util.LinkedHashMap ; 28 import javax.swing.event.EventListenerList ; 29 import org.openide.util.NbBundle; 30 31 36 public class ExTransferable extends Object implements Transferable { 37 38 public static final Transferable EMPTY = new Empty(); 39 40 42 public static final DataFlavor multiFlavor; 43 static { 44 try { 45 multiFlavor = new DataFlavor ( 46 "application/x-java-openide-multinode;class=org.openide.util.datatransfer.MultiTransferObject", NbBundle.getBundle(ExTransferable.class).getString("transferFlavorsMultiFlavorName"), 48 MultiTransferObject.class.getClassLoader()); 49 } catch (ClassNotFoundException e) { 50 throw new AssertionError (e); 51 } 52 } 53 54 55 private LinkedHashMap <DataFlavor ,Single> map; 56 57 58 private EventListenerList listeners; 59 60 64 private ExTransferable(final Transferable t) { 65 map = new LinkedHashMap <DataFlavor ,Single>(); 66 67 final DataFlavor [] df = t.getTransferDataFlavors(); 68 69 if (df != null) { 70 for (int i = 0; i < df.length; i++) { 71 try { 72 final int fi = i; 73 map.put( 74 df[i], 75 new Single(df[i]) { 76 public Object getData() throws IOException , UnsupportedFlavorException { 77 return t.getTransferData(df[fi]); 78 } 79 } 80 ); 81 } catch (Exception ex) { 82 } 84 } 85 } 86 } 87 88 91 public void put(Single single) { 92 map.put(single.flavor, single); 93 } 94 95 98 public void remove(DataFlavor flavor) { 99 map.remove(flavor); 100 } 101 102 105 public DataFlavor [] getTransferDataFlavors() { 106 return map.keySet().toArray(new DataFlavor [0]); 107 } 108 109 113 public boolean isDataFlavorSupported(DataFlavor flavor) { 114 return map.containsKey(flavor); 115 } 116 117 123 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException , IOException { 124 Single o = map.get(flavor); 125 126 if (o == null) { 127 throw new UnsupportedFlavorException (flavor); 128 } 129 130 return o.getTransferData(flavor); 131 } 132 133 141 public static ExTransferable create(Transferable t) { 142 if (t instanceof ExTransferable) { 145 return (ExTransferable) t; 146 } 147 148 return new ExTransferable(t); 149 } 150 151 155 public synchronized final void addTransferListener(TransferListener l) { 156 if (listeners == null) { 157 listeners = new EventListenerList (); 158 } 159 160 listeners.add(TransferListener.class, l); 161 } 162 163 165 public synchronized final void removeTransferListener(TransferListener l) { 166 if (listeners != null) { 167 listeners.remove(TransferListener.class, l); 168 } 169 } 170 171 175 final void fireAccepted(int action) { 176 if (listeners == null) { 177 return; 178 } 179 180 Object [] arr = listeners.getListenerList(); 181 182 for (int i = arr.length - 1; i >= 0; i -= 2) { 183 ((TransferListener) arr[i]).accepted(action); 184 } 185 } 186 187 190 final void fireRejected() { 191 if (listeners == null) { 192 return; 193 } 194 195 Object [] arr = listeners.getListenerList(); 196 197 for (int i = arr.length - 1; i >= 0; i -= 2) { 198 ((TransferListener) arr[i]).rejected(); 199 } 200 } 201 202 205 final void fireOwnershipLost() { 206 if (listeners == null) { 207 return; 208 } 209 210 Object [] arr = listeners.getListenerList(); 211 212 for (int i = arr.length - 1; i >= 0; i -= 2) { 213 ((TransferListener) arr[i]).ownershipLost(); 214 } 215 } 216 217 220 public static abstract class Single extends Object implements Transferable { 221 222 private DataFlavor flavor; 223 224 227 public Single(DataFlavor flavor) { 228 this.flavor = flavor; 229 } 230 231 235 public DataFlavor [] getTransferDataFlavors() { 236 return new DataFlavor [] { flavor }; 237 } 238 239 243 public boolean isDataFlavorSupported(DataFlavor flavor) { 244 return this.flavor.equals(flavor); 245 } 246 247 249 public Object getTransferData(DataFlavor flavor) 250 throws UnsupportedFlavorException , IOException { 251 if (!this.flavor.equals(flavor)) { 252 throw new UnsupportedFlavorException (flavor); 253 } 254 255 return getData(); 256 } 257 258 265 protected abstract Object getData() throws IOException , UnsupportedFlavorException ; 266 } 267 268 274 public static class Multi extends Object implements Transferable { 275 276 private static final DataFlavor [] flavorList = { multiFlavor }; 277 278 279 private MultiTransferObject transferObject; 280 281 285 public Multi(Transferable [] trans) { 286 transferObject = new TransferObjectImpl(trans); 287 } 288 289 292 public DataFlavor [] getTransferDataFlavors() { 293 return flavorList; 294 } 295 296 300 public boolean isDataFlavorSupported(DataFlavor flavor) { 301 return flavor.equals(multiFlavor); 302 } 303 304 310 public Object getTransferData(DataFlavor flavor) 311 throws UnsupportedFlavorException , IOException { 312 if (!isDataFlavorSupported(flavor)) { 313 throw new UnsupportedFlavorException (flavor); 314 } 315 316 return transferObject; 317 } 318 319 320 static class TransferObjectImpl implements MultiTransferObject { 321 322 private Transferable [] trans; 323 324 327 public TransferObjectImpl(Transferable [] trans) { 328 this.trans = trans; 329 } 330 331 334 public int getCount() { 335 return trans.length; 336 } 337 338 339 public Transferable getTransferableAt(int index) { 340 return trans[index]; 341 } 342 343 349 public boolean isDataFlavorSupported(int index, DataFlavor flavor) { 350 try { 351 return trans[index].isDataFlavorSupported(flavor); 352 } catch (Exception e) { 353 return false; 354 355 } 358 } 359 360 364 public boolean areDataFlavorsSupported(DataFlavor [] array) { 365 HashSet <DataFlavor > flav = new HashSet <DataFlavor >(); 366 367 for (int i = 0; i < array.length; i++) { 368 flav.add(array[i]); 369 } 370 371 372 outer: 375 for (int i = 0; i < trans.length; i++) { 376 DataFlavor [] flavors = trans[i].getTransferDataFlavors(); 378 379 if (flavors == null) { 380 return false; 381 } 382 383 for (int j = 0; j < flavors.length; j++) { 385 if (flav.contains(flavors[j])) { 386 continue outer; 388 } 389 } 390 391 return false; 393 } 394 395 return true; 396 } 397 398 402 public DataFlavor [] getTransferDataFlavors(int i) { 403 return trans[i].getTransferDataFlavors(); 404 } 405 406 411 public Object getTransferData(int indx, DataFlavor flavor) 412 throws UnsupportedFlavorException , IOException { 413 return trans[indx].getTransferData(flavor); 414 } 415 416 464 } 465 } 466 467 471 private static class Empty extends Object implements Transferable { 472 474 Empty() { 475 } 476 477 480 public DataFlavor [] getTransferDataFlavors() { 481 return new DataFlavor [] { }; 482 } 483 484 488 public boolean isDataFlavorSupported(DataFlavor flavor) { 489 return false; 490 } 491 492 495 public Object getTransferData(DataFlavor flavor) 496 throws UnsupportedFlavorException , IOException { 497 throw new UnsupportedFlavorException (flavor); 498 } 499 } 500 } 501 | Popular Tags |