1 7 8 package java.awt.datatransfer; 9 10 import java.awt.EventQueue ; 11 12 import java.util.Set ; 13 import java.util.HashSet ; 14 import java.util.Arrays ; 15 16 import java.io.IOException ; 17 18 import sun.awt.EventListenerAggregate; 19 20 21 37 public class Clipboard { 38 39 String name; 40 41 protected ClipboardOwner owner; 42 protected Transferable contents; 43 44 49 private EventListenerAggregate flavorListeners; 50 51 58 private Set currentDataFlavors; 59 60 65 public Clipboard(String name) { 66 this.name = name; 67 } 68 69 74 public String getName() { 75 return name; 76 } 77 78 103 public synchronized void setContents(Transferable contents, ClipboardOwner owner) { 104 final ClipboardOwner oldOwner = this.owner; 105 final Transferable oldContents = this.contents; 106 107 this.owner = owner; 108 this.contents = contents; 109 110 if (oldOwner != null && oldOwner != owner) { 111 EventQueue.invokeLater(new Runnable () { 112 public void run() { 113 oldOwner.lostOwnership(Clipboard.this, oldContents); 114 } 115 }); 116 } 117 fireFlavorsChanged(); 118 } 119 120 134 public synchronized Transferable getContents(Object requestor) { 135 return contents; 136 } 137 138 139 152 public DataFlavor [] getAvailableDataFlavors() { 153 Transferable cntnts = getContents(null); 154 if (cntnts == null) { 155 return new DataFlavor [0]; 156 } 157 return cntnts.getTransferDataFlavors(); 158 } 159 160 175 public boolean isDataFlavorAvailable(DataFlavor flavor) { 176 if (flavor == null) { 177 throw new NullPointerException ("flavor"); 178 } 179 180 Transferable cntnts = getContents(null); 181 if (cntnts == null) { 182 return false; 183 } 184 return cntnts.isDataFlavorSupported(flavor); 185 } 186 187 209 public Object getData(DataFlavor flavor) 210 throws UnsupportedFlavorException , IOException { 211 if (flavor == null) { 212 throw new NullPointerException ("flavor"); 213 } 214 215 Transferable cntnts = getContents(null); 216 if (cntnts == null) { 217 throw new UnsupportedFlavorException (flavor); 218 } 219 return cntnts.getTransferData(flavor); 220 } 221 222 223 237 public synchronized void addFlavorListener(FlavorListener listener) { 238 if (listener == null) { 239 return; 240 } 241 if (flavorListeners == null) { 242 currentDataFlavors = getAvailableDataFlavorSet(); 243 flavorListeners = new EventListenerAggregate(FlavorListener .class); 244 } 245 flavorListeners.add(listener); 246 } 247 248 265 public synchronized void removeFlavorListener(FlavorListener listener) { 266 if (listener == null || flavorListeners == null) { 267 return; 268 } 269 flavorListeners.remove(listener); 270 } 271 272 284 public synchronized FlavorListener [] getFlavorListeners() { 285 return flavorListeners == null ? new FlavorListener [0] : 286 (FlavorListener [])flavorListeners.getListenersCopy(); 287 } 288 289 296 private void fireFlavorsChanged() { 297 if (flavorListeners == null) { 298 return; 299 } 300 Set prevDataFlavors = currentDataFlavors; 301 currentDataFlavors = getAvailableDataFlavorSet(); 302 if (prevDataFlavors.equals(currentDataFlavors)) { 303 return; 304 } 305 FlavorListener [] flavorListenerArray = 306 (FlavorListener [])flavorListeners.getListenersInternal(); 307 for (int i = 0; i < flavorListenerArray.length; i++) { 308 final FlavorListener listener = flavorListenerArray[i]; 309 EventQueue.invokeLater(new Runnable () { 310 public void run() { 311 listener.flavorsChanged(new FlavorEvent (Clipboard.this)); 312 } 313 }); 314 } 315 } 316 317 326 private Set getAvailableDataFlavorSet() { 327 Set set = new HashSet (); 328 Transferable contents = getContents(null); 329 if (contents != null) { 330 DataFlavor [] flavors = contents.getTransferDataFlavors(); 331 if (flavors != null) { 332 set.addAll(Arrays.asList(flavors)); 333 } 334 } 335 return set; 336 } 337 } 338 339 340 341 342 | Popular Tags |