author | serb |
Thu, 30 Jul 2015 15:27:02 +0300 | |
changeset 32126 | 6611b32dfa58 |
parent 31887 | a0b91dedca5c |
child 35302 | e4d2275861c3 |
permissions | -rw-r--r-- |
26010
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
1 |
/* |
31887
a0b91dedca5c
8130242: DataFlavorComparator transitivity exception
ssadetsky
parents:
31061
diff
changeset
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
26010
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
4 |
* |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
10 |
* |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
15 |
* accompanied this code). |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
16 |
* |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
20 |
* |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
23 |
* questions. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
24 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
25 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
26 |
package sun.datatransfer; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
27 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
28 |
import java.awt.datatransfer.DataFlavor; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
29 |
import java.awt.datatransfer.FlavorMap; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
30 |
import java.io.IOException; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
31 |
import java.io.InputStream; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
32 |
import java.io.Reader; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
33 |
import java.lang.reflect.Constructor; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
34 |
import java.lang.reflect.InvocationTargetException; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
35 |
import java.lang.reflect.Method; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
36 |
import java.nio.ByteBuffer; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
37 |
import java.nio.CharBuffer; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
38 |
import java.nio.charset.Charset; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
39 |
import java.nio.charset.IllegalCharsetNameException; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
40 |
import java.nio.charset.StandardCharsets; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
41 |
import java.nio.charset.UnsupportedCharsetException; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
42 |
import java.util.Collections; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
43 |
import java.util.Comparator; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
44 |
import java.util.HashMap; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
45 |
import java.util.Iterator; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
46 |
import java.util.LinkedHashSet; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
47 |
import java.util.Map; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
48 |
import java.util.ServiceLoader; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
49 |
import java.util.Set; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
50 |
import java.util.SortedSet; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
51 |
import java.util.TreeSet; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
52 |
import java.util.function.Supplier; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
53 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
54 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
55 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
56 |
* Utility class with different datatransfer helper functions |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
57 |
* |
31061 | 58 |
* @since 1.9 |
26010
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
59 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
60 |
public class DataFlavorUtil { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
61 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
62 |
private DataFlavorUtil() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
63 |
// Avoid instantiation |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
64 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
65 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
66 |
private static Comparator<String> getCharsetComparator() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
67 |
return CharsetComparator.INSTANCE; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
68 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
69 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
70 |
public static Comparator<DataFlavor> getDataFlavorComparator() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
71 |
return DataFlavorComparator.INSTANCE; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
72 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
73 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
74 |
public static Comparator<Long> getIndexOrderComparator(Map<Long, Integer> indexMap) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
75 |
return new IndexOrderComparator(indexMap); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
76 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
77 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
78 |
public static Comparator<DataFlavor> getTextFlavorComparator() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
79 |
return TextFlavorComparator.INSTANCE; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
80 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
81 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
82 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
83 |
* Tracks whether a particular text/* MIME type supports the charset |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
84 |
* parameter. The Map is initialized with all of the standard MIME types |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
85 |
* listed in the DataFlavor.selectBestTextFlavor method comment. Additional |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
86 |
* entries may be added during the life of the JRE for text/<other> types. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
87 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
88 |
private static final Map<String, Boolean> textMIMESubtypeCharsetSupport; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
89 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
90 |
static { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
91 |
Map<String, Boolean> tempMap = new HashMap<>(17); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
92 |
tempMap.put("sgml", Boolean.TRUE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
93 |
tempMap.put("xml", Boolean.TRUE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
94 |
tempMap.put("html", Boolean.TRUE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
95 |
tempMap.put("enriched", Boolean.TRUE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
96 |
tempMap.put("richtext", Boolean.TRUE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
97 |
tempMap.put("uri-list", Boolean.TRUE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
98 |
tempMap.put("directory", Boolean.TRUE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
99 |
tempMap.put("css", Boolean.TRUE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
100 |
tempMap.put("calendar", Boolean.TRUE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
101 |
tempMap.put("plain", Boolean.TRUE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
102 |
tempMap.put("rtf", Boolean.FALSE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
103 |
tempMap.put("tab-separated-values", Boolean.FALSE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
104 |
tempMap.put("t140", Boolean.FALSE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
105 |
tempMap.put("rfc822-headers", Boolean.FALSE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
106 |
tempMap.put("parityfec", Boolean.FALSE); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
107 |
textMIMESubtypeCharsetSupport = Collections.synchronizedMap(tempMap); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
108 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
109 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
110 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
111 |
* Lazy initialization of Standard Encodings. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
112 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
113 |
private static class StandardEncodingsHolder { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
114 |
private static final SortedSet<String> standardEncodings = load(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
115 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
116 |
private static SortedSet<String> load() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
117 |
final SortedSet<String> tempSet = new TreeSet<>(getCharsetComparator().reversed()); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
118 |
tempSet.add("US-ASCII"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
119 |
tempSet.add("ISO-8859-1"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
120 |
tempSet.add("UTF-8"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
121 |
tempSet.add("UTF-16BE"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
122 |
tempSet.add("UTF-16LE"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
123 |
tempSet.add("UTF-16"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
124 |
tempSet.add(Charset.defaultCharset().name()); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
125 |
return Collections.unmodifiableSortedSet(tempSet); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
126 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
127 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
128 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
129 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
130 |
* Returns a {@code SortedSet} of Strings which are a total order of the standard |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
131 |
* character sets supported by the JRE. The ordering follows the same principles as |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
132 |
* {@link java.awt.datatransfer.DataFlavor#selectBestTextFlavor(java.awt.datatransfer.DataFlavor[])}. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
133 |
* So as to avoid loading all available character converters, optional, non-standard, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
134 |
* character sets are not included. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
135 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
136 |
public static Set<String> standardEncodings() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
137 |
return StandardEncodingsHolder.standardEncodings; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
138 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
139 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
140 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
141 |
* Converts an arbitrary text encoding to its canonical name. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
142 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
143 |
public static String canonicalName(String encoding) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
144 |
if (encoding == null) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
145 |
return null; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
146 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
147 |
try { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
148 |
return Charset.forName(encoding).name(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
149 |
} catch (IllegalCharsetNameException icne) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
150 |
return encoding; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
151 |
} catch (UnsupportedCharsetException uce) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
152 |
return encoding; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
153 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
154 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
155 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
156 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
157 |
* Tests only whether the flavor's MIME type supports the charset |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
158 |
* parameter. Must only be called for flavors with a primary type of |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
159 |
* "text". |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
160 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
161 |
public static boolean doesSubtypeSupportCharset(DataFlavor flavor) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
162 |
String subType = flavor.getSubType(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
163 |
if (subType == null) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
164 |
return false; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
165 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
166 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
167 |
Boolean support = textMIMESubtypeCharsetSupport.get(subType); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
168 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
169 |
if (support != null) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
170 |
return support; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
171 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
172 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
173 |
boolean ret_val = (flavor.getParameter("charset") != null); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
174 |
textMIMESubtypeCharsetSupport.put(subType, ret_val); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
175 |
return ret_val; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
176 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
177 |
public static boolean doesSubtypeSupportCharset(String subType, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
178 |
String charset) |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
179 |
{ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
180 |
Boolean support = textMIMESubtypeCharsetSupport.get(subType); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
181 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
182 |
if (support != null) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
183 |
return support; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
184 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
185 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
186 |
boolean ret_val = (charset != null); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
187 |
textMIMESubtypeCharsetSupport.put(subType, ret_val); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
188 |
return ret_val; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
189 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
190 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
191 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
192 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
193 |
* Returns whether this flavor is a text type which supports the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
194 |
* 'charset' parameter. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
195 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
196 |
public static boolean isFlavorCharsetTextType(DataFlavor flavor) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
197 |
// Although stringFlavor doesn't actually support the charset |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
198 |
// parameter (because its primary MIME type is not "text"), it should |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
199 |
// be treated as though it does. stringFlavor is semantically |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
200 |
// equivalent to "text/plain" data. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
201 |
if (DataFlavor.stringFlavor.equals(flavor)) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
202 |
return true; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
203 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
204 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
205 |
if (!"text".equals(flavor.getPrimaryType()) || |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
206 |
!doesSubtypeSupportCharset(flavor)) |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
207 |
{ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
208 |
return false; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
209 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
210 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
211 |
Class<?> rep_class = flavor.getRepresentationClass(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
212 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
213 |
if (flavor.isRepresentationClassReader() || |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
214 |
String.class.equals(rep_class) || |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
215 |
flavor.isRepresentationClassCharBuffer() || |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
216 |
char[].class.equals(rep_class)) |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
217 |
{ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
218 |
return true; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
219 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
220 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
221 |
if (!(flavor.isRepresentationClassInputStream() || |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
222 |
flavor.isRepresentationClassByteBuffer() || |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
223 |
byte[].class.equals(rep_class))) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
224 |
return false; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
225 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
226 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
227 |
String charset = flavor.getParameter("charset"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
228 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
229 |
// null equals default encoding which is always supported |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
230 |
return (charset == null) || isEncodingSupported(charset); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
231 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
232 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
233 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
234 |
* Returns whether this flavor is a text type which does not support the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
235 |
* 'charset' parameter. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
236 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
237 |
public static boolean isFlavorNoncharsetTextType(DataFlavor flavor) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
238 |
if (!"text".equals(flavor.getPrimaryType()) || doesSubtypeSupportCharset(flavor)) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
239 |
return false; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
240 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
241 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
242 |
return (flavor.isRepresentationClassInputStream() || |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
243 |
flavor.isRepresentationClassByteBuffer() || |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
244 |
byte[].class.equals(flavor.getRepresentationClass())); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
245 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
246 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
247 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
248 |
* If the specified flavor is a text flavor which supports the "charset" |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
249 |
* parameter, then this method returns that parameter, or the default |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
250 |
* charset if no such parameter was specified at construction. For non- |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
251 |
* text DataFlavors, and for non-charset text flavors, this method returns |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
252 |
* null. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
253 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
254 |
public static String getTextCharset(DataFlavor flavor) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
255 |
if (!isFlavorCharsetTextType(flavor)) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
256 |
return null; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
257 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
258 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
259 |
String encoding = flavor.getParameter("charset"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
260 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
261 |
return (encoding != null) ? encoding : Charset.defaultCharset().name(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
262 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
263 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
264 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
265 |
* Determines whether this JRE can both encode and decode text in the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
266 |
* specified encoding. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
267 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
268 |
private static boolean isEncodingSupported(String encoding) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
269 |
if (encoding == null) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
270 |
return false; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
271 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
272 |
try { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
273 |
return Charset.isSupported(encoding); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
274 |
} catch (IllegalCharsetNameException icne) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
275 |
return false; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
276 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
277 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
278 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
279 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
280 |
* Helper method to compare two objects by their Integer indices in the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
281 |
* given map. If the map doesn't contain an entry for either of the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
282 |
* objects, the fallback index will be used for the object instead. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
283 |
* |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
284 |
* @param indexMap the map which maps objects into Integer indexes. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
285 |
* @param obj1 the first object to be compared. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
286 |
* @param obj2 the second object to be compared. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
287 |
* @param fallbackIndex the Integer to be used as a fallback index. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
288 |
* @return a negative integer, zero, or a positive integer as the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
289 |
* first object is mapped to a less, equal to, or greater |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
290 |
* index than the second. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
291 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
292 |
static <T> int compareIndices(Map<T, Integer> indexMap, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
293 |
T obj1, T obj2, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
294 |
Integer fallbackIndex) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
295 |
Integer index1 = indexMap.getOrDefault(obj1, fallbackIndex); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
296 |
Integer index2 = indexMap.getOrDefault(obj2, fallbackIndex); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
297 |
return index1.compareTo(index2); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
298 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
299 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
300 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
301 |
* An IndexedComparator which compares two String charsets. The comparison |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
302 |
* follows the rules outlined in DataFlavor.selectBestTextFlavor. In order |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
303 |
* to ensure that non-Unicode, non-ASCII, non-default charsets are sorted |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
304 |
* in alphabetical order, charsets are not automatically converted to their |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
305 |
* canonical forms. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
306 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
307 |
private static class CharsetComparator implements Comparator<String> { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
308 |
static final CharsetComparator INSTANCE = new CharsetComparator(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
309 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
310 |
private static final Map<String, Integer> charsets; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
311 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
312 |
private static final Integer DEFAULT_CHARSET_INDEX = 2; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
313 |
private static final Integer OTHER_CHARSET_INDEX = 1; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
314 |
private static final Integer WORST_CHARSET_INDEX = 0; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
315 |
private static final Integer UNSUPPORTED_CHARSET_INDEX = Integer.MIN_VALUE; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
316 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
317 |
private static final String UNSUPPORTED_CHARSET = "UNSUPPORTED"; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
318 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
319 |
static { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
320 |
Map<String, Integer> charsetsMap = new HashMap<>(8, 1.0f); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
321 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
322 |
// we prefer Unicode charsets |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
323 |
charsetsMap.put(canonicalName("UTF-16LE"), 4); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
324 |
charsetsMap.put(canonicalName("UTF-16BE"), 5); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
325 |
charsetsMap.put(canonicalName("UTF-8"), 6); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
326 |
charsetsMap.put(canonicalName("UTF-16"), 7); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
327 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
328 |
// US-ASCII is the worst charset supported |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
329 |
charsetsMap.put(canonicalName("US-ASCII"), WORST_CHARSET_INDEX); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
330 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
331 |
charsetsMap.putIfAbsent(Charset.defaultCharset().name(), DEFAULT_CHARSET_INDEX); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
332 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
333 |
charsetsMap.put(UNSUPPORTED_CHARSET, UNSUPPORTED_CHARSET_INDEX); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
334 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
335 |
charsets = Collections.unmodifiableMap(charsetsMap); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
336 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
337 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
338 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
339 |
* Compares charsets. Returns a negative integer, zero, or a positive |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
340 |
* integer as the first charset is worse than, equal to, or better than |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
341 |
* the second. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
342 |
* <p> |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
343 |
* Charsets are ordered according to the following rules: |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
344 |
* <ul> |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
345 |
* <li>All unsupported charsets are equal. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
346 |
* <li>Any unsupported charset is worse than any supported charset. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
347 |
* <li>Unicode charsets, such as "UTF-16", "UTF-8", "UTF-16BE" and |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
348 |
* "UTF-16LE", are considered best. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
349 |
* <li>After them, platform default charset is selected. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
350 |
* <li>"US-ASCII" is the worst of supported charsets. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
351 |
* <li>For all other supported charsets, the lexicographically less |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
352 |
* one is considered the better. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
353 |
* </ul> |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
354 |
* |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
355 |
* @param charset1 the first charset to be compared |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
356 |
* @param charset2 the second charset to be compared. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
357 |
* @return a negative integer, zero, or a positive integer as the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
358 |
* first argument is worse, equal to, or better than the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
359 |
* second. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
360 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
361 |
public int compare(String charset1, String charset2) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
362 |
charset1 = getEncoding(charset1); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
363 |
charset2 = getEncoding(charset2); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
364 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
365 |
int comp = compareIndices(charsets, charset1, charset2, OTHER_CHARSET_INDEX); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
366 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
367 |
if (comp == 0) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
368 |
return charset2.compareTo(charset1); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
369 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
370 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
371 |
return comp; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
372 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
373 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
374 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
375 |
* Returns encoding for the specified charset according to the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
376 |
* following rules: |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
377 |
* <ul> |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
378 |
* <li>If the charset is <code>null</code>, then <code>null</code> will |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
379 |
* be returned. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
380 |
* <li>Iff the charset specifies an encoding unsupported by this JRE, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
381 |
* <code>UNSUPPORTED_CHARSET</code> will be returned. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
382 |
* <li>If the charset specifies an alias name, the corresponding |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
383 |
* canonical name will be returned iff the charset is a known |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
384 |
* Unicode, ASCII, or default charset. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
385 |
* </ul> |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
386 |
* |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
387 |
* @param charset the charset. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
388 |
* @return an encoding for this charset. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
389 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
390 |
static String getEncoding(String charset) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
391 |
if (charset == null) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
392 |
return null; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
393 |
} else if (!isEncodingSupported(charset)) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
394 |
return UNSUPPORTED_CHARSET; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
395 |
} else { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
396 |
// Only convert to canonical form if the charset is one |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
397 |
// of the charsets explicitly listed in the known charsets |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
398 |
// map. This will happen only for Unicode, ASCII, or default |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
399 |
// charsets. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
400 |
String canonicalName = canonicalName(charset); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
401 |
return (charsets.containsKey(canonicalName)) |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
402 |
? canonicalName |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
403 |
: charset; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
404 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
405 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
406 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
407 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
408 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
409 |
* An IndexedComparator which compares two DataFlavors. For text flavors, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
410 |
* the comparison follows the rules outlined in |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
411 |
* DataFlavor.selectBestTextFlavor. For non-text flavors, unknown |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
412 |
* application MIME types are preferred, followed by known |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
413 |
* application/x-java-* MIME types. Unknown application types are preferred |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
414 |
* because if the user provides his own data flavor, it will likely be the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
415 |
* most descriptive one. For flavors which are otherwise equal, the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
416 |
* flavors' string representation are compared in the alphabetical order. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
417 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
418 |
private static class DataFlavorComparator implements Comparator<DataFlavor> { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
419 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
420 |
static final DataFlavorComparator INSTANCE = new DataFlavorComparator(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
421 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
422 |
private static final Map<String, Integer> exactTypes; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
423 |
private static final Map<String, Integer> primaryTypes; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
424 |
private static final Map<Class<?>, Integer> nonTextRepresentations; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
425 |
private static final Map<String, Integer> textTypes; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
426 |
private static final Map<Class<?>, Integer> decodedTextRepresentations; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
427 |
private static final Map<Class<?>, Integer> encodedTextRepresentations; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
428 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
429 |
private static final Integer UNKNOWN_OBJECT_LOSES = Integer.MIN_VALUE; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
430 |
private static final Integer UNKNOWN_OBJECT_WINS = Integer.MAX_VALUE; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
431 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
432 |
static { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
433 |
{ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
434 |
Map<String, Integer> exactTypesMap = new HashMap<>(4, 1.0f); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
435 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
436 |
// application/x-java-* MIME types |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
437 |
exactTypesMap.put("application/x-java-file-list", 0); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
438 |
exactTypesMap.put("application/x-java-serialized-object", 1); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
439 |
exactTypesMap.put("application/x-java-jvm-local-objectref", 2); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
440 |
exactTypesMap.put("application/x-java-remote-object", 3); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
441 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
442 |
exactTypes = Collections.unmodifiableMap(exactTypesMap); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
443 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
444 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
445 |
{ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
446 |
Map<String, Integer> primaryTypesMap = new HashMap<>(1, 1.0f); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
447 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
448 |
primaryTypesMap.put("application", 0); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
449 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
450 |
primaryTypes = Collections.unmodifiableMap(primaryTypesMap); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
451 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
452 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
453 |
{ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
454 |
Map<Class<?>, Integer> nonTextRepresentationsMap = new HashMap<>(3, 1.0f); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
455 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
456 |
nonTextRepresentationsMap.put(java.io.InputStream.class, 0); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
457 |
nonTextRepresentationsMap.put(java.io.Serializable.class, 1); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
458 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
459 |
nonTextRepresentationsMap.put(RMI.remoteClass(), 2); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
460 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
461 |
nonTextRepresentations = Collections.unmodifiableMap(nonTextRepresentationsMap); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
462 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
463 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
464 |
{ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
465 |
Map<String, Integer> textTypesMap = new HashMap<>(16, 1.0f); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
466 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
467 |
// plain text |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
468 |
textTypesMap.put("text/plain", 0); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
469 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
470 |
// stringFlavor |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
471 |
textTypesMap.put("application/x-java-serialized-object", 1); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
472 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
473 |
// misc |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
474 |
textTypesMap.put("text/calendar", 2); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
475 |
textTypesMap.put("text/css", 3); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
476 |
textTypesMap.put("text/directory", 4); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
477 |
textTypesMap.put("text/parityfec", 5); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
478 |
textTypesMap.put("text/rfc822-headers", 6); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
479 |
textTypesMap.put("text/t140", 7); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
480 |
textTypesMap.put("text/tab-separated-values", 8); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
481 |
textTypesMap.put("text/uri-list", 9); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
482 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
483 |
// enriched |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
484 |
textTypesMap.put("text/richtext", 10); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
485 |
textTypesMap.put("text/enriched", 11); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
486 |
textTypesMap.put("text/rtf", 12); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
487 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
488 |
// markup |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
489 |
textTypesMap.put("text/html", 13); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
490 |
textTypesMap.put("text/xml", 14); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
491 |
textTypesMap.put("text/sgml", 15); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
492 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
493 |
textTypes = Collections.unmodifiableMap(textTypesMap); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
494 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
495 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
496 |
{ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
497 |
Map<Class<?>, Integer> decodedTextRepresentationsMap = new HashMap<>(4, 1.0f); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
498 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
499 |
decodedTextRepresentationsMap.put(char[].class, 0); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
500 |
decodedTextRepresentationsMap.put(CharBuffer.class, 1); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
501 |
decodedTextRepresentationsMap.put(String.class, 2); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
502 |
decodedTextRepresentationsMap.put(Reader.class, 3); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
503 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
504 |
decodedTextRepresentations = |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
505 |
Collections.unmodifiableMap(decodedTextRepresentationsMap); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
506 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
507 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
508 |
{ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
509 |
Map<Class<?>, Integer> encodedTextRepresentationsMap = new HashMap<>(3, 1.0f); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
510 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
511 |
encodedTextRepresentationsMap.put(byte[].class, 0); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
512 |
encodedTextRepresentationsMap.put(ByteBuffer.class, 1); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
513 |
encodedTextRepresentationsMap.put(InputStream.class, 2); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
514 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
515 |
encodedTextRepresentations = |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
516 |
Collections.unmodifiableMap(encodedTextRepresentationsMap); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
517 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
518 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
519 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
520 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
521 |
public int compare(DataFlavor flavor1, DataFlavor flavor2) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
522 |
if (flavor1.equals(flavor2)) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
523 |
return 0; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
524 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
525 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
526 |
int comp; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
527 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
528 |
String primaryType1 = flavor1.getPrimaryType(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
529 |
String subType1 = flavor1.getSubType(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
530 |
String mimeType1 = primaryType1 + "/" + subType1; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
531 |
Class<?> class1 = flavor1.getRepresentationClass(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
532 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
533 |
String primaryType2 = flavor2.getPrimaryType(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
534 |
String subType2 = flavor2.getSubType(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
535 |
String mimeType2 = primaryType2 + "/" + subType2; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
536 |
Class<?> class2 = flavor2.getRepresentationClass(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
537 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
538 |
if (flavor1.isFlavorTextType() && flavor2.isFlavorTextType()) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
539 |
// First, compare MIME types |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
540 |
comp = compareIndices(textTypes, mimeType1, mimeType2, UNKNOWN_OBJECT_LOSES); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
541 |
if (comp != 0) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
542 |
return comp; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
543 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
544 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
545 |
// Only need to test one flavor because they both have the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
546 |
// same MIME type. Also don't need to worry about accidentally |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
547 |
// passing stringFlavor because either |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
548 |
// 1. Both flavors are stringFlavor, in which case the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
549 |
// equality test at the top of the function succeeded. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
550 |
// 2. Only one flavor is stringFlavor, in which case the MIME |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
551 |
// type comparison returned a non-zero value. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
552 |
if (doesSubtypeSupportCharset(flavor1)) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
553 |
// Next, prefer the decoded text representations of Reader, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
554 |
// String, CharBuffer, and [C, in that order. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
555 |
comp = compareIndices(decodedTextRepresentations, class1, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
556 |
class2, UNKNOWN_OBJECT_LOSES); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
557 |
if (comp != 0) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
558 |
return comp; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
559 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
560 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
561 |
// Next, compare charsets |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
562 |
comp = CharsetComparator.INSTANCE.compare(getTextCharset(flavor1), |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
563 |
getTextCharset(flavor2)); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
564 |
if (comp != 0) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
565 |
return comp; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
566 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
567 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
568 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
569 |
// Finally, prefer the encoded text representations of |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
570 |
// InputStream, ByteBuffer, and [B, in that order. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
571 |
comp = compareIndices(encodedTextRepresentations, class1, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
572 |
class2, UNKNOWN_OBJECT_LOSES); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
573 |
if (comp != 0) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
574 |
return comp; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
575 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
576 |
} else { |
31887
a0b91dedca5c
8130242: DataFlavorComparator transitivity exception
ssadetsky
parents:
31061
diff
changeset
|
577 |
// First, prefer text types |
27057
2202074399cf
8058473: "Comparison method violates its general contract" when using Clipboard
anashaty
parents:
26037
diff
changeset
|
578 |
if (flavor1.isFlavorTextType()) { |
2202074399cf
8058473: "Comparison method violates its general contract" when using Clipboard
anashaty
parents:
26037
diff
changeset
|
579 |
return 1; |
2202074399cf
8058473: "Comparison method violates its general contract" when using Clipboard
anashaty
parents:
26037
diff
changeset
|
580 |
} |
2202074399cf
8058473: "Comparison method violates its general contract" when using Clipboard
anashaty
parents:
26037
diff
changeset
|
581 |
|
2202074399cf
8058473: "Comparison method violates its general contract" when using Clipboard
anashaty
parents:
26037
diff
changeset
|
582 |
if (flavor2.isFlavorTextType()) { |
2202074399cf
8058473: "Comparison method violates its general contract" when using Clipboard
anashaty
parents:
26037
diff
changeset
|
583 |
return -1; |
2202074399cf
8058473: "Comparison method violates its general contract" when using Clipboard
anashaty
parents:
26037
diff
changeset
|
584 |
} |
2202074399cf
8058473: "Comparison method violates its general contract" when using Clipboard
anashaty
parents:
26037
diff
changeset
|
585 |
|
31887
a0b91dedca5c
8130242: DataFlavorComparator transitivity exception
ssadetsky
parents:
31061
diff
changeset
|
586 |
// Next, prefer application types. |
a0b91dedca5c
8130242: DataFlavorComparator transitivity exception
ssadetsky
parents:
31061
diff
changeset
|
587 |
comp = compareIndices(primaryTypes, primaryType1, primaryType2, |
a0b91dedca5c
8130242: DataFlavorComparator transitivity exception
ssadetsky
parents:
31061
diff
changeset
|
588 |
UNKNOWN_OBJECT_LOSES); |
a0b91dedca5c
8130242: DataFlavorComparator transitivity exception
ssadetsky
parents:
31061
diff
changeset
|
589 |
if (comp != 0) { |
a0b91dedca5c
8130242: DataFlavorComparator transitivity exception
ssadetsky
parents:
31061
diff
changeset
|
590 |
return comp; |
a0b91dedca5c
8130242: DataFlavorComparator transitivity exception
ssadetsky
parents:
31061
diff
changeset
|
591 |
} |
a0b91dedca5c
8130242: DataFlavorComparator transitivity exception
ssadetsky
parents:
31061
diff
changeset
|
592 |
|
26010
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
593 |
// Next, look for application/x-java-* types. Prefer unknown |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
594 |
// MIME types because if the user provides his own data flavor, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
595 |
// it will likely be the most descriptive one. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
596 |
comp = compareIndices(exactTypes, mimeType1, mimeType2, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
597 |
UNKNOWN_OBJECT_WINS); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
598 |
if (comp != 0) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
599 |
return comp; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
600 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
601 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
602 |
// Finally, prefer the representation classes of Remote, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
603 |
// Serializable, and InputStream, in that order. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
604 |
comp = compareIndices(nonTextRepresentations, class1, class2, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
605 |
UNKNOWN_OBJECT_LOSES); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
606 |
if (comp != 0) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
607 |
return comp; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
608 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
609 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
610 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
611 |
// The flavours are not equal but still not distinguishable. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
612 |
// Compare String representations in alphabetical order |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
613 |
return flavor1.getMimeType().compareTo(flavor2.getMimeType()); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
614 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
615 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
616 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
617 |
/* |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
618 |
* Given the Map that maps objects to Integer indices and a boolean value, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
619 |
* this Comparator imposes a direct or reverse order on set of objects. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
620 |
* <p> |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
621 |
* If the specified boolean value is SELECT_BEST, the Comparator imposes the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
622 |
* direct index-based order: an object A is greater than an object B if and |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
623 |
* only if the index of A is greater than the index of B. An object that |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
624 |
* doesn't have an associated index is less or equal than any other object. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
625 |
* <p> |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
626 |
* If the specified boolean value is SELECT_WORST, the Comparator imposes the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
627 |
* reverse index-based order: an object A is greater than an object B if and |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
628 |
* only if A is less than B with the direct index-based order. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
629 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
630 |
private static class IndexOrderComparator implements Comparator<Long> { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
631 |
private final Map<Long, Integer> indexMap; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
632 |
private static final Integer FALLBACK_INDEX = Integer.MIN_VALUE; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
633 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
634 |
public IndexOrderComparator(Map<Long, Integer> indexMap) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
635 |
this.indexMap = indexMap; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
636 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
637 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
638 |
public int compare(Long obj1, Long obj2) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
639 |
return compareIndices(indexMap, obj1, obj2, FALLBACK_INDEX); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
640 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
641 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
642 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
643 |
private static class TextFlavorComparator extends DataFlavorComparator { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
644 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
645 |
static final TextFlavorComparator INSTANCE = new TextFlavorComparator(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
646 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
647 |
* Compares two <code>DataFlavor</code> objects. Returns a negative |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
648 |
* integer, zero, or a positive integer as the first |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
649 |
* <code>DataFlavor</code> is worse than, equal to, or better than the |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
650 |
* second. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
651 |
* <p> |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
652 |
* <code>DataFlavor</code>s are ordered according to the rules outlined |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
653 |
* for <code>selectBestTextFlavor</code>. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
654 |
* |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
655 |
* @param flavor1 the first <code>DataFlavor</code> to be compared |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
656 |
* @param flavor2 the second <code>DataFlavor</code> to be compared |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
657 |
* @return a negative integer, zero, or a positive integer as the first |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
658 |
* argument is worse, equal to, or better than the second |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
659 |
* @throws ClassCastException if either of the arguments is not an |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
660 |
* instance of <code>DataFlavor</code> |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
661 |
* @throws NullPointerException if either of the arguments is |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
662 |
* <code>null</code> |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
663 |
* |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
664 |
* @see java.awt.datatransfer.DataFlavor#selectBestTextFlavor |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
665 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
666 |
public int compare(DataFlavor flavor1, DataFlavor flavor2) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
667 |
if (flavor1.isFlavorTextType()) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
668 |
if (flavor2.isFlavorTextType()) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
669 |
return super.compare(flavor1, flavor2); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
670 |
} else { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
671 |
return 1; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
672 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
673 |
} else if (flavor2.isFlavorTextType()) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
674 |
return -1; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
675 |
} else { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
676 |
return 0; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
677 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
678 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
679 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
680 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
681 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
682 |
* A fallback implementation of {@link sun.datatransfer.DesktopDatatransferService} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
683 |
* used if there is no desktop. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
684 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
685 |
private static final class DefaultDesktopDatatransferService implements DesktopDatatransferService { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
686 |
static final DesktopDatatransferService INSTANCE = getDesktopService(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
687 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
688 |
private static DesktopDatatransferService getDesktopService() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
689 |
ServiceLoader<DesktopDatatransferService> loader = |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
690 |
ServiceLoader.load(DesktopDatatransferService.class, null); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
691 |
Iterator<DesktopDatatransferService> iterator = loader.iterator(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
692 |
if (iterator.hasNext()) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
693 |
return iterator.next(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
694 |
} else { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
695 |
return new DefaultDesktopDatatransferService(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
696 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
697 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
698 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
699 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
700 |
* System singleton FlavorTable. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
701 |
* Only used if there is no desktop |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
702 |
* to provide an appropriate FlavorMap. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
703 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
704 |
private volatile FlavorMap flavorMap; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
705 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
706 |
@Override |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
707 |
public void invokeOnEventThread(Runnable r) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
708 |
r.run(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
709 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
710 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
711 |
@Override |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
712 |
public String getDefaultUnicodeEncoding() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
713 |
return StandardCharsets.UTF_8.name(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
714 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
715 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
716 |
@Override |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
717 |
public FlavorMap getFlavorMap(Supplier<FlavorMap> supplier) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
718 |
FlavorMap map = flavorMap; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
719 |
if (map == null) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
720 |
synchronized (this) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
721 |
map = flavorMap; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
722 |
if (map == null) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
723 |
flavorMap = map = supplier.get(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
724 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
725 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
726 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
727 |
return map; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
728 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
729 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
730 |
@Override |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
731 |
public boolean isDesktopPresent() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
732 |
return false; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
733 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
734 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
735 |
@Override |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
736 |
public LinkedHashSet<DataFlavor> getPlatformMappingsForNative(String nat) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
737 |
return new LinkedHashSet<>(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
738 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
739 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
740 |
@Override |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
741 |
public LinkedHashSet<String> getPlatformMappingsForFlavor(DataFlavor df) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
742 |
return new LinkedHashSet<>(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
743 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
744 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
745 |
@Override |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
746 |
public void registerTextFlavorProperties(String nat, String charset, |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
747 |
String eoln, String terminators) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
748 |
// Not needed if desktop module is absent |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
749 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
750 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
751 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
752 |
public static DesktopDatatransferService getDesktopService() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
753 |
return DefaultDesktopDatatransferService.INSTANCE; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
754 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
755 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
756 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
757 |
* A class that provides access to java.rmi.Remote and java.rmi.MarshalledObject |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
758 |
* without creating a static dependency. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
759 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
760 |
public static class RMI { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
761 |
private static final Class<?> remoteClass = getClass("java.rmi.Remote"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
762 |
private static final Class<?> marshallObjectClass = getClass("java.rmi.MarshalledObject"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
763 |
private static final Constructor<?> marshallCtor = getConstructor(marshallObjectClass, Object.class); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
764 |
private static final Method marshallGet = getMethod(marshallObjectClass, "get"); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
765 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
766 |
private static Class<?> getClass(String name) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
767 |
try { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
768 |
return Class.forName(name, true, null); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
769 |
} catch (ClassNotFoundException e) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
770 |
return null; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
771 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
772 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
773 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
774 |
private static Constructor<?> getConstructor(Class<?> c, Class<?>... types) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
775 |
try { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
776 |
return (c == null) ? null : c.getDeclaredConstructor(types); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
777 |
} catch (NoSuchMethodException x) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
778 |
throw new AssertionError(x); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
779 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
780 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
781 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
782 |
private static Method getMethod(Class<?> c, String name, Class<?>... types) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
783 |
try { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
784 |
return (c == null) ? null : c.getMethod(name, types); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
785 |
} catch (NoSuchMethodException e) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
786 |
throw new AssertionError(e); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
787 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
788 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
789 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
790 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
791 |
* Returns java.rmi.Remote.class if RMI is present; otherwise {@code null}. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
792 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
793 |
static Class<?> remoteClass() { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
794 |
return remoteClass; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
795 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
796 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
797 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
798 |
* Returns {@code true} if the given class is java.rmi.Remote. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
799 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
800 |
public static boolean isRemote(Class<?> c) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
801 |
return (remoteClass != null) && remoteClass.isAssignableFrom(c); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
802 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
803 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
804 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
805 |
* Returns a new MarshalledObject containing the serialized representation |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
806 |
* of the given object. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
807 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
808 |
public static Object newMarshalledObject(Object obj) throws IOException { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
809 |
try { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
810 |
return marshallCtor == null ? null : marshallCtor.newInstance(obj); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
811 |
} catch (InstantiationException | IllegalAccessException x) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
812 |
throw new AssertionError(x); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
813 |
} catch (InvocationTargetException x) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
814 |
Throwable cause = x.getCause(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
815 |
if (cause instanceof IOException) |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
816 |
throw (IOException) cause; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
817 |
throw new AssertionError(x); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
818 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
819 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
820 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
821 |
/** |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
822 |
* Returns a new copy of the contained marshalled object. |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
823 |
*/ |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
824 |
public static Object getMarshalledObject(Object obj) |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
825 |
throws IOException, ClassNotFoundException { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
826 |
try { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
827 |
return marshallGet == null ? null : marshallGet.invoke(obj); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
828 |
} catch (IllegalAccessException x) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
829 |
throw new AssertionError(x); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
830 |
} catch (InvocationTargetException x) { |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
831 |
Throwable cause = x.getCause(); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
832 |
if (cause instanceof IOException) |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
833 |
throw (IOException) cause; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
834 |
if (cause instanceof ClassNotFoundException) |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
835 |
throw (ClassNotFoundException) cause; |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
836 |
throw new AssertionError(x); |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
837 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
838 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
839 |
|
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
840 |
} |
9a3cf8ee0776
8037485: Refactor java.awt.datatransfer to eliminate dependency on AWT
pchelko
parents:
diff
changeset
|
841 |
} |