1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
/* * @(#)Clipboard.java 1.24 06/04/07 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.awt.datatransfer; import java.awt.EventQueue; import java.util.Set; import java.util.HashSet; import java.util.Arrays; import java.io.IOException; import sun.awt.EventListenerAggregate; /** * A class that implements a mechanism to transfer data using * cut/copy/paste operations. * <p> * {@link FlavorListener}s may be registered on an instance of the * Clipboard class to be notified about changes to the set of * {@link DataFlavor}s available on this clipboard (see * {@link #addFlavorListener}). * * @see java.awt.Toolkit#getSystemClipboard * @see java.awt.Toolkit#getSystemSelection * * @version 1.24, 04/07/06 * @author Amy Fowler * @author Alexander Gerasimov */ public class Clipboard { String name; protected ClipboardOwner owner; protected Transferable contents; /** * An aggregate of flavor listeners registered on this local clipboard. * * @since 1.5 */ private EventListenerAggregate flavorListeners; /** * A set of <code>DataFlavor</code>s that is available on * this local clipboard. It is used for tracking changes * of <code>DataFlavor</code>s available on this clipboard. * * @since 1.5 */ private Set currentDataFlavors; /** * Creates a clipboard object. * * @see java.awt.Toolkit#getSystemClipboard */ public Clipboard(String name) { this.name = name; } /** * Returns the name of this clipboard object. * * @see java.awt.Toolkit#getSystemClipboard */ public String getName() { return name; } /** * Sets the current contents of the clipboard to the specified * transferable object and registers the specified clipboard owner * as the owner of the new contents. * <p> * If there is an existing owner different from the argument * <code>owner</code>, that owner is notified that it no longer * holds ownership of the clipboard contents via an invocation * of <code>ClipboardOwner.lostOwnership()</code> on that owner. * An implementation of <code>setContents()</code> is free not * to invoke <code>lostOwnership()</code> directly from this method. * For example, <code>lostOwnership()</code> may be invoked later on * a different thread. The same applies to <code>FlavorListener</code>s * registered on this clipboard. * <p> * The method throws <code>IllegalStateException</code> if the clipboard * is currently unavailable. For example, on some platforms, the system * clipboard is unavailable while it is accessed by another application. * * @param contents the transferable object representing the * clipboard content * @param owner the object which owns the clipboard content * @throws IllegalStateException if the clipboard is currently unavailable * @see java.awt.Toolkit#getSystemClipboard */ public synchronized void setContents(Transferable contents, ClipboardOwner owner) { final ClipboardOwner oldOwner = this.owner; final Transferable oldContents = this.contents; this.owner = owner; this.contents = contents; if (oldOwner != null && oldOwner != owner) { EventQueue.invokeLater(new Runnable() { public void run() { oldOwner.lostOwnership(Clipboard.this, oldContents); } }); } fireFlavorsChanged(); } /** * Returns a transferable object representing the current contents * of the clipboard. If the clipboard currently has no contents, * it returns <code>null</code>. The parameter Object requestor is * not currently used. The method throws * <code>IllegalStateException</code> if the clipboard is currently * unavailable. For example, on some platforms, the system clipboard is * unavailable while it is accessed by another application. * * @param requestor the object requesting the clip data (not used) * @return the current transferable object on the clipboard * @throws IllegalStateException if the clipboard is currently unavailable * @see java.awt.Toolkit#getSystemClipboard */ public synchronized Transferable getContents(Object requestor) { return contents; } /** * Returns an array of <code>DataFlavor</code>s in which the current * contents of this clipboard can be provided. If there are no * <code>DataFlavor</code>s available, this method returns a zero-length * array. * * @return an array of <code>DataFlavor</code>s in which the current * contents of this clipboard can be provided * * @throws IllegalStateException if this clipboard is currently unavailable * * @since 1.5 */ public DataFlavor[] getAvailableDataFlavors() { Transferable cntnts = getContents(null); if (cntnts == null) { return new DataFlavor[0]; } return cntnts.getTransferDataFlavors(); } /** * Returns whether or not the current contents of this clipboard can be * provided in the specified <code>DataFlavor</code>. * * @param flavor the requested <code>DataFlavor</code> for the contents * * @return <code>true</code> if the current contents of this clipboard * can be provided in the specified <code>DataFlavor</code>; * <code>false</code> otherwise * * @throws NullPointerException if <code>flavor</code> is <code>null</code> * @throws IllegalStateException if this clipboard is currently unavailable * * @since 1.5 */ public boolean isDataFlavorAvailable(DataFlavor flavor) { if (flavor == null) { throw new NullPointerException("flavor"); } Transferable cntnts = getContents(null); if (cntnts == null) { return false; } return cntnts.isDataFlavorSupported(flavor); } /** * Returns an object representing the current contents of this clipboard * in the specified <code>DataFlavor</code>. * The class of the object returned is defined by the representation * class of <code>flavor</code>. * * @param flavor the requested <code>DataFlavor</code> for the contents * * @return an object representing the current contents of this clipboard * in the specified <code>DataFlavor</code> * * @throws NullPointerException if <code>flavor</code> is <code>null</code> * @throws IllegalStateException if this clipboard is currently unavailable * @throws UnsupportedFlavorException if the requested <code>DataFlavor</code> * is not available * @throws IOException if the data in the requested <code>DataFlavor</code> * can not be retrieved * * @see DataFlavor#getRepresentationClass * * @since 1.5 */ public Object getData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { if (flavor == null) { throw new NullPointerException("flavor"); } Transferable cntnts = getContents(null); if (cntnts == null) { throw new UnsupportedFlavorException(flavor); } return cntnts.getTransferData(flavor); } /** * Registers the specified <code>FlavorListener</code> to receive * <code>FlavorEvent</code>s from this clipboard. * If <code>listener</code> is <code>null</code>, no exception * is thrown and no action is performed. * * @param listener the listener to be added * * @see #removeFlavorListener * @see #getFlavorListeners * @see FlavorListener * @see FlavorEvent * @since 1.5 */ public synchronized void addFlavorListener(FlavorListener listener) { if (listener == null) { return; } if (flavorListeners == null) { currentDataFlavors = getAvailableDataFlavorSet(); flavorListeners = new EventListenerAggregate(FlavorListener.class); } flavorListeners.add(listener); } /** * Removes the specified <code>FlavorListener</code> so that it no longer * receives <code>FlavorEvent</code>s from this <code>Clipboard</code>. * This method performs no function, nor does it throw an exception, if * the listener specified by the argument was not previously added to this * <code>Clipboard</code>. * If <code>listener</code> is <code>null</code>, no exception * is thrown and no action is performed. * * @param listener the listener to be removed * * @see #addFlavorListener * @see #getFlavorListeners * @see FlavorListener * @see FlavorEvent * @since 1.5 */ public synchronized void removeFlavorListener(FlavorListener listener) { if (listener == null || flavorListeners == null) { return; } flavorListeners.remove(listener); } /** * Returns an array of all the <code>FlavorListener</code>s currently * registered on this <code>Clipboard</code>. * * @return all of this clipboard's <code>FlavorListener</code>s or an empty * array if no listeners are currently registered * @see #addFlavorListener * @see #removeFlavorListener * @see FlavorListener * @see FlavorEvent * @since 1.5 */ public synchronized FlavorListener[] getFlavorListeners() { return flavorListeners == null ? new FlavorListener[0] : (FlavorListener[])flavorListeners.getListenersCopy(); } /** * Checks change of the <code>DataFlavor</code>s and, if necessary, * notifies all listeners that have registered interest for notification * on <code>FlavorEvent</code>s. * * @since 1.5 */ private void fireFlavorsChanged() { if (flavorListeners == null) { return; } Set prevDataFlavors = currentDataFlavors; currentDataFlavors = getAvailableDataFlavorSet(); if (prevDataFlavors.equals(currentDataFlavors)) { return; } FlavorListener[] flavorListenerArray = (FlavorListener[])flavorListeners.getListenersInternal(); for (int i = 0; i < flavorListenerArray.length; i++) { final FlavorListener listener = flavorListenerArray[i]; EventQueue.invokeLater(new Runnable() { public void run() { listener.flavorsChanged(new FlavorEvent(Clipboard.this)); } }); } } /** * Returns a set of <code>DataFlavor</code>s currently available * on this clipboard. * * @return a set of <code>DataFlavor</code>s currently available * on this clipboard * * @since 1.5 */ private Set getAvailableDataFlavorSet() { Set set = new HashSet(); Transferable contents = getContents(null); if (contents != null) { DataFlavor[] flavors = contents.getTransferDataFlavors(); if (flavors != null) { set.addAll(Arrays.asList(flavors)); } } return set; } }