public final class XMLClipboard extends Object implements ClipboardOwner
Node
s).
This class is needed because the clipboard is queried each time the editing context of the XML editor changes (which is: very often).
Modifier and Type | Class and Description |
---|---|
static class |
XMLClipboard.Info
Information about the contents of the XMLClipboard.
|
Modifier and Type | Field and Description |
---|---|
Clipboard |
clipboard |
static Node[] |
PLAIN_TEXT_NODES
A ready-to-use fixed piece of plain text wrapped as
an array of XML nodes.
|
static String |
XML_CLIPBOARD_CONTENT_PROPERTY
The name of the property which can be observed by registered
PropertyChangeListener s. |
Constructor and Description |
---|
XMLClipboard(Clipboard clipboard)
Constructs a clipboard cache delegating to specified clipboard.
|
Modifier and Type | Method and Description |
---|---|
void |
addPropertyChangeListener(PropertyChangeListener listener)
Adds a property change listener to the listener list.
|
Node[] |
copyNodes(String s)
Returns a copy of the parsed form of the contents of
the clipboard.
|
String |
get()
Returns the string contained in the system clipboard if any;
null otherwise. |
XMLClipboard.Info |
getInfo(XMLClipboard.Info info)
Returns information about the current content of the XMLClipboard.
|
Node[] |
getNodes(String s)
Returns the parsed form of the contents of the clipboard.
|
static String |
getPlainText(Node[] nodes)
Returns a string if specified nodes are just a wrapper
around plain text;
null if specified nodes
are actual XML nodes. |
PropertyChangeListener[] |
getPropertyChangeListeners()
Returns an array of all the registered property change listeners.
|
static XMLClipboard |
getSharedInstance()
Returns the shared instance of XMLClipboard.
|
static boolean |
isPlainText(Node[] nodes)
Tests whether specified nodes are just a wrapper around plain text.
|
void |
lostOwnership(Clipboard clipboard,
Transferable contents) |
void |
removePropertyChangeListener(PropertyChangeListener listener)
Removes a property change listener from the listener list.
|
boolean |
set(String s)
Copies specified string to the clipboard.
|
static void |
setSharedInstance(XMLClipboard clipboard)
Installs the shared instance of XMLClipboard.
|
void |
startCaching()
startCaching/
stopCaching() blocks can be used to
cache the string returned by get() when the application
knows that a sequence of invocations of get is likely to happen
in a piece of code (example: 3 invocations of get in the next 10ms). |
void |
stopCaching()
See
stopCaching() . |
static Node[] |
wrapPlainText(String s)
Converts specified plain text to an array of XML nodes.
|
public static final Node[] PLAIN_TEXT_NODES
May be used to perform some tests (see implementation of
TextEditor
).
public static final String XML_CLIPBOARD_CONTENT_PROPERTY
PropertyChangeListener
s.public final Clipboard clipboard
public XMLClipboard(Clipboard clipboard)
clipboard
- the clipboard being used; null
to
specify the system clipboardpublic static boolean isPlainText(Node[] nodes)
public static String getPlainText(Node[] nodes)
null
if specified nodes
are actual XML nodes.public static Node[] wrapPlainText(String s)
Note that s characters are not filtered, and this, because the returned nodes are not intended to be inserted as is in an XML document.
public boolean set(String s)
s
- string to be copied to the clipboard.
A string which starts with "<?xml " is considered to be well-formed XML (though the well-formedness is not checked here).
May be null
in which case the internal state of this
XMLClipboard is simply cleared.
true
if the string has been successfully copied to
the clipboard; false
otherwise (system clipboard busy)public String get()
null
otherwise.public void startCaching()
stopCaching()
blocks can be used to
cache the string returned by get()
when the application
knows that a sequence of invocations of get is likely to happen
in a piece of code (example: 3 invocations of get in the next 10ms).
This is just an optimization for very special cases which has been added because retrieving a string copied to the system clipboard by an external application may be quite slow. Of course, the methods of this class works without having to use startCaching/stopCaching blocks.
Note that startCaching/stopCaching blocks can be nested but make sure that there is a invocation of stopCaching for each invocation of startCaching.
public void stopCaching()
stopCaching()
.public XMLClipboard.Info getInfo(XMLClipboard.Info info)
info
- the information is stored in this object.
May be null
.public void addPropertyChangeListener(PropertyChangeListener listener)
The listener may observe a unique property:
XML_CLIPBOARD_CONTENT_PROPERTY
, which represents the contents
of this XMLClipboard.
Note that in the event received by the listeners,
PropertyChangeEvent.getOldValue()
and
PropertyChangeEvent.getNewValue()
both always return a
non-null XMLClipboard.Info
object.
public void removePropertyChangeListener(PropertyChangeListener listener)
public PropertyChangeListener[] getPropertyChangeListeners()
public void lostOwnership(Clipboard clipboard, Transferable contents)
lostOwnership
in interface ClipboardOwner
public Node[] getNodes(String s)
The returned array of nodes must not be modified by client code.
It is suitable to perform some tests (see implementation
of ElementEditor
, TextEditor
).
s
- the string for which a parsed form is to be returned.
Pass null
to specify the contents of the clipboard.get()
,
copyNodes(java.lang.String)
,
isPlainText(com.xmlmind.xml.doc.Node[])
,
getPlainText(com.xmlmind.xml.doc.Node[])
public Node[] copyNodes(String s)
s
- the string for which a parsed form is to be returned.
Pass null
to specify the contents of the clipboard.getNodes(java.lang.String)
,
isPlainText(com.xmlmind.xml.doc.Node[])
,
getPlainText(com.xmlmind.xml.doc.Node[])
public static final void setSharedInstance(XMLClipboard clipboard)
getSharedInstance()
public static final XMLClipboard getSharedInstance()
Creates and installs one if setSharedInstance(com.xmlmind.xmledit.edit.XMLClipboard)
has never
been invoked or has been set to null
.