package de.csc_dd.tracformatierer;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringBufferInputStream;
import java.io.StringReader;
import javax.swing.plaf.UIResource;
public class HtmlTransferable
implements Transferable, UIResource
{
protected String vPlainData;
protected String vHtmlData;
private static DataFlavor[] sHtmlFlavors;
private static DataFlavor[] sStringFlavors;
private static DataFlavor[] sPlainFlavors;
static
{
try
{
sHtmlFlavors = new DataFlavor[3];
sHtmlFlavors[0] = new DataFlavor("text/html;class=java.lang.String");
sHtmlFlavors[1] = new DataFlavor("text/html;class=java.io.Reader");
sHtmlFlavors[2] = new DataFlavor("text/html;charset=unicode;class=java.io.InputStream");
sPlainFlavors = new DataFlavor[3];
sPlainFlavors[0] = new DataFlavor("text/plain;class=java.lang.String");
sPlainFlavors[1] = new DataFlavor("text/plain;class=java.io.Reader");
sPlainFlavors[2] = new DataFlavor("text/plain;charset=unicode;class=java.io.InputStream");
sStringFlavors = new DataFlavor[2];
sStringFlavors[0] = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType+";class=java.lang.String");
sStringFlavors[1] = DataFlavor.stringFlavor;
}
catch (ClassNotFoundException cle)
{
System.err.println("error initializing javax.swing.plaf.basic.BasicTranserable");
}
}
public HtmlTransferable( String pPlainData, String pHtmlData )
{
this.vPlainData = pPlainData;
this.vHtmlData = pHtmlData;
}
/**
* Returns an array of DataFlavor objects indicating the flavors the data
* can be provided in. The array should be ordered according to preference
* for providing the data (from most richly descriptive to least descriptive).
* @return an array of data flavors in which this data can be transferred
*/
@Override
public DataFlavor[] getTransferDataFlavors()
{
int nHTML = (isHTMLSupported()) ? sHtmlFlavors.length : 0;
int nPlain = (isPlainSupported()) ? sPlainFlavors.length: 0;
int nString = (isPlainSupported()) ? sStringFlavors.length : 0;
int nFlavors = nHTML + nPlain + nString;
DataFlavor[] flavors = new DataFlavor[nFlavors];
// fill in the array
int nDone = 0;
if (nHTML > 0)
{
System.arraycopy(sHtmlFlavors, 0, flavors, nDone, nHTML);
nDone += nHTML;
}
if (nPlain > 0)
{
System.arraycopy(sPlainFlavors, 0, flavors, nDone, nPlain);
nDone += nPlain;
}
if (nString > 0)
{
System.arraycopy(sStringFlavors, 0, flavors, nDone, nString);
nDone += nString;
}
return flavors;
}
/**
* Returns whether or not the specified data flavor is supported for
* this object.
* @param pFlavor the requested flavor for the data
* @return boolean indicating whether or not the data flavor is supported
*/
@Override
public boolean isDataFlavorSupported(DataFlavor pFlavor)
{
DataFlavor[] flavors = getTransferDataFlavors();
for (int i = 0; i < flavors.length; i++)
{
if (flavors[i].equals(pFlavor))
{
return true;
}
}
return false;
}
/**
* Returns an object which represents the data to be transferred. The class
* of the object returned is defined by the representation class of the flavor.
*
* @param pFlavor the requested flavor for the data
* @see DataFlavor#getRepresentationClass
* @exception IOException if the data is no longer available
* in the requested flavor.
* @exception UnsupportedFlavorException if the requested data flavor is
* not supported.
*/
public Object getTransferData(DataFlavor pFlavor)
throws UnsupportedFlavorException, IOException
{
if (isHTMLFlavor(pFlavor))
{
String data = getHTMLData();
data = (data == null) ? "" : data;
if (String.class.equals(pFlavor.getRepresentationClass()))
{
return data;
}
else if (Reader.class.equals(pFlavor.getRepresentationClass()))
{
return new StringReader(data);
}
else if (InputStream.class.equals(pFlavor.getRepresentationClass()))
{
return new StringBufferInputStream(data);
}
// fall through to unsupported
}
else if (isPlainFlavor(pFlavor))
{
String data = getPlainData();
data = (data == null) ? "" : data;
if (String.class.equals(pFlavor.getRepresentationClass()))
{
return data;
}
else if (Reader.class.equals(pFlavor.getRepresentationClass()))
{
return new StringReader(data);
}
else if (InputStream.class.equals(pFlavor.getRepresentationClass()))
{
return new StringBufferInputStream(data);
}
// fall through to unsupported
}
else if (isStringFlavor(pFlavor))
{
String data = getPlainData();
data = (data == null) ? "" : data;
return data;
}
throw new UnsupportedFlavorException(pFlavor);
}
// --- html flavors ----------------------------------------------------------
/**
* Returns whether or not the specified data flavor is an HTML flavor that
* is supported.
* @param pFlavor the requested flavor for the data
* @return boolean indicating whether or not the data flavor is supported
*/
protected boolean isHTMLFlavor(DataFlavor pFlavor)
{
DataFlavor[] flavors = sHtmlFlavors;
for (int i = 0; i < flavors.length; i++)
{
if (flavors[i].equals(pFlavor))
{
return true;
}
}
return false;
}
/**
* Should the HTML flavors be offered? If so, the method
* getHTMLData should be implemented to provide something reasonable.
*/
protected boolean isHTMLSupported()
{
return vHtmlData != null;
}
/**
* Fetch the data in a text/html format
*/
protected String getHTMLData()
{
return vHtmlData;
}
// --- plain text flavors ----------------------------------------------------
/**
* Returns whether or not the specified data flavor is an plain flavor that
* is supported.
* @param pFlavor the requested flavor for the data
* @return boolean indicating whether or not the data flavor is supported
*/
protected boolean isPlainFlavor(DataFlavor pFlavor)
{
DataFlavor[] flavors = sPlainFlavors;
for (int i = 0; i < flavors.length; i++)
{
if (flavors[i].equals(pFlavor))
{
return true;
}
}
return false;
}
/**
* Should the plain text flavors be offered? If so, the method
* getPlainData should be implemented to provide something reasonable.
*/
protected boolean isPlainSupported()
{
return vPlainData != null;
}
/**
* Fetch the data in a text/plain format.
*/
protected String getPlainData()
{
return vPlainData;
}
// --- string flavorss --------------------------------------------------------
/**
* Returns whether or not the specified data flavor is a String flavor that
* is supported.
* @param pFlavor the requested flavor for the data
* @return boolean indicating whether or not the data flavor is supported
*/
protected boolean isStringFlavor(DataFlavor pFlavor)
{
DataFlavor[] flavors = sStringFlavors;
for (int i = 0; i < flavors.length; i++)
{
if (flavors[i].equals(pFlavor))
{
return true;
}
}
return false;
}
}