Methods Summary |
---|
public boolean | canImport(javax.swing.JComponent comp, java.awt.datatransfer.DataFlavor[] transferFlavors)Indicates whether a component would accept an import of the given
set of data flavors prior to actually attempting to import it.
PropertyDescriptor prop = getPropertyDescriptor(comp);
if (prop != null) {
Method writer = prop.getWriteMethod();
if (writer == null) {
// read-only property. ignore
return false;
}
Class[] params = writer.getParameterTypes();
if (params.length != 1) {
// zero or more than one argument, ignore
return false;
}
DataFlavor flavor = getPropertyDataFlavor(params[0], transferFlavors);
if (flavor != null) {
return true;
}
}
return false;
|
protected java.awt.datatransfer.Transferable | createTransferable(javax.swing.JComponent c)Creates a Transferable to use as the source for
a data transfer. Returns the representation of the data to
be transferred, or null if the component's
property is null
PropertyDescriptor property = getPropertyDescriptor(c);
if (property != null) {
return new PropertyTransferable(property, c);
}
return null;
|
public void | exportAsDrag(javax.swing.JComponent comp, java.awt.event.InputEvent e, int action)Causes the Swing drag support to be initiated. This is called by
the various UI implementations in the javax.swing.plaf.basic
package if the dragEnabled property is set on the component.
This can be called by custom UI
implementations to use the Swing drag support. This method can also be called
by a Swing extension written as a subclass of JComponent
to take advantage of the Swing drag support.
The transfer will not necessarily have been completed at the
return of this call (i.e. the call does not block waiting for the drop).
The transfer will take place through the Swing implementation of the
java.awt.dnd mechanism, requiring no further effort
from the developer. The exportDone method will be called
when the transfer has completed.
int srcActions = getSourceActions(comp);
int dragAction = srcActions & action;
if (! (e instanceof MouseEvent)) {
// only mouse events supported for drag operations
dragAction = NONE;
}
if (dragAction != NONE && !GraphicsEnvironment.isHeadless()) {
if (recognizer == null) {
recognizer = new SwingDragGestureRecognizer(new DragHandler());
}
recognizer.gestured(comp, (MouseEvent)e, srcActions, dragAction);
} else {
exportDone(comp, null, NONE);
}
|
protected void | exportDone(javax.swing.JComponent source, java.awt.datatransfer.Transferable data, int action)Invoked after data has been exported. This method should remove
the data that was transferred if the action was MOVE .
This method is implemented to do nothing since MOVE
is not a supported action of this implementation
(getSourceActions does not include MOVE ).
|
public void | exportToClipboard(javax.swing.JComponent comp, java.awt.datatransfer.Clipboard clip, int action)Causes a transfer from the given component to the
given clipboard. This method is called by the default cut and
copy actions registered in a component's action map.
The transfer will take place using the java.awt.datatransfer
mechanism, requiring no further effort from the developer. Any data
transfer will be complete and the exportDone
method will be called with the action that occurred, before this method
returns. Should the clipboard be unavailable when attempting to place
data on it, the IllegalStateException thrown by
{@link Clipboard#setContents(Transferable, ClipboardOwner)} will
be propogated through this method. However,
exportDone will first be called with an action
of NONE for consistency.
int clipboardAction = getSourceActions(comp) & action;
if (clipboardAction != NONE) {
Transferable t = createTransferable(comp);
if (t != null) {
try {
clip.setContents(t, null);
exportDone(comp, t, clipboardAction);
return;
} catch (IllegalStateException ise) {
exportDone(comp, t, NONE);
throw ise;
}
}
}
exportDone(comp, null, NONE);
|
public static javax.swing.Action | getCopyAction()Returns an Action that behaves like a 'copy' operation.
That is, this will invoke exportToClipboard with
a COPY argument on the TransferHandler
associated with the JComponent that is the source of
the ActionEvent .
return copyAction;
|
public static javax.swing.Action | getCutAction()Returns an Action that behaves like a 'cut' operation.
That is, this will invoke exportToClipboard with
a MOVE argument on the TransferHandler
associated with the JComponent that is the source of
the ActionEvent .
return cutAction;
|
private static java.awt.dnd.DropTargetListener | getDropTargetListener()
if (dropLinkage == null) {
dropLinkage = new DropHandler();
}
return dropLinkage;
|
public static javax.swing.Action | getPasteAction()Returns an Action that behaves like a 'paste' operation.
That is, this will invoke importData on the
TransferHandler
associated with the JComponent that is the source of
the ActionEvent .
return pasteAction;
|
private java.awt.datatransfer.DataFlavor | getPropertyDataFlavor(java.lang.Class k, java.awt.datatransfer.DataFlavor[] flavors)Fetches the data flavor from the array of possible flavors that
has data of the type represented by property type. Null is
returned if there is no match.
for(int i = 0; i < flavors.length; i++) {
DataFlavor flavor = flavors[i];
if ("application".equals(flavor.getPrimaryType()) &&
"x-java-jvm-local-objectref".equals(flavor.getSubType()) &&
k.isAssignableFrom(flavor.getRepresentationClass())) {
return flavor;
}
}
return null;
|
private java.beans.PropertyDescriptor | getPropertyDescriptor(javax.swing.JComponent comp)Fetches the property descriptor for the property assigned to this transfer
handler on the given component (transfer handler may be shared). This
returns null if the property descriptor can't be found
or there is an error attempting to fetch the property descriptor.
if (propertyName == null) {
return null;
}
Class k = comp.getClass();
BeanInfo bi;
try {
bi = Introspector.getBeanInfo(k);
} catch (IntrospectionException ex) {
return null;
}
PropertyDescriptor props[] = bi.getPropertyDescriptors();
for (int i=0; i < props.length; i++) {
if (propertyName.equals(props[i].getName())) {
Method reader = props[i].getReadMethod();
if (reader != null) {
Class[] params = reader.getParameterTypes();
if (params == null || params.length == 0) {
// found the desired descriptor
return props[i];
}
}
}
}
return null;
|
public int | getSourceActions(javax.swing.JComponent c)Returns the type of transfer actions supported by the source.
Some models are not mutable, so a transfer operation of COPY
only should be advertised in that case.
PropertyDescriptor prop = getPropertyDescriptor(c);
if (prop != null) {
return COPY;
}
return NONE;
|
public javax.swing.Icon | getVisualRepresentation(java.awt.datatransfer.Transferable t)Returns an object that establishes the look of a transfer. This is
useful for both providing feedback while performing a drag operation and for
representing the transfer in a clipboard implementation that has a visual
appearance. The implementation of the Icon interface should
not alter the graphics clip or alpha level.
The icon implementation need not be rectangular or paint all of the
bounding rectangle and logic that calls the icons paint method should
not assume the all bits are painted. null is a valid return value
for this method and indicates there is no visual representation provided.
In that case, the calling logic is free to represent the
transferable however it wants.
The default Swing logic will not do an alpha blended drag animation if
the return is null .
return null;
|
public boolean | importData(javax.swing.JComponent comp, java.awt.datatransfer.Transferable t)Causes a transfer to a component from a clipboard or a
DND drop operation. The Transferable represents
the data to be imported into the component.
PropertyDescriptor prop = getPropertyDescriptor(comp);
if (prop != null) {
Method writer = prop.getWriteMethod();
if (writer == null) {
// read-only property. ignore
return false;
}
Class[] params = writer.getParameterTypes();
if (params.length != 1) {
// zero or more than one argument, ignore
return false;
}
DataFlavor flavor = getPropertyDataFlavor(params[0], t.getTransferDataFlavors());
if (flavor != null) {
try {
Object value = t.getTransferData(flavor);
Object[] args = { value };
MethodUtil.invoke(writer, comp, args);
return true;
} catch (Exception ex) {
System.err.println("Invocation failed");
// invocation code
}
}
}
return false;
|