FileDocCategorySizeDatePackage
Clipboard.javaAPI DocJava SE 6 API11557Tue Jun 10 00:25:20 BST 2008java.awt.datatransfer

Clipboard

public class Clipboard extends Object
A class that implements a mechanism to transfer data using cut/copy/paste operations.

{@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

Fields Summary
String
name
protected ClipboardOwner
owner
protected Transferable
contents
private sun.awt.EventListenerAggregate
flavorListeners
An aggregate of flavor listeners registered on this local clipboard.
private Set
currentDataFlavors
A set of DataFlavors that is available on this local clipboard. It is used for tracking changes of DataFlavors available on this clipboard.
Constructors Summary
public Clipboard(String name)
Creates a clipboard object.

see
java.awt.Toolkit#getSystemClipboard

        this.name = name;
    
Methods Summary
public synchronized voidaddFlavorListener(java.awt.datatransfer.FlavorListener listener)
Registers the specified FlavorListener to receive FlavorEvents from this clipboard. If listener is null, 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

        if (listener == null) {
            return;
        }
        if (flavorListeners == null) {
            currentDataFlavors = getAvailableDataFlavorSet();
            flavorListeners = new EventListenerAggregate(FlavorListener.class);
        }
        flavorListeners.add(listener);
    
private voidfireFlavorsChanged()
Checks change of the DataFlavors and, if necessary, notifies all listeners that have registered interest for notification on FlavorEvents.

since
1.5

        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));
                }
            });
        }
    
private java.util.SetgetAvailableDataFlavorSet()
Returns a set of DataFlavors currently available on this clipboard.

return
a set of DataFlavors currently available on this clipboard
since
1.5

        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;
    
public java.awt.datatransfer.DataFlavor[]getAvailableDataFlavors()
Returns an array of DataFlavors in which the current contents of this clipboard can be provided. If there are no DataFlavors available, this method returns a zero-length array.

return
an array of DataFlavors in which the current contents of this clipboard can be provided
throws
IllegalStateException if this clipboard is currently unavailable
since
1.5

        Transferable cntnts = getContents(null);
        if (cntnts == null) {
            return new DataFlavor[0];
        }
        return cntnts.getTransferDataFlavors();
    
public synchronized java.awt.datatransfer.TransferablegetContents(java.lang.Object requestor)
Returns a transferable object representing the current contents of the clipboard. If the clipboard currently has no contents, it returns null. The parameter Object requestor is not currently used. The method throws IllegalStateException 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

        return contents;
    
public java.lang.ObjectgetData(java.awt.datatransfer.DataFlavor flavor)
Returns an object representing the current contents of this clipboard in the specified DataFlavor. The class of the object returned is defined by the representation class of flavor.

param
flavor the requested DataFlavor for the contents
return
an object representing the current contents of this clipboard in the specified DataFlavor
throws
NullPointerException if flavor is null
throws
IllegalStateException if this clipboard is currently unavailable
throws
UnsupportedFlavorException if the requested DataFlavor is not available
throws
IOException if the data in the requested DataFlavor can not be retrieved
see
DataFlavor#getRepresentationClass
since
1.5

        if (flavor == null) {
            throw new NullPointerException("flavor");
        }

        Transferable cntnts = getContents(null);
        if (cntnts == null) {
            throw new UnsupportedFlavorException(flavor);
        }
        return cntnts.getTransferData(flavor);
    
public synchronized java.awt.datatransfer.FlavorListener[]getFlavorListeners()
Returns an array of all the FlavorListeners currently registered on this Clipboard.

return
all of this clipboard's FlavorListeners or an empty array if no listeners are currently registered
see
#addFlavorListener
see
#removeFlavorListener
see
FlavorListener
see
FlavorEvent
since
1.5

        return flavorListeners == null ? new FlavorListener[0] :
                (FlavorListener[])flavorListeners.getListenersCopy();
    
public java.lang.StringgetName()
Returns the name of this clipboard object.

see
java.awt.Toolkit#getSystemClipboard

        return name;
    
public booleanisDataFlavorAvailable(java.awt.datatransfer.DataFlavor flavor)
Returns whether or not the current contents of this clipboard can be provided in the specified DataFlavor.

param
flavor the requested DataFlavor for the contents
return
true if the current contents of this clipboard can be provided in the specified DataFlavor; false otherwise
throws
NullPointerException if flavor is null
throws
IllegalStateException if this clipboard is currently unavailable
since
1.5

        if (flavor == null) {
            throw new NullPointerException("flavor");
        }

        Transferable cntnts = getContents(null);
        if (cntnts == null) {
            return false;
        }
        return cntnts.isDataFlavorSupported(flavor);
    
public synchronized voidremoveFlavorListener(java.awt.datatransfer.FlavorListener listener)
Removes the specified FlavorListener so that it no longer receives FlavorEvents from this Clipboard. This method performs no function, nor does it throw an exception, if the listener specified by the argument was not previously added to this Clipboard. If listener is null, 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

        if (listener == null || flavorListeners == null) {
            return;
        } 
        flavorListeners.remove(listener);
    
public synchronized voidsetContents(java.awt.datatransfer.Transferable contents, java.awt.datatransfer.ClipboardOwner owner)
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.

If there is an existing owner different from the argument owner, that owner is notified that it no longer holds ownership of the clipboard contents via an invocation of ClipboardOwner.lostOwnership() on that owner. An implementation of setContents() is free not to invoke lostOwnership() directly from this method. For example, lostOwnership() may be invoked later on a different thread. The same applies to FlavorListeners registered on this clipboard.

The method throws IllegalStateException 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

        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();