use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.
the class DuplicatedNativesTest method main.
public static void main(String[] args) throws Exception {
// 1. Check that returned natives do not contain duplicates.
SystemFlavorMap flavorMap = (SystemFlavorMap) SystemFlavorMap.getDefaultFlavorMap();
for (Map.Entry<DataFlavor, String> entry : flavorMap.getNativesForFlavors(null).entrySet()) {
List<String> natives = flavorMap.getNativesForFlavor(entry.getKey());
if (new HashSet<>(natives).size() != natives.size()) {
throw new RuntimeException("FAILED: returned natives contain duplicates: " + Arrays.toString(natives.toArray()));
}
}
// 2. Check that even if we set a duplicate it would be ignored.
flavorMap.setNativesForFlavor(DataFlavor.stringFlavor, new String[] { "test", "test", "test" });
List<String> natives = flavorMap.getNativesForFlavor(DataFlavor.stringFlavor);
if (new HashSet<>(natives).size() != natives.size()) {
throw new RuntimeException("FAILED: duplicates were not ignored: " + Arrays.toString(natives.toArray()));
}
}
use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.
the class MappingGenerationTest method test3.
/**
* Verifies that addUnencodedNativeForFlavor() for a particular text flavor
* doesn't affect mappings for other flavors.
*/
public static void test3() {
DataFlavor df1 = new DataFlavor("text/plain-test3", null);
DataFlavor df2 = new DataFlavor("text/plain-test3; charset=Unicode; class=java.io.Reader", null);
String nat = "native3";
List<String> natives = fm.getNativesForFlavor(df2);
fm.addUnencodedNativeForFlavor(df1, nat);
List<String> nativesNew = fm.getNativesForFlavor(df2);
if (!natives.equals(nativesNew)) {
System.err.println("orig=" + natives);
System.err.println("new=" + nativesNew);
throw new RuntimeException("Test failed");
}
}
use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.
the class MappingGenerationTest method test4.
/**
* Verifies that addUnencodedNativeForFlavor() really adds the specified
* flavor-to-native mapping to the existing mappings.
*/
public static void test4() {
DataFlavor df = new DataFlavor("text/plain-test4; charset=Unicode; class=java.io.Reader", null);
String nat = "native4";
List<String> natives = fm.getNativesForFlavor(df);
if (!natives.contains(nat)) {
fm.addUnencodedNativeForFlavor(df, nat);
List<String> nativesNew = fm.getNativesForFlavor(df);
natives.add(nat);
if (!natives.equals(nativesNew)) {
System.err.println("orig=" + natives);
System.err.println("new=" + nativesNew);
throw new RuntimeException("Test failed");
}
}
}
use of java.awt.datatransfer.DataFlavor in project jabref by JabRef.
the class EntryTableTransferHandler method importData.
/**
* This method is called when stuff is drag to the component.
*
* Imports the dropped URL or plain text as a new entry in the current library.
*
*/
@Override
public boolean importData(JComponent comp, Transferable t) {
// If the drop target is the main table, we want to record which
// row the item was dropped on, to identify the entry if needed:
int dropRow = -1;
if (comp instanceof JTable) {
dropRow = ((JTable) comp).getSelectedRow();
}
try {
// This flavor is used for dragged file links in Windows:
if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
// JOptionPane.showMessageDialog(null, "Received
// javaFileListFlavor");
@SuppressWarnings("unchecked") List<Path> files = ((List<File>) t.getTransferData(DataFlavor.javaFileListFlavor)).stream().map(File::toPath).collect(Collectors.toList());
return handleDraggedFiles(files, dropRow);
} else if (t.isDataFlavorSupported(urlFlavor)) {
URL dropLink = (URL) t.getTransferData(urlFlavor);
return handleDropTransfer(dropLink);
} else if (t.isDataFlavorSupported(stringFlavor)) {
String dropStr = (String) t.getTransferData(stringFlavor);
LOGGER.debug("Received stringFlavor: " + dropStr);
return handleDropTransfer(dropStr, dropRow);
}
} catch (IOException ioe) {
LOGGER.error("Failed to read dropped data", ioe);
} catch (UnsupportedFlavorException | ClassCastException ufe) {
LOGGER.error("Drop type error", ufe);
}
// all supported flavors failed
LOGGER.info("Can't transfer input: ");
DataFlavor[] inflavs = t.getTransferDataFlavors();
for (DataFlavor inflav : inflavs) {
LOGGER.info(" " + inflav);
}
return false;
}
use of java.awt.datatransfer.DataFlavor in project jdk8u_jdk by JetBrains.
the class DragSourceContext method readObject.
/**
* Deserializes this <code>DragSourceContext</code>. This method first
* performs default deserialization for all non-<code>transient</code>
* fields. This object's <code>Transferable</code> and
* <code>DragSourceListener</code> are then deserialized as well by using
* the next two objects in the stream. If the resulting
* <code>Transferable</code> is <code>null</code>, this object's
* <code>Transferable</code> is set to a dummy <code>Transferable</code>
* which supports no <code>DataFlavor</code>s.
*
* @since 1.4
*/
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
ObjectInputStream.GetField f = s.readFields();
DragGestureEvent newTrigger = (DragGestureEvent) f.get("trigger", null);
if (newTrigger == null) {
throw new InvalidObjectException("Null trigger");
}
if (newTrigger.getDragSource() == null) {
throw new InvalidObjectException("Null DragSource");
}
if (newTrigger.getComponent() == null) {
throw new InvalidObjectException("Null trigger component");
}
int newSourceActions = f.get("sourceActions", 0) & (DnDConstants.ACTION_COPY_OR_MOVE | DnDConstants.ACTION_LINK);
if (newSourceActions == DnDConstants.ACTION_NONE) {
throw new InvalidObjectException("Invalid source actions");
}
int triggerActions = newTrigger.getDragAction();
if (triggerActions != DnDConstants.ACTION_COPY && triggerActions != DnDConstants.ACTION_MOVE && triggerActions != DnDConstants.ACTION_LINK) {
throw new InvalidObjectException("No drag action");
}
trigger = newTrigger;
cursor = (Cursor) f.get("cursor", null);
useCustomCursor = f.get("useCustomCursor", false);
sourceActions = newSourceActions;
transferable = (Transferable) s.readObject();
listener = (DragSourceListener) s.readObject();
// Implementation assumes 'transferable' is never null.
if (transferable == null) {
if (emptyTransferable == null) {
emptyTransferable = new Transferable() {
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[0];
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return false;
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
throw new UnsupportedFlavorException(flavor);
}
};
}
transferable = emptyTransferable;
}
}
Aggregations