|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectinfo.codesaway.util.SystemClipboard
public class SystemClipboard
Utility class to simplify using the system's clipboard.
This class wraps functionality available from the Clipboard
class
to allow copying and retrieving a string, a list of files, or an image.
There exists methods to test if the clipboard is empty, or if not empty, if the clipboard can be represented as a string, text, a list of files, or an image.
The clear()
method (or alternatively, the empty()
method) provides a way to clear
the clipboard, removing its contents.
Method Summary | |
---|---|
static void |
addFlavorListener(FlavorListener listener)
Registers the specified FlavorListener to receive
FlavorEvent s from the system clipboard. |
static String |
asFilenames()
Returns the clipboard's contents with the DataFlavor.javaFileListFlavor as a list of filenames, using the
line separator string to delimit each filename. |
static String |
asFilenames(String delimiter)
Returns the clipboard's contents with the DataFlavor.javaFileListFlavor as a list of filenames, using the
specified delimiter to delimit each filename. |
static List<File> |
asFiles()
Returns the clipboard's contents with the DataFlavor.javaFileListFlavor . |
static Image |
asImage()
Returns the clipboard's contents with the DataFlavor.imageFlavor . |
static String |
asString()
Returns the clipboard's contents with the DataFlavor.stringFlavor . |
static String |
asText()
Returns the clipboard's contents as text. |
static void |
clear()
Clears the clipboard. |
static Clipboard |
clipboard()
Returns the system clipboard. |
static void |
copy(CharSequence text)
Copies the given character sequence to the clipboard. |
static void |
copy(Collection<File> files)
Copies the given files to the clipboard. |
static void |
copy(File... files)
Copies the given files to the clipboard. |
static void |
copy(Image image)
Copies the given image to the clipboard. |
static void |
empty()
Clears the clipboard. |
static DataFlavor[] |
getAvailableDataFlavors()
Returns an array of DataFlavor s in which the current
contents of the system clipboard can be provided. |
static Object |
getClipboard(DataFlavor flavor)
Returns the system clipboard's contents in the requested DataFlavor if possible. |
static Transferable |
getContents(Object requestor)
Returns a transferable object representing the current contents of the system clipboard. |
static Object |
getData(DataFlavor flavor)
Returns an object representing the current contents of the system clipboard in the specified DataFlavor . |
static FlavorListener[] |
getFlavorListeners()
Returns an array of all the FlavorListener s currently
registered on the system clipboard. |
static String |
getName()
Returns the name of the system clipboard. |
static boolean |
isDataFlavorAvailable(DataFlavor flavor)
Returns whether or not the current contents of the system clipboard can be provided in the specified DataFlavor . |
static boolean |
isEmpty()
Returns true if, and only if,
getAvailableDataFlavors() .length is 0. |
static boolean |
isFiles()
Returns whether the clipboard's contents can be represented as a List of File s. |
static boolean |
isImage()
Returns whether the clipboard's contents can be represented as an Image . |
static boolean |
isString()
Returns whether the clipboard's contents can be represented as a String . |
static boolean |
isText()
Returns whether the clipboard's contents can be represented as text. |
static void |
removeFlavorListener(FlavorListener listener)
Removes the specified FlavorListener so that it no longer
receives FlavorEvent s from the system clipboard. |
static void |
setContents(Transferable contents,
ClipboardOwner owner)
Sets the current contents of the system clipboard to the specified transferable object and registers the specified clipboard owner as the owner of the new contents. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static void clear()
public static void empty()
clear()
.
public static boolean isEmpty()
true
if, and only if,
getAvailableDataFlavors()
.length is 0.
true
if
getAvailableDataFlavors()
.length is 0, otherwise
false
public static void copy(CharSequence text)
DataFlavor.stringFlavor
is the DataFlavor
used
for the copy operation.
Note: if text
is null
, the clipboard
will be cleared
instead.
text
- the character sequence to copypublic static void copy(File... files)
DataFlavor.javaFileListFlavor
is the DataFlavor
used for the copy operation.
Note: if files
is null
, the clipboard
will be cleared
instead.
files
- the File
s to copypublic static void copy(Collection<File> files)
DataFlavor.javaFileListFlavor
is the DataFlavor
used for the copy operation.
Note: if files
is null
, the clipboard
will be cleared
instead.
files
- the File
s to copypublic static void copy(Image image)
DataFlavor.imageFlavor
is the DataFlavor
used for the copy operation.
Note: if image
is null
, the clipboard
will be cleared
instead.
image
- the Image
to copypublic static String asText()
For a String
, this is the string itself. For a
List
of File
s, it is the list of the filenames,
using the line separator string to delimit each filename.
The line separator string is defined by the system property
line.separator
, and is not necessarily a single newline
character ('\n'
).
null
if the
clipboard's contents cannot be expressed as text.asString()
,
asFilenames()
,
System.getProperty(String)
public static boolean isText()
true
if the current clipboard contents can be
provided as either DataFlavor.stringFlavor
or
DataFlavor.javaFileListFlavor
; false
otherwisepublic static String asString()
DataFlavor.stringFlavor
.
String
, or null
if they cannot.public static boolean isString()
String
.
true
if the current clipboard contents can be
provided as DataFlavor.stringFlavor
; false
otherwisepublic static List<File> asFiles()
DataFlavor.javaFileListFlavor
.
Note: the returned list cannot be modified - an
UnsupportedOperationException
will be thrown if any attempt is
made. This is done to prevent modification to the clipboard's internal
data, since such modifications do not change the clipboard.
However, you can create a new list containing the returned files, modify
this new list, and copy it at a later time - this will set the clipboard
(to the new list's contents).
List
of
File
s, or null
if they cannot.public static boolean isFiles()
List
of File
s.
true
if the current clipboard contents can be
provided as DataFlavor.javaFileListFlavor
;
false
otherwisepublic static String asFilenames()
DataFlavor.javaFileListFlavor
as a list of filenames, using the
line separator string to delimit each filename.
The line separator string is defined by the system property
line.separator
, and is not necessarily a single newline
character ('\n'
).
null
if
they cannot.System.getProperty(String)
public static String asFilenames(String delimiter)
DataFlavor.javaFileListFlavor
as a list of filenames, using the
specified delimiter to delimit each filename.
delimiter
- the string used to delimit each filename
null
if
they cannot.public static Image asImage()
DataFlavor.imageFlavor
.
Image
, or null
if they cannot.public static boolean isImage()
Image
.
true
if the current clipboard contents can be
provided as DataFlavor.imageFlavor
; false
otherwisepublic static Clipboard clipboard()
public static void addFlavorListener(FlavorListener listener)
FlavorListener
to receive
FlavorEvent
s from the system clipboard.
If listener
is null
, no exception
is thrown and no action is performed.
listener
- the listener to be addedremoveFlavorListener(java.awt.datatransfer.FlavorListener)
,
getFlavorListeners()
,
FlavorListener
,
FlavorEvent
public static DataFlavor[] getAvailableDataFlavors()
DataFlavor
s in which the current
contents of the system clipboard can be provided. If there are no
DataFlavor
s available, this method returns a zero-length
array.
DataFlavor
s in which the current
contents of the system clipboard can be provided
IllegalStateException
- if the system clipboard is currently unavailablepublic static Transferable getContents(Object requestor)
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.
requestor
- the object requesting the clip data (not used)
IllegalStateException
- if the clipboard is currently unavailableToolkit.getSystemClipboard()
public static Object getClipboard(DataFlavor flavor)
DataFlavor
if possible.
flavor
- the requested flavor for the data
null
if
the data doesn't support the given DataFlavor
NullPointerException
- if flavor
is null
public static Object getData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
DataFlavor
.
The class of the object returned is defined by the representation
class of flavor
.
flavor
- the requested DataFlavor
for the contents
DataFlavor
NullPointerException
- if flavor
is null
IllegalStateException
- if this clipboard is currently unavailable
UnsupportedFlavorException
- if the requested DataFlavor
is not available
IOException
- if the data in the requested DataFlavor
can not be retrievedDataFlavor.getRepresentationClass()
public static FlavorListener[] getFlavorListeners()
FlavorListener
s currently
registered on the system clipboard.
FlavorListener
s or an
empty array if no listeners are currently registeredaddFlavorListener(java.awt.datatransfer.FlavorListener)
,
removeFlavorListener(java.awt.datatransfer.FlavorListener)
,
FlavorListener
,
FlavorEvent
public static String getName()
Toolkit.getSystemClipboard()
public static boolean isDataFlavorAvailable(DataFlavor flavor)
DataFlavor
.
flavor
- the requested DataFlavor
for the contents
true
if the current contents of the system clipboard
can be provided in the specified DataFlavor
;
false
otherwise
NullPointerException
- if flavor
is null
IllegalStateException
- if the system clipboard is currently unavailablepublic static void removeFlavorListener(FlavorListener listener)
FlavorListener
so that it no longer
receives FlavorEvent
s from the system clipboard.
This method performs no function, nor does it throw an exception, if
the listener specified by the argument was not previously added to the
system clipboard.
If listener
is null
, no exception
is thrown and no action is performed.
listener
- the listener to be removedaddFlavorListener(java.awt.datatransfer.FlavorListener)
,
getFlavorListeners()
,
FlavorListener
,
FlavorEvent
public static void setContents(Transferable contents, ClipboardOwner owner)
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 FlavorListener
s
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.
contents
- the transferable object representing the
clipboard contentowner
- the object which owns the clipboard content
IllegalStateException
- if the system clipboard is currently unavailableToolkit.getSystemClipboard()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |