下面列出了java.awt.datatransfer.DataFlavor#isFlavorTextType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
@Nonnull
public Object getTransferData(@Nonnull final DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if (flavor.isFlavorTextType()) {
final StringBuilder result = new StringBuilder();
for (final Topic t : this.topics) {
if (result.length() > 0) {
result.append(END_OF_LINE).append(END_OF_LINE);
}
result.append(convertTopicToText(t));
}
return result.toString();
} else if (flavor.isMimeTypeEqual(MMD_DATA_FLAVOR)) {
return new NBMindMapTopicsContainer(this.topics);
} else {
throw new UnsupportedFlavorException(flavor);
}
}
/**
* Returns a Set of all DataFlavors for which
* 1) a mapping from at least one of the specified formats exists in the
* specified map and
* 2) the data translation for this mapping can be performed by the data
* transfer subsystem.
*
* @param formats the data formats
* @param map the FlavorTable which contains mappings between
* DataFlavors and data formats
* @throws NullPointerException if formats or map is {@code null}
*/
public Set<DataFlavor> getFlavorsForFormatsAsSet(long[] formats, FlavorTable map) {
Set<DataFlavor> flavorSet = new HashSet<>(formats.length);
for (long format : formats) {
List<DataFlavor> flavors = map.getFlavorsForNative(getNativeForFormat(format));
for (DataFlavor flavor : flavors) {
// Don't explicitly test for String, since it is just a special
// case of Serializable
if (flavor.isFlavorTextType() ||
flavor.isFlavorJavaFileListType() ||
DataFlavor.imageFlavor.equals(flavor) ||
flavor.isRepresentationClassSerializable() ||
flavor.isRepresentationClassInputStream() ||
flavor.isRepresentationClassRemote()) {
flavorSet.add(flavor);
}
}
}
return flavorSet;
}
/**
* Returns a Set of all DataFlavors for which
* 1) a mapping from at least one of the specified formats exists in the
* specified map and
* 2) the data translation for this mapping can be performed by the data
* transfer subsystem.
*
* @param formats the data formats
* @param map the FlavorTable which contains mappings between
* DataFlavors and data formats
* @throws NullPointerException if formats or map is <code>null</code>
*/
public Set getFlavorsForFormatsAsSet(long[] formats, FlavorTable map) {
Set flavorSet = new HashSet(formats.length);
for (int i = 0; i < formats.length; i++) {
String nat = getNativeForFormat(formats[i]);
List flavors = map.getFlavorsForNative(nat);
for (Iterator iter = flavors.iterator(); iter.hasNext(); ) {
DataFlavor flavor = (DataFlavor)iter.next();
// Don't explicitly test for String, since it is just a special
// case of Serializable
if (flavor.isFlavorTextType() ||
flavor.isFlavorJavaFileListType() ||
DataFlavor.imageFlavor.equals(flavor) ||
flavor.isRepresentationClassSerializable() ||
flavor.isRepresentationClassInputStream() ||
flavor.isRepresentationClassRemote())
{
flavorSet.add(flavor);
}
}
}
return flavorSet;
}
/**
* Returns a Set of all DataFlavors for which
* 1) a mapping from at least one of the specified formats exists in the
* specified map and
* 2) the data translation for this mapping can be performed by the data
* transfer subsystem.
*
* @param formats the data formats
* @param map the FlavorTable which contains mappings between
* DataFlavors and data formats
* @throws NullPointerException if formats or map is <code>null</code>
*/
public Set getFlavorsForFormatsAsSet(long[] formats, FlavorTable map) {
Set flavorSet = new HashSet(formats.length);
for (int i = 0; i < formats.length; i++) {
String nat = getNativeForFormat(formats[i]);
List flavors = map.getFlavorsForNative(nat);
for (Iterator iter = flavors.iterator(); iter.hasNext(); ) {
DataFlavor flavor = (DataFlavor)iter.next();
// Don't explicitly test for String, since it is just a special
// case of Serializable
if (flavor.isFlavorTextType() ||
flavor.isFlavorJavaFileListType() ||
DataFlavor.imageFlavor.equals(flavor) ||
flavor.isRepresentationClassSerializable() ||
flavor.isRepresentationClassInputStream() ||
flavor.isRepresentationClassRemote())
{
flavorSet.add(flavor);
}
}
}
return flavorSet;
}
@Override
public Object translateStream(InputStream str,
DataFlavor flavor, long format,
Transferable localeTransferable)
throws IOException
{
if (format == CF_HTML && flavor.isFlavorTextType()) {
str = new HTMLCodec(str,
EHTMLReadMode.getEHTMLReadMode(flavor));
}
return super.translateStream(str, flavor, format,
localeTransferable);
}
/**
* Returns a Set of all DataFlavors for which
* 1) a mapping from at least one of the specified formats exists in the
* specified map and
* 2) the data translation for this mapping can be performed by the data
* transfer subsystem.
*
* @param formats the data formats
* @param map the FlavorTable which contains mappings between
* DataFlavors and data formats
* @throws NullPointerException if formats or map is <code>null</code>
*/
public Set getFlavorsForFormatsAsSet(long[] formats, FlavorTable map) {
Set flavorSet = new HashSet(formats.length);
for (int i = 0; i < formats.length; i++) {
String nat = getNativeForFormat(formats[i]);
List flavors = map.getFlavorsForNative(nat);
for (Iterator iter = flavors.iterator(); iter.hasNext(); ) {
DataFlavor flavor = (DataFlavor)iter.next();
// Don't explicitly test for String, since it is just a special
// case of Serializable
if (flavor.isFlavorTextType() ||
flavor.isFlavorJavaFileListType() ||
DataFlavor.imageFlavor.equals(flavor) ||
flavor.isRepresentationClassSerializable() ||
flavor.isRepresentationClassInputStream() ||
flavor.isRepresentationClassRemote())
{
flavorSet.add(flavor);
}
}
}
return flavorSet;
}
@Override
public boolean canImport(TransferSupport support) {
boolean isCopySupported = (COPY & support.getSourceDropActions()) == COPY;
if (!isCopySupported) {
return false;
}
for (DataFlavor flavor : support.getDataFlavors()) {
if (flavor.isFlavorJavaFileListType() || flavor.isFlavorTextType()) {
support.setDropAction(COPY);
return true;
}
}
return false;
}
@Override
public Object translateStream(InputStream str,
DataFlavor flavor, long format,
Transferable localeTransferable)
throws IOException
{
if (format == CF_HTML && flavor.isFlavorTextType()) {
str = new HTMLCodec(str,
EHTMLReadMode.getEHTMLReadMode(flavor));
}
return super.translateStream(str, flavor, format,
localeTransferable);
}
@Override
public Object translateStream(InputStream str,
DataFlavor flavor, long format,
Transferable localeTransferable)
throws IOException
{
if (format == CF_HTML && flavor.isFlavorTextType()) {
str = new HTMLCodec(str,
EHTMLReadMode.getEHTMLReadMode(flavor));
}
return super.translateStream(str, flavor, format,
localeTransferable);
}
public Object translateStream(InputStream str,
DataFlavor flavor, long format,
Transferable localeTransferable)
throws IOException
{
if (format == CF_HTML && flavor.isFlavorTextType()) {
str = new HTMLCodec(str,
EHTMLReadMode.getEHTMLReadMode(flavor));
}
return super.translateStream(str, flavor, format,
localeTransferable);
}
/**
* Returns a Set of all DataFlavors for which
* 1) a mapping from at least one of the specified formats exists in the
* specified map and
* 2) the data translation for this mapping can be performed by the data
* transfer subsystem.
*
* @param formats the data formats
* @param map the FlavorTable which contains mappings between
* DataFlavors and data formats
* @throws NullPointerException if formats or map is <code>null</code>
*/
public Set getFlavorsForFormatsAsSet(long[] formats, FlavorTable map) {
Set flavorSet = new HashSet(formats.length);
for (int i = 0; i < formats.length; i++) {
String nat = getNativeForFormat(formats[i]);
List flavors = map.getFlavorsForNative(nat);
for (Iterator iter = flavors.iterator(); iter.hasNext(); ) {
DataFlavor flavor = (DataFlavor)iter.next();
// Don't explicitly test for String, since it is just a special
// case of Serializable
if (flavor.isFlavorTextType() ||
flavor.isFlavorJavaFileListType() ||
DataFlavor.imageFlavor.equals(flavor) ||
flavor.isRepresentationClassSerializable() ||
flavor.isRepresentationClassInputStream() ||
flavor.isRepresentationClassRemote())
{
flavorSet.add(flavor);
}
}
}
return flavorSet;
}
@Override
public Object translateStream(InputStream str,
DataFlavor flavor, long format,
Transferable localeTransferable)
throws IOException
{
if (format == CF_HTML && flavor.isFlavorTextType()) {
str = new HTMLCodec(str,
EHTMLReadMode.getEHTMLReadMode(flavor));
}
return super.translateStream(str, flavor, format,
localeTransferable);
}
@Override
public Object translateStream(InputStream str,
DataFlavor flavor, long format,
Transferable localeTransferable)
throws IOException
{
if (format == CF_HTML && flavor.isFlavorTextType()) {
str = new HTMLCodec(str,
EHTMLReadMode.getEHTMLReadMode(flavor));
}
return super.translateStream(str, flavor, format,
localeTransferable);
}
/**
* Returns a Map whose keys are all of the possible formats into which data
* in the specified DataFlavors can be translated. The value of each key
* is the DataFlavor in which the Transferable's data should be requested
* when converting to the format.
* <p>
* The map keys are sorted according to the native formats preference
* order.
*
* @param flavors the data flavors
* @param map the FlavorTable which contains mappings between
* DataFlavors and data formats
* @throws NullPointerException if flavors or map is <code>null</code>
*/
public SortedMap <Long, DataFlavor> getFormatsForFlavors(
DataFlavor[] flavors, FlavorTable map)
{
Map <Long,DataFlavor> formatMap =
new HashMap <> (flavors.length);
Map <Long,DataFlavor> textPlainMap =
new HashMap <> (flavors.length);
// Maps formats to indices that will be used to sort the formats
// according to the preference order.
// Larger index value corresponds to the more preferable format.
Map indexMap = new HashMap(flavors.length);
Map textPlainIndexMap = new HashMap(flavors.length);
int currentIndex = 0;
// Iterate backwards so that preferred DataFlavors are used over
// other DataFlavors. (See javadoc for
// Transferable.getTransferDataFlavors.)
for (int i = flavors.length - 1; i >= 0; i--) {
DataFlavor flavor = flavors[i];
if (flavor == null) continue;
// Don't explicitly test for String, since it is just a special
// case of Serializable
if (flavor.isFlavorTextType() ||
flavor.isFlavorJavaFileListType() ||
DataFlavor.imageFlavor.equals(flavor) ||
flavor.isRepresentationClassSerializable() ||
flavor.isRepresentationClassInputStream() ||
flavor.isRepresentationClassRemote())
{
List natives = map.getNativesForFlavor(flavor);
currentIndex += natives.size();
for (Iterator iter = natives.iterator(); iter.hasNext(); ) {
Long lFormat =
getFormatForNativeAsLong((String)iter.next());
Integer index = Integer.valueOf(currentIndex--);
formatMap.put(lFormat, flavor);
indexMap.put(lFormat, index);
// SystemFlavorMap.getNativesForFlavor will return
// text/plain natives for all text/*. While this is good
// for a single text/* flavor, we would prefer that
// text/plain native data come from a text/plain flavor.
if (("text".equals(flavor.getPrimaryType()) &&
"plain".equals(flavor.getSubType())) ||
flavor.equals(DataFlavor.stringFlavor))
{
textPlainMap.put(lFormat, flavor);
textPlainIndexMap.put(lFormat, index);
}
}
currentIndex += natives.size();
}
}
formatMap.putAll(textPlainMap);
indexMap.putAll(textPlainIndexMap);
// Sort the map keys according to the formats preference order.
Comparator comparator =
new IndexOrderComparator(indexMap, IndexedComparator.SELECT_WORST);
SortedMap sortedMap = new TreeMap(comparator);
sortedMap.putAll(formatMap);
return sortedMap;
}
@Override
public boolean isDataFlavorSupported(@Nonnull final DataFlavor flavor) {
return flavor.isFlavorTextType() || flavor.isMimeTypeEqual(MMD_DATA_FLAVOR);
}
/**
* Returns a Map whose keys are all of the possible formats into which data
* in the specified DataFlavors can be translated. The value of each key
* is the DataFlavor in which the Transferable's data should be requested
* when converting to the format.
* <p>
* The map keys are sorted according to the native formats preference
* order.
*
* @param flavors the data flavors
* @param map the FlavorTable which contains mappings between
* DataFlavors and data formats
* @throws NullPointerException if flavors or map is <code>null</code>
*/
public SortedMap <Long, DataFlavor> getFormatsForFlavors(
DataFlavor[] flavors, FlavorTable map)
{
Map <Long,DataFlavor> formatMap =
new HashMap <> (flavors.length);
Map <Long,DataFlavor> textPlainMap =
new HashMap <> (flavors.length);
// Maps formats to indices that will be used to sort the formats
// according to the preference order.
// Larger index value corresponds to the more preferable format.
Map indexMap = new HashMap(flavors.length);
Map textPlainIndexMap = new HashMap(flavors.length);
int currentIndex = 0;
// Iterate backwards so that preferred DataFlavors are used over
// other DataFlavors. (See javadoc for
// Transferable.getTransferDataFlavors.)
for (int i = flavors.length - 1; i >= 0; i--) {
DataFlavor flavor = flavors[i];
if (flavor == null) continue;
// Don't explicitly test for String, since it is just a special
// case of Serializable
if (flavor.isFlavorTextType() ||
flavor.isFlavorJavaFileListType() ||
DataFlavor.imageFlavor.equals(flavor) ||
flavor.isRepresentationClassSerializable() ||
flavor.isRepresentationClassInputStream() ||
flavor.isRepresentationClassRemote())
{
List natives = map.getNativesForFlavor(flavor);
currentIndex += natives.size();
for (Iterator iter = natives.iterator(); iter.hasNext(); ) {
Long lFormat =
getFormatForNativeAsLong((String)iter.next());
Integer index = Integer.valueOf(currentIndex--);
formatMap.put(lFormat, flavor);
indexMap.put(lFormat, index);
// SystemFlavorMap.getNativesForFlavor will return
// text/plain natives for all text/*. While this is good
// for a single text/* flavor, we would prefer that
// text/plain native data come from a text/plain flavor.
if (("text".equals(flavor.getPrimaryType()) &&
"plain".equals(flavor.getSubType())) ||
flavor.equals(DataFlavor.stringFlavor))
{
textPlainMap.put(lFormat, flavor);
textPlainIndexMap.put(lFormat, index);
}
}
currentIndex += natives.size();
}
}
formatMap.putAll(textPlainMap);
indexMap.putAll(textPlainIndexMap);
// Sort the map keys according to the formats preference order.
Comparator comparator =
new IndexOrderComparator(indexMap, IndexedComparator.SELECT_WORST);
SortedMap sortedMap = new TreeMap(comparator);
sortedMap.putAll(formatMap);
return sortedMap;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.isFlavorTextType();
}
public int compare(DataFlavor flavor1, DataFlavor flavor2) {
if (flavor1.equals(flavor2)) {
return 0;
}
int comp;
String primaryType1 = flavor1.getPrimaryType();
String subType1 = flavor1.getSubType();
String mimeType1 = primaryType1 + "/" + subType1;
Class<?> class1 = flavor1.getRepresentationClass();
String primaryType2 = flavor2.getPrimaryType();
String subType2 = flavor2.getSubType();
String mimeType2 = primaryType2 + "/" + subType2;
Class<?> class2 = flavor2.getRepresentationClass();
if (flavor1.isFlavorTextType() && flavor2.isFlavorTextType()) {
// First, compare MIME types
comp = compareIndices(textTypes, mimeType1, mimeType2, UNKNOWN_OBJECT_LOSES);
if (comp != 0) {
return comp;
}
// Only need to test one flavor because they both have the
// same MIME type. Also don't need to worry about accidentally
// passing stringFlavor because either
// 1. Both flavors are stringFlavor, in which case the
// equality test at the top of the function succeeded.
// 2. Only one flavor is stringFlavor, in which case the MIME
// type comparison returned a non-zero value.
if (doesSubtypeSupportCharset(flavor1)) {
// Next, prefer the decoded text representations of Reader,
// String, CharBuffer, and [C, in that order.
comp = compareIndices(decodedTextRepresentations, class1,
class2, UNKNOWN_OBJECT_LOSES);
if (comp != 0) {
return comp;
}
// Next, compare charsets
comp = CharsetComparator.INSTANCE.compare(getTextCharset(flavor1),
getTextCharset(flavor2));
if (comp != 0) {
return comp;
}
}
// Finally, prefer the encoded text representations of
// InputStream, ByteBuffer, and [B, in that order.
comp = compareIndices(encodedTextRepresentations, class1,
class2, UNKNOWN_OBJECT_LOSES);
if (comp != 0) {
return comp;
}
} else {
// First, prefer text types
if (flavor1.isFlavorTextType()) {
return 1;
}
if (flavor2.isFlavorTextType()) {
return -1;
}
// Next, prefer application types.
comp = compareIndices(primaryTypes, primaryType1, primaryType2,
UNKNOWN_OBJECT_LOSES);
if (comp != 0) {
return comp;
}
// Next, look for application/x-java-* types. Prefer unknown
// MIME types because if the user provides his own data flavor,
// it will likely be the most descriptive one.
comp = compareIndices(exactTypes, mimeType1, mimeType2,
UNKNOWN_OBJECT_WINS);
if (comp != 0) {
return comp;
}
// Finally, prefer the representation classes of Remote,
// Serializable, and InputStream, in that order.
comp = compareIndices(nonTextRepresentations, class1, class2,
UNKNOWN_OBJECT_LOSES);
if (comp != 0) {
return comp;
}
}
// The flavours are not equal but still not distinguishable.
// Compare String representations in alphabetical order
return flavor1.getMimeType().compareTo(flavor2.getMimeType());
}
/**
* Returns a Map whose keys are all of the possible formats into which data
* in the specified DataFlavors can be translated. The value of each key
* is the DataFlavor in which the Transferable's data should be requested
* when converting to the format.
* <p>
* The map keys are sorted according to the native formats preference
* order.
*
* @param flavors the data flavors
* @param map the FlavorTable which contains mappings between
* DataFlavors and data formats
* @throws NullPointerException if flavors or map is {@code null}
*/
public SortedMap<Long, DataFlavor> getFormatsForFlavors(DataFlavor[] flavors,
FlavorTable map)
{
Map<Long,DataFlavor> formatMap = new HashMap<>(flavors.length);
Map<Long,DataFlavor> textPlainMap = new HashMap<>(flavors.length);
// Maps formats to indices that will be used to sort the formats
// according to the preference order.
// Larger index value corresponds to the more preferable format.
Map<Long, Integer> indexMap = new HashMap<>(flavors.length);
Map<Long, Integer> textPlainIndexMap = new HashMap<>(flavors.length);
int currentIndex = 0;
// Iterate backwards so that preferred DataFlavors are used over
// other DataFlavors. (See javadoc for
// Transferable.getTransferDataFlavors.)
for (int i = flavors.length - 1; i >= 0; i--) {
DataFlavor flavor = flavors[i];
if (flavor == null) continue;
// Don't explicitly test for String, since it is just a special
// case of Serializable
if (flavor.isFlavorTextType() ||
flavor.isFlavorJavaFileListType() ||
DataFlavor.imageFlavor.equals(flavor) ||
flavor.isRepresentationClassSerializable() ||
flavor.isRepresentationClassInputStream() ||
flavor.isRepresentationClassRemote())
{
List<String> natives = map.getNativesForFlavor(flavor);
currentIndex += natives.size();
for (String aNative : natives) {
Long lFormat = getFormatForNativeAsLong(aNative);
Integer index = currentIndex--;
formatMap.put(lFormat, flavor);
indexMap.put(lFormat, index);
// SystemFlavorMap.getNativesForFlavor will return
// text/plain natives for all text/*. While this is good
// for a single text/* flavor, we would prefer that
// text/plain native data come from a text/plain flavor.
if (("text".equals(flavor.getPrimaryType()) &&
"plain".equals(flavor.getSubType())) ||
flavor.equals(DataFlavor.stringFlavor)) {
textPlainMap.put(lFormat, flavor);
textPlainIndexMap.put(lFormat, index);
}
}
currentIndex += natives.size();
}
}
formatMap.putAll(textPlainMap);
indexMap.putAll(textPlainIndexMap);
// Sort the map keys according to the formats preference order.
Comparator<Long> comparator = DataFlavorUtil.getIndexOrderComparator(indexMap).reversed();
SortedMap<Long, DataFlavor> sortedMap = new TreeMap<>(comparator);
sortedMap.putAll(formatMap);
return sortedMap;
}
/**
* Compares two {@code DataFlavor} objects. Returns a negative integer,
* zero, or a positive integer as the first {@code DataFlavor} is worse
* than, equal to, or better than the second.
* <p>
* {@code DataFlavor}s are ordered according to the rules outlined for
* {@link DataFlavor#selectBestTextFlavor selectBestTextFlavor}.
*
* @param flavor1 the first {@code DataFlavor} to be compared
* @param flavor2 the second {@code DataFlavor} to be compared
* @return a negative integer, zero, or a positive integer as the first
* argument is worse, equal to, or better than the second
* @throws ClassCastException if either of the arguments is not an
* instance of {@code DataFlavor}
* @throws NullPointerException if either of the arguments is
* {@code null}
* @see DataFlavor#selectBestTextFlavor
*/
public int compare(DataFlavor flavor1, DataFlavor flavor2) {
if (flavor1.isFlavorTextType()) {
if (flavor2.isFlavorTextType()) {
return super.compare(flavor1, flavor2);
} else {
return 1;
}
} else if (flavor2.isFlavorTextType()) {
return -1;
} else {
return 0;
}
}