Package jmri.util.iharder.dnd
Class TransferableObject
- java.lang.Object
-
- jmri.util.iharder.dnd.TransferableObject
-
- All Implemented Interfaces:
java.awt.datatransfer.Transferable
public class TransferableObject extends java.lang.Object implements java.awt.datatransfer.Transferable
At last an easy way to encapsulate your custom objects for dragging and dropping in your Java programs! When you need to create aTransferableobject, use this class to wrap your object. For example:
Or if you need to know when the data was actually dropped, like when you're moving data out of a list, say, you can use the... MyCoolClass myObj = new MyCoolClass(); Transferable xfer = new TransferableObject( myObj ); ...TransferableObject.Fetcherinner class to return your object Just in Time. For example:
The... final MyCoolClass myObj = new MyCoolClass(); TransferableObject.Fetcher fetcher = new TransferableObject.Fetcher() { public Object getObject(){ return myObj; } }; // end fetcher Transferable xfer = new TransferableObject( fetcher ); ...DataFlavorassociated withTransferableObjecthas the representation classnet.iharder.dnd.TransferableObject.classand MIME typeapplication/x-net.iharder.dnd.TransferableObject. This data flavor is accessible via the staticDATA_FLAVORproperty.This code is licensed for public use under the Common Public License version 0.5.
The Common Public License, developed by IBM and modeled after their industry-friendly IBM Public License, differs from other common open source licenses in several important ways:- You may include this software with other software that uses a different (even non-open source) license.
- You may use this software to make for-profit software.
- Your patent rights, should you generate patents, are protected.
Copyright 2001 Robert Harder
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceTransferableObject.FetcherInstead of passing your data directly to theTransferableObjectconstructor, you may want to know exactly when your data was received in case you need to remove it from its source (or do anyting else to it).
-
Field Summary
Fields Modifier and Type Field Description static java.awt.datatransfer.DataFlavorDATA_FLAVORThe defaultDataFlavorforTransferableObjecthas the representation classnet.iharder.dnd.TransferableObject.classand the MIME typeapplication/x-net.iharder.dnd.TransferableObject.static java.lang.StringMIME_TYPEThe MIME type forDATA_FLAVORisapplication/x-net.iharder.dnd.TransferableObject.
-
Constructor Summary
Constructors Constructor Description TransferableObject(java.lang.Class<?> dataClass, TransferableObject.Fetcher fetcher)Creates a newTransferableObjectthat will return the object that is returned by fetcher.TransferableObject(java.lang.Object data)Creates a newTransferableObjectthat wraps data.TransferableObject(TransferableObject.Fetcher fetcher)Creates a newTransferableObjectthat will return the object that is returned by fetcher.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.awt.datatransfer.DataFlavorgetCustomDataFlavor()Returns the customDataFlavorassociated with the encapsulated object ornullif theTransferableObject.Fetcherconstructor was used without passing aClass.java.lang.ObjectgetTransferData(java.awt.datatransfer.DataFlavor flavor)Returns the data encapsulated in thisTransferableObject.java.awt.datatransfer.DataFlavor[]getTransferDataFlavors()Returns a two- or three-element array containing first the custom data flavor, if one was created in the constructors, second the default DATA_FLAVOR associated with the TransferableObject, and third the java.awt.datatransfer.DataFlavor.stringFlavor.booleanisDataFlavorSupported(java.awt.datatransfer.DataFlavor flavor)Returnstrueif flavor is one of the supported flavors.
-
-
-
Field Detail
-
MIME_TYPE
public static final java.lang.String MIME_TYPE
The MIME type forDATA_FLAVORisapplication/x-net.iharder.dnd.TransferableObject.- Since:
- 1.1
- See Also:
- Constant Field Values
-
DATA_FLAVOR
public static final java.awt.datatransfer.DataFlavor DATA_FLAVOR
The defaultDataFlavorforTransferableObjecthas the representation classnet.iharder.dnd.TransferableObject.classand the MIME typeapplication/x-net.iharder.dnd.TransferableObject.- Since:
- 1.1
-
-
Constructor Detail
-
TransferableObject
public TransferableObject(java.lang.Object data)
Creates a newTransferableObjectthat wraps data. Along with theDATA_FLAVORassociated with this class, this creates a custom data flavor with a representation class determined fromdata.getClass()and the MIME typeapplication/x-net.iharder.dnd.TransferableObject.- Parameters:
data- The data to transfer- Since:
- 1.1
-
TransferableObject
public TransferableObject(TransferableObject.Fetcher fetcher)
Creates a newTransferableObjectthat will return the object that is returned by fetcher. No custom data flavor is set other than the defaultDATA_FLAVOR.- Parameters:
fetcher- TheTransferableObject.Fetcherthat will return the data object- Since:
- 1.1
- See Also:
TransferableObject.Fetcher
-
TransferableObject
public TransferableObject(java.lang.Class<?> dataClass, TransferableObject.Fetcher fetcher)
Creates a newTransferableObjectthat will return the object that is returned by fetcher. Along with theDATA_FLAVORassociated with this class, this creates a custom data flavor with a representation class dataClass and the MIME typeapplication/x-net.iharder.dnd.TransferableObject.- Parameters:
dataClass- TheClassto use in the custom data flavorfetcher- TheTransferableObject.Fetcherthat will return the data object- Since:
- 1.1
- See Also:
TransferableObject.Fetcher
-
-
Method Detail
-
getCustomDataFlavor
public java.awt.datatransfer.DataFlavor getCustomDataFlavor()
Returns the customDataFlavorassociated with the encapsulated object ornullif theTransferableObject.Fetcherconstructor was used without passing aClass.- Returns:
- The custom data flavor for the encapsulated object
- Since:
- 1.1
-
getTransferDataFlavors
public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors()
Returns a two- or three-element array containing first the custom data flavor, if one was created in the constructors, second the default DATA_FLAVOR associated with the TransferableObject, and third the java.awt.datatransfer.DataFlavor.stringFlavor.- Specified by:
getTransferDataFlavorsin interfacejava.awt.datatransfer.Transferable- Returns:
- An array of supported data flavors
- Since:
- 1.1
-
getTransferData
public java.lang.Object getTransferData(java.awt.datatransfer.DataFlavor flavor) throws java.awt.datatransfer.UnsupportedFlavorException, java.io.IOException
Returns the data encapsulated in thisTransferableObject. If theTransferableObject.Fetcherconstructor was used, then this is when thegetObject()method will be called. If the requested data flavor is not supported, then thegetObject()method will not be called.- Specified by:
getTransferDatain interfacejava.awt.datatransfer.Transferable- Parameters:
flavor- The data flavor for the data to return- Returns:
- The dropped data
- Throws:
java.awt.datatransfer.UnsupportedFlavorExceptionjava.io.IOException- Since:
- 1.1
-
isDataFlavorSupported
public boolean isDataFlavorSupported(java.awt.datatransfer.DataFlavor flavor)
Returnstrueif flavor is one of the supported flavors. Flavors are supported using theequals(...)method.- Specified by:
isDataFlavorSupportedin interfacejava.awt.datatransfer.Transferable- Parameters:
flavor- The data flavor to check- Returns:
- Whether or not the flavor is supported
- Since:
- 1.1
-
-