org.eclipse.swt.dnd
Class Clipboard

java.lang.Object
  |
  +--org.eclipse.swt.dnd.Clipboard

public class Clipboard
extends Object


Constructor Summary
Clipboard(Display display)
          Constructs a new instance of this class.
 
Method Summary
 void dispose()
          Disposes of the operating system resources associated with the clipboard.
 Object getContents(Transfer transfer)
          Retrieve the data of the specified type currently available on the system clipboard.
 void setContents(Object[] data, Transfer[] dataTypes)
          Place data of the specified type on the system clipboard.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, toString
 

Constructor Detail

Clipboard

public Clipboard(Display display)
Constructs a new instance of this class. Note: this may cause system resources to be allocated, depending on the platform. It is therefore mandatory that the Clipboard instance is disposed when no longer required.

Parameters:
display - the display on which to allocate the clipboard
Throws:
SWTException -
Method Detail

dispose

public void dispose()
Disposes of the operating system resources associated with the clipboard. The data will still be available on the system clipboard after the dispose method is called.

NOTE: On some platforms the data will not be available once the application has exited or the display has been disposed.


getContents

public Object getContents(Transfer transfer)
Retrieve the data of the specified type currently available on the system clipboard. Refer to the specific subclass of Transfer to determine the type of object returned.

The following example shows text and RTF text being retrieved from the clipboard:

            Clipboard clipboard = new Clipboard(display);
            TextTransfer textTransfer = TextTransfer.getInstance();
            String textData = (String)clipboard.getContents(textTransfer);
            if (textData != null) System.out.println("Text is "+textData);
            RTFTransfer rtfTransfer = RTFTransfer.getInstance();
            String rtfData = (String)clipboard.getContents(rtfTransfer);
            if (rtfData != null) System.out.println("RTF Text is "+rtfData);
            clipboard.dispose();
        

Parameters:
transfer - the transfer agent for the type of data being requested
Returns:
the data obtained from the clipboard or null if no data of this type is available
See Also:
Transfer

setContents

public void setContents(Object[] data,
                        Transfer[] dataTypes)
Place data of the specified type on the system clipboard. More than one type of data can be placed on the system clipboard at the same time. Setting the data clears any previous data of the same type from the system clipboard and also clears data of any other type currently on the system clipboard.

NOTE: On some platforms, the data is immediately copied to the system clipboard but on other platforms it is provided upon request. As a result, if the application modifes the data object it has set on the clipboard, that modification may or may not be available when the data is subsequently requested.

The following example shows text and RTF text being set on the clipboard:

                Clipboard clipboard=new Clipboard(display);
                String textData="Hello World";
                String rtfData="{\\rtf1\\b\\i Hello World}";
                TextTransfer textTransfer=TextTransfer.getInstance();
                RTFTransfer rtfTransfer=RTFTransfer.getInstance();
                clipboard.setContents(new Object[]{textData, rtfData}, new Transfer[]{textTransfer, rtfTransfer});
                clipboard.dispose();
        

Parameters:
data - the data to be set in the clipboard
dataTypes - the transfer agents that will convert the data to its platform specific format; each entry in the data array must have a corresponding dataType
Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if data is null or datatypes is null or the length of data is not the same as the length of dataTypes
SWTError -
  • ERROR_CANNOT_SET_CLIPBOARD - if the clipboard is locked or otherwise unavailable