Spec-Zone .ru
спецификации, руководства, описания, API
|
public class Clipboard
extends java.lang.Object
To access the general system clipboard, use the following code:
Clipboard clipboard = Clipboard.getSystemClipboard();
There is only ever one instance of the system clipboard in the application, so it is perfectly acceptable to stash a reference to it somewhere handy if you so choose.
The Clipboard operates on the concept of having a single conceptual item on the clipboard at any one time -- though it may be placed on the clipboard in different formats. For example, the user might select text in an HTML editor and press the ctrl+c or cmd+c to copy it. In this case, the same text might be available on the clipboard both as HTML and as plain text. There are two copies of the data on the clipboard, but they both represent the same data.
Content is specified on the Clipboard by using the setContent(java.util.Map)
method. First, construct a ClipboardContent object, then invoke setContent. Every time
setContent is called, any previous data on the clipboard is cleared and replaced with
this new content.
final Clipboard clipboard = Clipboard.getSystemClipboard();
final ClipboardContent content = new ClipboardContent();
content.putString("Some text");
content.putHtml("<b>Some</b> text");
clipboard.setContent(content);
The ClipboardContent
class is simply a map with convenience methods for dealing
with common data types added to a clipboard.
Because multiple representations of the same data may exist on the clipboard, and because different applications have different capabilities for handling different content types, it is important to place as many data representations on the clipboard as is practical to facilitate external applications. Note that sometimes the operating system might be helpful in some cases and add multiple types for you. For example, the Mac might set the plain text string for you when you specify the RTF type. How and under what circumstances this occurs is outside the realm of this specification, consult your OS documentation.
When reading data off the clipboard, it is important to look for the richest supported type first. For example, if I have a text document which supports embedding of images and media formats, when pasting content from the clipboard I should first check to see if the content can be represented as media or as an image. If not, then I might check for RTF or HTML or whatever rich text format is supported by my document type. If not, then I might just take a String.
Or for example, if I have a plain text document, then I would simple get a String representation and use that, if available. I can check to see if the clipboard "hasHtml" or "hasString".
if (clipboard.hasString()) { ... }
In addition to the common or built in types, you may put any arbitrary data onto the clipboard (assuming it is either a reference, or serializable. See more about references later). The clipboard system in JavaFX utilizes the same system as Cocoa for defining a hierarchical namespace for content types. See http://en.wikipedia.org/wiki/Uniform_Type_Identifier.
The DataFormat class defines an immutable object, and there are a number of static final fields for common DataFormat types. Of course application specific DataFormat types can also be declared and used. The following two methods are equivalent (and the second call will override the first!)
ClipboardContent content = new ClipboardContent();
content.putString("some text");
content.put(DataFormat.PLAIN_TEXT, "other text");
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the clipboard of any and all content.
|
java.lang.Object |
getContent(DataFormat dataFormat)
Returns the content stored in this clipboard of the given type, or null
if there is no content with this type.
|
java.util.Set<DataFormat> |
getContentTypes()
Gets the set of DataFormat types on this Clipboard instance which have
associated data registered on the clipboard.
|
java.util.List<java.io.File> |
getFiles()
Gets the List of Files from the clipboard which had previously
been registered.
|
java.lang.String |
getHtml()
Gets the HTML text String from the clipboard which had previously
been registered.
|
Image |
getImage()
Gets the Image from the clipboard which had previously
been registered.
|
java.lang.String |
getRtf()
Gets the RTF text String from the clipboard which had previously
been registered.
|
java.lang.String |
getString()
Gets the plain text String from the clipboard which had previously
been registered.
|
static Clipboard |
getSystemClipboard()
Gets the current system clipboard, through which data can be stored and
retrieved.
|
java.lang.String |
getUrl()
Gets the URL String from the clipboard which had previously
been registered.
|
boolean |
hasContent(DataFormat dataFormat)
Tests whether there is any content on this clipboard of the given DataFormat type.
|
boolean |
hasFiles()
Gets whether an List of Files (DataFormat.FILES) has been registered
on this Clipboard.
|
boolean |
hasHtml()
Gets whether an HTML text String (DataFormat.HTML) has been registered
on this Clipboard.
|
boolean |
hasImage()
Gets whether an Image (DataFormat.IMAGE) has been registered
on this Clipboard.
|
boolean |
hasRtf()
Gets whether an RTF String (DataFormat.RTF) has been registered
on this Clipboard.
|
boolean |
hasString()
Gets whether a plain text String (DataFormat.PLAIN_TEXT) has been registered
on this Clipboard.
|
boolean |
hasUrl()
Gets whether a url String (DataFormat.URL) has been registered
on this Clipboard.
|
boolean |
setContent(java.util.Map<DataFormat,java.lang.Object> content)
Puts content onto the clipboard.
|
public static Clipboard getSystemClipboard()
public final void clear()
getContentTypes()
before putting more content on the clipboard
will result in an empty set being returned.public final java.util.Set<DataFormat> getContentTypes()
public final boolean setContent(java.util.Map<DataFormat,java.lang.Object> content)
content
- The content to put on the clipboard. If null, the
clipboard is simply cleared and no new content added.java.lang.NullPointerException
- if null data reference is passed for any
formatpublic final java.lang.Object getContent(DataFormat dataFormat)
public final boolean hasContent(DataFormat dataFormat)
public final boolean hasString()
hasContent(DataFormat.PLAIN_TEXT)
returns true, false otherwisepublic final java.lang.String getString()
getContent(DataFormat.PLAIN_TEXT)
. If no such entry exists,
null is returned.public final boolean hasUrl()
public final java.lang.String getUrl()
getContent(DataFormat.URL)
. If no such entry exists,
null is returned.public final boolean hasHtml()
hasContent(DataFormat.HTML)
returns true, false otherwisepublic final java.lang.String getHtml()
getContent(DataFormat.HTML)
. If no such entry exists,
null is returned.public final boolean hasRtf()
public final java.lang.String getRtf()
getContent(DataFormat.RTF)
. If no such entry exists,
null is returned.public final boolean hasImage()
public final Image getImage()
getContent(DataFormat.IMAGE)
. If no such entry exists,
null is returned.public final boolean hasFiles()
public final java.util.List<java.io.File> getFiles()
getContent(DataFormat.FILES)
. If no such entry exists,
null is returned.Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved. Use is subject to